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

Inherits:
Object
  • Object
show all
Includes:
Cli::Common, PacketCommon, Machine::CertHelper, 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, #refresh, #ssh_key_exist?, #ssh_key_label, #user_data

Constructor Details

#initialize(token) ⇒ MasterProvisioner

Returns a new instance of MasterProvisioner.

Parameters:

  • token (String)

    Packet token



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

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



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

def http_client
  @http_client
end

Instance Method Details

#generate_nameObject



98
99
100
# File 'lib/kontena/machine/packet/master_provisioner.rb', line 98

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

#master_running?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/kontena/machine/packet/master_provisioner.rb', line 102

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

#run!(opts) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kontena/machine/packet/master_provisioner.rb', line 19

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(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
    spinner "Generating a self-signed SSL certificate" do
      ssl_cert = generate_self_signed_cert
    end
  end

  if respond_to?(:certificate_public_key) && !opts[:ssl_cert]
    ssl_cert_public = certificate_public_key(ssl_cert)
  end

  name = generate_name

  userdata_vars = opts.merge(
    ssl_cert: ssl_cert,
    server_name: name.sub('kontena-master-', '')
  )

  device = project.new_device(
    hostname: 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')
  )

  spinner "Creating a 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 = spinner "Looking for device public IP" do
    device_public_ip(device)
  end
  master_url = "https://#{public_ip['address']}"

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

  spinner "Waiting for #{device.hostname.colorize(:cyan)} to start (estimate 4 minutes)" do
    sleep 0.5 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}".colorize(:green)

  {
    name: name.sub('kontena-master-', ''),
    public_ip: public_ip['address'],
    code: opts[:initial_admin_code],
    provider: 'packet',
    ssl_certificate: ssl_cert_public,
    version: master_version
  }
end