class CORL::Machine::Vagrant

Public Instance Methods

command() click to toggle source
   # File lib/CORL/machine/vagrant.rb
69 def command
70   set_command unless @command
71   @command
72 end
create(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
138 def create(options = {})
139   super do |config|
140     start_machine(config)
141   end
142 end
create_image(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
187 def create_image(options = {})
188   super do |config|
189     success = true
190     if server && server.provider.action(:package) && machine_type.to_sym != :docker
191       stop = config.delete(:stop, false)
192 
193       # TODO: Decide how to handle versions??
194       # Timestamps stink since these things are huge (>600MB)
195       box_name = sprintf("%s", node.id).gsub(/\s+/, '-')
196       box_path = File.join(node.network.directory, 'boxes', "#{box_name}.box")
197       box_url  = "file://#{box_path}"
198       FileUtils.mkdir_p(File.dirname(box_path))
199       FileUtils.rm_f(box_path)
200 
201       begin
202         close_ssh_session
203         success = run(:package, config.defaults({ 'package.output' => box_path }), false)
204 
205         node.set_cache_setting(:box, box_name)
206         node.set_cache_setting(:box_url, box_url)
207 
208         if success
209           env.action_runner.run(::Vagrant::Action.action_box_add, {
210             :box_name  => box_name,
211             :box_url   => box_url,
212             :box_clean => false,
213             :box_force => true,
214             :ui        => ::Vagrant::UI::Prefixed.new(env.ui, "box")
215           })
216           load
217         end
218 
219       rescue => error
220         error(error.message, { :i18n => false })
221         success = false
222       end
223 
224       success = run(:up, config) if success && ! stop
225     else
226       warn("Packaging images not supported on Vagrant provider #{machine_type}", { :i18n => false })
227     end
228     success
229   end
230 end
created?() click to toggle source
   # File lib/CORL/machine/vagrant.rb
23 def created?
24   server && state != :not_created
25 end
destroy(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
250 def destroy(options = {})
251   super do |config|
252     # We should handle prompting internally to keep it consistent
253     success = run(:destroy, config.defaults({ :force_confirm_destroy => true }))
254 
255     if success
256       box_name = sprintf("%s", node.id).gsub(/\s+/, '-')
257       found    = false
258 
259       # TODO: Figure out box versions.
260 
261       env.boxes.all.each do |info|
262         registered_box_name     = info[0]
263         registered_box_version  = info[1]
264         registered_box_provider = info[2]
265 
266         if box_name == registered_box_name
267           found = true
268           break
269         end
270       end
271 
272       if found
273         env.action_runner.run(::Vagrant::Action.action_box_remove, {
274           :box_name     => box_name,
275           :box_provider => node.machine_type
276         })
277 
278         box_name = sprintf("%s", node.id).gsub(/\s+/, '-')
279         box_path = File.join(node.network.directory, 'boxes', "#{box_name}.box")
280         Util::Disk.delete(box_path)
281       end
282     end
283     close_ssh_session if success
284     success
285   end
286 end
download(remote_path, local_path, options = {}, &code) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
146 def download(remote_path, local_path, options = {}, &code)
147   super do |config, success|
148     ssh_download(remote_path, local_path, config, &code)
149   end
150 end
env() click to toggle source
   # File lib/CORL/machine/vagrant.rb
76 def env
77   return command.env if command
78   nil
79 end
exec(commands, options = {}, &code) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
162 def exec(commands, options = {}, &code)
163   super do |config|
164     ssh_exec(commands, config, &code)
165   end
166 end
image() click to toggle source
    # File lib/CORL/machine/vagrant.rb
121 def image
122   return server.config.vm.box if server
123   nil
124 end
load() click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
129 def load
130   super do
131     myself.server = plugin_name if command && plugin_name
132     ! plugin_name && @server.nil? ? false : true
133   end
134 end
machine_type() click to toggle source
    # File lib/CORL/machine/vagrant.rb
114 def machine_type
115   return server.provider_name if server
116   nil
117 end
machine_types() click to toggle source
    # File lib/CORL/machine/vagrant.rb
108 def machine_types
109   [ :virtualbox, :vmware_fusion, :hyperv, :docker ]
110 end
normalize(reload) click to toggle source
Calls superclass method
   # File lib/CORL/machine/vagrant.rb
15 def normalize(reload)
16   super
17   myself.plugin_name = node.plugin_name if node
18 end
public_ip() click to toggle source
   # File lib/CORL/machine/vagrant.rb
36 def public_ip
37   ip_address = nil
38   if server
39     ip_address = server.ssh_info[:host]
40 
41     if ip_address == '127.0.0.1'
42       ip_address = node.vm[:providers][machine_type][:private_network]
43       ip_address = ip_address[:ip] if ip_address.is_a?(Hash) && ip_address.has_key?(:ip)
44       ip_address = node[:public_ip] unless ip_address
45     end
46   end
47   ip_address
48 end
reload(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
178 def reload(options = {})
179   super do |config|
180     success = run(:reload, config)
181     success = init_ssh_session(true, config.get(:tries, 12), config.get(:sleep_time, 5)) if success
182   end
183 end
running?() click to toggle source
   # File lib/CORL/machine/vagrant.rb
29 def running?
30   server && state == :running
31 end
server() click to toggle source
   # File lib/CORL/machine/vagrant.rb
93 def server
94   command
95   load unless @server
96   @server
97 end
server=(id) click to toggle source
   # File lib/CORL/machine/vagrant.rb
83 def server=id
84   @server = nil
85 
86   if id.is_a?(String)
87     @server = new_machine(id)
88   elsif ! id.nil?
89     @server = id
90   end
91 end
start(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
242 def start(options = {})
243   super do |config|
244     start_machine(config)
245   end
246 end
state() click to toggle source
    # File lib/CORL/machine/vagrant.rb
101 def state
102   return server.state.id if server
103   :not_loaded
104 end
stop(options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
234 def stop(options = {})
235   super do |config|
236     create_image(config.import({ :stop => true }))
237   end
238 end
terminal(user, options = {}) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
170 def terminal(user, options = {})
171   super do |config|
172     ssh_terminal(user, config)
173   end
174 end
upload(local_path, remote_path, options = {}, &code) click to toggle source
Calls superclass method
    # File lib/CORL/machine/vagrant.rb
154 def upload(local_path, remote_path, options = {}, &code)
155   super do |config, success|
156     ssh_upload(local_path, remote_path, config, &code)
157   end
158 end

Protected Instance Methods

new_machine(id) click to toggle source
    # File lib/CORL/machine/vagrant.rb
307 def new_machine(id)
308   server = nil
309   if command && ! id.empty?
310     refresh_config
311     if env.vagrantfile.machine_names.include?(id.to_sym)
312       server = command.vm_machine(id.to_sym, nil, true)
313     end
314   end
315   server
316 end
refresh_config() click to toggle source
    # File lib/CORL/machine/vagrant.rb
291 def refresh_config
292   if env
293     @@lock.synchronize do
294       begin
295         CORL::Vagrant::Config.network = node.network
296         env.vagrantfile.reload
297       ensure
298         CORL::Vagrant::Config.network = nil
299       end
300     end
301   end
302 end
run(action, options = {}, symbolize_keys = true) click to toggle source
    # File lib/CORL/machine/vagrant.rb
338 def run(action, options = {}, symbolize_keys = true)
339   config = Config.ensure(options)
340 
341   if server
342     logger.debug("Running Vagrant action #{action} on machine #{node.id}")
343 
344     success = true
345     begin
346       params = config.export
347       params = string_map(params) unless symbolize_keys
348 
349       server.send(:action, action.to_sym, params)
350 
351     rescue => error
352       error(error.message, { :i18n => false })
353       error(Util::Data.to_yaml(error.backtrace), { :i18n => false })
354       success = false
355     end
356   end
357   success
358 end
set_command() click to toggle source
   # File lib/CORL/machine/vagrant.rb
52 def set_command
53   @command = nil
54 
55   begin
56     # Ensure we are running within Vagrant from the corl base command
57     require 'vagrant'
58 
59     logger.info("Setting up Vagrant for machine")
60     @command = CORL::Vagrant.command
61 
62   rescue LoadError
63   end
64 end
start_machine(options) click to toggle source
    # File lib/CORL/machine/vagrant.rb
321 def start_machine(options)
322   success = false
323 
324   if server
325     load
326     success = run(:up, options)
327 
328     # Make sure provisioner changes (key changes) are accounted for
329     # TODO: Is there a better way?
330     load if success
331   end
332   success
333 end