Class: Central::Machine::Vagrant::MasterProvisioner

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

Constant Summary collapse

API_URL =
'http://192.168.66.100:8080'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMasterProvisioner

Returns a new instance of MasterProvisioner.



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

def initialize
  @client = Excon.new(API_URL)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/central/machine/vagrant/master_provisioner.rb', line 13

def client
  @client
end

Instance Method Details

#erb(template, vars) ⇒ Object



63
64
65
# File 'lib/central/machine/vagrant/master_provisioner.rb', line 63

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

#generate_nameObject



73
74
75
# File 'lib/central/machine/vagrant/master_provisioner.rb', line 73

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

#master_running?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/central/machine/vagrant/master_provisioner.rb', line 67

def master_running?
  client.get(path: '/').status == 200
rescue
  false
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
53
54
55
56
57
58
59
60
61
# File 'lib/central/machine/vagrant/master_provisioner.rb', line 19

def run!(opts)
  name = generate_name
  version = opts[:version]
  memory = opts[:memory] || 1024
  auth_server = opts[:auth_server]
  vault_secret = opts[:vault_secret]
  vault_iv = opts[:vault_iv]
  vagrant_path = "#{Dir.home}/.central/vagrant_master/"
  if Dir.exist?(vagrant_path)
    abort('Oops... cannot create Central Machine! You can run only one Central Machine with Vagrant'.colorize(:red))
  end
  FileUtils.mkdir_p(vagrant_path)

  template = File.join(__dir__, '/Vagrantfile.master.rb.erb')
  cloudinit_template = File.join(__dir__, '/cloudinit.yml')
  vars = {
    name: name,
    version: version,
    memory: memory,
    auth_server: auth_server,
    vault_secret: vault_secret,
    vault_iv: vault_iv,
    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 #{name.colorize(:cyan)} to start " do
      sleep 1 until master_running?
    end
    puts "Central Machine is now running at #{API_URL}"
    puts "Use #{"cm login #{API_URL}".colorize(:light_black)} to complete Central Machine setup"
  end
end