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



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

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

#create_first_userObject



62
63
64
65
66
67
# File 'lib/ops_manager/appliance_deployment.rb', line 62

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



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

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

#current_versionObject



155
156
157
# File 'lib/ops_manager/appliance_deployment.rb', line 155

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

#deployObject



69
70
71
72
# File 'lib/ops_manager/appliance_deployment.rb', line 69

def deploy
  appliance.deploy_vm
  wait_for_https_alive 300
end

#desired_versionObject



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

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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ops_manager/appliance_deployment.rb', line 132

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.



124
125
126
127
128
# File 'lib/ops_manager/appliance_deployment.rb', line 124

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’



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ops_manager/appliance_deployment.rb', line 104

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



89
90
91
92
93
94
95
96
97
# File 'lib/ops_manager/appliance_deployment.rb', line 89

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
    a << { version: p['stemcell'].fetch('version'), product: product_name }
  end.uniq
end

#new_vm_nameObject



151
152
153
# File 'lib/ops_manager/appliance_deployment.rb', line 151

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

#provision_stemcellsObject



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

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
# 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
      if config[:single_tile_deploy]
        OpsManager::InstallationRunner.trigger!("none").wait_for_result
      else
        OpsManager::InstallationRunner.trigger!.wait_for_result
      end
    else
      puts "OpsManager at #{config[:ip]} version is already #{config[:desired_version]}. Skiping ...".green
    end
  end

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

#upgradeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ops_manager/appliance_deployment.rb', line 74

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

#wait_for_uaaObject



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

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