2012-01-22 10:38:02 -05:00
|
|
|
require 'fog/compute/models/server'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Ovirt
|
|
|
|
class Server < Fog::Compute::Server
|
|
|
|
# This will be the instance uuid which is globally unique across
|
|
|
|
# a oVirt deployment.
|
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :name
|
|
|
|
attribute :description
|
|
|
|
attribute :profile
|
|
|
|
attribute :display
|
|
|
|
attribute :storage, :aliases => 'disk_size'
|
|
|
|
attribute :creation_time
|
|
|
|
attribute :os
|
|
|
|
attribute :ip
|
|
|
|
attribute :status
|
|
|
|
attribute :cores, :aliases => 'cpus'
|
|
|
|
attribute :memory
|
|
|
|
attribute :host
|
|
|
|
attribute :cluster
|
|
|
|
attribute :template
|
2012-03-12 06:25:03 -04:00
|
|
|
attribute :interfaces
|
2012-04-09 09:42:58 -04:00
|
|
|
attribute :volumes
|
2012-01-22 10:38:02 -05:00
|
|
|
attribute :raw
|
2013-03-08 15:42:26 -05:00
|
|
|
attribute :quota
|
2014-09-16 09:23:12 -04:00
|
|
|
attribute :ips
|
2014-02-19 07:30:59 -05:00
|
|
|
|
2012-01-22 10:38:02 -05:00
|
|
|
def ready?
|
|
|
|
!(status =~ /down/i)
|
|
|
|
end
|
|
|
|
|
2012-03-12 06:25:03 -04:00
|
|
|
def locked?
|
2014-04-27 11:04:58 -04:00
|
|
|
@volumes = nil # force reload volumes
|
2013-02-24 09:00:44 -05:00
|
|
|
!!(status =~ /locked/i) || (attributes[:volumes]=nil) || volumes.any?{|v| !!(v.status =~ /locked/i)}
|
2012-03-12 06:25:03 -04:00
|
|
|
end
|
|
|
|
|
2012-01-22 10:38:02 -05:00
|
|
|
def stopped?
|
|
|
|
!!(status =~ /down/i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mac
|
2012-03-12 06:25:03 -04:00
|
|
|
interfaces.first.mac unless interfaces.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def interfaces
|
2014-03-17 09:38:45 -04:00
|
|
|
@interfaces ||= id.nil? ? [] : Fog::Compute::Ovirt::Interfaces.new(
|
2012-12-22 18:24:20 -05:00
|
|
|
:service => service,
|
2012-03-12 06:25:03 -04:00
|
|
|
:vm => self
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_interface attrs
|
|
|
|
wait_for { stopped? } if attrs[:blocking]
|
2012-12-22 18:24:20 -05:00
|
|
|
service.add_interface(id, attrs)
|
2012-03-12 06:25:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_interface attrs
|
|
|
|
wait_for { stopped? } if attrs[:blocking]
|
2012-12-22 18:24:20 -05:00
|
|
|
service.update_interface(id, attrs)
|
2012-03-12 06:25:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_interface attrs
|
|
|
|
wait_for { stopped? } if attrs[:blocking]
|
2012-12-22 18:24:20 -05:00
|
|
|
service.destroy_interface(id, attrs)
|
2012-01-22 10:38:02 -05:00
|
|
|
end
|
|
|
|
|
2012-04-09 09:42:58 -04:00
|
|
|
def volumes
|
2014-03-17 09:38:45 -04:00
|
|
|
@volumes ||= id.nil? ? [] : Fog::Compute::Ovirt::Volumes.new(
|
2012-12-22 18:24:20 -05:00
|
|
|
:service => service,
|
2012-04-09 09:42:58 -04:00
|
|
|
:vm => self
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_volume attrs
|
|
|
|
wait_for { stopped? } if attrs[:blocking]
|
2012-12-22 18:24:20 -05:00
|
|
|
service.add_volume(id, attrs)
|
2012-04-09 09:42:58 -04:00
|
|
|
end
|
|
|
|
|
2014-11-06 19:09:40 -05:00
|
|
|
def destroy_volume attrs
|
2014-10-15 10:53:32 -04:00
|
|
|
wait_for { stopped? } if attrs[:blocking]
|
2014-11-06 19:09:40 -05:00
|
|
|
service.destroy_volume(id, attrs)
|
2014-10-15 10:53:32 -04:00
|
|
|
end
|
|
|
|
|
2014-11-06 19:09:40 -05:00
|
|
|
def update_volume attrs
|
2012-04-09 09:42:58 -04:00
|
|
|
wait_for { stopped? } if attrs[:blocking]
|
2014-11-06 19:09:40 -05:00
|
|
|
service.update_volume(id, attrs)
|
2012-04-09 09:42:58 -04:00
|
|
|
end
|
|
|
|
|
2012-01-22 10:38:02 -05:00
|
|
|
def start(options = {})
|
2013-02-24 09:00:44 -05:00
|
|
|
wait_for { !locked? } if options[:blocking]
|
2012-12-22 18:24:20 -05:00
|
|
|
service.vm_action(:id =>id, :action => :start)
|
2012-01-22 10:38:02 -05:00
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:09:40 -05:00
|
|
|
def start_with_cloudinit(options = {})
|
|
|
|
wait_for { !locked? } if options[:blocking]
|
|
|
|
user_data = Hash[YAML.load(options[:user_data]).map{|a| [a.first.to_sym, a.last]}]
|
|
|
|
service.vm_start_with_cloudinit(:id =>id, :user_data =>user_data)
|
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
2012-01-22 10:38:02 -05:00
|
|
|
def stop(options = {})
|
2012-12-22 18:24:20 -05:00
|
|
|
service.vm_action(:id =>id, :action => :stop)
|
2012-01-22 10:38:02 -05:00
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def reboot(options = {})
|
2012-02-08 06:58:10 -05:00
|
|
|
stop unless stopped?
|
2012-02-19 07:51:06 -05:00
|
|
|
start options.merge(:blocking => true)
|
2012-02-08 06:58:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def suspend(options = {})
|
2012-12-22 18:24:20 -05:00
|
|
|
service.vm_action(:id =>id, :action => :suspend)
|
2012-01-22 10:38:02 -05:00
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(options = {})
|
2012-02-08 06:58:10 -05:00
|
|
|
(stop unless stopped?) rescue nil #ignore failure, destroy the machine anyway.
|
2012-01-22 10:38:02 -05:00
|
|
|
wait_for { stopped? }
|
2012-12-22 18:24:20 -05:00
|
|
|
service.destroy_vm(:id => id)
|
2012-01-22 10:38:02 -05:00
|
|
|
end
|
|
|
|
|
2012-03-21 10:00:57 -04:00
|
|
|
def ticket(options = {})
|
|
|
|
raise "Can not set console ticket, Server is not ready. Server status: #{status}" unless ready?
|
2012-12-22 18:24:20 -05:00
|
|
|
service.vm_ticket(id, options)
|
2012-03-21 10:00:57 -04:00
|
|
|
end
|
|
|
|
|
2012-01-22 10:38:02 -05:00
|
|
|
def save
|
2012-12-22 21:45:05 -05:00
|
|
|
if persisted?
|
2012-12-22 18:24:20 -05:00
|
|
|
service.update_vm(attributes)
|
2012-02-20 07:18:54 -05:00
|
|
|
else
|
2012-12-22 18:24:20 -05:00
|
|
|
self.id = service.create_vm(attributes).id
|
2012-02-20 07:18:54 -05:00
|
|
|
end
|
2012-01-22 10:38:02 -05:00
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|