Class: ForemanHyperv::Hyperv
- Inherits:
-
ComputeResource
- Object
- ComputeResource
- ForemanHyperv::Hyperv
- Defined in:
- app/models/foreman_hyperv/hyperv.rb
Class Method Summary collapse
Instance Method Summary collapse
- #associated_host(vm) ⇒ Object
- #available_hypervisors ⇒ Object
- #capabilities ⇒ Object
- #create_vm(args = {}) ⇒ Object
- #destroy_vm(uuid) ⇒ Object
- #editable_network_interfaces? ⇒ Boolean
- #hypervisor ⇒ Object
-
#max_cpu_count ⇒ Object
TODO.
- #max_memory ⇒ Object
- #new_cdrom(attr = {}) ⇒ Object
- #new_interface(attr = {}) ⇒ Object
- #new_vm(attr = {}) ⇒ Object
- #new_volume(attr = {}) ⇒ Object
- #provided_attributes ⇒ Object
- #save_vm(uuid, attr) ⇒ Object
- #stop_vm(uuid) ⇒ Object
- #supports_update? ⇒ Boolean
- #switches ⇒ Object
- #test_connection(options = {}) ⇒ Object
Class Method Details
.model_name ⇒ Object
13 14 15 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 13 def self.model_name ComputeResource.model_name end |
.provider_friendly_name ⇒ Object
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_hypervisors ⇒ Object
154 155 156 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 154 def available_hypervisors client.hosts end |
#capabilities ⇒ Object
5 6 7 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 5 def capabilities [:build] 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 |
# 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}" pre_create = { boot_device: 'NetworkAdapter', 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 rescue StandardError => e vm.stop turn_off: true raise e end |
#destroy_vm(uuid) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 117 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
142 143 144 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 142 def editable_network_interfaces? true end |
#hypervisor ⇒ Object
158 159 160 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 158 def hypervisor client.hosts.first end |
#max_cpu_count ⇒ Object
TODO
29 30 31 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 29 def max_cpu_count hypervisor.logical_processor_count end |
#max_memory ⇒ Object
33 34 35 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 33 def max_memory hypervisor.memory_capacity end |
#new_cdrom(attr = {}) ⇒ Object
138 139 140 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 138 def new_cdrom(attr = {}) client.dvd_drives.new attr end |
#new_interface(attr = {}) ⇒ Object
130 131 132 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 130 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
134 135 136 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 134 def new_volume(attr = {}) client.vhds.new attr end |
#provided_attributes ⇒ Object
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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 97 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
150 151 152 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 150 def supports_update? true end |
#switches ⇒ Object
146 147 148 |
# File 'app/models/foreman_hyperv/hyperv.rb', line 146 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( = {}) super client.valid? rescue Fog::Hyperv::Errors::ServiceError, ArgumentError, WinRM::WinRMAuthorizationError => e errors[:base] << e. end |