Class: Fission::Action::Snapshot::Creator

Inherits:
Object
  • Object
show all
Defined in:
lib/fission/action/snapshot/creator.rb

Instance Method Summary collapse

Constructor Details

#initialize(vm) ⇒ Creator

Internal: Creates a new SnapshotCreator object. This accepts a VM object.

vm - An instance of VM

Examples:

Fission::Action::SnapshotCreator.new @my_vm

Returns a new SnapshotCreator object



17
18
19
# File 'lib/fission/action/snapshot/creator.rb', line 17

def initialize(vm)
  @vm = vm
end

Instance Method Details

#create_snapshot(name) ⇒ Object

Public: Creates a snapshot for a VM. The VM must be running in order to create a snapshot. Snapshot names must be unique.

name - The desired name of the snapshot. The name must be unique.

Examples

@creator.create_snapshot('foo_snap_1')

Returns a Response with the result. If successful, the Response’s data attribute will be nil. If there is an error, an unsuccessful Response will be returned.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fission/action/snapshot/creator.rb', line 33

def create_snapshot(name)
  unless @vm.exists?
    return Response.new :code => 1, :message => 'VM does not exist'
  end

  running_response = @vm.running?
  return running_response unless running_response.successful?

  unless running_response.data
    message = 'The VM must be running in order to take a snapshot.'
    return Response.new :code => 1, :message => message
  end

  conf_file_response = @vm.conf_file
  return conf_file_response unless conf_file_response.successful?

  snapshots_response = @vm.snapshots
  return snapshots_response unless snapshots_response.successful?

  if snapshots_response.data.include? name
    message = "There is already a snapshot named '#{name}'."
    return Response.new :code => 1, :message => message
  end

  command = "#{vmrun_cmd} snapshot "
  command << "'#{conf_file_response.data}' \"#{name}\" 2>&1"

  command_exec = Fission::Action::ShellExecutor.new command
  Response.from_shell_executor command_exec.execute
end