Class: Central::Machine::Vagrant::NodeProvisioner

Inherits:
Object
  • Object
show all
Includes:
RandomName
Defined in:
lib/central/machine/vagrant/node_provisioner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ NodeProvisioner

Returns a new instance of NodeProvisioner.

Parameters:



15
16
17
# File 'lib/central/machine/vagrant/node_provisioner.rb', line 15

def initialize(api_client)
  @api_client = api_client
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



12
13
14
# File 'lib/central/machine/vagrant/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/vagrant/node_provisioner.rb', line 12

def client
  @client
end

Instance Method Details

#erb(template, vars) ⇒ Object



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

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

#generate_nameObject



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

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

#node_exists_in_stack?(stack, name) ⇒ Boolean

Returns:

  • (Boolean)


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

def node_exists_in_stack?(stack, name)
  api_client.get("stacks/#{stack}/nodes")['nodes'].find { |n| n['name'] == name }
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
# File 'lib/central/machine/vagrant/node_provisioner.rb', line 19

def run!(opts)
  stack = opts[:stack]
  name = opts[:name] || generate_name
  version = opts[:version]
  vagrant_path = "#{Dir.home}/.central/#{stack}/#{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,
    memory: opts[:memory] || 1024,
    master_uri: opts[:master_uri],
    stack_token: opts[:stack_token],
    cloudinit: "#{vagrant_path}/cloudinit.yml"
  }
  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)
  Dir.chdir(vagrant_path) do
    ShellSpinner "Creating Vagrant machine #{name.colorize(:cyan)} " do
      Open3.popen2('vagrant box update && vagrant up') do |_stdin, output, _wait|
        while o = output.gets
          print o if ENV['DEBUG']
        end
      end
    end
    ShellSpinner "Waiting for node #{name.colorize(:cyan)} join to stack #{stack.colorize(:cyan)} " do
      sleep 1 until node_exists_in_stack?(stack, name)
    end
  end
end