Class: Kontena::Machine::Vagrant::NodeProvisioner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#erb, #run_command

Constructor Details

#initialize(api_client) ⇒ NodeProvisioner

Returns a new instance of NodeProvisioner.

Parameters:

  • api_client (Kontena::Client)

    Kontena api client



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

def initialize(api_client)
  @api_client = api_client
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



10
11
12
# File 'lib/kontena/machine/vagrant/node_provisioner.rb', line 10

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/kontena/machine/vagrant/node_provisioner.rb', line 10

def client
  @client
end

Instance Method Details

#generate_nameObject



67
68
69
# File 'lib/kontena/machine/vagrant/node_provisioner.rb', line 67

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

#node_exists_in_grid?(grid, name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/kontena/machine/vagrant/node_provisioner.rb', line 71

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

#run!(opts) ⇒ Object



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

def run!(opts)
  grid = opts[:grid]
  if opts[:name]
    name = "#{opts[:name]}-#{opts[:instance_number]}"
  else
    name = generate_name
  end
  version = opts[:version]
  vagrant_path = "#{Dir.home}/.kontena/#{grid}/#{name}"
  FileUtils.mkdir_p(vagrant_path)

  template = File.join(__dir__ , '/Vagrantfile.node.rb.erb')
  cloudinit_template = File.join(__dir__ , '/cloudinit.yml')
  vars = {
    name: name,
    version: version,
    cpus: opts[:cpus] || 1,
    memory: opts[:memory] || 1024,
    network_address: opts[:network_address],
    master_uri: opts[:master_uri],
    grid_token: opts[:grid_token],
    coreos_channel: opts[:coreos_channel],
    cloudinit: "#{vagrant_path}/cloudinit.yml"
  }
  spinner "Generating Vagrant config" do
    vagrant_data = erb(File.read(template), vars)
    cloudinit = erb(File.read(cloudinit_template), vars)
    File.write("#{vagrant_path}/Vagrantfile", vagrant_data)
    File.write("#{vagrant_path}/cloudinit.yml", cloudinit)
  end

  node = nil
  Dir.chdir(vagrant_path) do
    spinner "Triggering CoreOS Container Linux box update"
    system('vagrant box update')
    spinner "Executing 'vagrant up'"
    run_command('vagrant up')
    spinner "'vagrant up' executed successfully"
    spinner "Waiting for node #{name.colorize(:cyan)} to join grid #{grid.colorize(:cyan)} " do
      sleep 1 until node = node_exists_in_grid?(grid, name)
    end
  end
  set_labels(
    node,
    [
      "provider=vagrant"
    ]
  )
end

#set_labels(node, labels) ⇒ Object



75
76
77
78
# File 'lib/kontena/machine/vagrant/node_provisioner.rb', line 75

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