Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #
- # Copyright (C) 2016 Chris Blake <chris@servernetworktech.com>
- #
- import netifaces
- import os
- from proxmoxer import ProxmoxAPI
- import subprocess
- import sys
- # Configure API call information
- proxmox = ProxmoxAPI('10.XX.XX.XX', user='ReadOnlyAPI@pve',
- password='PASSGOESHERE', verify_ssl=False)
- # Get the mac of eth0 (as we always have this int)
- mac = netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr'].upper()
- ip = netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
- # Default hostname we will use with regex
- name="debian"
- # Var to hold userdata
- userdata = ""
- # Set return so we can break out later to save resources
- found=0
- # For each node
- for node in proxmox.cluster.resources.get(type='node'):
- if found == 1:
- break
- # For each VM
- for vm in proxmox.nodes(node['node']).qemu.get():
- if found == 1:
- break
- # For each VM config (can't be tied to above call (GAY!))
- vmconfig = proxmox.nodes(node['node']).qemu(vm['vmid']).config.get()
- # Parse out net info only!
- for key in vmconfig.keys():
- if found == 1:
- break
- if "net" in key:
- # Did we find our MAC?
- if mac in vmconfig[key]:
- name = vm['name']
- found=1
- if "==USERDATA-START==" in vmconfig['description']:
- userdata = vmconfig['description'].rpartition('==USERDATA-END==')[0].rpartition('==USERDATA-START==')[2]
- break
- # Set mac if we can
- if found == 1:
- # Replace hostname in core files
- os.system('sed -i \'s|debian|' + name + '|g\' /etc/hostname')
- os.system('sed -i \'s|debian|' + name + '|g\' /etc/hosts')
- # Reload hostname
- os.system('hostname -F /etc/hostname')
- # Verify new hostname
- if name not in str(subprocess.check_output("hostname -f", shell=True)):
- os.system('echo "\n\nError with Prevision!\nOur IP is: ' + ip + '" > /dev/console')
- sys.exit(1)
- else:
- # Now let's check for userdata and run if needed
- if userdata != "":
- with open("/tmp/cloud-init.sh", 'a') as out:
- out.write(userdata)
- os.system('chmod +x /tmp/cloud-init.sh')
- # We want to wait for this, otherwise we won't know our IP
- os.system('bash /tmp/cloud-init.sh | tee /var/log/cloud-init-output.log > /dev/console 2>&1')
- # All good, let's clear console a bit and print out our IP for peoplez
- os.system('sleep 5 && echo "\n\nOur IP is: ' + ip + '" > /dev/console')
- sys.exit(0)
- else:
- os.system('echo "\n\nError with Prevision!\nOur IP is: ' + ip + '" > /dev/console')
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement