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

#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




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

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
      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



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

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

#destroy(options = {}) ⇒ Object




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

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

def download(remote_path, local_path, options = {})
  super do |config, success|
    begin
      server.communicate.tap do |comm|
        comm.download(remote_path, local_path)  
      end
      success = true
        
    rescue Exception => error
      ui.error(error.message)
      success = false
    end
    success
  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




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

def exec(commands, options = {})
  super do |config, results|
    codes :vagrant_exec_failed
      
    begin
      server.communicate.tap do |comm|            
        commands.each do |command|
          as_admin = command.match(/^sudo\s+/) ? true : false
          result   = Util::Shell::Result.new(command.gsub(/^sudo\s+/, '').strip)
          
          result.status = comm.execute(result.command, { :sudo => as_admin }) do |type, data|
            case type
            when :stdout
              result.append_output(data)
              yield(:output, result.command, data) if block_given?      
            when :stderr
              result.append_errors(data)
              yield(:error, result.command, data) if block_given?  
            end  
          end
          results << result
        end
      end
        
    rescue Exception => error
      ui.error(error.message)
      results << Util::Shell::Result.new(command, code.vagrant_exec_failed)
    end      
    results
  end    
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




182
183
184
185
186
# File 'lib/CORL/machine/vagrant.rb', line 182

def reload(options = {})
  super do |config|
    run(:reload, config)
  end
end

#running?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




239
240
241
242
243
# File 'lib/CORL/machine/vagrant.rb', line 239

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




231
232
233
234
235
# File 'lib/CORL/machine/vagrant.rb', line 231

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

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




174
175
176
177
178
# File 'lib/CORL/machine/vagrant.rb', line 174

def terminal(user, options = {})
  super do |config|
    run(:ssh, { :ssh_opts => config.export }) 
  end
end

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




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

def upload(local_path, remote_path, options = {})
  super do |config, success|
    begin
      server.communicate.tap do |comm|
        comm.upload(local_path, remote_path)  
      end
      success = true
        
    rescue Exception => error
      ui.error(error.message)
      success = false
    end
    success
  end  
end