Class: Central::Machine::DigitalOcean::NodeProvisioner

Inherits:
Object
  • Object
show all
Includes:
RandomName
Defined in:
lib/central/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 (Central::Client)

    Central api client

  • token (String)

    Digital Ocean token



16
17
18
19
# File 'lib/central/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/central/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/central/machine/digital_ocean/node_provisioner.rb', line 12

def client
  @client
end

Instance Method Details

#droplet_exists_in_stack?(stack, droplet) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#erb(template, vars) ⇒ Object



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

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

#generate_nameObject



58
59
60
# File 'lib/central/machine/digital_ocean/node_provisioner.rb', line 58

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

def run!(opts)
  abort('Invalid ssh key') unless File.exist?(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],
    stack_token: opts[:stack_token]
  }

  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)
  ShellSpinner "Creating DigitalOcean droplet #{droplet.name.colorize(:cyan)} " do
    sleep 5 until client.droplets.find(id: created.id).status == 'active'
  end
  node = nil
  ShellSpinner "Waiting for node #{droplet.name.colorize(:cyan)} join to stack #{opts[:stack].colorize(:cyan)} " do
    sleep 2 until node = droplet_exists_in_stack?(opts[:stack], droplet)
  end
  set_labels(node, ["region=#{opts[:region]}"])
end

#set_labels(node, labels) ⇒ Object



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

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

#ssh_key(public_key) ⇒ Object



62
63
64
# File 'lib/central/machine/digital_ocean/node_provisioner.rb', line 62

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

#user_data(vars) ⇒ Object



53
54
55
56
# File 'lib/central/machine/digital_ocean/node_provisioner.rb', line 53

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