Module: ProxmoxContainerHelper

Included in:
ForemanFogProxmox::Proxmox
Defined in:
app/helpers/proxmox_container_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_container_interface(interface_attributes, interfaces_to_delete, interfaces_to_add) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/helpers/proxmox_container_helper.rb', line 140

def add_container_interface(interface_attributes, interfaces_to_delete, interfaces_to_add)
  interface_attributes.delete_if { |_key, value| ForemanFogProxmox::Value.empty?(value) }
  nic = {}
  id = interface_attributes['id']
  logger.debug("parse_container_interface(): id=#{id}")
  delete = interface_attributes['_delete'].to_i == 1
  if delete
    interfaces_to_delete.push(id.to_s)
  else
    nic.store(:id, id)
    nic.store(:hwaddr, interface_attributes['macaddr']) if interface_attributes['macaddr']
    nic.store(:name, interface_attributes['name'].to_s)
    nic.store(:bridge, interface_attributes['bridge'].to_s) if interface_attributes['bridge']
    nic.store(:ip, interface_attributes['ip'].to_s) if interface_attributes['ip']
    nic.store(:ip6, interface_attributes['ip6'].to_s) if interface_attributes['ip6']
    nic.store(:gw, interface_attributes['gw'].to_s) if interface_attributes['gw']
    nic.store(:gw6, interface_attributes['gw6'].to_s) if interface_attributes['gw6']
    nic.store(:rate, interface_attributes['rate'].to_i) if interface_attributes['rate']
    nic.store(:tag, interface_attributes['tag'].to_i) if interface_attributes['tag']
    logger.debug("parse_container_interface(): add nic=#{nic}")
    interfaces_to_add.push(Fog::Proxmox::NicHelper.flatten(nic))
  end
end

#config_general_or_ostemplate_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/helpers/proxmox_container_helper.rb', line 43

def config_general_or_ostemplate_key?(key)
  config_keys[:general].include?(key) || ostemplate_keys.include?(key)
end

#config_keysObject



25
26
27
28
29
30
31
32
33
# File 'app/helpers/proxmox_container_helper.rb', line 25

def config_keys
  general_a = ['node_id', 'name', 'type', 'config_attributes', 'volumes_attributes', 'interfaces_attributes']
  general_a += ['firmware_type', 'provision_method', 'container_volumes', 'server_volumes', 'start_after_create']
  keys = { general: general_a }
  keys.store(:main, ['name', 'type', 'node_id', 'vmid', 'interfaces', 'mount_points', 'disks'])
  keys.store(:cpu, ['arch', 'cpulimit', 'cpuunits', 'cores'])
  keys.store(:memory, ['memory', 'swap'])
  keys
end

#ostemplate_keysObject



35
36
37
# File 'app/helpers/proxmox_container_helper.rb', line 35

def ostemplate_keys
  ['ostemplate', 'ostemplate_storage', 'ostemplate_file']
end

#parse_container_cpu(args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/proxmox_container_helper.rb', line 84

def parse_container_cpu(args)
  cpu = {}
  args.delete_if { |_key, value| ForemanFogProxmox::Value.empty?(value) }
  cpu.store(:arch, args['arch'].to_s) if args['arch']
  cpu.store(:cpulimit, args['cpulimit'].to_i) if args['cpulimit']
  cpu.store(:cpuunits, args['cpuunits'].to_i) if args['cpuunits']
  cpu.store(:cores, args['cores'].to_i) if args['cores']
  logger.debug("parse_container_cpu(): #{cpu}")
  cpu
end

#parse_container_interfaces(interfaces_attributes) ⇒ Object



132
133
134
135
136
137
138
# File 'app/helpers/proxmox_container_helper.rb', line 132

def parse_container_interfaces(interfaces_attributes)
  interfaces_to_add = []
  interfaces_to_delete = []
  interfaces_attributes&.each_value { |value| add_container_interface(value, interfaces_to_delete, interfaces_to_add) }
  logger.debug("parse_container_interfaces(): interfaces_to_add=#{interfaces_to_add}, interfaces_to_delete=#{interfaces_to_delete}")
  [interfaces_to_add, interfaces_to_delete]
end

#parse_container_memory(args) ⇒ Object



75
76
77
78
79
80
81
82
# File 'app/helpers/proxmox_container_helper.rb', line 75

def parse_container_memory(args)
  memory = {}
  args.delete_if { |_key, value| ForemanFogProxmox::Value.empty?(value) }
  memory.store(:memory, args['memory'].to_i) if args['memory']
  memory.store(:swap, args['swap'].to_i) if args['swap']
  logger.debug("parse_container_memory(): #{memory}")
  memory
