Class: Kontena::Machine::Packet::NodeProvisioner

Inherits:
Object
  • Object
show all
Includes:
Cli::ShellSpinner, PacketCommon, RandomName
Defined in:
lib/kontena/machine/packet/node_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(api_client, token) ⇒ NodeProvisioner

Returns a new instance of NodeProvisioner.

Parameters:

  • api_client (Kontena::Client)

    Kontena api client

  • token (String)

    Digital Ocean token



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

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

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



9
10
11
# File 'lib/kontena/machine/packet/node_provisioner.rb', line 9

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/kontena/machine/packet/node_provisioner.rb', line 9

def client
  @client
end

Instance Method Details

#device_exists_in_grid?(grid, device) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/kontena/machine/packet/node_provisioner.rb', line 65

def device_exists_in_grid?(grid, device)
  api_client.get("grids/#{grid}/nodes")['nodes'].find{|n| n['name'] == device.hostname}
end

#generate_nameObject



61
62
63
# File 'lib/kontena/machine/packet/node_provisioner.rb', line 61

def generate_name
  "#{super}-#{rand(1..99)}"
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
# File 'lib/kontena/machine/packet/node_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(opts[:ssh_key]) if opts[:ssh_key]

  userdata_vars = {
    version: opts[:version],
    master_uri: opts[:master_uri],
    grid_token: opts[:grid_token],
  }

  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: false,
    userdata: user_data(userdata_vars, 'cloudinit.yml')
  )

  spinner "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 1
    end
  end

  node = nil
  spinner "Waiting for node #{device.hostname.colorize(:cyan)} join to grid #{opts[:grid].colorize(:cyan)} (estimate 4 minutes) " do
    sleep 0.5 until node = device_exists_in_grid?(opts[:grid], device)
  end
  set_labels(node, ["region=#{opts[:facility]}", "provider=packet"])
end

#set_labels(node, labels) ⇒ Object



69
70
71
72
# File 'lib/kontena/machine/packet/node_provisioner.rb', line 69

def set_labels(node, labels)
  data = {labels: labels}
  api_client.put("nodes/#{node['id']}", data, {}, {'Kontena-Grid-Token' => node['grid']['token']})
end