inprovise-vbox 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjQxMmE3NWUxMWNiODNmNGZmMjQ2Nzc0M2NhY2EyN2ZmYTcxOTNmOQ==
4
+ MTc2NzIyNTlkODRmYjBjNzJiYjYyOTA2MTczYWM1ZmMxYzM0YzI0Nw==
5
5
  data.tar.gz: !binary |-
6
- MzhhZjA1MWVkNDI4ZjAwODEwZGMyMTRjYzM0MjFkNTBmMWJjNzI4ZQ==
6
+ OTJmZmQ1YTY3ZWVhZDE5NDliNTVhYzlmNWZkNTIxMGFkMDA3ZTcwNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmMwNzg1NzA4NzlhMThjYWUyMzc2ZWQ1MWMzYjg5MTkzM2MwNDI3MTEyY2Ni
10
- YTg4Y2IwYTgyNGRmNmZjZDJiN2ZiNzRkNDIxYTI5ODAzNzNmOWZhNGE2N2I5
11
- NTUzMTQ2NWE2YTk0NDE3MjNlOGM5ZmM2ZDExZjI2ZTcwNzE4OTI=
9
+ YjFkMWM1ZmM3NDE4YTU4N2ExZWRiYjM5YjNjOTc4OWI2Yzc4NWJmNzgxYmE1
10
+ NjFmYjdmZmJiMjc3ZjFmZjMwNjE0MWFmNDZlODZmMGVhNTI3YzZlZmEyZTJm
11
+ OTM1ZjdlZDQ2NTA3NTQ2N2I0OTE0Y2Q3MGRiYzBmODc3ZDU3MWQ=
12
12
  data.tar.gz: !binary |-
13
- ZDE4ZGIyYWZlZmJlMTZmOWQxNjhkOTQxZTY0YmMxMTE4NDUwZTFmOGJhYWQy
14
- NmY0ZTcxMDAxNzg1NzdhZmZkMTM5NDRiOWRkMDRjNTBmODNhMjgwNWU0ODc2
15
- MTY3ODBlMDUxMjM4OTRjMDRhMjQ4MTk1ODNlNWM3NTlmYWRjYWU=
13
+ ZDcyN2VkMmI2N2I1NzI5ZjhhNzM2OWNmYzBhZWFhZWMxOGQzMTVhYTVlMGRk
14
+ ZWZmMzE3MTI3NDk1ZTI1Njc5ZDFkY2JjNjBmMGQ0ZmE1MmIwNjVlNDdkNGFm
15
+ NGNjM2EyYzI4ZGVkYmQ1NzA1NDA4Njc3YzM5MGE4MTlmZTUwZjY=
@@ -67,71 +67,71 @@ module Inprovise::VBox
67
67
  # generate name if none set
68
68
  configuration[:name] ||= "#{name}_#{self.hash}_#{Time.now.to_f}"
69
69
 
70
- vbox_script = self
71
- vbox_scrname = self.name
70
+ vbs = self
72
71
  # define the scripts that do the actual work as dependencies
73
72
  # 1. install the virtual machine
74
73
  @vm_script = Inprovise::DSL.script "#{name}#vbox_vm" do
75
74
 
76
75
  # verify VM
77
76
  validate do
78
- vbox_cfg = config[vbox_scrname.to_sym]
77
+ vmname = vbs.vbox_name(self)
79
78
  # look for existing target with VM name on :apply unless node creation is suppressed
80
- if command == :apply && !vbox_cfg.no_node
81
- if tgt = Inprovise::Infrastructure.find(vbox_cfg.name)
79
+ if command == :apply && !vbs.vbox_no_node(self)
80
+ if tgt = Inprovise::Infrastructure.find(vmname)
82
81
  type = Inprovise::Infrastructure::Group === tgt ? 'group' : 'node'
83
- raise ArgumentError, "VBox #{vbox_cfg.name} clashes with existing #{type}"
82
+ raise ArgumentError, "VBox #{vmname} clashes with existing #{type}"
84
83
  end
85
84
  end
