Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (C) 2016 Chris Blake <chris@servernetworktech.com>
  4. #
  5. import netifaces
  6. import os
  7. from proxmoxer import ProxmoxAPI
  8. import subprocess
  9. import sys
  10.  
  11. # Configure API call information
  12. proxmox = ProxmoxAPI('10.XX.XX.XX', user='ReadOnlyAPI@pve',
  13. password='PASSGOESHERE', verify_ssl=False)
  14.  
  15. # Get the mac of eth0 (as we always have this int)
  16. mac = netifaces.ifaddresses('eth0')[netifaces.AF_LINK][0]['addr'].upper()
  17. ip = netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
  18.  
  19. # Default hostname we will use with regex
  20. name="debian"
  21.  
  22. # Var to hold userdata
  23. userdata = ""
  24.  
  25. # Set return so we can break out later to save resources
  26. found=0
  27.  
  28. # For each node
  29. for node in proxmox.cluster.resources.get(type='node'):
  30. if found == 1:
  31. break
  32. # For each VM
  33. for vm in proxmox.nodes(node['node']).qemu.get():
  34. if found == 1:
  35. break
  36. # For each VM config (can't be tied to above call (GAY!))
  37. vmconfig = proxmox.nodes(node['node']).qemu(vm['vmid']).config.get()
  38. # Parse out net info only!
  39. for key in vmconfig.keys():
  40. if found == 1:
  41. break
  42. if "net" in key:
  43. # Did we find our MAC?
  44. if mac in vmconfig[key]:
  45. name = vm['name']
  46. found=1
  47. if "==USERDATA-START==" in vmconfig['description']:
  48. userdata = vmconfig['description'].rpartition('==USERDATA-END==')[0].rpartition('==USERDATA-START==')[2]
  49. break
  50.  
  51. # Set mac if we can
  52. if found == 1:
  53. # Replace hostname in core files
  54. os.system('sed -i \'s|debian|' + name + '|g\' /etc/hostname')
  55. os.system('sed -i \'s|debian|' + name + '|g\' /etc/hosts')
  56.  
  57. # Reload hostname
  58. os.system('hostname -F /etc/hostname')
  59.  
  60. # Verify new hostname
  61. if name not in str(subprocess.check_output("hostname -f", shell=True)):
  62. os.system('echo "\n\nError with Prevision!\nOur IP is: ' + ip + '" > /dev/console')
  63. sys.exit(1)
  64. else:
  65. # Now let's check for userdata and run if needed
  66. if userdata != "":
  67. with open("/tmp/cloud-init.sh", 'a') as out:
  68. out.write(userdata)
  69. os.system('chmod +x /tmp/cloud-init.sh')
  70. # We want to wait for this, otherwise we won't know our IP
  71. os.system('bash /tmp/cloud-init.sh | tee /var/log/cloud-init-output.log > /dev/console 2>&1')
  72.  
  73. # All good, let's clear console a bit and print out our IP for peoplez
  74. os.system('sleep 5 && echo "\n\nOur IP is: ' + ip + '" > /dev/console')
  75. sys.exit(0)
  76. else:
  77. os.system('echo "\n\nError with Prevision!\nOur IP is: ' + ip + '" > /dev/console')
  78. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement