Class: ForemanOpennebula::Opennebula

Inherits:
ComputeResource
  • Object
show all
Includes:
KeyPairComputeResource
Defined in:
app/models/foreman_opennebula/opennebula.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KeyPairComputeResource

#delete_key_from_resource, #get_compute_key_pairs, #key_pairs, #recreate

Class Method Details

.model_nameObject



14
15
16
# File 'app/models/foreman_opennebula/opennebula.rb', line 14

def self.model_name
  ComputeResource.model_name
end

.provider_friendly_nameObject



10
11
12
# File 'app/models/foreman_opennebula/opennebula.rb', line 10

def self.provider_friendly_name
  'OpenNebula'
end

Instance Method Details

#associated_host(vm) ⇒ Object



206
207
208
# File 'app/models/foreman_opennebula/opennebula.rb', line 206

def associated_host(vm)
  associate_by('mac', vm.interfaces.map(&:mac))
end

#available_clustersObject



46
47
48
49
50
51
# File 'app/models/foreman_opennebula/opennebula.rb', line 46

def available_clusters
  cluster_pool = ::OpenNebula::ClusterPool.new(client.client)
  rc = cluster_pool.info
  raise ::Foreman::Exception.new N_(rc.message) if ::OpenNebula.is_error?(rc)
  cluster_pool.sort_by(&:name)
end

#available_flavorsObject



30
31
32
33
# File 'app/models/foreman_opennebula/opennebula.rb', line 30

def available_flavors
  # TODO: Sort by name.
  flavors.sort_by { |flavor| flavor.id.to_i }
end

#available_hostsObject



53
54
55
56
57
58
# File 'app/models/foreman_opennebula/opennebula.rb', line 53

def available_hosts
  host_pool = ::OpenNebula::HostPool.new(client.client)
  rc = host_pool.info
  raise ::Foreman::Exception.new N_(rc.message) if ::OpenNebula.is_error?(rc)
  host_pool.sort_by(&:name)
end

#available_imagesObject



39
40
41
42
43
44
# File 'app/models/foreman_opennebula/opennebula.rb', line 39

def available_images
  image_pool = ::OpenNebula::ImagePool.new(client.client)
  rc = image_pool.info
  raise ::Foreman::Exception.new N_(rc.message) if ::OpenNebula.is_error?(rc)
  image_pool.sort_by(&:name)
end

#available_networksObject



35
36
37
# File 'app/models/foreman_opennebula/opennebula.rb', line 35

def available_networks
  networks.sort_by(&:name)
end

#available_roles(vmgroup_id) ⇒ Object



67
68
69
# File 'app/models/foreman_opennebula/opennebula.rb', line 67

def available_roles(vmgroup_id)
  available_vmgroups.detect { |vmgroup| vmgroup.id == vmgroup_id.to_i }.role_names.sort
end

#available_usersObject



71
72
73
74
75
76
# File 'app/models/foreman_opennebula/opennebula.rb', line 71

def available_users
  user_pool = ::OpenNebula::UserPool.new(client.client)
  rc = user_pool.info
  raise ::Foreman::Exception.new N_(rc.message) if ::OpenNebula.is_error?(rc)
  user_pool
end

#available_vmgroupsObject



60
61
62
63
64
65
# File 'app/models/foreman_opennebula/opennebula.rb', line 60

def available_vmgroups
  vmgroup_pool = ::OpenNebula::VMGroupPool.new(client.client)
  rc = vmgroup_pool.info
  raise ::Foreman::Exception.new N_(rc.message) if ::OpenNebula.is_error?(rc)
  vmgroup_pool.sort_by(&:name)
end

#capabilitiesObject



18
19
20
# File 'app/models/foreman_opennebula/opennebula.rb', line 18

def capabilities
  [:build, :image, :key_pair]
end

#console(uuid) ⇒ Object



200
201
202
203
204
# File 'app/models/foreman_opennebula/opennebula.rb', line 200

def console(uuid)
  vm = find_vm_by_uuid(uuid)
  WsProxy.start(:host => vm.host, :host_port => vm.display[:port],
    :password => vm.display[:password]).merge(:type => vm.display[:type], :name => vm.name)
end

#create_vm(args = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
# File 'app/models/foreman_opennebula/opennebula.rb', line 78