86
- trigger 'vbox:vbox-verify', vbox_cfg.name, (command == :apply && vbox_cfg.autostart)
85
+ trigger 'vbox:vbox-verify', vmname, (command == :apply && vbs.vbox_autostart(self))
87
86
  end
88
87
 
89
88
  # apply : installation
90
89
  apply do
91
- vbox_cfg = config[vbox_scrname.to_sym]
90
+ vmname = vbs.vbox_name(self)
91
+ vmimg = vbs.vbox_image(self)
92
92
  # 1. verify config
93
- raise ArgumentError, "Cannot access VBox image #{vbox_cfg.image}" unless remote(vbox_cfg.image).file?
93
+ raise ArgumentError, "Cannot access VBox image #{vmimg}" unless remote(vmimg).file?
94
94
  # 2. execute virt-install
95
- log("Installing VBox #{vbox_cfg.name}".bold)
95
+ log("Installing VBox #{vmname}".bold)
96
96
  cmdline = 'virt-install --connect qemu:///system --hvm --virt-type kvm --import --wait 0 '
97
- cmdline << '--autostart ' if vbox_cfg.autostart
98
- cmdline << "--name #{vbox_cfg.name} --memory #{vbox_cfg.memory} --vcpus #{vbox_cfg.cpus} "
99
- cmdline << "--os-variant #{vbox_cfg.os} " if vbox_cfg.os
100
- cmdline << case vbox_cfg.network
97
+ cmdline << '--autostart ' if vbs.vbox_autostart(self)
98
+ cmdline << "--name #{vmname} --memory #{vbs.vbox_memory(self)} --vcpus #{vbs.vbox_cpus(self)} "
99
+ cmdline << "--os-variant #{vbs.vbox_os(self)} " if vbs.vbox_os(self)
100
+ cmdline << case vbs.vbox_network(self)
101
101
  when :hostnet
102
- "--network network=#{vbox_cfg.netname || 'default'} "
102
+ "--network network=#{vbs.vbox_netname(self) || 'default'} "
103
103
  when :bridge
104
- "--network bridge=#{vbox_cfg.netname || 'virbr0' } "
104
+ "--network bridge=#{vbs.vbox_netname(self) || 'virbr0' } "
105
105
  end
106
106
  cmdline << '--graphics spice '
107
- cmdline << "--disk path=#{vbox_cfg.image},device=disk,boot_order=1"
108
- cmdline << ",bus=#{vbox_cfg.diskbus}" if vbox_cfg.diskbus
109
- cmdline << ",format=#{vbox_cfg.format}" if vbox_cfg.format
107
+ cmdline << "--disk path=#{vbs.vbox_image(self)},device=disk,boot_order=1"
108
+ cmdline << ",bus=#{vbs.vbox_diskbus(self)}" if vbs.vbox_diskbus(self)
109
+ cmdline << ",format=#{vbs.vbox_format(self)}" if vbs.vbox_format(self)
110
110
  cmdline << ' --disk device=cdrom,boot_order=2,bus=ide'
111
- cmdline << " #{vbox_cfg.install_opts}" if vbox_cfg.install_opts
111
+ cmdline << " #{vbs.vbox_install_opts(self)}" if vbs.vbox_install_opts(self)
112
112
  sudo(cmdline)
113
113
  10.times do
114
114
  sleep(1)
115
- break if trigger 'vbox:vbox-verify', vbox_cfg.name, vbox_cfg.autostart
115
+ break if trigger 'vbox:vbox-verify', vmname, vbs.vbox_autostart(self)
116
116
  end
117
117
  end
118
118
 
119
119
  # revert : uninstall
120
120
  revert do
121
- vbox_cfg = config[vbox_scrname.to_sym]
122
- trigger 'vbox:vbox-shutdown', vbox_cfg.name
123
- log.print("Waiting for shutdown of VBox #{vbox_cfg.name}. Please wait ...|".bold)
121
+ vmname = vbs.vbox_name(self)
122
+ trigger 'vbox:vbox-shutdown', vmname
123
+ log.print("Waiting for shutdown of VBox #{vmname}. Please wait ...|".bold)
124
124
  30.times do |n|
