Class: Kontena::Machine::Vagrant::MasterProvisioner

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

Constant Summary collapse

API_URL =
'http://192.168.66.100:8080'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#erb, #run_command

Constructor Details

#initializeMasterProvisioner

Returns a new instance of MasterProvisioner.



16
17
18
# File 'lib/kontena/machine/vagrant/master_provisioner.rb', line 16

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/kontena/machine/vagrant/master_provisioner.rb', line 14

def client
  @client
end

Instance Method Details

#generate_nameObject



90
91
92
# File 'lib/kontena/machine/vagrant/master_provisioner.rb', line 90

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

#master_running?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/kontena/machine/vagrant/master_provisioner.rb', line 84

def master_running?
  client.get(path: '/').status == 200
rescue
  false
end

#run!(opts) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kontena/machine/vagrant/master_provisioner.rb', line 20

def run!(opts)
  name = opts[:name] || generate_name
  version = opts[:version]
  memory = opts[:memory] || 1024
  vault_secret = opts[:vault_secret]
  vault_iv = opts[:vault_iv]
  initial_admin_code = opts[:initial_admin_code]
  coreos_channel = opts[:coreos_channel]
  vagrant_path = "#{Dir.home}/.krates/vagrant_master/"
  if Dir.exist?(vagrant_path)
    puts "Oops... cannot create Krates Master because installation path already exists."
    puts "If you are sure that no Krates Master(s) exist on this machine, remove folder: #{vagrant_path}"
    abort
  end
  FileUtils.mkdir_p(vagrant_path)

  template = File.join(__dir__ , '/Vagrantfile.master.rb.erb')
  cloudinit_template = File.join(__dir__ , '/cloudinit.yml')
  vars = {
    name: name,
    server_name: name.sub('krates-master-', ''),
    version: version,
    memory: memory,
    vault_secret: vault_secret,
    initial_admin_code: initial_admin_code,
    vault_iv: vault_iv,
    coreos_channel: coreos_channel,
    cloudinit: "#{vagrant_path}/cloudinit.yml"
  }
  spinner "Creating 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

  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"
  end

  spinner "Waiting for #{name.colorize(:cyan)} to start " do
    sleep 1 until master_running?
  end

  master_version = nil
  spinner "Retrieving #{name.colorize(:cyan)} version" do
    master_version = JSON.parse(client.get(path: '/'))["version"] rescue nil
  end

  spinner "Krates Master #{master_version} is now running at #{API_URL}"

  {
    name: name.sub('krates-master-', ''),
    public_ip: API_URL.split('//').last,
    provider: 'vagrant',
    version: master_version,
    code: opts[:initial_admin_code]
  }
end