Class: Cult::Drivers::VirtualBoxDriver

Inherits:
Cult::Driver show all
Defined in:
lib/cult/drivers/virtual_box_driver.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cult::Driver

driver_name, inspect, #inspect, named_array_identifier, new, to_s, #to_s, try_requires!

Methods included from Common

#await_ssh, #backoff_loop, #connect_timeout, #distro_name, #fetch_mapped, included, #slugify, #ssh_key_info

Constructor Details

#initialize(api_key:) ⇒ VirtualBoxDriver

Returns a new instance of VirtualBoxDriver.



7
8
# File 'lib/cult/drivers/virtual_box_driver.rb', line 7

def initialize(api_key:)
end

Class Method Details

.setup!Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cult/drivers/virtual_box_driver.rb', line 154

def self.setup!
  super

  inst = new(api_key: nil)

  return {
    driver: driver_name,
    api_key: nil,
    configurations: {
      sizes:  inst.sizes,
      zones:  inst.zones,
      images: inst.images,
    }
  }
end

Instance Method Details

#await_ip_address(name, index, protocol) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/cult/drivers/virtual_box_driver.rb', line 69

def await_ip_address(name, index, protocol)
  puts "Awaiting IP address from VirtualBox Guest Additions"
  unset_ip_data(name, index, protocol)

  backoff_loop do
    if (ip = get_ip_data(name, index, protocol))
      return ip
    end
  end
end

#destroy!(id:, ssh_key_id:) ⇒ Object



81
82
83
84
# File 'lib/cult/drivers/virtual_box_driver.rb', line 81

def destroy!(id:, ssh_key_id:)
  system 'VBoxManage', 'controlvm', id, 'poweroff'
  system 'VBoxManage', 'unregistervm', id, '--delete'
end

#esc(s) ⇒ Object



44
45
46
# File 'lib/cult/drivers/virtual_box_driver.rb', line 44

def esc(s)
  Shellwords.escape(s)
end

#get_ip_data(name, index, protocol) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cult/drivers/virtual_box_driver.rb', line 56

def get_ip_data(name, index, protocol)
  cmd = "VBoxManage guestproperty get #{esc(name)} " +
        "#{ip_property_name(index, protocol)}"
  s = `#{cmd}`

  if $?.success? && (m = s.match(/^Value: (.+)$/))
    m[1]
  else
    nil
  end
end

#guest_command(name, cmd) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/cult/drivers/virtual_box_driver.rb', line 98

def guest_command(name, cmd)
  cmd = "VBoxManage guestcontrol #{esc(name)} " +
        "--username root --password password " +
        "run -- /bin/sh -c #{esc(cmd)}"
  puts cmd
  `#{cmd}`
end

#guest_copy(name, src, dst) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/cult/drivers/virtual_box_driver.rb', line 87

def guest_copy(name, src, dst)
  # NOTE: Bug in current (Sep 2016) VBox has a fucked copyto, where
  # setting target-directory to the full path is a workaround
  cmd = "VBoxManage guestcontrol #{esc(name)} " +
        "--username root --password password " +
        "copyto #{esc(src)} --target-directory #{esc(dst)}"
  puts cmd
  `#{cmd}`
end

#images_mapObject



21
22
23
24
25
26
# File 'lib/cult/drivers/virtual_box_driver.rb', line 21

def images_map
  %x(VBoxManage list vms).each_line.map do |line|
    words = Shellwords.split(line.chomp)
    [ distro_name(words[0]), words[1] ]
  end.to_h
end

#ip_property_name(index, protocol) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/cult/drivers/virtual_box_driver.rb', line 35

def ip_property_name(index, protocol)
  protocols = {
    ipv4: 'V4',
    ipv6: 'V6'
  }
  "/VirtualBox/GuestInfo/Net/#{index}/#{protocols[protocol]}/IP"
end

#provision!(name:, size:, zone:, image:, ssh_public_key:) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cult/drivers/virtual_box_driver.rb', line 107

def provision!(name:, size:, zone:, image:, ssh_public_key:)
  transaction do |xac|
    # Todo: transaction
    system 'VBoxManage', 'clonevm',
            fetch_mapped(name: :image, from: images_map, key: image),
           '--name', name, '--register'

    xac.rollback do
      destroy!(id: name, ssh_key_id: nil)
    end

    system_spec = sizes_map[size]
    system 'VBoxManage', 'modifyvm', name,
                         '--groups', '/Cult',
                         '--memory', system_spec[:ram].to_s,
                         '--cpus', system_spec[:cores].to_s

    system 'VBoxManage', 'startvm', name, '--type', 'headless'

    public_ip  = await_ip_address(name, 0, :ipv4)
    private_ip = public_ip

    await_ssh(public_ip)

    guest_command(name, "mkdir -m 0600 /root/.ssh")
    guest_copy(name, ssh_public_key, "/root/.ssh/authorized_keys")
    guest_command(name, "chmod 0644 /root/.ssh/authorized_keys")
    guest_command(name, "passwd -l root")

    return {
        name:          name,
        size:          size,
        zone:          zone,
        image:         image,

        id:           name,
        created_at:   Time.now.iso8601,
        host:         public_ip,
        ipv4_public:  public_ip,
        ipv4_private: private_ip,
        ipv6_public:  nil,
        ipv6_private: nil,
        meta:         {}
    }
  end
end

#sizes_mapObject



11
12
13
14
15
16
17
# File 'lib/cult/drivers/virtual_box_driver.rb', line 11

def sizes_map
  # Cores = GB Ram is a pretty good scaling factor, and
  # closely maps what VPS providers are doing.
  [1, 2, 4, 6, 8, 10, 12, 16].map do |size|
    [ "#{size}gb", { ram: size * 1024, cores: size } ]
  end.to_h
end

#unset_ip_data(name, index, protocol) ⇒ Object



49
50
51
52
53
# File 'lib/cult/drivers/virtual_box_driver.rb', line 49

def unset_ip_data(name, index, protocol)
  cmd = "VBoxManage guestproperty unset #{esc(name)} " +
        "#{ip_property_name(index, protocol)}"
  `#{cmd}`
end

#zonesObject



30
31
32
# File 'lib/cult/drivers/virtual_box_driver.rb', line 30

def zones
  ['local']
end