Class: Octo::OctoHelperMethods
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Octo::OctoHelperMethods
- Defined in:
- lib/chef/knife/octo.rb
Instance Method Summary collapse
- #create_local_file_by_id(thing) ⇒ Object
- #create_local_file_by_name(thing) ⇒ Object
- #delete_local_file ⇒ Object
- #eid_from_name(env_name) ⇒ Object
- #envname_from_id(env_id) ⇒ Object
-
#generic_call(method, resource, params) ⇒ Object
generic helper methods.
-
#initialize(apikey, instance) ⇒ OctoHelperMethods
constructor
A new instance of OctoHelperMethods.
-
#local_file_by_id(e_id, e_name) ⇒ Object
couple with create_local_file_by_id ; e_name will be used in a future release.
-
#local_file_by_key(key, value) ⇒ Object
couple with create_local_file_by_name ; e_name will be used in a future release.
- #mid_from_mname(m_name) ⇒ Object
- #parse_deployment(d_object, islong) ⇒ Object
- #parse_env_deets(env_object, islong) ⇒ Object
- #parse_env_list(env_list_object, islong) ⇒ Object
- #parse_machine_deets(m_object, islong) ⇒ Object
- #parse_machine_list(m_list_object, islong) ⇒ Object
- #parse_project_list(p_list_object, islong, manyitems) ⇒ Object
- #parse_project_release(r_object) ⇒ Object
-
#parse_variable_list(var_list_object, islong) ⇒ Object
specific helper methods.
- #parse_variable_sets(var_name, islong) ⇒ Object
- #parse_variable_sets_helper(var_obj, islong) ⇒ Object
Constructor Details
#initialize(apikey, instance) ⇒ OctoHelperMethods
Returns a new instance of OctoHelperMethods.
11 12 13 14 |
# File 'lib/chef/knife/octo.rb', line 11 def initialize(apikey, instance) @apikey = apikey @instance = instance end |
Instance Method Details
#create_local_file_by_id(thing) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/chef/knife/octo.rb', line 157 def create_local_file_by_id(thing) tmp_location = "/tmp/ajnfieneom" request = generic_call('GET', "#{thing}/all", false) things = [] request['message'].each do |single| things << { "#{single['Id']}" => "#{single['Name']}" } end if request['response'] == 'ok' file = File.open("#{tmp_location}",'w+') file.write(JSON.pretty_generate(things)) end file.read end |
#create_local_file_by_name(thing) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/chef/knife/octo.rb', line 173 def create_local_file_by_name(thing) tmp_location = "/tmp/ajnfieneoma" request = generic_call('GET', "#{thing}/all", false) things = [] request['message'].each do |single| things << { "#{single['Name']}" => "#{single['Id']}" } end if request['response'] == 'ok' file = File.open("#{tmp_location}",'w+') file.write(JSON.pretty_generate(things)) file.close end end |
#delete_local_file ⇒ Object
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/chef/knife/octo.rb', line 212 def delete_local_file tmp_location = "/tmp/ajnfieneom" tmp_location_another = "/tmp/ajnfieneoma" if File.file?(tmp_location) File.delete(tmp_location) end if File.file?(tmp_location_another) File.delete(tmp_location_another) end end |
#eid_from_name(env_name) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/chef/knife/octo.rb', line 107 def eid_from_name(env_name) output = { 'response' => 'error', 'message' => false } request = generic_call('GET', 'environments/all', false) if request['response'] == 'ok' parsed_list = parse_env_list(request['message'],true) JSON.parse(parsed_list).each do |some_env| if some_env['name'] == env_name output['response'] = 'ok' output ['message'] = some_env['properties']['id'] end end if output['message'].class.to_s == 'FalseClass' output['response'] = 'notok' output ['message'] = 'Environment name not found' end else output['response'] = request['response'] output ['message'] = request['message'] end return output end |
#envname_from_id(env_id) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/chef/knife/octo.rb', line 91 def envname_from_id(env_id) output = { 'response' => 'error', 'message' => false } request = generic_call('GET', "environments/#{env_id}", false) if request['response'] == 'ok' output['response'] = 'ok' output['message'] = request['message']['Name'] else output['response'] = request['response'] output ['message'] = request['message'] end return output end |
#generic_call(method, resource, params) ⇒ Object
generic helper methods
18 19 20 21 22 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/chef/knife/octo.rb', line 18 def generic_call(method, resource, params) instance = @instance apikey = @apikey output = { 'response' => 'error', 'message' => false } # Format URI object uri = "https://#{instance}/api/#{resource}" if params # logic to do params here for a PUSH / POST, currently not supported end uri = URI.parse(uri) # Format HTTPS request object header = { 'X-Octopus-ApiKey' => apikey } # Create HTTP object http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if method == 'GET' # put try catch here req = Net::HTTP::Get.new(uri.request_uri, header) else # put try catch here req = Net::HTTP::Post.new(uri.request_uri, header) end # Execute HTTP request begin result = http.request(req) rescue => exception print "#{'ERROR'.red} : Could not send request to #{'https://'.red}#{instance.red} - #{exception}\n" exit else "" # had an older colorize block here end ## Validate result case result when Net::HTTPOK output['response'] = 'ok' result_hash = {} begin result_hash = JSON.parse("#{result.body.force_encoding('UTF-8')}") rescue result_hash = { 'HTTP success type' => "#{result.class.name}" } if result.body result_hash['body'] = "#{result.body}" end if result. result_hash['message'] = "#{result.message}" end if result['location'] result_hash['location'] = "#{result['location']}" end end output['message'] = result_hash else output['response'] = 'notok' output['message'] = "#{'ERROR - '.red} HTTP Response: #{result.class.name}" if result. output['message'] += " - Message: #{result.message}" end if result.body output['message'] += "\nCaused by: #{JSON.parse(result.body)['ErrorMessage'].red}\n" end end return output end |
#local_file_by_id(e_id, e_name) ⇒ Object
couple with create_local_file_by_id ; e_name will be used in a future release
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/chef/knife/octo.rb', line 190 def local_file_by_id(e_id,e_name) # couple with create_local_file_by_id ; e_name will be used in a future release tmp_location = "/tmp/ajnfieneom" file = File.read("#{tmp_location}") json_envs = JSON.parse(file) json_envs.each do |json_env| unless json_env["#{e_id}"].nil? return json_env["#{e_id}"] end end end |
#local_file_by_key(key, value) ⇒ Object
couple with create_local_file_by_name ; e_name will be used in a future release
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/chef/knife/octo.rb', line 201 def local_file_by_key(key,value) # couple with create_local_file_by_name ; e_name will be used in a future release tmp_location = "/tmp/ajnfieneoma" file = File.read("#{tmp_location}") json_things = JSON.parse(file) json_things.each do |json_thing| unless json_thing["#{key}"].nil? return json_thing["#{key}"] end end end |
#mid_from_mname(m_name) ⇒ 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/chef/knife/octo.rb', line 132 def mid_from_mname(m_name) output = { 'response' => 'error', 'message' => false } request = generic_call('GET', 'machines/all', false) if request['response'] == 'ok' parsed_list = parse_machine_list(request['message'],true) JSON.parse(parsed_list).each do |some_machine| if some_machine['name'] == m_name output['response'] = 'ok' output ['message'] = some_machine['properties']['id'] end end if output['message'].class.to_s == 'FalseClass' output['response'] = 'notok' output ['message'] = 'Machine name not found' end else output['response'] = request['response'] output ['message'] = request['message'] end return output end |
#parse_deployment(d_object, islong) ⇒ Object
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/chef/knife/octo.rb', line 626 def parse_deployment(d_object,islong) output = { 'response' => 'error', 'message' => false } variable_sets = [] if islong.class.to_s == 'TrueClass' # long result begin steps = [] d_object['StepsToExecute'].each do |item| steps << { 'name' => item['ActionName'], 'properties' => { 'num' => item['ActionNumber'], 'roles' => item['Roles'], 'targets' => item['MachineNames'], 'excluded_machines' => item['ExcludedMachines'], } } end variable_sets << { 'steps' => steps } output['response'] = 'ok' output ['message'] = JSON.pretty_generate(variable_sets) rescue output['response'] = 'notok' output ['message'] = 'Could not parse data appropriately.' end return output else # not long begin steps = [] d_object['StepsToExecute'].each do |item| steps.push("#{item['ActionNumber']} : #{item['ActionName']}") end variable_sets << { 'steps' => steps } output['response'] = 'ok' output ['message'] = JSON.pretty_generate(variable_sets) rescue output['response'] = 'notok' output ['message'] = 'Could not parse data appropriately.' ensure return output end # ends end end |
#parse_env_deets(env_object, islong) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/chef/knife/octo.rb', line 344 def parse_env_deets(env_object,islong) output = { 'response' => 'error', 'message' => false } variable_sets = [] env_object['Items'].each do |item| if islong.class.to_s == 'TrueClass' variable_sets << { 'name' => item['Name'], 'properties' => { 'description' => item['Description'], 'id' => item['Id'], 'uri' => item['Endpoint']['Uri'], 'thumbprint' => item['Thumbprint'], 'isdisabled' => item['IsDisabled'], 'health' => item['HealthStatus'], 'status' => item['Status'], 'summary' => item['StatusSummary'], 'isinprocess' => item['IsInProcess'], 'roles' => item['Roles'], 'environments' => item['EnvironmentIds'] } } else variable_sets << item['Name'] end end if islong.class.to_s == 'TrueClass' # TRANSPOSE ENVS variable_sets.each do |single_var| new_envs = [] unless single_var['properties']['environments'].nil? single_var['properties']['environments'].each do |env_id| new_envs.push(local_file_by_id(env_id,false)) end end single_var['properties']['environments'] = new_envs end # END OF ENV TRANSPOSING end JSON.pretty_generate(variable_sets) end |
#parse_env_list(env_list_object, islong) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/chef/knife/octo.rb', line 326 def parse_env_list (env_list_object,islong) variable_sets = [] env_list_object.each do |item| if islong.class.to_s == 'TrueClass' variable_sets << { 'name' => item['Name'], 'properties' => { 'description'=> item['Description'], 'id'=> item['Id'] } } else variable_sets << item['Name'] end end return JSON.pretty_generate(variable_sets) end |
#parse_machine_deets(m_object, islong) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/chef/knife/octo.rb', line 406 def parse_machine_deets (m_object,islong) output = { 'response' => 'error', 'message' => false } variable_sets = [] if islong.class.to_s == 'TrueClass' variable_sets << { 'name' => m_object['Name'], 'properties' => { 'id' => m_object['Id'], 'uri' => m_object['Endpoint']['Uri'], 'thumbprint' => m_object['Thumbprint'], 'isdisabled' => m_object['IsDisabled'], 'health' => m_object['HealthStatus'], 'status' => m_object['Status'], 'summary' => m_object['StatusSummary'], 'isinprocess' => m_object['IsInProcess'], 'roles' => m_object['Roles'], 'environments' => m_object['EnvironmentIds'] } } else variable_sets << { 'name' => m_object['Name'], 'uri' => m_object['Endpoint']['Uri'], } end if islong.class.to_s == 'TrueClass' variable_sets.each do |single_hash| new_environments = [] single_hash['properties']['environments'].each do |env_id| request = envname_from_id(env_id) if request['response'] == 'ok' new_environments.push(request['message']) end end single_hash['properties']['environments'] = new_environments end end JSON.pretty_generate(variable_sets) end |
#parse_machine_list(m_list_object, islong) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/chef/knife/octo.rb', line 388 def parse_machine_list (m_list_object,islong) variable_sets = [] m_list_object.each do |item| if islong.class.to_s == 'TrueClass' variable_sets << { 'name' => item['Name'], 'properties' => { 'health'=> item['HealthStatus'], 'id'=> item['Id'] } } else variable_sets << item['Name'] end end return JSON.pretty_generate(variable_sets) end |
#parse_project_list(p_list_object, islong, manyitems) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
# File 'lib/chef/knife/octo.rb', line 449 def parse_project_list (p_list_object,islong,manyitems) output = { 'response' => 'error', 'message' => false } instance = @instance variable_sets = [] if manyitems.class.to_s == 'TrueClass' # it's a list p_list_object.each do |item| if islong.class.to_s == 'TrueClass' variable_sets << { 'name' => item['Name'], 'properties' => { 'id'=> item['Id'], 'deploymentid'=> item['DeploymentProcessId'], 'variableid'=> item['VariableSetId'], 'variablesets'=> item['IncludedLibraryVariableSetIds'], 'url' => "http://#{instance}#{item['Links']['Web']}" } } else variable_sets << { 'name' => item['Name'], 'url' => "http://#{instance}#{item['Links']['Web']}" } end end else # it is not a list and is a single item if islong.class.to_s == 'TrueClass' ## steps steps = generic_call('GET', "deploymentprocesses/#{p_list_object['DeploymentProcessId']}", false) if steps['response'] == 'ok' process = [] steps['message']['Steps'].each do |step| new_envs = [] step['Actions'][0]['Environments'].each do |envy_id| new_envs.push(local_file_by_id(envy_id,false)) end excl_envs = [] step['Actions'][0]['ExcludedEnvironments'].each do |excl_env_id| excl_envs.push(local_file_by_id(excl_env_id,false)) end process << { # not using Id here but it can be used "name" => step['Name'], "packages_required" => step['RequiresPackagesToBeAcquired'], "roles" => step['Properties']['Octopus.Action.TargetRoles'], "condition" => step['Condition'], "start_trigger" => step['StartTrigger'], "environments" => new_envs, "excluded_environments" => excl_envs, "isdisabled" => step['Actions'][0]['IsDisabled'], "details" => step['Actions'][0]['Properties'], } end end ## steps end variable_sets << { 'name' => p_list_object['Name'], 'properties' => { 'id'=> p_list_object['Id'], 'deploymentid'=> p_list_object['DeploymentProcessId'], 'variableid'=> p_list_object['VariableSetId'], 'variablesets'=> p_list_object['IncludedLibraryVariableSetIds'], 'url' => "http://#{instance}#{p_list_object['Links']['Web']}", }, 'releases' => [], 'process' => process } else ## steps steps = generic_call('GET', "deploymentprocesses/#{p_list_object['DeploymentProcessId']}", false) if steps['response'] == 'ok' process = [] steps['message']['Steps'].each do |step| process.push(step['Name']) end end ## steps end variable_sets << { 'name' => p_list_object['Name'], 'id' => p_list_object['Id'], 'url' => "http://#{instance}#{p_list_object['Links']['Web']}", 'process' => process } end end if islong.class.to_s == 'TrueClass' # logic for transforming library variables will go here create_local_file_by_id('libraryvariablesets') variable_sets.each do |single_var| new_vars = [] unless single_var['properties']['variablesets'].nil? single_var['properties']['variablesets'].each do |var_id| new_vars.push(local_file_by_id(var_id,false)) end end single_var['properties']['variablesets'] = new_vars end # end of logic for transforming library variables case variable_sets when Array # single item or manyitems is FALSE p_id = variable_sets[0]['properties']['id'] release_obj = generic_call('GET', "projects/#{p_id}/releases", false) if release_obj['response'] = 'ok' releases = [] release_obj['message']['Items'].each do |item| releases << { 'version' => item['Version'], 'properties' => { 'url' => "https://#{instance}#{item['Links']['Web']}", 'id' => item['Id'], } } end variable_sets[0]['releases'] = releases else output['response'] = 'notok' output ['message'] = release_obj['message'] end print "\n" output['response'] = 'ok' output['message'] = JSON.pretty_generate(variable_sets) when Hash # many items or manyitems is TRUE output['response'] = 'ok' output['message'] = JSON.pretty_generate(variable_sets) else output['response'] = 'notok' output['message'] = 'Could not parse data appropriately.' return output end else output['response'] = 'ok' output['message'] = JSON.pretty_generate(variable_sets) end if output['message'].class.to_s == 'FalseClass' output['response'] = 'notok' output ['message'] = 'Could not parse data appropriately.' end return output end |
#parse_project_release(r_object) ⇒ Object
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/chef/knife/octo.rb', line 598 def parse_project_release(r_object) output = { 'response' => 'error', 'message' => false } instance = @instance variable_sets = [] begin package_steps = [] r_object['SelectedPackages'].each do |package| package_steps.push(package['StepName']) end variable_sets << { 'Assembled' => r_object['Assembled'], 'url' => "http://#{instance}#{r_object['Links']['Web']}", 'package-steps' => package_steps, 'id' => r_object['Id'] } output['response'] = 'ok' output['message'] = JSON.pretty_generate(variable_sets) rescue output['response'] = 'notok' output ['message'] = 'Could not parse data appropriately.' end return output end |
#parse_variable_list(var_list_object, islong) ⇒ Object
specific helper methods
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/chef/knife/octo.rb', line 226 def parse_variable_list (var_list_object,islong) instance = @instance variable_sets = [] var_list_object.each do |item| if item['ContentType'] == 'Variables' if islong.class.to_s == 'TrueClass' variable_sets << { name: item['Name'], properties: { description: item['Description'], id: item['Id'], link: "http://#{instance}/app#/library/variables/#{item['Id']}" } } else variable_sets << item['Name'] end end end return JSON.pretty_generate(variable_sets) end |
#parse_variable_sets(var_name, islong) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/chef/knife/octo.rb', line 288 def parse_variable_sets (var_name,islong) output = { 'response' => 'error', 'message' => false } create_local_file_by_name('libraryvariablesets') unless local_file_by_key(var_name,false).nil? var_id = local_file_by_key(var_name,false) case var_id when String "" else output['response'] = 'notok' output['message'] = "Incorrect Variable Set provided" return output end request = generic_call('GET', "/variables/variableset-#{var_id}", false) if request['response'] == 'ok' output['response'] = 'ok' output['message'] = parse_variable_sets_helper(request['message'],islong) else output['response'] = 'notok' output['message'] = request['message'] end end #end if output['response'].class.to_s == 'FalseClass' output['response'] = 'notok' output['message'] = "Incorrect Variable Set provided" end delete_local_file return output end |
#parse_variable_sets_helper(var_obj, islong) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/chef/knife/octo.rb', line 248 def parse_variable_sets_helper (var_obj,islong) variable_sets = [] var_obj['Variables'].each do |item| if islong.class.to_s == 'TrueClass' # long variable_sets << { 'name' => item['Name'], 'value' => item['Value'], 'isencrypted' => item['IsSensitive'], 'type' => item['Type'], 'iseditable' => item['IsEditable'], 'scope' => item['Scope']['Environment'] # alter this here if we ever use Roles } else # short variable_sets.push(item['Name']) end end ## #DO env things here if islong.class.to_s == 'TrueClass' # long substitue # NEW CREATE ENV FILE create_local_file_by_id('environments') variable_sets.each do |single_var| new_envs = [] unless single_var['scope'].nil? single_var['scope'].each do |env_id| new_envs.push(local_file_by_id(env_id,false)) end end # SECRET single_var['scope'] = new_envs if single_var['isencrypted'].class.to_s == 'TrueClass' single_var['value'] = '************' end end end return JSON.pretty_generate(variable_sets) end |