class Pvcglue::Cloud

Attributes

bypass_mode[RW]
current_hostname[RW]
current_node[RW]
data[RW]
maintenance_mode[RW]
passenger_ruby[RW]
port_in_node_context[RW]
stage_env[RW]

Public Instance Methods

additional_linked_dirs() click to toggle source
# File lib/pvcglue/cloud.rb, line 305
def additional_linked_dirs
  project[:additional_linked_dirs] || stage[:additional_linked_dirs] || ""
end
app_and_stage_name() click to toggle source

app_stage_name = “#{ENV}_#{ENV}”.downcase

# File lib/pvcglue/cloud.rb, line 253
def app_and_stage_name
  "#{app_name}_#{stage_name}".downcase
end
app_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 192
def app_name
  Pvcglue.configuration.application_name
end
application_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 86
def application_dir
  Pvcglue.configuration.application_dir
end
authorized_keys() click to toggle source
# File lib/pvcglue/cloud.rb, line 196
def authorized_keys
  project[:authorized_keys]
end
client_body_timeout() click to toggle source
# File lib/pvcglue/cloud.rb, line 313
def client_body_timeout
  project[:client_body_timeout] || stage[:client_body_timeout] || "60s"
end
client_header_timeout() click to toggle source
# File lib/pvcglue/cloud.rb, line 309
def client_header_timeout
  project[:client_header_timeout] || stage[:client_header_timeout] || "60s"
end
client_max_body_size() click to toggle source
# File lib/pvcglue/cloud.rb, line 325
def client_max_body_size
  project[:client_max_body_size] || stage[:client_max_body_size] || "1m"
end
current_node_data() click to toggle source
# File lib/pvcglue/cloud.rb, line 39
def current_node_data
  current_node.values.first
end
current_node_without_nil_check() click to toggle source
# File lib/pvcglue/cloud.rb, line 35
def current_node_without_nil_check
  @current_node
end
data=(data) click to toggle source
# File lib/pvcglue/cloud.rb, line 23
def data=(data)
  # @data = data.with_indifferent_access # We may not want this dependency.
  # @data = data.to_dot # We may not want this dependency.
  # @data = Hashie::Mash.new(data) # We may not want this dependency.
  @data = ::SafeMash.new(data)
end
db_rebuild() click to toggle source
# File lib/pvcglue/cloud.rb, line 372
def db_rebuild
  !!stage[:db_rebuild]
end
delayed_job_args() click to toggle source
# File lib/pvcglue/cloud.rb, line 285
def delayed_job_args
  stage[:delayed_job_args]
