Skip to content

Instantly share code, notes, and snippets.

@kenkeiter
Created May 9, 2011 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenkeiter/962696 to your computer and use it in GitHub Desktop.
Save kenkeiter/962696 to your computer and use it in GitHub Desktop.
Loose implementation of DC driver for Spherical
require 'deltacloud/base_driver'
require 'spherical'
module Deltacloud::Drivers::VMware
class VMwareDriver < Deltacloud::BaseDriver
# Configure hardware profile options
define_hardware_profile 'default' do
# this is defined by the image -- can't reconfigure this yet.
end
# Configure instance state machine
define_instance_states do
start.to(:pending) .on( :create )
pending.to(:stopped) .automatically
stopped.to(:running) .on( :start )
running.to(:running) .on( :reboot )
running.to(:shutting_down) .on( :stop )
shutting_down.to(:stopped) .automatically
stopped.to(:finish) .on( :destroy )
end
# List all images, across all datacenters. Note: Deltacloud API does not
# yet support filtering images by realm.
def images(credentials, opts=nil)
cloud = build_host_interface(credentials)
images = cloud.datacenters.collect do |datacenter|
datacenter.inventory.collect do |image|
Image.new {:id => image.id,
:name => image.name,
#:architecture => image.arch,
#:owner_id => image.owner,
:description => image.about}
end
end
images.flatten!
end
# List all datacenters managed by the vSphere or vCenter entrypoint.
def realms(credentials, opts=nil)
cloud = build_host_interface(credentials)
cloud.datacenters.collect do |datacenter|
Realm.new {:id => datacenter.id,
:name => datacenter.name,
:state=> datacenter.state}
end
end
# List all running instances, across all datacenters. DeltaCloud API does
# not yet support filtering instances by realm.
def instances(credentials, opts=nil)
cloud = build_host_interface(credentials)
instances = cloud.datacenters.collect do |datacenter|
datacenter.instances.collect do |instance|
Instance.new {:id => instance.id,
#:owner_id => instance.owner,
#:image_id => instance.image_id,
:name => instance.name,
:realm_id => datacenter.id,
#:state => instance.state,
#:actions => instance.actions,
#:public_addresses => instance.ip_addys,
#:private_addresses => instance.priv_ip_addys,
#:instance_profile => profile}
end
end
instances.flatten!
end
def create_instance(credentials, image_id, opts)
cloud = build_host_interface(credentials)
hwp_bounds = hardware_profiles(credentials, {:name => 'default'}).first
raise unless hwp_bounds.include? :cpu, opts[:hwp_cpu] &&
hwp_bounds.include? :memory, opts[:hwp_memory]
# * find an image in your datacenter and call mark_as_template on it.
# * then, clone(name, dest, store) on it.
# (look at the spherical doc for more info)
end
# Reboot an instance, given its id.
def reboot_instance(credentials, id)
cloud = build_host_interface(credentials)
instance = dereference_instance_id(cloud, id)
instance.restart!
end
# Start an instance, given its id.
def start_instance(credentials, id)
cloud = build_host_interface(credentials)
instance = dereference_instance_id(cloud, id)
instance.start
end
# Stop an instance, given its id.
def stop_instance(credentials, id)
cloud = build_host_interface(credentials)
instance = dereference_instance_id(cloud, id)
instance.halt!
end
# Destroy an instance, given its id. Note that this will destory all
# instance data.
def destroy_instance(credentials, id)
cloud = build_host_interface(credentials)
instance = dereference_instance_id(cloud, id)
instance.destroy!
end
#######
private
#######
def build_host_instance(credentials)
Spherical::Host.new :api => host_endpoint,
:username => credentials.user,
:password => credentials.password
end
def host_endpoint
Deltacloud::Drivers::driver_config[:vmware][:entrypoints]['default']['default']
end
# Given an instance of the class representing an instance, generate a
# unique, reversable ID that can be used to refer to that
# instance asynchronously.
def generate_instance_id(host, inst)
# use the instance's UUID -- you can search by uuid later to deref it.
end
# Given a host instance, and a unique instance ID, get an instance of the
# class representing that VM instance.
def dereference_instance_id(host, id)
# use Spherical's find_by_uuid method to get a hold of an instance
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment