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

#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



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

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

#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



79
80
81
82
83
84
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 79

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

  firmware.secure_boot == :On
end

#secure_boot_enabled=(enabled) ⇒ Object



70
71
72
73
74
75
76
77
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 70

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

  @secure_boot = enabled
  return unless persisted?

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

#select_nic(fog_nics, nic) ⇒ Object



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
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 99

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



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

def vlan
  nic = network_adapters.first

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

#vlan=(vlan) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 52

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



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

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