end
delayed_job_gem_installed?() click to toggle source
# File lib/pvcglue/cloud.rb, line 355
def delayed_job_gem_installed?
  gem_installed?(:delayed_job) do
    raise('Not implemented, yet.  :(')
    gem_file_data =~ /^\s*gem\s+['"]whenever['"]/
  end
end
delayed_job_worker_count() click to toggle source
# File lib/pvcglue/cloud.rb, line 419
def delayed_job_worker_count
  return 0 unless gems[:delayed_job]
  (stage[:delayed_job_workers] || 1).to_i
end
deploy_to_app_current_bin_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 168
def deploy_to_app_current_bin_dir
  File.join(deploy_to_app_current_dir, Pvcglue.configuration.rails_bin_dir)
end
deploy_to_app_current_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 148
def deploy_to_app_current_dir
  File.join(deploy_to_app_dir, 'current')
end
deploy_to_app_current_public_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 164
def deploy_to_app_current_public_dir
  File.join(deploy_to_app_current_dir, 'public')
end
deploy_to_app_current_temp_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 184
def deploy_to_app_current_temp_dir
  File.join(deploy_to_app_current_dir, 'tmp')
end
deploy_to_app_dir() click to toggle source

ENV = “#{ENV}/#{ENV}/#{ENV}”

# File lib/pvcglue/cloud.rb, line 144
def deploy_to_app_dir
  File.join(deploy_to_base_dir, app_name, stage_name)
end
deploy_to_app_shared_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 152
def deploy_to_app_shared_dir
  File.join(deploy_to_app_dir, 'shared')
end
deploy_to_app_shared_pids_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 156
def deploy_to_app_shared_pids_dir
  File.join(deploy_to_app_shared_dir, 'pids')
end
deploy_to_base_dir() click to toggle source

ENV = stage_data || '/sites'

# File lib/pvcglue/cloud.rb, line 138
def deploy_to_base_dir
  # stage[:deploy_to] || '/sites' # TODO:  verify if server setup supports `:deploy_to` override
  web_app_base_dir # TODO:  server setup does not yet support `:deploy_to` override, and would have to be refactored at a higher level than stage.
end
dev_ip_addresses() click to toggle source
# File lib/pvcglue/cloud.rb, line 239
def dev_ip_addresses
  return ['127.0.0.1']
  # TODO:  Add this functionality back in later
  project[:dev_ip_addresses].values.each_with_object([]) { |address, addresses| addresses << address }
end
domains() click to toggle source
# File lib/pvcglue/cloud.rb, line 257
def domains
  stage[:domains]
end
dos_burst() click to toggle source
# File lib/pvcglue/cloud.rb, line 301
def dos_burst
  project[:dos_burst] || stage[:dos_burst] || "30"
end
dos_conn_limit_per_ip() click to toggle source
# File lib/pvcglue/cloud.rb, line 293
def dos_conn_limit_per_ip
  project[:dos_conn_limit_per_ip] || stage[:dos_conn_limit_per_ip] || "10"
end
dos_rate() click to toggle source
# File lib/pvcglue/cloud.rb, line 297
def dos_rate
  project[:dos_rate] || stage[:dos_rate] || "1"
end
env_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 160
def env_file_name
  File.join(deploy_to_app_shared_dir, ".env.#{stage_name_validated}")
end
env_local_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 82
def env_local_file_name
  File.join(application_dir, Pvcglue::Env.stage_env_file_name_base)
end
exclude_tables() click to toggle source
# File lib/pvcglue/cloud.rb, line 211
def exclude_tables
  project[:excluded_db_tables] || ['versions']
end
find_machine(name) click to toggle source
# File lib/pvcglue/cloud.rb, line 553
def find_machine(name)
  name.tr!('=', '') # "==dev-lb==" ==> "dev-lb"
  find_or_raise(data.machines, name)
end
find_minion_by_name(minion_name, raise_error = true) click to toggle source

find node by full node_name or by matching prefix of node_name

# File lib/pvcglue/cloud.rb, line 91
def find_minion_by_name(minion_name, raise_error = true)
  # raise(Thor::Error, "Node not specified.") if node_name.nil? || node_name.empty?
  raise('Minion not specified.') if minion_name.nil? || minion_name.empty?
  return {minion_name => minions_filtered[minion_name]} if minions[minion_name]
  minions.each do |key, value|
    return {key => value} if key.start_with?(minion_name)
  end
  raise("Not found:  #{minion_name} in #{stage_name}.") if raise_error
  # raise(Thor::Error, "Not found:  #{node_name} in #{stage_name}.")
  nil
end
find_or_raise(data, name, key = 'name') click to toggle source
# File lib/pvcglue/cloud.rb, line 518
def find_or_raise(data, name, key = 'name')
  found = data.detect { |item| item[key] == name }
  # raise(Thor::Error, "Error:  #{name} not found.") unless found
  unless found
    puts "name=#{name}, key=#{key}, data="
    ap data
    raise("Error:  #{name} not found.")
  end
  found
end
firewall_allow_incoming_from_ip() click to toggle source
# File lib/pvcglue/cloud.rb, line 226
def firewall_allow_incoming_from_ip
  raise 'Not used currently > 0.9'
  # Incoming connections to any port are allowed from these ip addresses
  addresses = dev_ip_addresses
  addresses.concat(stage_internal_addresses)
  # puts addresses.inspect
  if stage_name == 'local'
    addresses << Pvcglue::Local.vagrant_config # Yes, this is a hack, and should be refactored.  :)
  end
  # puts addresses.inspect
  addresses
end
firewall_allow_incoming_on_port() click to toggle source
# File lib/pvcglue/cloud.rb, line 215
def firewall_allow_incoming_on_port
  raise 'Not used currently > 0.9'
  # These ports allow incoming connections from any ip address
  ports = []
  from_all = project[:ssh_allowed_from_all_port].to_i
  ports << from_all if from_all > 0
  ports.concat [80, 443] if current_node.values.first[:allow_public_access]
  ports.concat ["2000:3000"] if stage_name == 'local'
  ports
end
gem_file_data() click to toggle source
# File lib/pvcglue/cloud.rb, line 337
def gem_file_data
  @gem_file_data ||= begin
    File.read(Pvcglue.configuration.gemfile_file_name)
  end
end
gem_installed?(gem) { || ... } click to toggle source
# File lib/pvcglue/cloud.rb, line 343
def gem_installed?(gem)
  value = gems[gem]
  return value unless value == 'auto'
  yield
end
gems() click to toggle source
# File lib/pvcglue/cloud.rb, line 333
def gems
  project[:gems] || {}
end
generated_file_warning() click to toggle source
# File lib/pvcglue/cloud.rb, line 368
def generated_file_warning
  'This is a generated file.  Do not modify...or else!  :)'
end
get_manager_minion() click to toggle source
# File lib/pvcglue/cloud.rb, line 467
def get_manager_minion
  minion = Pvcglue::Minion.new
  minion.machine_name = 'pvcglue-manager'
  minion.roles = ['manager']
  minion.public_ip = Pvcglue.configuration.cloud_manager
  minion.connection = Pvcglue::Connection.new(minion)
  # minion.root_users = machine.root_users
  # minion.users = machine.users
  # minion.cloud_id = machine.cloud_id
  minion.remote_user_name = minion_manager_user_name
  # minion.all_data = data
  # minion.project = project
  # minion.stage = stage
  # minion.cloud_provider = data.cloud_provider
  # minion.cloud = ::Pvcglue.cloud
  # minion.cloud_provider.name == 'not-supported'
  minion

end
get_minions() click to toggle source
# File lib/pvcglue/cloud.rb, line 559
def get_minions
  minions = ::SafeMash.new
  stage.stack.each do |item|
    # ap item
    machine = find_machine(item.machine_name)
    # ap machine
    minion = minions[machine.name]
    # ap minion
    unless minion
      # TODO:  check for duplicate roles:  ie. 2 web servers with the same id
      minion = Pvcglue::Minion.new
      minion.machine_name = item.machine_name
      minion.roles = []
      minion.private_ip = machine.private_ip
      minion.public_ip = machine.public_ip
      minion.connection = Pvcglue::Connection.new(minion)
      minion.root_users = machine.root_users
      minion.users = machine.users
      minion.cloud_id = machine.cloud_id
      minion.remote_user_name = minion_user_name
      minion.machine_options = machine
      minion.stage_options = item

      minion.all_data = data
      minion.project = project
      minion.stage = stage
      minion.default_cloud_provider = data.default_cloud_provider
      minion.cloud = ::Pvcglue.cloud
    end

    minion.roles << item.role

    minions[machine.name] = minion
  end
  # ap minions
  minions
end
get_project() click to toggle source
# File lib/pvcglue/cloud.rb, line 539
def get_project
  find_or_raise(data.projects, @app_name_override || app_name)
end
lb_nginx_load_balancing_method() click to toggle source
# File lib/pvcglue/cloud.rb, line 281
def lb_nginx_load_balancing_method
  stage[:lb_nginx_load_balancing_method]
end
letsencrypt_full() click to toggle source
# File lib/pvcglue/cloud.rb, line 445
def letsencrypt_full
  '/var/www/letsencrypt_root/.well-known/acme-challenge'
end
letsencrypt_root() click to toggle source
# File lib/pvcglue/cloud.rb, line 449
def letsencrypt_root
  '/var/www/letsencrypt_root'
end
local_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 78
def local_file_name
  File.join(application_dir, Pvcglue::Manager.cloud_data_file_name_base)
end
maintenance_bypass_mode_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 180
def maintenance_bypass_mode_file_name
  File.join(maintenance_files_dir, 'maintenance_bypass.off')
end
maintenance_files_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 172
def maintenance_files_dir
  File.join(deploy_to_app_dir, 'maintenance')
end
maintenance_mode_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 176
def maintenance_mode_file_name
  File.join(maintenance_files_dir, 'maintenance.on')
end
manager_minion() click to toggle source
# File lib/pvcglue/cloud.rb, line 463
def manager_minion
  @manager_minion ||= get_manager_minion
end
minion_manager_user_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 441
def minion_manager_user_name
  'manager'
end
minion_user_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 513
def minion_user_name
  "#{minion_user_name_base}-#{stage_name}"
end
minion_user_name_base() click to toggle source
# File lib/pvcglue/cloud.rb, line 508
def minion_user_name_base
  # project[:user_name_base] || 'deploy'
  project[:user_name_base] || raise('user_name_base is required')
end
minions() click to toggle source
# File lib/pvcglue/cloud.rb, line 549
def minions
  @minions ||= get_minions
end
minions_filtered(role_filter = 'all') click to toggle source

# find node by full node_name or by matching prefix of node_name def find_node(node_name, raise_error = true)

# raise(Thor::Error, "Node not specified.") if node_name.nil? || node_name.empty?
raise('Node not specified.') if node_name.nil? || node_name.empty?
return {node_name => nodes_in_stage[node_name]} if nodes_in_stage[node_name]
nodes_in_stage.each do |key, value|
  return {key => value} if key.start_with?(node_name)
end
raise("Not found:  #{node_name} in #{stage_name}.") if raise_error
# raise(Thor::Error, "Not found:  #{node_name} in #{stage_name}.")

end

# File lib/pvcglue/cloud.rb, line 115
def minions_filtered(role_filter = 'all')
  # # puts (stage_roles.values.each_with_object({}) { |node, nodes| nodes.merge!(node) }).inspect
  # # stage_roles.values.each_with_object({}) { |node, nodes| nodes.merge!(node) }
  # nodes = stage_roles.each_with_object({}) do |(role, node), nodes|
  #   if role_filter == 'all' || role == role_filter
  #     nodes.merge!(node)
  #   end
  # end
  # # puts nodes.inspect
  # # puts "nodes_in_stage: only first returned"+"!*"*80
  # # out = {}
  # # out["memcached"] = nodes["memcached"]
  # # puts out.inspect
  # # out
  if role_filter == 'all'
    minions
  else
    minions.select { |minion_name, minion| minion.has_role?(role_filter) }
  end

end
monit_alert() click to toggle source
# File lib/pvcglue/cloud.rb, line 433
def monit_alert
  project[:monit_alert] || ""
end
monit_disk_usage_threshold() click to toggle source
# File lib/pvcglue/cloud.rb, line 437
def monit_disk_usage_threshold
  stage[:monit_disk_usage_threshold] || project[:monit_disk_usage_threshold] || "80%"
end
monit_mailserver() click to toggle source
# File lib/pvcglue/cloud.rb, line 429
def monit_mailserver
  project[:monit_mailserver] || ""
end
nginx_config_path() click to toggle source
# File lib/pvcglue/cloud.rb, line 376
def nginx_config_path
  '/etc/nginx'
end
nginx_config_ssl_path() click to toggle source
# File lib/pvcglue/cloud.rb, line 380
def nginx_config_ssl_path
  File.join(nginx_config_path, 'ssl', "#{app_and_stage_name}")
end
nginx_ssl_crt_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 384
def nginx_ssl_crt_file_name
  File.join(nginx_config_ssl_path, "#{app_and_stage_name}.crt")
end
nginx_ssl_key_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 388
def nginx_ssl_key_file_name
  File.join(nginx_config_ssl_path, "#{app_and_stage_name}.key")
end
old_stage() click to toggle source
# File lib/pvcglue/cloud.rb, line 65
def old_stage
  # puts project.inspect
  # puts project[:stages].inspect
  # puts project[:stages][stage_name].inspect
  project[:stages][stage_name]
end
port_in_context(context) click to toggle source
# File lib/pvcglue/cloud.rb, line 400
def port_in_context(context)
  case context
  when :bootstrap, :manager
    port = "22"
  when :env, :build, :shell, :deploy, :maintenance
    port = project[:ssh_allowed_from_all_port] || "22"
  else
    raise "Context not specified or invalid"
  end
  puts "Setting port to #{port}"
  @port_in_node_context = port
end
project() click to toggle source
# File lib/pvcglue/cloud.rb, line 529
def project
  @project ||= begin
    get_project
  end
end
project_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 535
def project_name
  project.name
end
proxy_read_timeout() click to toggle source
# File lib/pvcglue/cloud.rb, line 317
def proxy_read_timeout
  project[:proxy_read_timeout] || stage[:proxy_read_timeout] || "60s"
end
proxy_send_timeout() click to toggle source
# File lib/pvcglue/cloud.rb, line 321
def proxy_send_timeout
  project[:proxy_send_timeout] || stage[:proxy_send_timeout] || "60s"
end
reload_minions!() click to toggle source

def set_manager_as_project

# raise('project already initialized') if @project
# @project = find_or_raise(data.projects, 'pvcglue_manager')
@app_name_override = 'pvcglue_manager'
set_stage('manager')

end

# File lib/pvcglue/cloud.rb, line 495
def reload_minions!
  @data = nil
  @project = nil
  @stage = nil
  @minions = nil
end
repo_url() click to toggle source
# File lib/pvcglue/cloud.rb, line 289
def repo_url
  project[:repo_url]
end
resque_worker_count() click to toggle source
# File lib/pvcglue/cloud.rb, line 424
def resque_worker_count
  return 0 unless gems[:resque]
  (stage[:resque_workers] || 1).to_i
end
restart_txt_file_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 188
def restart_txt_file_name
  File.join(deploy_to_app_current_temp_dir, 'restart.txt')
end
service_directory() click to toggle source
# File lib/pvcglue/cloud.rb, line 453
def service_directory
  '/lib/systemd/system/'
end
service_extension() click to toggle source
# File lib/pvcglue/cloud.rb, line 457
def service_extension
  '.service'
end
set_ssl_mode_override(mode) click to toggle source
# File lib/pvcglue/cloud.rb, line 277
def set_ssl_mode_override(mode)
  @ssl_mode_override = mode
end
set_stage(stage) click to toggle source
# File lib/pvcglue/cloud.rb, line 49
def set_stage(stage)
  @stage_name = stage
  @stage_name.downcase! if @stage_name
end
sidekiq_gem_installed?() click to toggle source
# File lib/pvcglue/cloud.rb, line 362
def sidekiq_gem_installed?
  gem_installed?(:sidekiq) do
    gem_file_data =~ /^\s*gem\s+['"]sidekiq['"]/
  end
end
ssh_ports() click to toggle source
# File lib/pvcglue/cloud.rb, line 200
def ssh_ports
  ports = []
  from_all = project[:ssh_allowed_from_all_port].to_i
  ports << from_all if from_all > 0
  ports
end
ssl_crt() click to toggle source
# File lib/pvcglue/cloud.rb, line 392
def ssl_crt
  stage[:ssl_crt]
end
ssl_key() click to toggle source
# File lib/pvcglue/cloud.rb, line 396
def ssl_key
  stage[:ssl_key]
end
ssl_mode() click to toggle source
# File lib/pvcglue/cloud.rb, line 269
def ssl_mode
  stage[:ssl].to_sym || :none
end
ssl_mode_override() click to toggle source
# File lib/pvcglue/cloud.rb, line 273
def ssl_mode_override
  @ssl_mode_override || ssl_mode
end
stage() click to toggle source
# File lib/pvcglue/cloud.rb, line 543
def stage
  @stage ||= begin
    find_or_raise(project.stages, stage_name)
  end
end
stage_internal_addresses() click to toggle source
# File lib/pvcglue/cloud.rb, line 245
def stage_internal_addresses
  minions_filtered.values.each_with_object([]) do |value, addresses|
    addresses << value[:public_ip]
    addresses << value[:private_ip] if value[:private_ip]
  end
end
stage_name() click to toggle source
# File lib/pvcglue/cloud.rb, line 54
def stage_name
  @stage_name
end
stage_name_validated() click to toggle source
# File lib/pvcglue/cloud.rb, line 58
def stage_name_validated
  # TODO:  Document better or fix root cause
  # Work-around for orca file packages that are loaded when required, but stage_name is not going to be used
  # raise "stage_name is required in this context" unless @stage_name
  @stage_name
end
stage_roles() click to toggle source
# File lib/pvcglue/cloud.rb, line 72
def stage_roles
  raise("Stage not defined:  #{stage_name}.") if stage.nil?
  # raise(Thor::Error, "Stage not defined:  #{stage_name}.") if stage.nil?
  stage[:roles]
end
swapfile_size() click to toggle source
# File lib/pvcglue/cloud.rb, line 329
def swapfile_size
  project[:swapfile_size] || stage[:swapfile_size] || 1024
end
timezone() click to toggle source
# File lib/pvcglue/cloud.rb, line 207
def timezone
  project[:time_zone] || 'America/Los_Angeles'
end
web_app_base_dir() click to toggle source
# File lib/pvcglue/cloud.rb, line 502
def web_app_base_dir
  # '/sites'
  "/home/#{minion_user_name}/www"
end
web_nginx_http() click to toggle source
# File lib/pvcglue/cloud.rb, line 261
def web_nginx_http
  current_node.values.first[:web_nginx_http] || []
end
web_nginx_server() click to toggle source
# File lib/pvcglue/cloud.rb, line 265
def web_nginx_server
  current_node.values.first[:web_nginx_server] || []
end
whenever_gem_installed?() click to toggle source
# File lib/pvcglue/cloud.rb, line 349
def whenever_gem_installed?
  gem_installed?(:whenever) do
    gem_file_data =~ /^\s*gem\s+['"]whenever['"]/
  end
end