Class: Kontena::Machine::Packet::MasterProvisioner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PacketCommon

#api_retry, #check_or_create_ssh_key, #create_ssh_key, #device_public_ip, #erb, #find_device, #find_facility, #find_os, #find_plan, #find_project, #login, #ssh_key_exist?, #ssh_key_label, #user_data

Methods included from CertHelper

#generate_self_signed_cert

Constructor Details

#initialize(token) ⇒ MasterProvisioner

Returns a new instance of MasterProvisioner.

Parameters:

  • token (String)

    Packet token



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

def initialize(token)
  @client = (token)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/kontena/machine/packet/master_provisioner.rb', line 11

def client
  @client
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



11
12
13
# File 'lib/kontena/machine/packet/master_provisioner.rb', line 11

def http_client
  @http_client
end

Instance Method Details

#generate_nameObject



80
81
82
# File 'lib/kontena/machine/packet/master_provisioner.rb', line 80

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

#master_running?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/kontena/machine/packet/master_provisioner.rb', line 84

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

#run!(opts) ⇒ Object



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

def run!(opts)
  abort('Project does not exist in Packet') unless project = find_project(opts[:project])
  abort('Facility does not exist in Packet') unless facility = find_facility(opts[:facility])
  abort('Operating system coreos_stable does not exist in Packet') unless os = find_os('coreos_stable')
  abort('Device type does not exist in Packet') unless plan = find_plan(opts[:plan])

  check_or_create_ssh_key(File.expand_path(opts[:ssh_key])) if opts[:ssh_key]

  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

  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 = project.new_device(
    hostname: generate_name,
    facility: facility.to_hash,
    operating_system: os.to_hash,
    plan: plan.to_hash,
    billing_cycle: opts[:billing],
    locked: true,
    userdata: user_data(userdata_vars, 'cloudinit_master.yml')
  )

  ShellSpinner "Creating Packet device #{device.hostname.colorize(:cyan)} " do
    api_retry "Packet API reported an error, please try again" do
      response = client.create_device(device)
      raise response.body unless response.success?
    end

    until device && [:active, :provisioning, :powering_on].include?(device.state)
      device = find_device(project.id, device.hostname) rescue nil
      sleep 5
    end
  end

  public_ip = device_public_ip(device)
  master_url = "https://#{public_ip['address']}"

  Excon.defaults[:ssl_verify_peer] = false
  @http_client = Excon.new("#{master_url}", :connect_timeout => 10)

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

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