def create_vm(args = {})
  vm_attrs = {:name => args[:name]}
  vm_attrs[:flavor] = flavors.get(args[:template_id])
  vm_attrs[:flavor].template_id = args[:template_id]
  vm_attrs[:flavor].cpu = args[:cpu] if args[:cpu].present?
  vm_attrs[:flavor].vcpu = args[:vcpu] if args[:vcpu].present?
  vm_attrs[:flavor].memory = args[:memory] if args[:memory].present?

  if args[:disk_size].present?
    [vm_attrs[:flavor].disk].flatten.compact.first[:SIZE] = args[:disk_size]
  end

  vm_attrs[:flavor].nic = args[:interfaces_attributes].map do |_, attrs|
    nic = {
      :vnet => networks.get(attrs[:vnet])
    }
    nic[:ip] = attrs['ip'] if args['provision_method'] == 'image' && attrs['ip'].present?
    if vm_attrs[:flavor].nic_default.present?
      vm_attrs[:flavor].nic_default.each do |param, value|
        nic[param.downcase.to_sym] = value
      end
    end
    new_interface(nic)
  end

  if args[:scheduler_hint_filter].present? && args[:scheduler_hint_data].present?
    if args[:scheduler_hint_filter] == 'Cluster'
      cluster = available_clusters.detect { |c| c.id == args[:scheduler_hint_data].to_i }
      vm_attrs[:flavor].sched_requirements = "CLUSTER_ID = #{cluster.id}"
    elsif args[:scheduler_hint_filter] == 'Host'
      host = available_hosts.detect { |h| h.id == args[:scheduler_hint_data].to_i }
      vm_attrs[:flavor].sched_requirements = "ID = #{host.id}"
    else
      vm_attrs[:flavor].sched_requirements = args[:scheduler_hint_data]
    end
  end

  if args[:vmgroup_id].present? && args[:vmgroup_role].present?
    vm_attrs[:flavor].vmgroup = { 'VMGROUP_ID' => args[:vmgroup_id], 'ROLE' => args[:vmgroup_role] }
  end

  if args['provision_method'] == 'image' && args['image_id'].present?
    [vm_attrs[:flavor].disk].flatten.compact.first.delete('IMAGE')
    [vm_attrs[:flavor].disk].flatten.compact.first['IMAGE_ID'] = args[:image_id]

    vm_attrs[:flavor].context['SET_HOSTNAME'] = args[:name]
    vm_attrs[:flavor].context['USER_DATA'] = args[:user_data] if args[:user_data].present?

    vm_attrs[:flavor].os['BOOT'] = 'disk0'
  else
    vm_attrs[:flavor].os['BOOT'] = 'nic0,disk0'
  end

  vm = super(vm_attrs)
  vm.wait_for { vm.status == 1 }
  find_vm_by_uuid(vm.id)
end

#possible_scheduler_hintsObject



26
27
28
# File 'app/models/foreman_opennebula/opennebula.rb', line 26

def possible_scheduler_hints
  %w[Cluster Host Raw]
end

#provided_attributesObject



22
23
24
# File 'app/models/foreman_opennebula/opennebula.rb', line 22

def provided_attributes
  super.merge({ :mac => :mac })
end

#save_vm(uuid, attr) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/models/foreman_opennebula/opennebula.rb', line 151

def save_vm(uuid, attr)
  vm = find_vm_by_uuid(uuid)
  changed_attrs = %i[cpu vcpu memory].reject { |k| vm.send(k).to_s == attr[k].to_s }

  if !vm.ready? && !vm.poweroff?
    raise ::Foreman::Exception.new N_("The VM status: #{vm.status}. It should be ACTIVE (3) or POWEROFF (8)")
  end

  last_state = vm.state
  if vm.ready?
    vm.onevm_object.poweroff(true)
    vm.wait_for { poweroff? }
  end

  if changed_attrs.any?
    flavor = client.flavors.new
    %i[cpu vcpu memory].each { |k| flavor.send("#{k}=", attr[k].to_s) }
    vm.onevm_object.resize(flavor.to_s, true)

    renewed_vm = find_vm_by_uuid(uuid)
    unchanged_attrs = changed_attrs.reject { |k| renewed_vm.send(k).to_s == attr[k].to_s }
    if unchanged_attrs.any?
      raise ::Foreman::Exception.new N_("Parameter #{unchanged_attrs.join(', ')} was not changed. "\
                                        'Check if the host has enough resources for this VM')
    end
  end

  if vm.disk_size.to_i != attr[:disk_size].to_i
    disk_id = vm.disks.first.disk_id.to_i
    rc = vm.onevm_object.disk_resize(disk_id, attr[:disk_size].to_i)
    raise ::Foreman::Exception.new N_(rc.message) if ::OpenNebula.is_error?(rc)
    sleep(1) # wait before resume vm
  end

  if last_state == 'RUNNING'
    vm.onevm_object.resume
    sleep(2)
    vm_ready(vm)
  end
  vm
end

#supports_update?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/foreman_opennebula/opennebula.rb', line 141

def supports_update?
  true
end

#test_connection(options = {}) ⇒ Object



193
194
195
196
197
198
# File 'app/models/foreman_opennebula/opennebula.rb', line 193

def test_connection(options = {})
  super
  %i[url user password].all? { |i| errors[i].empty? } && !client.client.get_version.is_a?(OpenNebula::Error)
rescue => e
  errors[:base] << e.message
end

#update_required?(old_attrs, new_attrs) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
# File 'app/models/foreman_opennebula/opennebula.rb', line 145

def update_required?(old_attrs, new_attrs)
  vm = find_vm_by_uuid(old_attrs[:uuid])
  old_attrs[:disk_size] = vm.disk_size
  super(old_attrs, new_attrs)
end

#vm_ready(vm) ⇒ Object

Raises:

  • (Foreman::Exception)


136
137
138
139
# File 'app/models/foreman_opennebula/opennebula.rb', line 136

def vm_ready(vm)
  vm.wait_for { ready? || poweroff? }
  raise Foreman::Exception.new(N_('Failed to run VM %{name}'), { :name => vm.name }) if vm.poweroff?
end