2011-08-11 12:53:52 -04:00
|
|
|
require 'fog/compute/models/server'
|
2011-05-11 21:35:02 -04:00
|
|
|
|
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Ninefold
|
2011-05-11 21:35:02 -04:00
|
|
|
|
2011-08-11 12:53:52 -04:00
|
|
|
class Server < Fog::Compute::Server
|
2011-07-22 16:38:48 -04:00
|
|
|
extend Fog::Deprecation
|
|
|
|
deprecate :serviceofferingid, :flavor_id
|
|
|
|
deprecate :templateid, :image_id
|
2011-05-11 21:35:02 -04:00
|
|
|
|
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :account
|
|
|
|
attribute :cpunumber
|
|
|
|
attribute :cpuspeed
|
|
|
|
|
|
|
|
attribute :cpuused
|
2011-07-22 16:38:48 -04:00
|
|
|
attribute :created, :type => :time
|
2011-05-11 21:35:02 -04:00
|
|
|
attribute :displayname
|
|
|
|
attribute :domain
|
|
|
|
attribute :domainid
|
2011-07-22 16:38:48 -04:00
|
|
|
attribute :flavor_id, :aliases => :serviceofferingid
|
2011-05-11 21:35:02 -04:00
|
|
|
attribute :forvirtualnetwork
|
|
|
|
attribute :group
|
|
|
|
attribute :groupid
|
|
|
|
attribute :guestosid
|
|
|
|
attribute :haenable
|
|
|
|
attribute :hostid
|
|
|
|
attribute :hostname
|
|
|
|
attribute :hypervisor
|
2011-07-22 16:38:48 -04:00
|
|
|
attribute :image_id, :aliases => :templateid
|
2011-05-11 21:35:02 -04:00
|
|
|
#attribute :ipaddress
|
|
|
|
attribute :isodisplaytext
|
|
|
|
attribute :isoid
|
|
|
|
attribute :isoname
|
|
|
|
attribute :jobid
|
|
|
|
attribute :jobstatus
|
|
|
|
attribute :memory
|
|
|
|
attribute :name
|
|
|
|
attribute :networkkbsread
|
|
|
|
attribute :networkkbswrite
|
2011-07-22 16:38:48 -04:00
|
|
|
attribute :nic
|
2011-05-11 21:35:02 -04:00
|
|
|
attribute :password
|
|
|
|
attribute :passwordenabled
|
|
|
|
attribute :rootdeviceid
|
|
|
|
attribute :rootdevicetype
|
2011-07-22 16:38:48 -04:00
|
|
|
attribute :securitygroup
|
2011-05-11 21:35:02 -04:00
|
|
|
attribute :serviceofferingname
|
|
|
|
attribute :state
|
|
|
|
attribute :templatedisplaytext
|
|
|
|
attribute :templatename
|
|
|
|
attribute :zoneid
|
|
|
|
attribute :zonename
|
|
|
|
|
|
|
|
# used for creation only.
|
|
|
|
attribute :networkids
|
|
|
|
attribute :diskofferingid
|
|
|
|
attribute :keypair
|
|
|
|
attribute :securitygroupids
|
|
|
|
attribute :size
|
|
|
|
attribute :userdata
|
|
|
|
|
|
|
|
#attribute :account_id, :aliases => "account", :squash => "id"
|
|
|
|
#attribute :image_id, :aliases => "image", :squash => "id"
|
|
|
|
#attribute :flavor_id, :aliases => "server_type", :squash => "id"
|
|
|
|
#attribute :zone_id, :aliases => "zone", :squash => "id"
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(attributes={})
|
2011-07-22 12:53:06 -04:00
|
|
|
merge_attributes({
|
2011-09-27 18:41:11 -04:00
|
|
|
:flavor_id => 105, # '1CPU, 384MB, 80GB HDD'
|
|
|
|
:image_id => 421 # 'XEN Basic Ubuntu 10.04 Server x64 PV r2.0'
|
2011-07-22 12:53:06 -04:00
|
|
|
})
|
2011-05-11 21:35:02 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is temporary - we need to model nics.
|
|
|
|
def ipaddress
|
|
|
|
nic[0] ? nic[0]['ipaddress'] : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def reboot
|
|
|
|
requires :identity
|
2012-12-22 18:24:54 -05:00
|
|
|
self.jobid = extract_job_id(service.reboot_virtual_machine(:id => identity))
|
2011-05-11 21:35:02 -04:00
|
|
|
puts "jobid: " + jobid.to_s
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
requires :identity
|
2012-12-22 18:24:54 -05:00
|
|
|
self.jobid = extract_job_id(service.start_virtual_machine(:id => identity))
|
2011-05-11 21:35:02 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
requires :identity
|
2012-12-22 18:24:54 -05:00
|
|
|
self.jobid = extract_job_id(service.stop_virtual_machine(:id => identity))
|
2011-05-11 21:35:02 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :identity
|
2012-12-22 18:24:54 -05:00
|
|
|
self.jobid = extract_job_id(service.destroy_virtual_machine(:id => identity))
|
2011-05-11 21:35:02 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor
|
2011-07-22 16:38:48 -04:00
|
|
|
requires :flavor_id
|
2012-12-22 18:24:54 -05:00
|
|
|
service.flavors.get(flavor_id)
|
2011-05-11 21:35:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
2011-07-22 16:38:48 -04:00
|
|
|
requires :image_id
|
2012-12-22 18:24:54 -05:00
|
|
|
service.images.get(image_id)
|
2011-05-11 21:35:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ready?
|
|
|
|
if jobid
|
|
|
|
# we do this by polling the last job id status.
|
2012-12-22 18:24:54 -05:00
|
|
|
res = service.query_async_job_result(:jobid => jobid)
|
2011-05-11 21:35:02 -04:00
|
|
|
if res['jobstatus'] == 0
|
|
|
|
false
|
|
|
|
else
|
|
|
|
# update with new values.
|
|
|
|
merge_attributes(res['jobresult']['virtualmachine'])
|
2011-05-11 22:47:30 -04:00
|
|
|
true
|
2011-05-11 21:35:02 -04:00
|
|
|
end
|
|
|
|
else # No running job, we are ready. Refresh data.
|
|
|
|
reload
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2011-06-04 05:51:08 -04:00
|
|
|
raise "Operation not supported" if self.identity
|
2011-07-22 16:38:48 -04:00
|
|
|
requires :flavor_id, :image_id, :zoneid
|
2011-05-11 21:35:02 -04:00
|
|
|
|
|
|
|
unless networkids
|
|
|
|
# No network specified, use first in this zone.
|
2012-12-22 18:24:54 -05:00
|
|
|
networks = service.list_networks(:zoneid => zoneid)
|
2011-05-11 21:35:02 -04:00
|
|
|
if networks.empty?
|
|
|
|
raise "No networks. Please create one, or specify a network ID"
|
|
|
|
else
|
2011-05-17 00:28:20 -04:00
|
|
|
# use the network with the lowest ID - the safe default
|
|
|
|
self.networkids = networks.sort {|x,y| x['id'] <=> y['id']}[0]['id']
|
2011-05-11 21:35:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
options = {
|
2011-07-22 16:38:48 -04:00
|
|
|
:serviceofferingid => flavor_id,
|
|
|
|
:templateid => image_id,
|
2011-05-11 21:35:02 -04:00
|
|
|
:name => name,
|
|
|
|
:zoneid => zoneid,
|
|
|
|
:networkids => networkids,
|
|
|
|
:account => account,
|
|
|
|
:diskofferingid => diskofferingid,
|
|
|
|
:displayname => displayname,
|
|
|
|
:domainid => domainid,
|
|
|
|
:group => group,
|
|
|
|
:hypervisor => hypervisor,
|
|
|
|
:keypair => keypair,
|
|
|
|
:securitygroupids => securitygroupids,
|
|
|
|
:size => size,
|
|
|
|
:userdata => userdata
|
|
|
|
}.delete_if {|k,v| v.nil? || v == "" }
|
2012-12-22 18:24:54 -05:00
|
|
|
data = service.deploy_virtual_machine(options)
|
2011-05-11 21:35:02 -04:00
|
|
|
merge_attributes(data)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def extract_job_id(job)
|
|
|
|
if job.kind_of? Integer
|
|
|
|
job
|
|
|
|
else
|
|
|
|
job['jobid'] || job['id']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|