125
125
  sleep(1)
126
126
  log.print("\b" + %w{| / - \\}[(n+1) % 4].bold)
127
- break unless trigger 'vbox:vbox-verify', vbox_cfg.name
127
+ break unless trigger 'vbox:vbox-verify', vmname
128
128
  end
129
- if trigger('vbox:vbox-verify', vbox_cfg.name)
130
- trigger('vbox:vbox-kill', vbox_cfg.name)
129
+ if trigger('vbox:vbox-verify', vmname)
130
+ trigger('vbox:vbox-kill', vmname)
131
131
  sleep(1)
132
132
  end
133
133
  log.println("\bdone".bold)
134
- trigger('vbox:vbox-delete', vbox_cfg.name) unless trigger('vbox:vbox-verify', vbox_cfg.name)
134
+ trigger('vbox:vbox-delete', vmname) unless trigger('vbox:vbox-verify', vmname)
135
135
  end
136
136
  end
137
137
 
@@ -140,47 +140,52 @@ module Inprovise::VBox
140
140
 
141
141
  # add a node object for the new VM unless suppressed
142
142
  apply do
143
- vbox_cfg = config[vbox_scrname.to_sym]
144
- unless vbox_cfg.no_node
143
+ vmname = vbs.vbox_name(self)
144
+ unless vbs.vbox_no_node(self)
145
145
  # get MAC and IP for VM
146
- log.print("Determining IP address for VBox #{vbox_cfg.name}. Please wait ...|".bold)
146
+ log.print("Determining IP address for VBox #{vmname}. Please wait ...|".bold)
147
147
  mac = addr = nil
148
148
  150.times do |n|
149
149
  sleep(2)
150
150
  log.print("\b" + %w{| / - \\}[(n+1) % 4].bold)
151
- mac, addr = trigger 'vbox:vbox-ifaddr', vbox_cfg.name
151
+ mac, addr = trigger 'vbox:vbox-ifaddr', vmname
152
152
  if addr
153
153
  break
154
154
  end
155
155
  end
156
156
  log.println("\bdone".bold)
157
- raise RuntimeError, "Failed to determin IP address for VBox #{vbox_cfg.name}" unless addr
158
- log("VBox #{vbox_cfg.name} : mac=#{mac}, addr=#{addr}") if Inprovise.verbosity > 0
159
- vbox_opts = vbox_cfg.to_h
157
+ raise RuntimeError, "Failed to determin IP address for VBox #{vmname}" unless addr
158
+ log("VBox #{vmname} : mac=#{mac}, addr=#{addr}") if Inprovise.verbosity > 0
159
+ vbox_opts = vbs.vbox_config_hash(self)
160
160
  vbox_opts.delete(:no_node)
161
161
  vbox_opts.delete(:no_sniff)
162
- node = Inprovise::Infrastructure::Node.new(vbox_cfg.name, {:host => addr, :user => vbox_cfg.user, :vbox => vbox_opts})
162
+ node_opts = vbox_opts.delete(:node) || {}
163
+ node_name = node_opts.delete(:name) || vmname
164
+ node_opts[:host] ||= addr
165
+ node_opts[:user] ||= vbs.vbox_user(self)
166
+ node_opts[:vbox] = vbox_opts
167
+ node = Inprovise::Infrastructure::Node.new(node_name, node_opts)
163
168
  Inprovise::Infrastructure.save
164
- unless vbox_cfg.no_sniff
169
+ unless vbs.vbox_no_sniff(self)
165
170
  # retry on (comm) failure
166
171
  Inprovise::Sniffer.run_sniffers_for(node) rescue Inprovise::Sniffer.run_sniffers_for(node)
167
172
  Inprovise::Infrastructure.save
168
173
  end
169
- log("Added new node #{node.to_s}".bold)
174
+ log("Added new node #{node}".bold)
170
175
  end
171
176
  end
172
177
 
173
178
  # remove the node object for the VM unless node creation suppressed
174
179
  revert do
175
- vbox_cfg = config[vbox_scrname.to_sym]
176
- unless vbox_cfg.no_node
177
- tgt = Inprovise::Infrastructure.find(vbox_cfg.name)
180
+ vmname = vbs.vbox_name(self)
181
+ unless vbs.vbox_no_node(self)
182
+ tgt = Inprovise::Infrastructure.find(vmname)
178
183
  if tgt && Inprovise::Infrastructure::Node === tgt
179
- Inprovise::Infrastructure.deregister(vbox_cfg.name)
184
+ Inprovise::Infrastructure.deregister(vmname)
180
185
  Inprovise::Infrastructure.save
181
- log("Removed node #{tgt.to_s}".bold)
186
+ log("Removed node #{tgt}".bold)
182
187
  else
183
- log("No existing node #{vbox_cfg.name} found!".yellow)
188
+ log("No existing node #{vmname} found!".yellow)
184
189
  end
185
190
  end
186
191
  end
@@ -194,6 +199,92 @@ module Inprovise::VBox
194
199
  self
195
200
  end
196
201
 
202
+ def vbox_name(context)
203
+ value_for context, context.config[name.to_sym][:name]
204
+ end
205
+
206
+ def vbox_autostart(context)
207
+ value_for context, context.config[name.to_sym][:autostart]
208
+ end
209
+
210
+ def vbox_no_node(context)
211
+ value_for context, context.config[name.to_sym][:no_node]
212
+ end
213
+
214
+ def vbox_no_sniff(context)
215
+ value_for context, context.config[name.to_sym][:no_sniff]
216
+ end
217
+
218
+ def vbox_image(context)
219
+ value_for context, context.config[name.to_sym][:image]
220
+ end
221
+
222
+ def vbox_memory(context)
223
+ value_for context, context.config[name.to_sym][:memory]
224
+ end
225
+
226
+ def vbox_cpus(context)
227
+ value_for context, context.config[name.to_sym][:cpus]
228
+ end
229
+
230
+ def vbox_network(context)
231
+ value_for context, context.config[name.to_sym][:network]
232
+ end
233
+
234
+ def vbox_netname(context)
235
+ value_for context, context.config[name.to_sym][:netname]
236
+ end
237
+
238
+ def vbox_os(context)
239
+ value_for context, context.config[name.to_sym][:os]
240
+ end
241
+
242
+ def vbox_user(context)
243
+ value_for context, context.config[name.to_sym][:user]
244
+ end
245
+
246
+ def vbox_diskbus(context)
247
+ value_for context, context.config[name.to_sym][:diskbus]
248
+ end
249
+
250
+ def vbox_format(context)
251
+ value_for context, context.config[name.to_sym][:format]
252
+ end
253
+
254
+ def vbox_install_opts(context)
255
+ value_for context, context.config[name.to_sym][:install_opts]
256
+ end
257
+
258
+ def vbox_config_hash(context)
259
+ context.config[name.to_sym].to_h.reduce({}) do |h, (k,v)|
260
+ case h[k] = value_for(context, v)
261
+ when OpenStruct
262
+ h[k] = config_to_hash(h[k])
263
+ end
264
+ h
265
+ end
266
+ end
267
+
268
+ def config_to_hash(cfg)
269
+ cfg.to_h.reduce({}) do |h, (k,v)|
270
+ h[k] = case v
271
+ when OpenStruct
272
+ config_to_hash(v)
273
+ else
274
+ v
275
+ end
276
+ h
277
+ end
278
+ end
279
+ private :config_to_hash
280
+
281
+ def value_for(context, option)
282
+ return nil if option.nil?
283
+ return context.instance_exec(&option) if option.respond_to?(:call)
284
+ option
285
+ end
286
+ private :value_for
287
+
197
288
  end
198
289
 
199
290
  end
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Inprovise; module VBox
7
7
 
8
- VERSION = '0.2.1'
8
+ VERSION = '0.2.2'
9
9
 
10
10
  end; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inprovise-vbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Corino