class Opsmgr::Cmd::BoshCommand

Attributes

current_ops_manager[R]
env_settings[R]
om_version[R]

Public Class Methods

new(env_name:, om_version:) click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 17
def initialize(env_name:, om_version:)
  @env_settings = Opsmgr::Environments.for(env_name).settings
  configure_capybara

  @env_name = env_name
  @om_version = om_version
  @current_ops_manager = setup_or_login

  configure_client_credentials
end

Public Instance Methods

command() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 28
def command
  ip = ENV['DIRECTOR_IP_OVERRIDE'] || director_ip

  if Gem::Version.new(om_version) >= Gem::Version.new('1.7')
    return "BOSH_CLIENT=#{@bosh_client} BOSH_CLIENT_SECRET=#{@bosh_client_secret} bosh -t #{ip} --ca-cert #{root_cert_file}"
  end

  %W(
    bosh
    -t #{ip}
    -u director
    -p #{director_password}
  ).join(' ')
end
director_ip() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 43
def director_ip
  status_page = current_ops_manager.product_status_for(director_tile_name)

  partitioned_job_name = 'director-partition-null-az'
  availability_zones = env_settings['ops_manager']['availability_zones']
  if availability_zones
    az_name = availability_zones.first['iaas_identifier'] || 'first-az'
    partitioned_job_name = "director-partition-#{current_ops_manager.availability_zone_guid_for_name(az_name)}"
  end

  if Gem::Version.new(om_version) >= Gem::Version.new('1.8')
    partitioned_job_name = 'director'
  end

  director_status = status_page.job_status(partitioned_job_name)

  director_status.ips.first
end

Private Instance Methods

api_client() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 75
def api_client
  @api_client ||= Opsmgr::Api::Client.new(Opsmgr::Environments.for(@env_name), @om_version)
end
configure_capybara() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 102
def configure_capybara
  Capybara.configure do |c|
    c.default_driver = :webkit
    c.run_server = false
    c.app_host = env_settings['ops_manager']['url']
  end

  Capybara::Webkit.configure do |c|
    c.ignore_ssl_errors = true
    c.allow_url(env_settings['ops_manager']['url'])
    c.allow_url("fonts.googleapis.com")
  end

  page.current_window.resize_to(1024, 1600) # avoid overlapping footer spec failures
end
configure_client_credentials() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 64
def configure_client_credentials
  return unless Gem::Version.new(om_version) >= Gem::Version.new('1.7')

  installation_settings = api_client.installation_settings.as_hash
  uaa_credentials = installation_settings['products'].find { |p| p['identifier'] == 'p-bosh' }['uaa_credentials']


  @bosh_client = uaa_credentials['identity']
  @bosh_client_secret = uaa_credentials['password']
end
create_web_ui(version_module) click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 175
def create_web_ui(version_module)
  version_module::WebUi.new(browser: self)
end
director_password() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 118
def director_password
  browser = self

  browser.visit '/'
  browser.click_on "show-#{director_tile_name}-configure-action"
  browser.click_on 'show-credentials-action'

  credentials = ''

  browser.within "#installations_credentials" do
    credentials = browser.find(:xpath, ".//td[@class='with-table']/table[@class='table']/tbody/tr[4]/td[@class='value value-col']").text
  end

  credentials.split(' / ')[1]
end
director_tile_name() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 134
def director_tile_name
  om_version.to_f >= 1.6 ? 'p-bosh' : 'microbosh'
end
om_1_4() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 155
def om_1_4
  @om_1_4 ||= create_web_ui(OpsManagerUiDrivers::Version14)
end
om_1_5() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 159
def om_1_5
  @om_1_5 ||= create_web_ui(OpsManagerUiDrivers::Version15)
end
om_1_6() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 163
def om_1_6
  @om_1_6 ||= create_web_ui(OpsManagerUiDrivers::Version16)
end
om_1_7() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 167
def om_1_7
  @om_1_7 ||= create_web_ui(OpsManagerUiDrivers::Version17)
end
om_1_8() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 171
def om_1_8
  @om_1_8 ||= create_web_ui(OpsManagerUiDrivers::Version18)
end
ops_manager_driver(om_version) click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 138
def ops_manager_driver(om_version)
  case om_version
  when '1.4'
    om_1_4
  when '1.5'
    om_1_5
  when '1.6'
    om_1_6
  when '1.7'
    om_1_7
  when '1.8'
    om_1_8
  else
    fail "Unsupported Ops Manager Version #{om_version.inspect}"
  end
end
root_cert_file() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 79
def root_cert_file
  return @file_path if @file_path
  tmp_dir = ENV.fetch('TMPDIR', '/tmp')
  FileUtils.mkpath(tmp_dir)
  @file_path = tmp_dir + '/root_ca_certificate'

  result = api_client.root_ca_certificate
  fail result.message if result.is_a?(Api::Error)
  File.write(@file_path, result)

  @file_path
end
setup_or_login() click to toggle source
# File lib/opsmgr/cmd/bosh_command.rb, line 92
def setup_or_login
  driver = ops_manager_driver(om_version)

  driver.setup_page.setup_or_login(
    user: env_settings['ops_manager']['username'],
    password: env_settings['ops_manager']['password'],
  )
  driver
end