Class: Kontena::Machine::Upcloud::MasterProvisioner
- Inherits:
-
Object
- Object
- Kontena::Machine::Upcloud::MasterProvisioner
- Includes:
- Cli::Common, UpcloudCommon, Machine::CertHelper, RandomName
- Defined in:
- lib/kontena/machine/upcloud/master_provisioner.rb
Instance Attribute Summary collapse
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_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(upcloud_username, upcloud_password) ⇒ MasterProvisioner
constructor
A new instance of MasterProvisioner.
- #master_running? ⇒ Boolean
- #run!(opts) ⇒ 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(upcloud_username, upcloud_password) ⇒ MasterProvisioner
Returns a new instance of MasterProvisioner.
18 19 20 21 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 18 def initialize(upcloud_username, upcloud_password) @username = upcloud_username @password = upcloud_password end |
Instance Attribute Details
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
15 16 17 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 15 def http_client @http_client end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
15 16 17 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 15 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
15 16 17 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 15 def username @username end |
Instance Method Details
#erb(template, vars) ⇒ Object
150 151 152 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 150 def erb(template, vars) ERB.new(template, nil, '%<>-').result(OpenStruct.new(vars).instance_eval { binding }) end |
#generate_name ⇒ Object
140 141 142 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 140 def generate_name "kontena-master-#{super}-#{rand(1..9)}" end |
#master_running? ⇒ Boolean
144 145 146 147 148 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 144 def master_running? http_client.get(path: '/').status == 200 rescue false end |
#run!(opts) ⇒ Object
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 23 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-') if opts[:ssl_cert] abort('Invalid ssl cert') unless File.exists?(File.(opts[:ssl_cert])) ssl_cert = File.read(File.(opts[:ssl_cert])) else spinner "Generating a self-signed SSL certificate" do ssl_cert = generate_self_signed_cert end end 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]) if opts[:name] server_name = opts[:name] hostname = opts[:name].start_with?('kontena-master') ? opts[:name] : "kontena-master-#{opts[:name]}" else hostname = generate_name server_name = hostname.sub('kontena-master-', '') end server_name = opts[:name] hostname = opts[:name] || generate_name userdata_vars = opts.merge( ssl_cert: ssl_cert, server_name: server_name ) device_data = { server: { zone: opts[:zone], title: "Kontena Master #{server_name}", 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 an Upcloud server #{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 device_public_ip = device_data[:ip_addresses][:ip_address].find do |ip| ip[:access].eql?('public') && ip[:family].eql?('IPv4') end abort('Server public ip not found, destroy manually.') unless device_public_ip master_url = "https://#{device_public_ip[:address]}" Excon.defaults[:ssl_verify_peer] = false @http_client = Excon.new("#{master_url}", :connect_timeout => 10) spinner "Waiting for #{hostname.colorize(:cyan)} to start" do sleep 1 until master_running? end master_version = nil spinner "Retrieving Kontena Master version" do master_version = JSON.parse(@http_client.get(path: '/').body)["version"] rescue nil end spinner "Kontena Master #{master_version} is now running at #{master_url}" { name: server_name, public_ip: device_public_ip[:address], provider: 'upcloud', version: master_version, code: opts[:initial_admin_code] } end |
#user_data(vars) ⇒ Object
135 136 137 138 |
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 135 def user_data(vars) cloudinit_template = File.join(__dir__ , '/cloudinit_master.yml') erb(File.read(cloudinit_template), vars) end |