Class: PcfPause::Pause

Inherits:
Object
  • Object
show all
Defined in:
lib/pcf_pause/pause.rb

Constant Summary collapse

TARGET_JOBS =
%w(
  nats consul_server etcd_server diego_database nfs_server router ccdb
  uaadb consoledb cloud_controller ha_proxy health_manager clock_global
  cloud_controller_worker collector uaa diego_brain diego_cell doppler
  loggregator_trafficcontroller
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = Logger.new(STDOUT)) ⇒ Pause

Returns a new instance of Pause.



23
24
25
# File 'lib/pcf_pause/pause.rb', line 23

def initialize(logger = Logger.new(STDOUT))
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



21
22
23
# File 'lib/pcf_pause/pause.rb', line 21

def logger
  @logger
end

Instance Method Details

#connect_to_bosh(password, url, username) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pcf_pause/pause.rb', line 76

def connect_to_bosh(password, url, username)
  logger.info("Connecting to OpsMan at #{url}")
  ops_man = PcfPause::OpsManInfo.new(url, username, password)

  manifest = ops_man.get_director_manifest
  uaa_secret = manifest['jobs'][0]['properties']['uaa']['clients']['ops_manager']['secret']
  director_ip = manifest['jobs'][0]['properties']['director']['address']

  product_id = ops_man.get_product_id 'cf'
  bosh_vms_value = ops_man.get_vm_names product_id
  bosh_command = "BOSH_CLIENT=ops_manager BOSH_CLIENT_SECRET=#{uaa_secret} BUNDLE_GEMFILE=/home/tempest-web/tempest/web/vendor/bosh/Gemfile bundle exec bosh -n --ca-cert /var/tempest/workspaces/default/root_ca_certificate -d /var/tempest/workspaces/default/deployments/#{product_id}.yml -t #{director_ip}"
  return bosh_command, bosh_vms_value
end

#exec_ssh(ssh, command) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/pcf_pause/pause.rb', line 102

def exec_ssh(ssh, command)
  logger.debug 'SSH: executing command'
  ssh.exec!(command) do |ch, stream, data|
    puts data
    ch.on_request "exit-status" do |ch, data|
      exit_status = data.read_long
      raise "process terminated with exit status: #{exit_status}" if exit_status != 0
    end
  end
end

#get_job_sequence(bosh_vms_value) ⇒ Object



34
35
36
37
38
# File 'lib/pcf_pause/pause.rb', line 34

def get_job_sequence(bosh_vms_value)
  bosh_vms_value.select do |vm_name|
    TARGET_JOBS.any? { |job| vm_name.start_with?(job) }
  end
end

#smoke_tests(url, username, password, key_path = '') ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/pcf_pause/pause.rb', line 40

def smoke_tests(url, username, password, key_path='')
  bosh_command, bosh_vms_value = connect_to_bosh(password, url, username)

  Net::SSH.start(URI.parse(url).host, 'ubuntu', keys: [key_path]) do |ssh|
    exec_ssh ssh, "#{bosh_command} #{smoke_tests_command}"
  end
  logger.info 'Smoke tests have finished'
end

#smoke_tests_commandObject



98
99
100
# File 'lib/pcf_pause/pause.rb', line 98

def smoke_tests_command
  "run errand smoke-tests"
end

#start(url, username, password, key_path = '') ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pcf_pause/pause.rb', line 49

def start(url, username, password, key_path='')
  bosh_command, bosh_vms_value = connect_to_bosh(password, url, username)

  logger.info("Preparing to start instances")

  Net::SSH.start(URI.parse(url).host, 'ubuntu', keys: [key_path]) do |ssh|
    exec_ssh ssh, "#{bosh_command} #{start_command}"
    logger.info "Started"
  end
  logger.info 'All instances started'
end

#start_commandObject



90
91
92
# File 'lib/pcf_pause/pause.rb', line 90

def start_command
  "start --force"
end

#stop(url, username, password, key_path = '') ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pcf_pause/pause.rb', line 61

def stop(url, username, password, key_path='')
  bosh_command, bosh_vms_value = connect_to_bosh(password, url, username)

  stop_sequence = get_job_sequence(bosh_vms_value).reverse

  logger.info("Preparing to stop instances #{stop_sequence.join(', ')}")
  Net::SSH.start(URI.parse(url).host, 'ubuntu', keys: [key_path]) do |ssh|
    stop_sequence.each do |job_name|
      exec_ssh ssh, "#{bosh_command} #{stop_command(job_name)}"
      logger.info "Stopped #{job_name}"
    end
  end
  logger.info 'All instances stopped'
end

#stop_command(job_name) ⇒ Object



94
95
96
# File 'lib/pcf_pause/pause.rb', line 94

def stop_command(job_name)
  "stop #{job_name} --hard"
end