Class: Kontena::Machine::Upcloud::MasterProvisioner

Inherits:
Object
  • Object
show all
Includes:
CertHelper, RandomName, UpcloudCommon
Defined in:
lib/kontena/machine/upcloud/master_provisioner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UpcloudCommon

#abort_unless_api_access, #api_access?, #client, #find_plan, #find_template, #get_server, #zone_exist?

Methods included from CertHelper

#generate_self_signed_cert

Constructor Details

#initialize(upcloud_username, upcloud_password) ⇒ MasterProvisioner



17
18
19
20
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 17

def initialize(upcloud_username, upcloud_password)
  @username = upcloud_username
  @password = upcloud_password
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



14
15
16
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 14

def http_client
  @http_client
end

#passwordObject (readonly)

Returns the value of attribute password.



14
15
16
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 14

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



14
15
16
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 14

def username
  @username
end

Instance Method Details

#erb(template, vars) ⇒ Object



132
133
134
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 132

def erb(template, vars)
  ERB.new(template, nil, '%<>-').result(OpenStruct.new(vars).instance_eval { binding })
end

#generate_nameObject



122
123
124
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 122

def generate_name
  "kontena-master-#{super}-#{rand(1..9)}"
end

#master_running?Boolean



126
127
128
129
130
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 126

def master_running?
  http_client.get(path: '/').status == 200
rescue
  false
end

#run!(opts) ⇒ Object



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
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
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 22

def run!(opts)
  if File.readable?(File.expand_path(opts[:ssh_key]))
    ssh_key = File.read(File.expand_path(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.expand_path(opts[:ssl_cert]))
    ssl_cert = File.read(File.expand_path(opts[:ssl_cert]))
  else
    ShellSpinner "Generating 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])

  hostname = generate_name

  userdata_vars = {
      ssl_cert: ssl_cert,
      auth_server: opts[:auth_server],
      version: opts[:version],
      vault_secret: opts[:vault_secret],
      vault_iv: opts[:vault_iv],
      mongodb_uri: opts[:mongodb_uri]
  }

  device_data = {
    server: {
      zone: opts[:zone],
      title: "Kontena Master #{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

  ShellSpinner "Creating Upcloud master #{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)

  ShellSpinner "Waiting for #{hostname.colorize(:cyan)} to start" do
    sleep 5 until master_running?
  end

  puts "Kontena Master is now running at #{master_url}"
  puts "Use #{"kontena login --name=#{hostname.sub('kontena-master-', '')} #{master_url}".colorize(:light_black)} to complete Kontena Master setup"
end

#user_data(vars) ⇒ Object



117
118
119
120
# File 'lib/kontena/machine/upcloud/master_provisioner.rb', line 117

def user_data(vars)
  cloudinit_template = File.join(__dir__ , '/cloudinit_master.yml')
  erb(File.read(cloudinit_template), vars)
end