Class: Inprovise::VBox::VBoxScript

Inherits:
Script
  • Object
show all
Defined in:
lib/inprovise/vbox/vbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ VBoxScript

configuration

:name
:virt_type
:arch
:image
:format
:diskbus
:memory
:cdrom
:cdrombus
:cpus
:os
:network
:netname
:nic
:autostart
:kernel
:kernel_args
:install_opts


98
99
100
101
102
# File 'lib/inprovise/vbox/vbox.rb', line 98

def initialize(name)
  super(name)
  @vm_script = nil
  @node_script = nil
end

Instance Method Details

#configure(cfg = nil, &definition) ⇒ Object

overload Script#configure



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/inprovise/vbox/vbox.rb', line 289

def configure(cfg=nil, &definition)
  @configuration = Inprovise::Config.new.merge!(cfg) if cfg
  vbs = self
  config_block = block_given? ? definition : nil
  command(:configure).clear
  command(:configure) do
    self.instance_eval(&config_block) if config_block
    config['vbox'] = Inprovise::Infrastructure.find(config[vbs.name][:name])
  end
  @configuration
end

#setupObject



104
105
106
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/inprovise/vbox/vbox.rb', line 104

def setup
  # take care of defaults
  @configuration ||= Inprovise::Config.new
  @configuration[:arch] ||= 'x86_64'
  @configuration[:memory] ||= 1024
  @configuration[:cpus] ||= 1
  @configuration[:network] ||= :hostnet
  # generate name if none set
  @configuration[:name] ||= "#{name}_#{self.hash}_#{Time.now.to_f}"
  # create default configuration callback
  self.configure

  vbs = self
  # define the scripts that do the actual work as dependencies
  # 1. install the virtual machine
  @vm_script = Inprovise::DSL.script "#{name}#vbox_vm" do

    # verify VM
    validate do
      vmname = vbs.vbox_name(self)
      if trigger 'vbox:vbox-verify', vmname
        true  # vm with this name already running
      else
        # look for existing target with VM name on :apply unless node creation is suppressed
        if command == :apply && !vbs.vbox_no_node(self)
          if tgt = Inprovise::Infrastructure.find(vmname)
            type = Inprovise::Infrastructure::Group === tgt ? 'group' : 'node'
            raise ArgumentError, "VBox #{vmname} clashes with existing #{type}"
          end
        end
        false
      end
    end

    # apply : installation
    apply do
      # 1. trigger virt-install
      trigger('vbox:vbox-install', vbs.vbox_name(self), {
          :virt_type => vbs.vbox_virt_type(self),
          :arch => vbs.vbox_arch(self),
          :autostart => vbs.vbox_autostart(self),
          :memory => vbs.vbox_memory(self),
          :cpus => vbs.vbox_cpus(self),
          :os => vbs.vbox_os(self),
          :network => vbs.vbox_network(self) || :hostnet,
          :netname => vbs.vbox_netname(self),
          :source=> vbs.vbox_source(self),
          :nic => vbs.vbox_nic(self),
          :graphics => vbs.vbox_graphics(self),
          :image => vbs.vbox_image(self),
          :diskbus => vbs.vbox_diskbus(self),
          :format => vbs.vbox_format(self),
          :cdrom => vbs.vbox_cdrom(self),
          :cdrombus => vbs.vbox_cdrombus(self),
          :kernel => vbs.vbox_kernel(self),
          :kernel_args => vbs.vbox_kernel_args(self),
          :install_opts => vbs.vbox_install_opts(self)
        })
      # wait to startup
      10.times do
        sleep(1)
        break if trigger 'vbox:vbox-verify', vbs.vbox_name(self), true, vbs.vbox_autostart(self)
      end
    end

    # revert : uninstall
    revert do
      vmname = vbs.vbox_name(self)
      if trigger 'vbox:vbox-verify', vmname, true
        trigger 'vbox:vbox-shutdown', vmname
        msg = "Waiting for shutdown of VBox #{vmname}. Please wait ..."
        log.print("#{msg}|\r", :bold)
        30.times do |n|
          sleep(1)
          log.print("#{msg}#{%w{| / - \\}[(n+1) % 4]}\r", :bold)
          break unless trigger 'vbox:vbox-verify', vmname, true
        end
        if trigger('vbox:vbox-verify', vmname, true)
          trigger('vbox:vbox-kill', vmname)
          sleep(1)
        end
        log.println("#{msg}done", :bold)
      end
      trigger('vbox:vbox-delete', vmname) unless trigger('vbox:vbox-verify', vmname, true)
    end
  end

  # 2. add an Inprovise node if the VM was installed successfully
  @node_script = Inprovise::DSL.script "#{name}#vbox_node" do

    validate do
      if vbs.vbox_no_node(self)
        config.command != :revert
      else
        vmname = vbs.vbox_name(self)
        if tgt = Inprovise::Infrastructure.find(vmname)
          raise ArgumentError, "VBox #{vmname} clashes with existing group" if Inprovise::Infrastructure::Group === tgt
          true
        else
          false
        end
      end
    end

    # add a node object for the new VM unless suppressed
    apply do
      vmname = vbs.vbox_name(self)
      unless vbs.vbox_no_node(self)
        # get MAC and IP for VM
        msg = "Determining IP address for VBox #{vmname}. Please wait ..."
        log.print("#{msg}|\r", :bold)
        mac = addr = nil
        150.times do |n|
          sleep(2)
          log.print("#{msg}#{%w{| / - \\}[(n+1) % 4]}\r", :bold)
          mac, addr = trigger 'vbox:vbox-ifaddr', vmname
          if addr
            break
          end
        end
        log.println("#{msg}done", :bold)
        raise RuntimeError, "Failed to determine IP address for VBox #{vmname}" unless addr
        log("VBox #{vmname} : mac=#{mac}, addr=#{addr}") if Inprovise.verbosity > 0
        vbox_opts = vbs.vbox_config_hash(self)
        vbox_opts.delete(:no_node)
        vbox_opts.delete(:no_sniff)
        vbox_opts[:mac] = mac   # record mac-address
        node_opts = vbox_opts.delete(:node) || {}
        node_group = [node_opts.delete(:group)].flatten.compact
        node_opts[:host] ||= addr
        node_opts[:user] ||= vbs.vbox_user(self)
        node_opts[:vbox] = vbox_opts
        node = Inprovise::Infrastructure::Node.new(vmname, node_opts)
        Inprovise::Infrastructure.save
        node_group.each do |grpnm|
          grp = Inprovise::Infrastructure.find(grpnm)
          raise ArgumentError, "Invalid Group name '#{grpnm}'." unless grp.nil? || Inprovise::Infrastructure::Group === grp
          grp = Inprovise::Infrastructure::Group.new(grpnm) unless grp
          node.add_to(grp)
        end
        Inprovise::Infrastructure.save unless node_group.empty?
        unless vbs.vbox_no_sniff(self)
          (1..10).each do |i|
            begin
              Inprovise::Sniffer.run_sniffers_for(node)
              break
            rescue
              raise if i == 10
              sleep(5)  # maybe VM needs more time to start up SSH
              node.disconnect!
              # retry on (comm) failure
            end
          end
          Inprovise::Infrastructure.save
        end
        log("Added new node #{node}", :bold)
        config['vbox'] = node
      end
    end

    # remove the node object for the VM unless node creation suppressed
    revert do
      vmname = vbs.vbox_name(self)
      unless vbs.vbox_no_node(self)
        tgt = Inprovise::Infrastructure.find(vmname)
        if tgt && Inprovise::Infrastructure::Node === tgt
          Inprovise::Infrastructure.deregister(vmname)
          Inprovise::Infrastructure.save
          log("Removed node #{tgt}", :bold)
        else
          log("No existing node #{vmname} found!", :yellow)
        end
      end
    end

  end

  # add dependencies in correct order
  # MUST preceed any user defined dependencies
  dependencies.insert(0, @vm_script.name, @node_script.name)

  self
