Class: ForemanHyperv::Hyperv

Inherits:
ComputeResource
  • Object
show all
Defined in:
app/models/foreman_hyperv/hyperv.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.model_nameObject



13
14
15
# File 'app/models/foreman_hyperv/hyperv.rb', line 13

def self.model_name
  ComputeResource.model_name
end

.provider_friendly_nameObject



9
10
11
# File 'app/models/foreman_hyperv/hyperv.rb', line 9

def self.provider_friendly_name
  'Hyper-V'
end

Instance Method Details

#associated_host(vm) ⇒ Object



37
38
39
# File 'app/models/foreman_hyperv/hyperv.rb', line 37

def associated_host(vm)
  associate_by('mac', vm.clean_mac_addresses)
end

#available_hypervisorsObject Also known as: hosts



159
160
161
# File 'app/models/foreman_hyperv/hyperv.rb', line 159

def available_hypervisors
  client.hosts
end

#capabilitiesObject



5
6
7
# File 'app/models/foreman_hyperv/hyperv.rb', line 5

def capabilities
  [:build]
end

#clustersObject



164
165
166
167
168
169
170
171
172
# File 'app/models/foreman_hyperv/hyperv.rb', line 164

def clusters
  if client.respond_to? :supports_clusters?
    return [] unless client.supports_clusters?
  end

  client.clusters
rescue
  []
end

#create_vm(args = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/foreman_hyperv/hyperv.rb', line 54

def create_vm(args = {})
  args = vm_instance_defaults.merge(args.to_hash.deep_symbolize_keys)
  client.logger.debug "Creating a VM with arguments; #{args}"

  args[:computer_name] = args[:computer_name].presence || '.'

  pre_create = {
    boot_device: 'NetworkAdapter',
    computer_name: args[:computer_name],
    generation: args[:generation].to_i,
    memory_startup: args[:memory_startup].presence.to_i,
    name: args[:name],
    no_vhd: true
  }

  vm = client.servers.create pre_create

  post_save = {
    dynamic_memory_enabled: Foreman::Cast.to_bool(args[:dynamic_memory_enabled]),
    memory_minimum: args[:memory_minimum].presence.to_i,
    memory_maximum: args[:memory_maximum].presence.to_i,
    notes: args[:notes].presence,
    processor_count: args[:processor_count].to_i
  }
  post_save.each do |k, v|
    vm.send("#{k}=".to_sym, v)
  end

  vm.save if vm.dirty?

  if vm.generation == 2 && args[:secure_boot_enabled].present?
    f = vm.firmware
    f.secure_boot = Foreman::Cast.to_bool(args[:secure_boot_enabled]) ? :On : :Off
    f.save if f.dirty?
  end

  create_interfaces(vm, args[:interfaces_attributes])
  create_volumes(vm, args[:volumes_attributes])

  vm.set_vlan(args[:vlan].to_i) if args[:vlan].presence && vm.respond_to?(:set_vlan)

  vm
rescue StandardError => e
  vm.stop turn_off: true

  raise e
end

#destroy_vm(uuid) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/foreman_hyperv/hyperv.rb', line 122

def destroy_vm(uuid)
  vm = find_vm_by_uuid(uuid)
  vm.stop force: true if vm.ready?
  vm.hard_drives.each do |hd|
    hd.vhd.destroy if hd.path
  end
  # TODO: Remove the empty VM folder
  vm.destroy
rescue ActiveRecord::RecordNotFound, Fog::Errors::NotFound
  # if the VM does not exists, we don't really care.
  true
end

#editable_network_interfaces?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/foreman_hyperv/hyperv.rb', line 147

def editable_network_interfaces?
  true
end

#hypervisorObject



174
175
176
# File 'app/models/foreman_hyperv/hyperv.rb', line 174

def hypervisor
  client.hosts.first
end

#max_cpu_countObject

TODO



29
30
31
# File 'app/models/foreman_hyperv/hyperv.rb', line 29

def max_cpu_count
  hypervisor.logical_processor_count
end

#max_memoryObject



33
34
35
# File 'app/models/foreman_hyperv/hyperv.rb', line 33

def max_memory
  hypervisor.memory_capacity
end

#new_cdrom(attr = {}) ⇒ Object



143
144
145
# File 'app/models/foreman_hyperv/hyperv.rb', line 143

def new_cdrom(attr = {})
  client.dvd_drives.new attr
end

#new_interface(attr = {}) ⇒ Object



135
136
137
# File 'app/models/foreman_hyperv/hyperv.rb', line 135

def new_interface(attr = {})
  client.network_adapters.new attr
end

#new_vm(attr = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'app/models/foreman_hyperv/hyperv.rb', line 41

def new_vm(attr = {})
  vm = super
  interfaces = nested_attributes_for :interfaces, attr[:interfaces_attributes]
  interfaces.map { |i| vm.interfaces << new_interface(i) }
  volumes = nested_attributes_for :volumes, attr[:volumes_attributes]
  volumes.map { |v| vm.volumes << new_volume(v) }
  vm
end

#new_volume(attr = {}) ⇒ Object



139
140
141
# File 'app/models/foreman_hyperv/hyperv.rb', line 139

def new_volume(attr = {})
  client.vhds.new attr
end

#provided_attributesObject



24
25
26
# File 'app/models/foreman_hyperv/hyperv.rb', line 24

def provided_attributes
  super.merge(mac: :mac)
end

#save_vm(uuid, attr) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/foreman_hyperv/hyperv.rb', line 102

def save_vm(uuid, attr)
  vm = find_vm_by_uuid(uuid)
  client.logger.debug "Saving a VM with arguments; #{attr}"
  attr.each do |k, v|
    vm.send("#{k}=".to_sym, v) if vm.respond_to?("#{k}=".to_sym)
  end

  if vm.generation == 2 && attr[:secure_boot_enabled].present?
    f = vm.firmware
    f.secure_boot = Foreman::Cast.to_bool(attr[:secure_boot_enabled]) ? :On : :Off
    f.save if f.dirty?
  end

  update_interfaces(vm, attr[:interfaces_attributes])
  update_volumes(vm, attr[:volumes_attributes])

  vm.save if vm.dirty?
  vm
end

#stop_vm(uuid) ⇒ Object



50
51
52
# File 'app/models/foreman_hyperv/hyperv.rb', line 50

def stop_vm(uuid)
  find_vm_by_uuid(uuid).stop force: true
end

#supports_update?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'app/models/foreman_hyperv/hyperv.rb', line 155

def supports_update?
  true
end

#switchesObject



151
152
153
# File 'app/models/foreman_hyperv/hyperv.rb', line 151

def switches
  client.switches.all # _quick_query: true
end

#test_connection(options = {}) ⇒ Object



17
18
19
20
21
22
# File 'app/models/foreman_hyperv/hyperv.rb', line 17

def test_connection(options = {})
  super
  client.valid?
rescue Fog::Hyperv::Errors::ServiceError, ArgumentError, WinRM::WinRMAuthorizationError => e
  errors[:base] << e.message
end