Class: Deltacloud::Drivers::Virtualbox::VirtualboxDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb

Instance Method Summary collapse

Methods inherited from BaseDriver

#catched_exceptions_list, declare_feature, define_hardware_profile, define_instance_states, feature, feature_decl_for, feature_decls, #features, features, #filter_hardware_profiles, #filter_on, #find_hardware_profile, #hardware_profile, hardware_profiles, #hardware_profiles, #has_collection?, #image, #instance, #instance_actions_for, instance_state_machine, #instance_state_machine, #realm, #safely, #storage_snapshot, #storage_snapshots, #storage_volume, #storage_volumes, #supported_collections

Instance Method Details

#create_instance(credentials, image_id, opts) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 99

def create_instance(credentials, image_id, opts)
  image = images(credentials, { :id => image_id }).first

  # Choose a name for the new vm unless one was given
  name = opts[:name]
  if name.nil? or name.empty?
    # random uniqueish name w/o having to pull in UUID gem
    name = "#{image.name} - #{(Time.now.to_f * 1000).to_i}#{rand(1000)}"
  end

  hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id)
  parent_vm = VirtualBox::VM.find(image.id)

  # Create new virtual machine
  vm = VirtualBox::VM.create(name, parent_vm.os_type_id)
  new_uid = vm.uuid

  # Add Hardware profile to this machine
  vm.memory_size = ((hwp.memory.value*1.024)*1000).to_i
  vm.cpu_count = hwp.cpu.value.to_i
  vm.vram_size = 16

  # Copy the network adapter settings from the parent vm
  vm.network_adapters[0].enabled = true
  vm.network_adapters[0].attachment_type = parent_vm.network_adapters[0].attachment_type
  vm.network_adapters[0].host_interface = parent_vm.network_adapters[0].host_interface

  # Store some metadata using extra data fields
  vm.extra_data['deltacloud_hwp_id'] = hwp.name
  vm.extra_data['deltacloud_image_id'] = image_id

  vm.save

  # Clone the disk image in a separate thread because it can take a long time
  Thread.new do
    # Reload the vm objects because they probably aren't safe to
    # reuse across threads
    parent_vm = VirtualBox::VM.find(image.id)
    vm = VirtualBox::VM.find(new_uid)

    # Add storage
    # This will 'reuse' existing image
    parent_hdd = hard_disk(parent_vm)
    new_location = File.join(File.dirname(parent_hdd.location), "#{name}.vdi")

    new_hd = parent_hdd.clone(new_location, "VDI")
    vm.add_storage_controller('IDE Controller', :ide, :piix4)
    vm.attach_storage('IDE Controller', 0, 0, :hard_disk, new_hd.uuid)
    vm.start
    if opts[:user_data]
      user_data = opts[:user_data].gsub("\n", '') # remove newlines from base64 encoded text
      vm.guest_property["/Deltacloud/UserData"] = user_data
    end
  end
  instances(credentials, :id => new_uid).first
end

#destroy_instance(credentials, id) ⇒ Object



173
174
175
176
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 173

def destroy_instance(credentials, id)
  vm = VirtualBox::VM.find(id)
  vm.destroy(:destroy_medium => :delete)
end

#images(credentials, opts = nil) ⇒ Object



84
85
86
87
88
89
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 84

def images(credentials, opts = nil)
  images = convert_images(VirtualBox::VM.all)
  images = filter_on( images, :id, opts )
  images = filter_on( images, :architecture, opts )
  images.sort_by{|e| [e.owner_id, e.description]}
end

#instances(credentials, opts = nil) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 91

def instances(credentials, opts = nil)
  instances = convert_instances(VirtualBox::VM.all)
  instances = filter_on( instances, :id, opts )
  instances = filter_on( instances, :state, opts )
  instances = filter_on( instances, :image_id, opts )
  instances
end

#realms(credentials, opts = nil) ⇒ Object



77
78
79
80
81
82
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 77

def realms(credentials, opts = nil)
  return REALMS if ( opts.nil? )
  results = REALMS
  results = filter_on( results, :id, opts )
  results
end

#reboot_instance(credentials, id) ⇒ Object



156
157
158
159
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 156

def reboot_instance(credentials, id)
  vm = VirtualBox::VM.find(id)
  vm.control(:reset)
end

#start_instance(credentials, id) ⇒ Object



168
169
170
171
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 168

def start_instance(credentials, id)
  vm = VirtualBox::VM.find(id)
  vm.start
end

#stop_instance(credentials, id) ⇒ Object



161
162
163
164
165
166
# File 'lib/deltacloud/drivers/virtualbox/virtualbox_driver.rb', line 161

def stop_instance(credentials, id)
  vm = VirtualBox::VM.find(id)
  unless vm.shutdown
    vm.stop
  end
end