Class: Bosh::Cli::Director
- Includes:
- VersionCalc
- Defined in:
- lib/cli/director.rb
Constant Summary collapse
- DIRECTOR_HTTP_ERROR_CODES =
[400, 403, 500]
- API_TIMEOUT =
86400 * 3
- CONNECT_TIMEOUT =
30
Instance Attribute Summary collapse
-
#director_uri ⇒ Object
readonly
Returns the value of attribute director_uri.
Instance Method Summary collapse
- #apply_resolutions(deployment_name, resolutions, options = {}) ⇒ Object
- #authenticated? ⇒ Boolean
- #cancel_task(task_id) ⇒ Object
- #change_job_state(deployment_name, manifest_yaml, job_name, index, new_state, options = {}) ⇒ Object
- #cleanup_ssh(deployment_name, job, user_regex, indexes, options = {}) ⇒ Object
- #create_property(deployment_name, property_name, value) ⇒ Object
- #create_user(username, password) ⇒ Object
- #delete_deployment(name, options = {}) ⇒ Object
- #delete_property(deployment_name, property_name) ⇒ Object
- #delete_release(name, options = {}) ⇒ Object
- #delete_stemcell(name, version, options = {}) ⇒ Object
- #deploy(manifest_yaml, options = {}) ⇒ Object
- #download_resource(id) ⇒ Object
- #exists? ⇒ Boolean
- #fetch_logs(deployment_name, job_name, index, log_type, filters = nil, options = {}) ⇒ Object
- #fetch_vm_state(deployment_name, options = {}) ⇒ Object
- #get_current_time ⇒ Object
- #get_deployment(name) ⇒ Object
- #get_property(deployment_name, property_name) ⇒ Object
- #get_release(name) ⇒ Object
- #get_status ⇒ Object
- #get_task(task_id) ⇒ Object
- #get_task_output(task_id, offset, log_type = nil) ⇒ Object
- #get_task_result(task_id) ⇒ Object
- #get_task_result_log(task_id) ⇒ Object
- #get_task_state(task_id) ⇒ Object
- #get_time_difference ⇒ Object
- #get_version ⇒ Object
-
#initialize(director_uri, user = nil, password = nil) ⇒ Director
constructor
A new instance of Director.
- #list_deployments ⇒ Object
- #list_problems(deployment_name) ⇒ Object
- #list_properties(deployment_name) ⇒ Object
- #list_recent_tasks(count = 30) ⇒ Object
- #list_releases ⇒ Object
- #list_running_tasks ⇒ Object
- #list_stemcells ⇒ Object
- #list_vms(name) ⇒ Object
- #match_packages(manifest_yaml) ⇒ Object
- #parse_error_message(status, body) ⇒ Object
- #perform_cloud_scan(deployment_name, options = {}) ⇒ Object
-
#rename_job(deployment_name, manifest_yaml, old_name, new_name, force = false, options = {}) ⇒ Object
TODO: should pass ‘force’ with options, not as a separate argument.
- #request(method, uri, content_type = nil, payload = nil, headers = {}, options = {}) ⇒ Object
-
#request_and_track(method, uri, options = {}) ⇒ Object
Perform director HTTP request and track director task (if request started one).
- #setup_ssh(deployment_name, job, index, user, public_key, password, options = {}) ⇒ Object
- #update_property(deployment_name, property_name, value) ⇒ Object
- #upload_and_track(method, uri, filename, options = {}) ⇒ Object
- #upload_release(filename, options = {}) ⇒ Object
- #upload_stemcell(filename, options = {}) ⇒ Object
- #uuid ⇒ Object
Methods included from VersionCalc
#major_version, #minor_version, #version_cmp, #version_greater, #version_less, #version_same
Constructor Details
#initialize(director_uri, user = nil, password = nil) ⇒ Director
Returns a new instance of Director.
15 16 17 18 19 20 21 22 23 |
# File 'lib/cli/director.rb', line 15 def initialize(director_uri, user = nil, password = nil) if director_uri.nil? || director_uri =~ /^\s*$/ raise DirectorMissing, "no director URI given" end @director_uri = director_uri @user = user @password = password end |
Instance Attribute Details
#director_uri ⇒ Object (readonly)
Returns the value of attribute director_uri.
13 14 15 |
# File 'lib/cli/director.rb', line 13 def director_uri @director_uri end |
Instance Method Details
#apply_resolutions(deployment_name, resolutions, options = {}) ⇒ Object
344 345 346 347 348 349 350 351 352 |
# File 'lib/cli/director.rb', line 344 def apply_resolutions(deployment_name, resolutions, = {}) = .dup url = "/deployments/#{deployment_name}/problems" [:content_type] = "application/json" [:payload] = JSON.generate("resolutions" => resolutions) request_and_track(:put, url, ) end |
#authenticated? ⇒ Boolean
38 39 40 41 42 43 44 45 46 |
# File 'lib/cli/director.rb', line 38 def authenticated? status = get_status # Backward compatibility: older directors return 200 # only for logged in users return true if !status.has_key?("version") !status["user"].nil? rescue DirectorError false end |
#cancel_task(task_id) ⇒ Object
410 411 412 413 414 415 |
# File 'lib/cli/director.rb', line 410 def cancel_task(task_id) response_code, body = delete("/task/#{task_id}") raise AuthError if response_code == 401 raise MissingTask, "No task##{task_id} found" if response_code == 404 [body, response_code] end |
#change_job_state(deployment_name, manifest_yaml, job_name, index, new_state, options = {}) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/cli/director.rb', line 229 def change_job_state(deployment_name, manifest_yaml, job_name, index, new_state, = {}) = .dup url = "/deployments/#{deployment_name}/jobs/#{job_name}" url += "/#{index}" if index url += "?state=#{new_state}" [:payload] = manifest_yaml [:content_type] = "text/yaml" request_and_track(:put, url, ) end |
#cleanup_ssh(deployment_name, job, user_regex, indexes, options = {}) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/cli/director.rb', line 208 def cleanup_ssh(deployment_name, job, user_regex, indexes, = {}) = .dup url = "/deployments/#{deployment_name}/ssh" payload = { "command" => "cleanup", "deployment_name" => deployment_name, "target" => { "job" => job, "indexes" => (indexes || []).compact }, "params" => { "user_regex" => user_regex } } [:payload] = JSON.generate(payload) [:content_type] = "application/json" request_and_track(:post, url, ) end |
#create_property(deployment_name, property_name, value) ⇒ Object
305 306 307 308 309 |
# File 'lib/cli/director.rb', line 305 def create_property(deployment_name, property_name, value) url = "/deployments/#{deployment_name}/properties" payload = JSON.generate("name" => property_name, "value" => value) post(url, "application/json", payload) end |
#create_user(username, password) ⇒ Object
48 49 50 51 52 |
# File 'lib/cli/director.rb', line 48 def create_user(username, password) payload = JSON.generate("username" => username, "password" => password) response_code, _ = post("/users", "application/json", payload) response_code == 204 end |
#delete_deployment(name, options = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cli/director.rb', line 137 def delete_deployment(name, = {}) = .dup force = .delete(:force) url = "/deployments/#{name}" extras = [] extras << "force=true" if force request_and_track(:delete, add_query_string(url, extras), ) end |
#delete_property(deployment_name, property_name) ⇒ Object
317 318 319 320 |
# File 'lib/cli/director.rb', line 317 def delete_property(deployment_name, property_name) url = "/deployments/#{deployment_name}/properties/#{property_name}" delete(url, "application/json") end |
#delete_release(name, options = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cli/director.rb', line 149 def delete_release(name, = {}) = .dup force = .delete(:force) version = .delete(:version) url = "/releases/#{name}" extras = [] extras << "force=true" if force extras << "version=#{version}" if version request_and_track(:delete, add_query_string(url, extras), ) end |
#delete_stemcell(name, version, options = {}) ⇒ Object
132 133 134 135 |
# File 'lib/cli/director.rb', line 132 def delete_stemcell(name, version, = {}) = .dup request_and_track(:delete, "/stemcells/#{name}/#{version}", ) end |
#deploy(manifest_yaml, options = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/cli/director.rb', line 163 def deploy(manifest_yaml, = {}) = .dup recreate = .delete(:recreate) [:content_type] = "text/yaml" [:payload] = manifest_yaml url = "/deployments" extras = [] extras << "recreate=true" if recreate request_and_track(:post, add_query_string(url, extras), ) end |
#download_resource(id) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/cli/director.rb', line 293 def download_resource(id) status, tmp_file, _ = get("/resources/#{id}", nil, nil, {}, :file => true) if status == 200 tmp_file else raise DirectorError, "Cannot download resource `#{id}': HTTP status #{status}" end end |
#exists? ⇒ Boolean
29 30 31 32 33 34 35 36 |
# File 'lib/cli/director.rb', line 29 def exists? get_status true rescue AuthError true # For compatibility with directors that return 401 for /info rescue DirectorError false end |
#fetch_logs(deployment_name, job_name, index, log_type, filters = nil, options = {}) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/cli/director.rb', line 260 def fetch_logs(deployment_name, job_name, index, log_type, filters = nil, = {}) = .dup url = "/deployments/#{deployment_name}/jobs/#{job_name}" url += "/#{index}/logs?type=#{log_type}&filters=#{filters}" status, task_id = request_and_track(:get, url, ) # TODO: this should be done in command handler, not in director.rb return nil if status != :done get_task_result(task_id) end |
#fetch_vm_state(deployment_name, options = {}) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/cli/director.rb', line 274 def fetch_vm_state(deployment_name, = {}) = .dup url = "/deployments/#{deployment_name}/vms?format=full" status, task_id = request_and_track(:get, url, ) # TODO: this should be done in command handler, not in director.rb if status != :done raise DirectorError, "Failed to fetch VMs information from director" end output = get_task_result_log(task_id) output.to_s.split("\n").map do |vm_state| JSON.parse(vm_state) end end |
#get_current_time ⇒ Object
354 355 356 357 |
# File 'lib/cli/director.rb', line 354 def get_current_time _, _, headers = get("/info") Time.parse(headers[:date]) rescue nil end |
#get_deployment(name) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/cli/director.rb', line 109 def get_deployment(name) status, body = get_json_with_status("/deployments/#{name}") if status == 404 raise DeploymentNotFound, "Deployment `#{name}' not found" end body end |
#get_property(deployment_name, property_name) ⇒ Object
322 323 324 325 |
# File 'lib/cli/director.rb', line 322 def get_property(deployment_name, property_name) url = "/deployments/#{deployment_name}/properties/#{property_name}" get_json_with_status(url) end |
#get_release(name) ⇒ Object
94 95 96 |
# File 'lib/cli/director.rb', line 94 def get_release(name) get_json("/releases/#{name}") end |
#get_status ⇒ Object
65 66 67 |
# File 'lib/cli/director.rb', line 65 def get_status get_json("/info") end |
#get_task(task_id) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/cli/director.rb', line 365 def get_task(task_id) response_code, body = get("/tasks/#{task_id}") raise AuthError if response_code == 401 raise MissingTask, "Task #{task_id} not found" if response_code == 404 if response_code != 200 raise TaskTrackError, "Got HTTP #{response_code} " + "while tracking task state" end JSON.parse(body) rescue JSON::ParserError raise TaskTrackError, "Cannot parse task JSON, " + "incompatible director version" end |
#get_task_output(task_id, offset, log_type = nil) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/cli/director.rb', line 394 def get_task_output(task_id, offset, log_type = nil) uri = "/tasks/#{task_id}/output" uri += "?type=#{log_type}" if log_type headers = {"Range" => "bytes=#{offset}-"} response_code, body, headers = get(uri, nil, nil, headers) if response_code == 206 && headers[:content_range].to_s =~ /bytes \d+-(\d+)\/\d+/ new_offset = $1.to_i + 1 else new_offset = nil end [body, new_offset] end |
#get_task_result(task_id) ⇒ Object
385 386 387 |
# File 'lib/cli/director.rb', line 385 def get_task_result(task_id) get_task(task_id)["result"] end |
#get_task_result_log(task_id) ⇒ Object
389 390 391 392 |
# File 'lib/cli/director.rb', line 389 def get_task_result_log(task_id) log, _ = get_task_output(task_id, 0, "result") log end |
#get_task_state(task_id) ⇒ Object
381 382 383 |
# File 'lib/cli/director.rb', line 381 def get_task_state(task_id) get_task(task_id)["state"] end |
#get_time_difference ⇒ Object
359 360 361 362 363 |
# File 'lib/cli/director.rb', line 359 def get_time_difference # This includes the round-trip to director ctime = get_current_time ctime ? Time.now - ctime : 0 end |
#get_version ⇒ Object
61 62 63 |
# File 'lib/cli/director.rb', line 61 def get_version get_status["version"] end |
#list_deployments ⇒ Object
77 78 79 |
# File 'lib/cli/director.rb', line 77 def list_deployments get_json("/deployments") end |
#list_problems(deployment_name) ⇒ Object
339 340 341 342 |
# File 'lib/cli/director.rb', line 339 def list_problems(deployment_name) url = "/deployments/#{deployment_name}/problems" get_json(url) end |
#list_properties(deployment_name) ⇒ Object
327 328 329 330 |
# File 'lib/cli/director.rb', line 327 def list_properties(deployment_name) url = "/deployments/#{deployment_name}/properties" get_json(url) end |
#list_recent_tasks(count = 30) ⇒ Object
89 90 91 92 |
# File 'lib/cli/director.rb', line 89 def list_recent_tasks(count = 30) count = [count.to_i, 100].min get_json("/tasks?limit=#{count}") end |
#list_releases ⇒ Object
73 74 75 |
# File 'lib/cli/director.rb', line 73 def list_releases get_json("/releases") end |
#list_running_tasks ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/cli/director.rb', line 81 def list_running_tasks if version_less(get_version, "0.3.5") get_json("/tasks?state=processing") else get_json("/tasks?state=processing,cancelling,queued") end end |
#list_stemcells ⇒ Object
69 70 71 |
# File 'lib/cli/director.rb', line 69 def list_stemcells get_json("/stemcells") end |
#list_vms(name) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/cli/director.rb', line 117 def list_vms(name) status, body = get_json_with_status("/deployments/#{name}/vms") if status == 404 raise DeploymentNotFound, "Deployment `#{name}' not found" end body end |
#match_packages(manifest_yaml) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cli/director.rb', line 98 def match_packages(manifest_yaml) url = "/packages/matches" status, body = post(url, "text/yaml", manifest_yaml) if status == 200 JSON.parse(body) else err((status, body)) end end |
#parse_error_message(status, body) ⇒ Object
511 512 513 514 515 516 517 518 519 520 |
# File 'lib/cli/director.rb', line 511 def (status, body) parsed_body = JSON.parse(body.to_s) rescue {} if parsed_body["code"] && parsed_body["description"] "Error %s: %s" % [parsed_body["code"], parsed_body["description"]] else "HTTP %s: %s" % [status, body] end end |
#perform_cloud_scan(deployment_name, options = {}) ⇒ Object
332 333 334 335 336 337 |
# File 'lib/cli/director.rb', line 332 def perform_cloud_scan(deployment_name, = {}) = .dup url = "/deployments/#{deployment_name}/scans" request_and_track(:post, url, ) end |
#rename_job(deployment_name, manifest_yaml, old_name, new_name, force = false, options = {}) ⇒ Object
TODO: should pass ‘force’ with options, not as a separate argument
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/cli/director.rb', line 244 def rename_job(deployment_name, manifest_yaml, old_name, new_name, force = false, = {}) = .dup url = "/deployments/#{deployment_name}/jobs/#{old_name}" extras = [] extras << "new_name=#{new_name}" extras << "force=true" if force [:content_type] = "text/yaml" [:payload] = manifest_yaml request_and_track(:put, add_query_string(url, extras), ) end |
#request(method, uri, content_type = nil, payload = nil, headers = {}, options = {}) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/cli/director.rb', line 462 def request(method, uri, content_type = nil, payload = nil, headers = {}, = {}) headers = headers.dup tmp_file = nil headers["Content-Type"] = content_type if content_type if [:file] tmp_file = File.open(File.join(Dir.mktmpdir, "streamed-response"), "w") response_reader = lambda do |part| tmp_file.write(part) end else response_reader = nil end response = perform_http_request(method, @director_uri + uri, payload, headers, &response_reader) if [:file] tmp_file.close body = tmp_file.path else body = response.body end if DIRECTOR_HTTP_ERROR_CODES.include?(response.code) raise DirectorError, (response.code, body) end headers = response.headers.inject({}) do |hash, (k, v)| # Some HTTP clients symbolize headers, some do not. # To make it easier to switch between them, we try # to symbolize them ourselves. hash[k.to_s.downcase.gsub(/-/, "_").to_sym] = v hash end [response.code, body, headers] rescue URI::Error, SocketError, Errno::ECONNREFUSED => e raise DirectorInaccessible, "cannot access director (#{e.})" rescue SystemCallError => e raise DirectorError, "System call error while talking to director: #{e}" end |
#request_and_track(method, uri, options = {}) ⇒ Object
Perform director HTTP request and track director task (if request started one).
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/cli/director.rb', line 428 def request_and_track(method, uri, = {}) = .dup content_type = .delete(:content_type) payload = .delete(:payload) track_opts = http_status, _, headers = request(method, uri, content_type, payload) location = headers[:location] redirected = http_status == 302 task_id = nil if redirected if location =~ /\/tasks\/(\d+)\/?$/ # Looks like we received task URI task_id = $1 tracker = Bosh::Cli::TaskTracker.new(self, task_id, track_opts) status = tracker.track else status = :non_trackable end else status = :failed end [status, task_id] end |
#setup_ssh(deployment_name, job, index, user, public_key, password, options = {}) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/cli/director.rb', line 178 def setup_ssh(deployment_name, job, index, user, public_key, password, = {}) = .dup url = "/deployments/#{deployment_name}/ssh" payload = { "command" => "setup", "deployment_name" => deployment_name, "target" => { "job" => job, "indexes" => [index].compact }, "params" => { "user" => user, "public_key" => public_key, "password" => password } } [:payload] = JSON.generate(payload) [:content_type] = "application/json" status, task_id = request_and_track(:post, url, ) # TODO: this needs to be done in command handler, not in director.rb return nil if status != :done JSON.parse(get_task_result_log(task_id)) end |
#update_property(deployment_name, property_name, value) ⇒ Object
311 312 313 314 315 |
# File 'lib/cli/director.rb', line 311 def update_property(deployment_name, property_name, value) url = "/deployments/#{deployment_name}/properties/#{property_name}" payload = JSON.generate("value" => value) put(url, "application/json", payload) end |
#upload_and_track(method, uri, filename, options = {}) ⇒ Object
455 456 457 458 459 460 |
# File 'lib/cli/director.rb', line 455 def upload_and_track(method, uri, filename, = {}) file = FileWithProgressBar.open(filename, "r") request_and_track(method, uri, .merge(:payload => file)) ensure file. if file end |
#upload_release(filename, options = {}) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/cli/director.rb', line 125 def upload_release(filename, = {}) = .dup [:content_type] = "application/x-compressed" upload_and_track(:post, "/releases", filename, ) end |
#upload_stemcell(filename, options = {}) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/cli/director.rb', line 54 def upload_stemcell(filename, = {}) = .dup [:content_type] = "application/x-compressed" upload_and_track(:post, "/stemcells", filename, ) end |
#uuid ⇒ Object
25 26 27 |
# File 'lib/cli/director.rb', line 25 def uuid @uuid ||= get_status["uuid"] end |