|
8 | 8 | require 'timeout' |
9 | 9 | require 'etc' |
10 | 10 |
|
11 | | -class PEAdm |
12 | | - class Task |
13 | | - class PuppetInfraUpgrade |
14 | | - def initialize(params) |
15 | | - @type = params['type'] |
16 | | - @targets = params['targets'] |
17 | | - @timeout = params['wait_until_connected_timeout'] |
18 | | - @token_file = params['token_file'] |
19 | | - end |
| 11 | +# Class to run and execute the `puppet infra upgrade` command as a task. |
| 12 | +class PuppetInfraUpgrade |
| 13 | + def initialize(params) |
| 14 | + @type = params['type'] |
| 15 | + @targets = params['targets'] |
| 16 | + @timeout = params['wait_until_connected_timeout'] |
| 17 | + @token_file = params['token_file'] |
| 18 | + end |
20 | 19 |
|
21 | | - def execute! |
22 | | - exit 0 if @targets.empty? |
23 | | - token_file = @token_file || File.join(Etc.getpwuid.dir, '.puppetlabs', 'token') |
| 20 | + def execute! |
| 21 | + exit 0 if @targets.empty? |
| 22 | + token_file = @token_file || File.join(Etc.getpwuid.dir, '.puppetlabs', 'token') |
24 | 23 |
|
25 | | - cmd = ['/opt/puppetlabs/bin/puppet-infrastructure', '--render-as', 'json', 'upgrade'] |
26 | | - cmd << '--token-file' << token_file unless @token_file.nil? |
27 | | - cmd << @type << @targets.join(',') |
| 24 | + cmd = ['/opt/puppetlabs/bin/puppet-infrastructure', '--render-as', 'json', 'upgrade'] |
| 25 | + cmd << '--token-file' << token_file unless @token_file.nil? |
| 26 | + cmd << @type << @targets.join(',') |
28 | 27 |
|
29 | | - wait_until_connected(nodes: @targets, token_file: token_file, timeout: @timeout) |
| 28 | + wait_until_connected(nodes: @targets, token_file: token_file, timeout: @timeout) |
30 | 29 |
|
31 | | - stdouterr, status = Open3.capture2e(*cmd) |
32 | | - STDOUT.puts stdouterr |
| 30 | + stdouterr, status = Open3.capture2e(*cmd) |
| 31 | + STDOUT.puts stdouterr |
33 | 32 |
|
34 | | - # Exit code 11 indicates PuppetDB sync in progress, just not yet |
35 | | - # finished. We consider that success. |
36 | | - if [0, 11].include?(status.exitstatus) |
37 | | - return |
38 | | - else |
39 | | - exit status.exitstatus |
40 | | - end |
41 | | - end |
| 33 | + # Exit code 11 indicates PuppetDB sync in progress, just not yet |
| 34 | + # finished. We consider that success. |
| 35 | + if [0, 11].include?(status.exitstatus) |
| 36 | + nil |
| 37 | + else |
| 38 | + exit status.exitstatus |
| 39 | + end |
| 40 | + end |
42 | 41 |
|
43 | | - def inventory_uri |
44 | | - @inventory_uri ||= URI.parse('https://localhost:8143/orchestrator/v1/inventory') |
45 | | - end |
| 42 | + def inventory_uri |
| 43 | + @inventory_uri ||= URI.parse('https://localhost:8143/orchestrator/v1/inventory') |
| 44 | + end |
46 | 45 |
|
47 | | - def request_object(nodes:, token_file:) |
48 | | - token = File.read(token_file) |
49 | | - body = { |
50 | | - 'nodes' => nodes, |
51 | | - }.to_json |
| 46 | + def request_object(nodes:, token_file:) |
| 47 | + token = File.read(token_file) |
| 48 | + body = { |
| 49 | + 'nodes' => nodes, |
| 50 | + }.to_json |
52 | 51 |
|
53 | | - request = Net::HTTP::Post.new(inventory_uri.request_uri) |
54 | | - request['Content-Type'] = 'application/json' |
55 | | - request['X-Authentication'] = token |
56 | | - request.body = body |
| 52 | + request = Net::HTTP::Post.new(inventory_uri.request_uri) |
| 53 | + request['Content-Type'] = 'application/json' |
| 54 | + request['X-Authentication'] = token |
| 55 | + request.body = body |
57 | 56 |
|
58 | | - request |
59 | | - end |
| 57 | + request |
| 58 | + end |
60 | 59 |
|
61 | | - def http_object |
62 | | - http = Net::HTTP.new(inventory_uri.host, inventory_uri.port) |
63 | | - http.use_ssl = true |
64 | | - http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 60 | + def http_object |
| 61 | + http = Net::HTTP.new(inventory_uri.host, inventory_uri.port) |
| 62 | + http.use_ssl = true |
| 63 | + http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
65 | 64 |
|
66 | | - http |
67 | | - end |
| 65 | + http |
| 66 | + end |
68 | 67 |
|
69 | | - def wait_until_connected(nodes:, token_file:, timeout: 120) |
70 | | - http = http_object |
71 | | - request = request_object(nodes: nodes, token_file: token_file) |
72 | | - inventory = {} |
73 | | - Timeout.timeout(timeout) do |
74 | | - loop do |
75 | | - response = http.request(request) |
76 | | - unless response.is_a? Net::HTTPSuccess |
77 | | - raise "Unexpected result from orchestrator: #{response.class}\n#{response}" |
78 | | - end |
79 | | - inventory = JSON.parse(response.body) |
80 | | - break if inventory['items'].all? { |item| item['connected'] } |
81 | | - sleep(1) |
82 | | - end |
| 68 | + def wait_until_connected(nodes:, token_file:, timeout: 120) |
| 69 | + http = http_object |
| 70 | + request = request_object(nodes: nodes, token_file: token_file) |
| 71 | + inventory = {} |
| 72 | + Timeout.timeout(timeout) do |
| 73 | + loop do |
| 74 | + response = http.request(request) |
| 75 | + unless response.is_a? Net::HTTPSuccess |
| 76 | + raise "Unexpected result from orchestrator: #{response.class}\n#{response}" |
83 | 77 | end |
84 | | - rescue Timeout::Error |
85 | | - raise 'Timed out waiting for nodes to be connected to orchestrator: ' + |
86 | | - inventory['items'].reject { |item| item['connected'] } |
87 | | - .map { |item| item['name'] } |
88 | | - .to_s |
| 78 | + inventory = JSON.parse(response.body) |
| 79 | + break if inventory['items'].all? { |item| item['connected'] } |
| 80 | + sleep(1) |
89 | 81 | end |
90 | 82 | end |
| 83 | + rescue Timeout::Error |
| 84 | + raise 'Timed out waiting for nodes to be connected to orchestrator: ' + |
| 85 | + inventory['items'].reject { |item| item['connected'] } |
| 86 | + .map { |item| item['name'] } |
| 87 | + .to_s |
91 | 88 | end |
92 | 89 | end |
93 | 90 |
|
94 | 91 | # Run the task unless an environment flag has been set, signaling not to. The |
95 | 92 | # environment flag is used to disable auto-execution and enable Ruby unit |
96 | 93 | # testing of this task. |
97 | 94 | unless ENV['RSPEC_UNIT_TEST_MODE'] |
98 | | - upgrade = PEAdm::Task::PuppetInfraUpgrade.new(JSON.parse(STDIN.read)) |
| 95 | + upgrade = PuppetInfraUpgrade.new(JSON.parse(STDIN.read)) |
99 | 96 | upgrade.execute! |
100 | 97 | end |
0 commit comments