Class: OpsManager::ApplianceDeployment

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ops_manager/appliance_deployment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ ApplianceDeployment

Returns a new instance of ApplianceDeployment.



19
20
21
# File 'lib/ops_manager/appliance_deployment.rb', line 19

def initialize(config_file)
  @config_file = config_file
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



17
18
19
# File 'lib/ops_manager/appliance_deployment.rb', line 17

def config_file
  @config_file
end

Instance Method Details

#applianceObject



56
57
58
59
60
61
62
# File 'lib/ops_manager/appliance_deployment.rb', line 56

def appliance 
  @appliance ||= if config[:provider] =~/vsphere/i
    OpsManager::Appliance::Vsphere.new(config)
  else
    OpsManager::Appliance::AWS.new(config)
  end
end

#create_first_userObject



64
65
66
67
68
69
# File 'lib/ops_manager/appliance_deployment.rb', line 64

def create_first_user
  puts '====> Creating initial user'.green
  until( create_user.code.to_i == 200) do
    print ' .'.green ; sleep 1
  end
end

#current_nameObject



166
167
168
# File 'lib/ops_manager/appliance_deployment.rb', line 166

def current_name
  @current_name ||= "#{config[:name]}-#{current_version}"
end

#current_versionObject



162
163
164
# File 'lib/ops_manager/appliance_deployment.rb', line 162

def current_version
  @current_version ||= OpsManager::Semver.new(version_from_diagnostic_report)
end

#deployObject



71
72
73
74
# File 'lib/ops_manager/appliance_deployment.rb', line 71

def deploy
  appliance.deploy_vm
  wait_for_https_alive 300
end

#desired_versionObject



170
171
172
# File 'lib/ops_manager/appliance_deployment.rb', line 170

def desired_version
  @desired_version ||= OpsManager::Semver.new(config[:desired_version])
end

#download_current_stemcellsObject

Lists all the available stemcells in the current installation_settings. Downloads those stemcells.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ops_manager/appliance_deployment.rb', line 139

def download_current_stemcells
  print "====> Downloading existing stemcells ...".green
  puts "no stemcells found".green if list_current_stemcells.empty?
  FileUtils.mkdir_p current_stemcell_dir
  list_current_stemcells.each do |stemcell_info|
    stemcell_version = stemcell_info[:version]
    product_name = stemcell_info[:product]
    release_id = find_stemcell_release(stemcell_version, product_name)
    accept_product_release_eula(product_name, release_id)
    stemcell_regex = /vsphere/
    if config[:provider] == "AWS"
      stemcell_regex = /aws/
    end

    file_id, file_name = find_stemcell_file(release_id, stemcell_regex, product_name)
    download_product_release_file(product_name, release_id, file_id, write_to: "#{current_stemcell_dir}/#{file_name}")
  end
end

#find_stemcell_file(release_id, filename, product_name) ⇒ Object

Finds stemcell’s pivotal network release file. #

Parameters:

  • release_id (String)

    the version number, eg: ‘2362.17’

  • filename (Regex)

    the version number, eg: /vsphere/

Returns:

  • id and name [Array] the pivotal network file ID and Filename for the matching stemcell.



131
132
133
134
135
# File 'lib/ops_manager/appliance_deployment.rb', line 131

def find_stemcell_file(release_id, filename, product_name)
  files = JSON.parse(get_product_release_files(product_name, release_id).body).fetch('product_files')
  file = files.select{ |r| r.fetch('aws_object_key') =~ filename }.first
  return file['id'], file['aws_object_key'].split('/')[-1]
end

#find_stemcell_release(version, product_name) ⇒ Object

Finds available stemcell’s pivotal network release. If it can not find the exact version it will try to find the newest minor version available. #

Parameters:

  • version (String)

    the version number, eg: ‘2362.17’



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ops_manager/appliance_deployment.rb', line 111

def find_stemcell_release(version, product_name)
  version  = OpsManager::Semver.new(version)
  releases = stemcell_releases(product_name).collect do |r|
    {
      release_id:  r['id'],
      version:     OpsManager::Semver.new(r['version']),
    }
  end
  releases.keep_if{ |r| r[:version].major == version.major }
  exact_version = releases.select {|r| r[:version] == version }
  return exact_version.first[:release_id] unless exact_version.empty?
  releases_sorted_by_version = releases.sort_by{ |r| r[:version].minor }.reverse
  return releases_sorted_by_version.first[:release_id] unless releases_sorted_by_version.empty?
end

#list_current_stemcellsObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ops_manager/appliance_deployment.rb', line 93

def list_current_stemcells
  JSON.parse(installation_settings).fetch('products').inject([]) do |a, p|
    product_name = "stemcells"
    if p['stemcell'].fetch('os') =~ /windows/i
      product_name = "stemcells-windows-server"
    end
    if p['stemcell'].fetch('os') =~ /ubuntu-xenial/i
      product_name = "stemcells-ubuntu-xenial"
    end
    a << { version: p['stemcell'].fetch('version'), product: product_name }
  end.uniq
end

#new_vm_nameObject



158
159
160
# File 'lib/ops_manager/appliance_deployment.rb', line 158

def new_vm_name
  @new_vm_name ||= "#{config[:name]}-#{config[:desired_version]}"
end

#provision_stemcellsObject



174
175
176
177
178
179
# File 'lib/ops_manager/appliance_deployment.rb', line 174

def provision_stemcells
  reset_access_token
  Dir.glob("#{current_stemcell_dir}/*").each do |stemcell_filepath|
    import_stemcell(stemcell_filepath)
  end
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ops_manager/appliance_deployment.rb', line 23

def run
  OpsManager.set_conf(:target, config[:ip])
  OpsManager.set_conf(:username, config[:username])
  OpsManager.set_conf(:password, config[:password])
  OpsManager.set_conf(:pivnet_token, config[:pivnet_token])


  case
  when current_version.empty?
    puts "No OpsManager deployed at #{config[:ip]}. Deploying ...".green
    deploy
    create_first_user
  when current_version < desired_version then
    puts "OpsManager at #{config[:ip]} version is #{current_version}. Upgrading to #{desired_version} .../".green
    upgrade
  when current_version == desired_version then
    if pending_changes?
      puts "OpsManager at #{config[:ip]} version has pending changes. Applying changes...".green
      products = "all"
      if config[:selected_deployments]
        products = config[:selected_deployments]
      elsif config[:single_tile_deploy]
        products = "none"
      end
      OpsManager::InstallationRunner.trigger!(products).wait_for_result
    else
      puts "OpsManager at #{config[:ip]} version is already #{config[:desired_version]}. Skiping ...".green
    end
  end

  puts '====> Finish!'.green
end

#upgradeObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ops_manager/appliance_deployment.rb', line 76

def upgrade
  get_installation_assets
  download_current_stemcells
  appliance.stop_current_vm(current_name)
  deploy
  upload_installation_assets
  wait_for_uaa
  provision_stemcells
  products = "all"
  if config[:selected_deployments]
    products = config[:selected_deployments]
  elsif config[:single_tile_deploy]
    products = "none"
  end
  OpsManager::InstallationRunner.trigger!(products).wait_for_result
end

#wait_for_uaaObject



181
182
183
184
185
186
# File 'lib/ops_manager/appliance_deployment.rb', line 181

def wait_for_uaa
  puts '====> Waiting for UAA to become available ...'.green
  while !uaa_available?
    sleep(5)
  end
end