Class: Kontena::Machine::Upcloud::NodeProvisioner
- Inherits:
-
Object
- Object
- Kontena::Machine::Upcloud::NodeProvisioner
- Includes:
- Cli::ShellSpinner, UpcloudCommon, RandomName
- Defined in:
- lib/kontena/machine/upcloud/node_provisioner.rb
Instance Attribute Summary collapse
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #erb(template, vars) ⇒ Object
- #generate_name ⇒ Object
-
#initialize(api_client, upcloud_username, upcloud_password) ⇒ NodeProvisioner
constructor
A new instance of NodeProvisioner.
- #node_exists_in_grid?(grid, hostname) ⇒ Boolean
- #run!(opts) ⇒ Object
- #set_labels(node, labels) ⇒ Object
- #user_data(vars) ⇒ Object
Methods included from UpcloudCommon
#abort_unless_api_access, #api_access?, #find_plan, #find_template, #get_server, #upcloud_client, #zone_exist?
Constructor Details
#initialize(api_client, upcloud_username, upcloud_password) ⇒ NodeProvisioner
Returns a new instance of NodeProvisioner.
18 19 20 21 22 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 18 def initialize(api_client, upcloud_username, upcloud_password) @api_client = api_client @username = upcloud_username @password = upcloud_password end |
Instance Attribute Details
#api_client ⇒ Object (readonly)
Returns the value of attribute api_client.
13 14 15 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 13 def api_client @api_client end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
13 14 15 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 13 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
13 14 15 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 13 def username @username end |
Instance Method Details
#erb(template, vars) ⇒ Object
110 111 112 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 110 def erb(template, vars) ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding }) end |
#generate_name ⇒ Object
102 103 104 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 102 def generate_name "#{super}-#{rand(1..99)}" end |
#node_exists_in_grid?(grid, hostname) ⇒ Boolean
106 107 108 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 106 def node_exists_in_grid?(grid, hostname) api_client.get("grids/#{grid}/nodes")['nodes'].find{|n| n['name'] == hostname} end |
#run!(opts) ⇒ Object
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 90 91 92 93 94 95 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 24 def run!(opts) if File.readable?(File.(opts[:ssh_key])) ssh_key = File.read(File.(opts[:ssh_key])).strip end abort('Invalid ssh key') unless ssh_key && ssh_key.start_with?('ssh-') userdata_vars = { version: opts[:version], master_uri: opts[:master_uri], grid_token: opts[:grid_token], } abort_unless_api_access abort('CoreOS template not found on Upcloud') unless coreos_template = find_template('CoreOS Stable') abort('Server plan not found on Upcloud') unless plan = find_plan(opts[:plan]) abort('Zone not found on Upcloud') unless zone_exist?(opts[:zone]) hostname = generate_name device_data = { server: { zone: opts[:zone], title: "Kontena Grid #{opts[:grid]} Node #{hostname}", hostname: hostname, plan: plan[:name], vnc: 'off', timezone: 'UTC', user_data: user_data(userdata_vars), firewall: 'off', storage_devices: { storage_device: [ { action: 'clone', storage: coreos_template[:uuid], title: "From template #{coreos_template[:title]}", size: plan[:storage_size], tier: 'maxiops' } ] }, login_user: { create_password: 'no', username: 'root', ssh_keys: { ssh_key: [ssh_key] } } } }.to_json spinner "Creating Upcloud node #{hostname.colorize(:cyan)} " do response = post('server', body: device_data) if response.has_key?(:error) abort("\nUpcloud server creation failed (#{response[:error].fetch(:error_message, '')})") end device_data = response[:server] until device_data && device_data.fetch(:state, nil).to_s == 'maintenance' device_data = get("server/#{device[:uuid]}").fetch(:server, {}) rescue nil sleep 5 end end node = nil spinner "Waiting for node #{hostname.colorize(:cyan)} join to grid #{opts[:grid].colorize(:cyan)} " do sleep 2 until node = node_exists_in_grid?(opts[:grid], hostname) end set_labels(node, ["region=#{opts[:zone]}", "provider=upcloud"]) end |
#set_labels(node, labels) ⇒ Object
114 115 116 117 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 114 def set_labels(node, labels) data = {labels: labels} api_client.put("nodes/#{node['id']}", data, {}, {'Kontena-Grid-Token' => node['grid']['token']}) end |
#user_data(vars) ⇒ Object
97 98 99 100 |
# File 'lib/kontena/machine/upcloud/node_provisioner.rb', line 97 def user_data(vars) cloudinit_template = File.join(__dir__ , '/cloudinit.yml') erb(File.read(cloudinit_template), vars) end |