end

#vbox_arch(context) ⇒ Object



325
326
327
# File 'lib/inprovise/vbox/vbox.rb', line 325

def vbox_arch(context)
  value_for context, context.config[name.to_sym][:arch]
end

#vbox_autostart(context) ⇒ Object



309
310
311
# File 'lib/inprovise/vbox/vbox.rb', line 309

def vbox_autostart(context)
  value_for context, context.config[name.to_sym][:autostart]
end

#vbox_cdrom(context) ⇒ Object



373
374
375
# File 'lib/inprovise/vbox/vbox.rb', line 373

def vbox_cdrom(context)
  value_for context, context.config[name.to_sym][:cdrom]
end

#vbox_cdrombus(context) ⇒ Object



377
378
379
# File 'lib/inprovise/vbox/vbox.rb', line 377

def vbox_cdrombus(context)
  value_for context, context.config[name.to_sym][:cdrombus]
end

#vbox_config_hash(context) ⇒ Object



393
394
395
396
397
398
399
400
401
# File 'lib/inprovise/vbox/vbox.rb', line 393

def vbox_config_hash(context)
  context.config[name.to_sym].to_h.reduce({}) do |h, (k,v)|
    case h[k] = value_for(context, v)
      when Inprovise::Config
      h[k] = h[k].to_h
    end
    h
  end
