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



32
33
34
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 32

def cluster
  @cluster
end

#cluster_nameObject



36
37
38
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 36

def cluster_name
  cluster&.name
end

#cluster_name=(name) ⇒ Object



40
41
42
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 40

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

#compute_attributesObject



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

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



11
12
13
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 11

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

#interfacesObject



19
20
21
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 19

def interfaces
  network_adapters
end

#interfaces_attributes=(_attributes) ⇒ Object



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

def interfaces_attributes=(_attributes); end

#macObject



15
16
17
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 15

def mac
  network_adapters.first.mac
end

#secure_boot_enabledObject



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

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

  firmware.secure_boot == :On
end

#secure_boot_enabled=(enabled) ⇒ Object



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

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



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
135
136
137
138
139
140
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 98

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 }

  if !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'] }
    if nic_attrs['vlan_operation_mode'] == 'Access'
      potential.select! { |fn| fn.access_vlan_id.to_s == nic_attrs['access_vlan_id'] }
    elsif nic_attrs['vlan_operation_mode'] == 'Trunk'
      potential.select! { |fn| fn.native_vlan_id.to_s == nic_attrs['native_vlan_id'] }
      potential.select! { |fn| fn.allowed_vlan_ids.split(',').map(&:strip) == nic_attrs['allowed_vlan_ids'].split(',').map(&:strip) } \
        if nic_attrs['allowed_vlan_ids'].present?
    elsif nic_attrs['vlan_operation_mode'] == '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'
        potential.select! { |fn| fn.secondary_vlan_ids.split(',').map(&:strip) == nic_attrs['secondary_vlan_ids'].split(',').map(&:strip) } \
          if nic_attrs['secondary_vlan_ids'].present?
      else
        potential.select! { |fn| fn.secondary_vlan_id.to_s == nic_attrs['secondary_vlan_id'].to_s } \
          if nic_attrs['secondary_vlan_id'].present?
      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



7
8
9
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 7

def to_s
  name
end

#vlanObject



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

def vlan
  nic = network_adapters.first

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

#vlan=(vlan) ⇒ Object



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

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 > 0
    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



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

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

#volumesObject



23
24
25
# File 'app/models/concerns/fog_extensions/hyperv/server.rb', line 23

def volumes
  hard_drives
end

#volumes_attributes=(_attributes) ⇒ Object



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

def volumes_attributes=(_attributes); end