Class: Kontena::Machine::DigitalOcean::NodeProvisioner

Inherits:
Object
  • Object
show all
Includes:
Cli::ShellSpinner, RandomName
Defined in:
lib/kontena/machine/digital_ocean/node_provisioner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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



16
17
18
19
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 16

def initialize(api_client, token)
  @api_client = api_client
  @client = DropletKit::Client.new(access_token: token)
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



12
13
14
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 12

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 12

def client
  @client
end

Instance Method Details

#droplet_exists_in_grid?(grid, droplet) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 78

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

#erb(template, vars) ⇒ Object



82
83
84
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 82

def erb(template, vars)
  ERB.new(template).result(OpenStruct.new(vars).instance_eval { binding })
end

#generate_nameObject



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

def generate_name
  "#{super}-#{rand(1..99)}"
end

#run!(opts) ⇒ Object



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
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 21

def run!(opts)
  abort('Invalid ssh key') unless File.exists?(File.expand_path(opts[:ssh_key]))

  ssh_key = ssh_key(File.read(File.expand_path(opts[:ssh_key])).strip)
  abort('Ssh key does not exist in Digital Ocean') unless ssh_key

  userdata_vars = {
    version: opts[:version],
    master_uri: opts[:master_uri],
    grid_token: opts[:grid_token],
  }
  droplets = []
  opts[:count].to_i.times do
    droplet = DropletKit::Droplet.new(
      name: opts[:name] || generate_name,
      region: opts[:region],
      image: 'coreos-stable',
      size: opts[:size],
      private_networking: true,
      user_data: user_data(userdata_vars),
      ssh_keys: [ssh_key.id]
    )
    created = client.droplets.create(droplet)
    spinner "Creating DigitalOcean droplet #{droplet.name.colorize(:cyan)} " do
      sleep 1 until client.droplets.find(id: created.id).status == 'active'
    end
    droplets << droplet
  end
  droplets.each do |droplet|
    node = nil
    spinner "Waiting for node #{droplet.name.colorize(:cyan)} join to grid #{opts[:grid].colorize(:cyan)} " do
      sleep 1 until node = droplet_exists_in_grid?(opts[:grid], droplet)
    end
    set_labels(
      node,
      [
        "region=#{opts[:region]}",
        "az=#{opts[:region]}",
        "provider=digitalocean"
      ]
    )
  end
end

#set_labels(node, labels) ⇒ Object



86
87
88
89
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 86

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

#ssh_key(public_key) ⇒ Object



74
75
76
# File 'lib/kontena/machine/digital_ocean/node_provisioner.rb', line 74

def ssh_key(public_key)
  ssh_key = client.ssh_keys.all.find{|key| key.public_key == public_key}
end

#user_data(vars) ⇒ Object



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

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