Module: FogExtensions::Hyperv::Server

Extended by:
ActiveSupport::Concern
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/models/concerns/fog_extensions/hyperv/server.rb

Instance Method Summary collapse

Instance Method Details

#boot_deviceObject



46
47
48
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 46

def boot_device
  :NetworkAdapter
end

#clusterObject

Override fog configuration with explicit cluster_name handling



34
35
36
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 34

def cluster
  @cluster
end

#cluster_nameObject



38
39
40
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 38

def cluster_name
  cluster&.name
end

#cluster_name=(name) ⇒ Object



42
43
44
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 42

def cluster_name=(name)
  @cluster = service.clusters.get(name)
end

#compute_attributesObject



105
106
107
108
109
110
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 105

def compute_attributes
  attributes.merge(
    interfaces_attributes: vm.network_adapters.each_with_index.to_h { |nic, idx| [idx, nic.compute_attributes] },
    volumes_attributes: vm.hard_drives.each_with_index.to_h { |hdd, idx| [idx, hdd.compute_attributes] }
  )
end

#folder_nameObject



13
14
15
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 13

def folder_name
  name.gsub(/[^0-9A-Za-z.-]/, '_')
end

#foreman_firmwareObject



56
57
58
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 56

def foreman_firmware
  generation_num == 1 ? 'bios' : 'efi'
end

#foreman_firmware_typeObject



50
51
52
53
54
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 50

def foreman_firmware_type
  return 'bios' if generation_num == 1

  secure_boot_enabled ? 'uefi_secure_boot' : 'uefi'
end

#interfacesObject



21
22
23
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 21

def interfaces
  network_adapters
end

#interfaces_attributes=(_attributes) ⇒ Object



29
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 29

def interfaces_attributes=(_attributes); end

#macObject



17
18
19
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 17

def mac
  network_adapters.first.mac
end

#secure_boot_enabledObject



92
93
94
95
96
97
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 92

def secure_boot_enabled
  return false if generation != :UEFI
  return unless persisted?

  firmware.secure_boot == :On
end

#secure_boot_enabled=(enabled) ⇒ Object



84
85
86
87
88
89
90
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 84

def secure_boot_enabled=(enabled)
  return if generation != :UEFI
  return unless persisted?

  firmware.secure_boot = enabled ? :On : :Off
  firmware.save
end

#select_nic(fog_nics, nic) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 112

def select_nic(fog_nics, nic)
  nic_attrs = nic.compute_attributes

  # Match for exact data
  match   = fog_nics.detect { |fn| fn.id == nic_attrs['id'].presence }
  match ||= fog_nics.detect { |fn| fn.mac == nic.mac }
  # match ||= fog_nics.detect { |fn| fn.name == nic_attrs['name'].presence }

  unless match
    # Match on networking, limit potentials down to identical configuration and then pick the first
    potential = fog_nics.select do |fn|
      fn.switch_id == nic_attrs['switch_id'].presence || fn.switch_name == nic_attrs['switch_name'].presence
    end
    potential.select! { |fn| fn.vlan_operation_mode.to_s == nic_attrs['vlan_operation_mode'] }
    case nic_attrs['vlan_operation_mode']
    when 'Access'
      potential.select! { |fn| fn.access_vlan_id.to_s == nic_attrs['access_vlan_id'] }
    when 'Trunk'
      potential.select! { |fn| fn.native_vlan_id.to_s == nic_attrs['native_vlan_id'] }
      if nic_attrs['allowed_vlan_ids'].present?
        potential.select! do |fn|
          fn.allowed_vlan_ids.split(',').map(&:strip) == nic_attrs['allowed_vlan_ids'].split(',').map(&:strip)
        end
      end
    when 'Private'
      potential.select! { |fn| fn.vlan_private_mode.to_s == nic_attrs['vlan_private_mode'] }
      potential.select! { |fn| fn.primary_vlan_id.to_s == nic_attrs['primary_vlan_id'].to_s } \
        if nic_attrs['primary_vlan_id'].present?

      if nic_attrs['vlan_private_mode'] == 'Promiscuous'
        if nic_attrs['secondary_vlan_ids'].present?
          potential.select! do |fn|
            fn.secondary_vlan_ids.split(',').map(&:strip) == nic_attrs['secondary_vlan_ids'].split(',').map(&:strip)
          end
        end
      elsif nic_attrs['secondary_vlan_id'].present?
        potential.select! { |fn| fn.secondary_vlan_id.to_s == nic_attrs['secondary_vlan_id'].to_s }
      end
    end

    match = potential.first
  end
  return unless match

  # Store Hyper-V ID in compute attributes
  nic.compute_attributes['identity'] = match.id

  match
end

#to_sObject



9
10
11
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 9

def to_s
  name
end

#vlanObject



60
61
62
63
64
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 60

def vlan
  nic = network_adapters.first

  nic.access_vlan_id || nic.native_vlan_id || nic.primary_vlan_id
end

#vlan=(vlan) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 66

def vlan=(vlan)
  logger.warn "using vlan=#{vlan.inspect} on Hyper-V VM, this can lead to unexpected results"
  nic = network_adapters.first
  if vlan.present? && vlan.to_i.positive?
    nic.vlan_operation_mode = :Access if nic.vlan_operation_mode == :Untagged
    case nic.vlan_operation_mode
    when :Access
      nic.access_vlan_id = vlan
    when :Trunk
      nic.native_vlan_id = vlan
    when :Private
      nic.primary_vlan_id = vlan
    end
  else
    nic.vlan_operation_mode = :Untagged
  end
end

#vm_descriptionObject



99
100
101
102
103
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 99

def vm_description
  format _('%{cpus} CPUs and %{ram} memory'),
         cpus: processor_count,
         ram: number_to_human_size(memory_startup)
end

#volumesObject



25
26
27
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 25

def volumes
  hard_drives
end

#volumes_attributes=(_attributes) ⇒ Object



31
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 31

def volumes_attributes=(_attributes); end