end

#parse_container_ostemplate(args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/proxmox_container_helper.rb', line 95

def parse_container_ostemplate(args)
  ostemplate = args['ostemplate']
  ostemplate_file = args['ostemplate_file']
  ostemplate ||= ostemplate_file
  ostemplate_storage = args['ostemplate_storage']
  ostemplate_storage, ostemplate_file, _size = Fog::Proxmox::DiskHelper.extract_storage_volid_size(ostemplate) unless ForemanFogProxmox::Value.empty?(ostemplate)
  parsed_ostemplate = { ostemplate: ostemplate, ostemplate_file: ostemplate_file, ostemplate_storage: ostemplate_storage }
  logger.debug("parse_container_ostemplate(): #{parsed_ostemplate}")
  parsed_ostemplate
end

#parse_container_vm(args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/proxmox_container_helper.rb', line 47

def parse_container_vm(args)
  logger.debug("parse_container_vm args=#{args}")
  args = ActiveSupport::HashWithIndifferentAccess.new(args)
  return {} unless args
  return {} if args.empty?
  return {} unless args['type'] == 'lxc'

  config = args['config_attributes']
  config ||= args.reject { |key, _value| config_keys[:main].include? key }
  ostemplate = parse_ostemplate_without_keys(args)
  ostemplate = parse_ostemplate_without_keys(config) unless ostemplate[:ostemplate]
  volumes = parse_container_volumes(args['volumes_attributes'])
  cpu = parse_container_cpu(config.select { |key, _value| config_keys[:cpu].include? key })
  memory = parse_container_memory(config.select { |key, _value| config_keys[:memory].include? key })
  interfaces_attributes = args['interfaces_attributes']
  interfaces_to_add, interfaces_to_delete = parse_container_interfaces(interfaces_attributes)
  logger.debug("config_keys[:general]: #{config_keys[:general]}")
  parsed_vm = args.reject { |key, value| config_general_or_ostemplate_key?(key) || ForemanFogProxmox::Value.empty?(value) }
  parsed_config = config.reject { |key, value| config_keys.include?(key) || ForemanFogProxmox::Value.empty?(value) }
  logger.debug("parse_container_config(): #{parsed_config}")
  parsed_vm = parsed_vm.merge(parsed_config).merge(cpu).merge(memory).merge(ostemplate)
  interfaces_to_add.each { |interface| parsed_vm = parsed_vm.merge(interface) }
  parsed_vm = parsed_vm.merge(delete: interfaces_to_delete.join(',')) unless interfaces_to_delete.empty?
  volumes.each { |volume| parsed_vm = parsed_vm.merge(volume) }
  logger.debug("parse_container_vm(): #{parsed_vm}")
  parsed_vm
end

#parse_container_volume(args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/proxmox_container_helper.rb', line 106

def parse_container_volume(args)
  disk = {}
  id = args['id']
  id = "mp#{args['device']}" if args.key?('device')
  logger.debug("parse_container_volume() args=#{args}")
  return args if ForemanFogProxmox::Value.empty?(id) || Fog::Proxmox::DiskHelper.server_disk?(id)

  args.delete_if { |_key, value| ForemanFogProxmox::Value.empty?(value) }
  disk.store(:id, id)
  disk.store(:volid, args['volid'])
  disk.store(:storage, args['storage'].to_s)
  disk.store(:size, args['size'].to_i)
  options = args.reject { |key, _value| ['id', 'volid', 'device', 'storage', 'size', '_delete'].include? key }
  disk.store(:options, options)
  logger.debug("parse_container_volume(): disk=#{disk}")
  Fog::Proxmox::DiskHelper.flatten(disk)
end

#parse_container_volumes(args) ⇒ Object



124
125
126
127
128
129
130
# File 'app/helpers/proxmox_container_helper.rb', line 124

def parse_container_volumes(args)
  logger.debug("parse_container_volumes() args=#{args}")
  volumes = []
  args&.each_value { |value| volumes.push(parse_container_volume(value)) }
  logger.debug("parse_container_volumes(): volumes=#{volumes}")
  volumes
end

#parse_ostemplate_without_keys(args) ⇒ Object



39
40
41
# File 'app/helpers/proxmox_container_helper.rb', line 39

def parse_ostemplate_without_keys(args)
  parse_container_ostemplate(args.select { |key, _value| ostemplate_keys.include? key })
end