Class: CORL::Machine::Vagrant

Inherits:
Object
  • Object
show all
Defined in:
lib/CORL/machine/vagrant.rb

Constant Summary collapse

@@lock =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#close_ssh_sessionObject




369
370
371
# File 'lib/CORL/machine/vagrant.rb', line 369

def close_ssh_session
  Util::SSH.close_session(node.public_ip, node.user)
end

#commandObject




41
42
43
44
# File 'lib/CORL/machine/vagrant.rb', line 41

def command
  set_command unless @command
  @command
end

#create(options = {}) ⇒ Object




96
97
98
99
100
# File 'lib/CORL/machine/vagrant.rb', line 96

def create(options = {})
  super do |config|
    start_machine(config)
  end
end

#create_image(options = {}) ⇒ Object




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
# File 'lib/CORL/machine/vagrant.rb', line 176

def create_image(options = {})
  super do |config|
    stop = config.delete(:stop, false)
    
    # TODO: Decide how to handle versions??  
    # Timestamps stink since these things are huge (>600MB)
    box_name = sprintf("%s", node.id).gsub(/\s+/, '-')
    box_path = File.join(node.network.directory, 'boxes', "#{box_name}.box")
    box_url  = "file://#{box_path}"
    FileUtils.mkdir_p(File.dirname(box_path))
    FileUtils.rm_f(box_path)
    
    begin
      close_ssh_session
      success = run(:package, config.defaults({ 'package.output' => box_path }), false)
    
      node.set_cache_setting(:box, box_name)
      node.set_cache_setting(:box_url, box_url)
      
      if success    
        env.action_runner.run(::Vagrant::Action.action_box_add, {
          :box_name  => box_name,
          :box_url   => box_url,          
          :box_clean => false,
          :box_force => true,
          :ui        => ::Vagrant::UI::Prefixed.new(env.ui, "box")
        })
        load
      end
      
    rescue Exception => error
      ui.error(error.message)
      success = false
    end
    
    success = run(:up, config) if success && ! stop
    success
  end
end

#created?Boolean


Checks

Returns:

  • (Boolean)


11
12
13
# File 'lib/CORL/machine/vagrant.rb', line 11

def created?
  server && state != :not_created
end

#destroy(options = {}) ⇒ Object




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
# File 'lib/CORL/machine/vagrant.rb', line 234

def destroy(options = {})
  super do |config|
    # We should handle prompting internally to keep it consistent
    success = run(:destroy, config.defaults({ :force_confirm_destroy => true }))
    
    if success
      box_name = sprintf("%s", node.id).gsub(/\s+/, '-')
      found    = false
      
      # TODO: Figure out box versions.
      
      env.boxes.all.each do |info|
        registered_box_name     = info[0]
        registered_box_version  = info[1]
        registered_box_provider = info[2]
        
        if box_name == registered_box_name
          found = true
          break
        end
      end        
      
      if found
        env.action_runner.run(::Vagrant::Action.action_box_remove, {
          :box_name     => box_name,
          :box_provider => node.machine_type
        })
        
        box_name = sprintf("%s", node.id).gsub(/\s+/, '-')
        box_path = File.join(node.network.directory, 'boxes', "#{box_name}.box")
        Util::Disk.delete(box_path)
      end
    end
    close_ssh_session if success
    success
  end
end

#download(remote_path, local_path, options = {}) ⇒ Object




104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/CORL/machine/vagrant.rb', line 104

def download(remote_path, local_path, options = {})
  super do |config, success|
    begin
      if init_ssh_session
        Util::SSH.download(node.public_ip, node.user, remote_path, local_path, config.export) do |name, received, total|
          yield(name, received, total) if block_given?
        end
        true
      else
        false
      end
    rescue Exception => error
      ui.error(error.message)
      false
    end
  end  
end

#envObject




48
49
50
51
# File 'lib/CORL/machine/vagrant.rb', line 48

def env
  return command.env if command
  nil
end

#exec(commands, options = {}) ⇒ Object




144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/CORL/machine/vagrant.rb', line 144

def exec(commands, options = {})
  super do |config, results|
    if init_ssh_session
      results = Util::SSH.exec(node.public_ip, node.user, commands) do |type, command, data|
        yield(type, command, data) if block_given?  
      end
    else
      results = nil
    end
    results
  end
end

#init_ssh_session(reset = false, tries = 5, sleep_secs = 5) ⇒ Object




347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/CORL/machine/vagrant.rb', line 347

def init_ssh_session(reset = false, tries = 5, sleep_secs = 5)
  success = true
      
  begin
    Util::SSH.session(node.public_ip, node.user, node.ssh_port, node.private_key, reset)
          
  rescue Exception => error
    if tries > 1
      sleep(sleep_secs)
      
      tries -= 1
      reset  = true
      retry
    else
      success = false
    end
  end
  success
end

#loadObject


Management



87
88
89
90
91
92
# File 'lib/CORL/machine/vagrant.rb', line 87

def load
  super do
    myself.server = plugin_name if command && plugin_name
    ! plugin_name && @server.nil? ? false : true
  end    
end

#machine_typesObject




80
81
82
# File 'lib/CORL/machine/vagrant.rb', line 80

def machine_types
  [ :virtualbox, :vmware_fusion, :hyperv ]
end

#reload(options = {}) ⇒ Object




167
168
169
170
171
172
# File 'lib/CORL/machine/vagrant.rb', line 167

def reload(options = {})
  super do |config|
    success = run(:reload, config)
    success = init_ssh_session(true, config.get(:tries, 5), config.get(:sleep_time, 5)) if success
  end
end

#running?Boolean


Returns:

  • (Boolean)


17
18
19
# File 'lib/CORL/machine/vagrant.rb', line 17

def running?
  server && state == :running
end

#serverObject



65
66
67
68
69
# File 'lib/CORL/machine/vagrant.rb', line 65

def server
  command
  load unless @server
  @server
end

#server=(id) ⇒ Object




55
56
57
58
59
60
61
62
63
# File 'lib/CORL/machine/vagrant.rb', line 55

def server=id
  @server = nil
  
  if id.is_a?(String)
    @server = new_machine(id)
  elsif ! id.nil?
    @server = id
  end
end

#start(options = {}) ⇒ Object




226
227
228
229
230
# File 'lib/CORL/machine/vagrant.rb', line 226

def start(options = {})
  super do |config|
    start_machine(config)  
  end
end

#stateObject




73
74
75
76
# File 'lib/CORL/machine/vagrant.rb', line 73

def state
  return server.state.id if server
  :not_loaded
end

#stop(options = {}) ⇒ Object




218
219
220
221
222
# File 'lib/CORL/machine/vagrant.rb', line 218

def stop(options = {})
  super do |config|
    create_image(config.import({ :stop => true }))
  end
end

#terminal(user, options = {}) ⇒ Object




159
160
161
162
163
# File 'lib/CORL/machine/vagrant.rb', line 159

def terminal(user, options = {})
  super do |config|
    Util::SSH.terminal(node.public_ip, user, config.export)
  end
end

#upload(local_path, remote_path, options = {}) ⇒ Object




124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/CORL/machine/vagrant.rb', line 124

def upload(local_path, remote_path, options = {})
  super do |config, success|
    begin
      if init_ssh_session
        Util::SSH.upload(node.public_ip, node.user, local_path, remote_path, config.export) do |name, sent, total|
          yield(name, sent, total) if block_given?
        end
        true
      else
        false
      end
    rescue Exception => error
      ui.error(error.message)
      false
    end
  end  
end