Class: OpsManager::Api::Opsman

Inherits:
Base
  • Object
show all
Defined in:
lib/ops_manager/api/opsman.rb

Instance Attribute Summary

Attributes inherited from Base

#silent

Instance Method Summary collapse

Methods inherited from Base

#add_authentication, #delete, #get, #http_for, #initialize, #multipart_post, #post, #print_green, #put, #say_green, #uri_for

Methods included from Logging

#logger, logger, logger=

Constructor Details

This class inherits a constructor from OpsManager::Api::Base

Instance Method Details

#access_tokenObject



203
204
205
# File 'lib/ops_manager/api/opsman.rb', line 203

def access_token
  @access_token ||= get_token.info['access_token']
end

#add_staged_products(name, version) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/ops_manager/api/opsman.rb', line 70

def add_staged_products(name, version)
  print_green( "====> Adding available product to the installation ...")
  body = "name=#{name}&product_version=#{version}"
  res = authenticated_post('/api/v0/staged/products', body: body)
  raise OpsManager::ProductDeploymentError.new(res.body) if res.code =~ /404|500/
  say_green('done')
  res
end

#create_userObject



9
10
11
12
# File 'lib/ops_manager/api/opsman.rb', line 9

def create_user
  body= "setup[decryption_passphrase]=#{password}&setup[decryption_passphrase_confirmation]=#{password}&setup[eula_accepted]=true&setup[identity_provider]=internal&setup[admin_user_name]=#{username}&setup[admin_password]=#{password}&setup[admin_password_confirmation]=#{password}"
  post("/api/v0/setup" , body: body)
end

#delete_products(opts = {}) ⇒ Object



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

def delete_products(opts = {})
  print_green '====> Deleating unused products ...'
  res = authenticated_delete('/api/v0/products', opts)
  say_green 'done'
  res
end

#get_available_productsObject



122
123
124
# File 'lib/ops_manager/api/opsman.rb', line 122

def get_available_products
  authenticated_get("/api/v0/available_products")
end

#get_diagnostic_reportObject



126
127
128
129
130
# File 'lib/ops_manager/api/opsman.rb', line 126

def get_diagnostic_report
  authenticated_get("/api/v0/diagnostic_report")
rescue Errno::ETIMEDOUT , Errno::EHOSTUNREACH, Net::HTTPFatalError, Net::OpenTimeout, HTTPClient::ConnectTimeoutError, CF::UAA::HTTPException
  nil
end

#get_ensure_availabilityObject



157
158
159
# File 'lib/ops_manager/api/opsman.rb', line 157

def get_ensure_availability
  get("/login/ensure_availability")
end

#get_installation(id) ⇒ Object



79
80
81
82
83
# File 'lib/ops_manager/api/opsman.rb', line 79

def get_installation(id)
  res = authenticated_get("/api/v0/installations/#{id}")
  raise OpsManager::InstallationError.new(res.body) if res.body =~  /failed/
  res
end

#get_installation_assetsObject



48
49
50
51
52
53
54
# File 'lib/ops_manager/api/opsman.rb', line 48

def get_installation_assets
  opts = { write_to: "installation_assets.zip" }
  print_green '====> Download installation assets ...'
  res = authenticated_get("/api/v0/installation_asset_collection", opts)
  say_green 'done'
  res
end

#get_installation_logs(id) ⇒ Object



85
86
87
# File 'lib/ops_manager/api/opsman.rb', line 85

def get_installation_logs(id)
  authenticated_get("/api/v0/installations/#{id}/logs")
end

#get_installation_settings(opts = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/ops_manager/api/opsman.rb', line 32

def get_installation_settings(opts = {})
  print_green '====> Downloading installation settings ...'
  res = authenticated_get("/api/installation_settings", opts)
  say_green 'done'
  res
end

#get_installations(opts = {}) ⇒ Object



93
94
95
96
97
98
# File 'lib/ops_manager/api/opsman.rb', line 93

def get_installations(opts = {})
  print_green '====> Getting installations ...'
  res = authenticated_get('/api/v0/installations')
  say_green 'done'
  res
end

#get_pending_changes(opts = {}) ⇒ Object



28
29
30
# File 'lib/ops_manager/api/opsman.rb', line 28

def get_pending_changes(opts = {})
  authenticated_get("/api/v0/staged/pending_changes", opts)
end

#get_staged_products(opts = {}) ⇒ Object



24
25
26
# File 'lib/ops_manager/api/opsman.rb', line 24

def get_staged_products(opts = {})
  authenticated_get("/api/v0/staged/products", opts)
end

#get_staged_products_errands(product_guid) ⇒ Object



89
90
91
# File 'lib/ops_manager/api/opsman.rb', line 89

def get_staged_products_errands(product_guid)
  authenticated_get("/api/v0/staged/products/#{product_guid}/errands" )
end

#get_tokenObject



