Class: ComputeResource

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Authorization, Taxonomix
Defined in:
app/models/compute_resource.rb

Constant Summary collapse

PROVIDERS =
%w[ Libvirt Ovirt EC2 Vmware Openstack Rackspace].delete_if{|p| p == "Libvirt" && !SETTINGS[:libvirt]}
STI_PREFIX =

to STI avoid namespace issues when loading the class, we append Foreman::Model in our database type column

"Foreman::Model"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, included, #permission_failed?

Methods included from Taxonomix

included

Class Method Details

.new_provider(args) ⇒ Object

allows to create a specific compute class based on the provider.


47
48
49
50
51
52
53
# File 'app/models/compute_resource.rb', line 47

def self.new_provider args
  raise ::Foreman::Exception.new(N_("must provide a provider")) unless provider = args[:provider]
  PROVIDERS.each do |p|
    return "#{STI_PREFIX}::#{p}".constantize.new(args) if p.downcase == provider.downcase
  end
  raise ::Foreman::Exception.new N_("unknown provider")
end

Instance Method Details

#as_json(options = {}) ⇒ Object


165
166
167
168
# File 'app/models/compute_resource.rb', line 165

def as_json(options={})
  options ||= {}
  super({:except => [:password]}.merge(options))
end

#available_imagesObject


179
180
181
# File 'app/models/compute_resource.rb', line 179

def available_images
  []
end

#capabilitiesObject


55
56
57
# File 'app/models/compute_resource.rb', line 55

def capabilities
  []
end

#console(uuid = nil) ⇒ Object


170
171
172
# File 'app/models/compute_resource.rb', line 170

def console uuid = nil
  raise ::Foreman::Exception.new(N_("%s console is not supported at this time"), provider)
end

#create_vm(args = {}) ⇒ Object


121
122
123
124
125
126
127
# File 'app/models/compute_resource.rb', line 121

def create_vm args = {}
  client.servers.create vm_instance_defaults.merge(args.to_hash.symbolize_keys)
rescue Fog::Errors::Error => e
  logger.debug "Fog error: #{e.message}\n " + e.backtrace.join("\n ")
  errors.add(:base, e.message.to_s)
  false
end

#destroy_vm(uuid) ⇒ Object


129
130
131
132
133
134
# File 'app/models/compute_resource.rb', line 129

def destroy_vm uuid
  find_vm_by_uuid(uuid).destroy
rescue ActiveRecord::RecordNotFound
  # if the VM does not exists, we don't really care.
  true
end

#find_vm_by_uuid(uuid) ⇒ Object


109
110
111
# File 'app/models/compute_resource.rb', line 109

def find_vm_by_uuid uuid
  client.servers.get(uuid) || raise(ActiveRecord::RecordNotFound)
end

#hardware_profile(id, opts = {}) ⇒ Object


153
154
# File 'app/models/compute_resource.rb', line 153

def hardware_profile(id,opts={})
end

#hardware_profiles(opts = {}) ⇒ Object


150
151
# File 'app/models/compute_resource.rb', line 150

def hardware_profiles(opts={})
end

#new_interface(attr = {}) ⇒ Object

return fog new interface ( network adapter )


100
101
102
# File 'app/models/compute_resource.rb', line 100

def new_interface attr={}
  client.interfaces.new attr
end

#new_vm(attr = {}) ⇒ Object

returns a new fog server instance


94
95
96
97
# File 'app/models/compute_resource.rb', line 94

def new_vm attr={}
  test_connection
  client.servers.new vm_instance_defaults.merge(attr.to_hash.symbolize_keys) if errors.empty?
end

#pingObject


68
69
70
71
# File 'app/models/compute_resource.rb', line 68

def ping
  test_connection
  errors
end

#provided_attributesObject

attributes that this provider can provide back to the host object


60
61
62
# File 'app/models/compute_resource.rb', line 60

def provided_attributes
  {:uuid => :identity}
end

#providerObject


136
137
138
# File 'app/models/compute_resource.rb', line 136

def provider
  read_attribute(:type).to_s.gsub("#{STI_PREFIX}::","")
end

#provider=(value) ⇒ Object


140
141
142
143
144
# File 'app/models/compute_resource.rb', line 140

def provider=(value)
  if PROVIDERS.include? value
    self.type = "#{STI_PREFIX}::#{value}"
  end
end

#provider_friendly_nameObject


87
88
89
90
91
# File 'app/models/compute_resource.rb', line 87

def provider_friendly_name
  list = SETTINGS[:libvirt] ? ["Libvirt"] : []
  list += %w[ oVirt EC2 VMWare OpenStack Rackspace ]
  list[PROVIDERS.index(provider)] rescue ""
end

#save_vm(uuid, attr) ⇒ Object


73
74
75
76
77
# File 'app/models/compute_resource.rb', line 73

def save_vm uuid, attr
  vm = find_vm_by_uuid(uuid)
  vm.attributes.merge!(attr.symbolize_keys)
  vm.save
end

#start_vm(uuid) ⇒ Object


113
114
115
# File 'app/models/compute_resource.rb', line 113

def start_vm uuid
  find_vm_by_uuid(uuid).start
end

#stop_vm(uuid) ⇒ Object


117
118
119
# File 'app/models/compute_resource.rb', line 117

def stop_vm uuid
  find_vm_by_uuid(uuid).stop
end

#supports_update?Boolean

by default, our compute providers do not support updating an existing instance


175
176
177
# File 'app/models/compute_resource.rb', line 175

def supports_update?
  false
end

#test_connection(options = {}) ⇒ Object


64
65
66
# File 'app/models/compute_resource.rb', line 64

def test_connection options = {}
  valid?
end

#to_labelObject


83
84
85
# File 'app/models/compute_resource.rb', line 83

def to_label
  "#{name} (#{provider_friendly_name})"
end

#to_paramObject


79
80
81
# File 'app/models/compute_resource.rb', line 79

def to_param
  "#{id}-#{name.parameterize}"
end

#update_required?(old_attrs, new_attrs) ⇒ Boolean


156
157
158
159
160
161
162
163
# File 'app/models/compute_resource.rb', line 156

def update_required?(old_attrs, new_attrs)
  old_attrs.merge(new_attrs) do |k,old_v,new_v|
    update_required?(old_v, new_v) if old_v.is_a?(Hash)
    return true unless old_v == new_v
    new_v
  end
  false
end

#vm_instance_defaultsObject


146
147
148
# File 'app/models/compute_resource.rb', line 146

def vm_instance_defaults
  ActiveSupport::HashWithIndifferentAccess.new(:name => "foreman_#{Time.now.to_i}")
end

#vmsObject

return a list of virtual machines


105
106
107
# File 'app/models/compute_resource.rb', line 105

def vms
  client.servers
end