end

#vbox_cpus(context) ⇒ Object



333
334
335
# File 'lib/inprovise/vbox/vbox.rb', line 333

def vbox_cpus(context)
  value_for context, context.config[name.to_sym][:cpus]
end

#vbox_diskbus(context) ⇒ Object



365
366
367
# File 'lib/inprovise/vbox/vbox.rb', line 365

def vbox_diskbus(context)
  value_for context, context.config[name.to_sym][:diskbus]
end

#vbox_format(context) ⇒ Object



369
370
371
# File 'lib/inprovise/vbox/vbox.rb', line 369

def vbox_format(context)
  value_for context, context.config[name.to_sym][:format]
end

#vbox_graphics(context) ⇒ Object



353
354
355
# File 'lib/inprovise/vbox/vbox.rb', line 353

def vbox_graphics(context)
  value_for context, context.config[name.to_sym][:graphics]
end

#vbox_image(context) ⇒ Object



321
322
323
# File 'lib/inprovise/vbox/vbox.rb', line 321

def vbox_image(context)
  value_for context, context.config[name.to_sym][:image]
end

#vbox_install_opts(context) ⇒ Object



389
390
391
# File 'lib/inprovise/vbox/vbox.rb', line 389

def vbox_install_opts(context)
  value_for context, context.config[name.to_sym][:install_opts]
end

#vbox_kernel(context) ⇒ Object



381
382
383
# File 'lib/inprovise/vbox/vbox.rb', line 381

def vbox_kernel(context)
  value_for context, context.config[name.to_sym][:kernel]
end

#vbox_kernel_args(context) ⇒ Object



385
386
387
# File 'lib/inprovise/vbox/vbox.rb', line 385

def vbox_kernel_args(context)
  value_for context, context.config[name.to_sym][:kernel_args]
end

#vbox_memory(context) ⇒ Object



329
330
331
# File 'lib/inprovise/vbox/vbox.rb', line 329

def vbox_memory(context)
  value_for context, context.config[name.to_sym][:memory]
end

#vbox_name(context) ⇒ Object



301
302
303
# File 'lib/inprovise/vbox/vbox.rb', line 301

def vbox_name(context)
  value_for context, context.config[name.to_sym][:name]
end

#vbox_netname(context) ⇒ Object



341
342
343
# File 'lib/inprovise/vbox/vbox.rb', line 341

def vbox_netname(context)
  value_for context, context.config[name.to_sym][:netname]
end

#vbox_network(context) ⇒ Object



337
338
339
# File 'lib/inprovise/vbox/vbox.rb', line 337

def vbox_network(context)
  value_for context, context.config[name.to_sym][:network]
end

#vbox_nic(context) ⇒ Object



349
350
351
# File 'lib/inprovise/vbox/vbox.rb', line 349

def vbox_nic(context)
  value_for context, context.config[name.to_sym][:nic]
end

#vbox_no_node(context) ⇒ Object



313
314
315
# File 'lib/inprovise/vbox/vbox.rb', line 313

def vbox_no_node(context)
  value_for context, context.config[name.to_sym][:no_node]
end

#vbox_no_sniff(context) ⇒ Object



317
318
319
# File 'lib/inprovise/vbox/vbox.rb', line 317

def vbox_no_sniff(context)
  value_for context, context.config[name.to_sym][:no_sniff]
end

#vbox_os(context) ⇒ Object



357
358
359
# File 'lib/inprovise/vbox/vbox.rb', line 357

def vbox_os(context)
  value_for context, context.config[name.to_sym][:os]
end

#vbox_source(context) ⇒ Object



345
346
347
# File 'lib/inprovise/vbox/vbox.rb', line 345

def vbox_source(context)
  value_for context, context.config[name.to_sym][:source]
end

#vbox_user(context) ⇒ Object



361
362
363
# File 'lib/inprovise/vbox/vbox.rb', line 361

def vbox_user(context)
  value_for context, context.config[name.to_sym][:user]
end

#vbox_virt_type(context) ⇒ Object



305
306
307
# File 'lib/inprovise/vbox/vbox.rb', line 305

def vbox_virt_type(context)
  value_for context, context.config[name.to_sym][:virt_type]
end