diff --git a/app/helpers/proxmox_vm_cloudinit_helper.rb b/app/helpers/proxmox_vm_cloudinit_helper.rb index 9d12c4ac3..4a2de2b2b 100644 --- a/app/helpers/proxmox_vm_cloudinit_helper.rb +++ b/app/helpers/proxmox_vm_cloudinit_helper.rb @@ -41,4 +41,37 @@ def parse_server_cloudinit(args) end cloudinit_h end + + def create_cloudinit_iso(vm_name, configs) + iso = File.join('/var/lib/vz/template/iso', "#{vm_name}_cloudinit.iso") + arguments = ["-output #{iso}", '-volid', 'cidata', '-joliet', '-rock'] + ssh = Fog::SSH.new(URI.parse(fog_credentials[:proxmox_url]).host, fog_credentials[:proxmox_username].split('@')[0], { password: fog_credentials[:proxmox_password] }) + wd = ssh.run([%({mktemp -d})]).first.stdout.chomp + configs.each do |config| + begin + ssh.run([%({echo "#{config[1]}" >> "#{wd}/#{config[0]}"})]) + rescue ::Foreman::Exception => e + logger.warn("Error writing to the file #{config[0]}: #{e}") + end + arguments.append(File.join(wd, config[0])) + end + logger.warn("iso image generation args: #{arguments}") + raise Foreman::Exception.new(N_('ISO build failed')) unless ssh.run([%({genisoimage #{arguments.join(' ')}})]) + ssh.run([%(rm -rf #{wd})]) + iso + end + + def parse_cloudinit_config(cloned_vm, args) + config_data = args.delete('user_data').split('#network-config').append("/ninstance-id: #{vm.name}") + configs = ['user-data', 'network-config', 'meta-data'].zip(config_data).to_h + + iso = create_cloudinit_iso(cloned_vm.name, configs) + args.merge(attach_cloudinit_iso(cloned_vm.node_id, iso)) + end + + def attach_cloudinit_iso(node, iso) + storage = storages(node, 'iso')[0] + volume = storage.volumes.detect { |v| v.volid.include? File.basename(iso) } + { ide2: "#{volume.volid},media=cdrom" } + end end diff --git a/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb b/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb index cfe5ddfa7..4d4b8f76a 100644 --- a/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb +++ b/app/models/foreman_fog_proxmox/proxmox_vm_commands.rb @@ -42,6 +42,7 @@ def create_vm(args = {}) remove_volume_keys(args) if image_id vm = clone_from_image(image_id, vmid) + args = parse_cloudinit_config(vm, args) if args[:user_data] vm.update(compute_clone_attributes(args, vm.container?, type)) update_pool(vm, args[:pool]) if args[:pool] else