161
162
163
164
165
166
167
# File 'lib/ops_manager/api/opsman.rb', line 161

def get_token
  token_issuer.owner_password_grant(username, password, 'opsman.admin').tap do |token|
    logger.info "UAA Token: #{token.inspect}"
  end
rescue  CF::UAA::TargetError
  nil
end

#import_stemcell(filepath, products = "all") ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ops_manager/api/opsman.rb', line 132

def import_stemcell(filepath, products = "all")
  return unless filepath
  tar = UploadIO.new(filepath, 'multipart/form-data')
  print_green "====> Uploading stemcell: #{filepath} ..."

  opts = { "stemcell[file]" => tar }
  # if we specify specific products, don't update stemcell associations everywhere
  if products != "all"
    opts["stemcell[floating]"] = false
 end

  res = nil
  3.times do
    res = authenticated_multipart_post("/api/v0/stemcells", opts)
    case res.code
      when '200' ; break
      when '503' ; sleep(60)
    end
  end
  raise OpsManager::StemcellUploadError.new(res.body) unless res.code == '200'

  say_green 'done'
  res
end

#passwordObject



190
191
192
# File 'lib/ops_manager/api/opsman.rb', line 190

def password
  @password ||= OpsManager.get_conf(:password)
end

#pending_changes(opts = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ops_manager/api/opsman.rb', line 169

def pending_changes(opts = {})
  print_green '====> Getting pending changes ...'
  res = authenticated_get('/api/v0/staged/pending_changes')
  pendingChanges = JSON.parse(res.body)

  if pendingChanges['product_changes'].count == 0
    puts "\nNo pending changes"
  else
    pendingChanges['product_changes'].each do |product|
      puts "\n#{product['guid']}"
    end
  end

  say_green 'done'
  res
end

#reset_access_tokenObject



199
200
201
# File 'lib/ops_manager/api/opsman.rb', line 199

def reset_access_token
  @access_token = nil
end

#targetObject



194
195
196
# File 'lib/ops_manager/api/opsman.rb', line 194

def target
  @target ||= OpsManager.get_conf(:target)
end

#trigger_installation(opts = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/ops_manager/api/opsman.rb', line 63

def trigger_installation(opts = {})
  print_green('====> Applying changes')
  res = authenticated_post('/api/v0/installations', opts)
  raise OpsManager::InstallationError.new(res.body) if res.code =~  /422/
  res
end

#upgrade_product_installation(guid, product_version) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/ops_manager/api/opsman.rb', line 100

def upgrade_product_installation(guid, product_version)
  print_green "====> Bumping product installation #{guid} product_version to #{product_version} ..."
  opts = { :body => { 'to_version' => product_version }.to_json }
  res = authenticated_put("/api/v0/staged/products/#{guid}", opts)
  raise OpsManager::UpgradeError.new(res.body) unless res.code == '200'
  say_green 'done'
  res
end

#upload_installation_assetsObject



39
40
41
42
43
44
45
46
# File 'lib/ops_manager/api/opsman.rb', line 39

def upload_installation_assets
  print_green( '====> Uploading installation assets ...')
  zip = UploadIO.new("#{Dir.pwd}/installation_assets.zip", 'application/x-zip-compressed')
  opts = {:passphrase => @password, "installation[file]" => zip }
  res = multipart_post( "/api/v0/installation_asset_collection", opts)
  say_green 'done'
  res
end

#upload_installation_settings(filepath = 'installation_settings.json') ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/ops_manager/api/opsman.rb', line 14

def upload_installation_settings(filepath = 'installation_settings.json')
  print_green '====> Uploading installation settings ...'
  yaml = UploadIO.new(filepath, 'text/yaml')
  opts = { "installation[file]" => yaml}
  res = authenticated_multipart_post("/api/installation_settings", opts)
  raise OpsManager::InstallationSettingsError.new(res.body) unless res.code == '200'
  say_green 'done'
  res
end

#upload_product(filepath) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ops_manager/api/opsman.rb', line 109

def upload_product(filepath)
  return unless filepath
  tar = UploadIO.new(filepath, 'multipart/form-data')
  print_green "====> Uploading product: #{filepath} ..."
#print "====> Uploading product ...".green
  opts = { "product[file]" => tar }
  res = authenticated_multipart_post("/api/v0/available_products" , opts)

  raise OpsManager::ProductUploadError.new(res.body) unless res.code == '200'
  say_green 'done'
  res
end

#usernameObject



186
187
188
# File 'lib/ops_manager/api/opsman.rb', line 186

def username
  @username ||= OpsManager.get_conf(:username)
end

#wait_for_https_alive(limit) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/ops_manager/api/opsman.rb', line 207

def wait_for_https_alive(limit)
  @retry_counter = 0
  res = nil
  until(@retry_counter >= limit or (res = check_alive).code.to_i < 400) do
    sleep 1
    @retry_counter += 1
  end
  res
end