Skip to content

Commit

Permalink
Provide a hostname for the VM or use random one
Browse files Browse the repository at this point in the history
This patch adds an option to provide a VM hostname. If it is not
provided then a random name xcp-ng-XXXXX is generated where XXXXX are
random lowercase charaters.

Signed-off-by: Guillaume <[email protected]>
  • Loading branch information
gthvn1 committed Aug 24, 2023
1 parent 2fa3e69 commit 3644204
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions scripts/install_xcpng.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import atexit
import logging
import os
import random
import requests
import string
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -41,12 +43,16 @@ def generate_boot_conf(directory, installer, action):
{rt}
""")

def generate_answerfile(directory, installer, hostname_or_ip, action, hdd):
def generate_answerfile(directory, installer, hostname_or_ip, target_hostname, action, hdd):
pxe = pxe_address()
password = host_data(hostname_or_ip)['password']
cmd = ['openssl', 'passwd', '-6', password]
res = subprocess.run(cmd, stdout=subprocess.PIPE)
encrypted_password = res.stdout.decode().strip()
if target_hostname is None:
target_hostname = "xcp-ng-" + "".join(
random.choice(string.ascii_lowercase) for i in range(5)
)
with open(f'{directory}/answerfile.xml', 'w') as answerfile:
if action == 'install':
answerfile.write(f"""<?xml version="1.0"?>
Expand All @@ -58,6 +64,7 @@ def generate_answerfile(directory, installer, hostname_or_ip, action, hdd):
<source type="url">{installer}</source>
<admin-interface name="eth0" proto="dhcp" />
<timezone>Europe/Paris</timezone>
<hostname>{target_hostname}</hostname>
<script stage="filesystem-populated" type="url">
http://{pxe}/configs/presets/scripts/filesystem-populated.py
</script>
Expand Down Expand Up @@ -174,6 +181,11 @@ def main():
"In case of a restore, specify the version of the installer."
)
parser.add_argument("--installer", help="URL of the installer")
parser.add_argument(
"-t", "--target-hostname",
help="The hostname of the VM in which XCP-ng will be installed. By default "
"a hostname is generated starting with xcp-ng-XXXXX where XXXXX is "
"randomly generated using lowercase characters.")
args = parser.parse_args()

# *** "fail early" checks
Expand Down Expand Up @@ -217,7 +229,7 @@ def main():
with tempfile.TemporaryDirectory(suffix=mac_address) as tmp_local_path:
logging.info('Generate files: answerfile.xml and boot.conf')
hdd = 'nvme0n1' if vm.is_uefi else 'sda'
generate_answerfile(tmp_local_path, installer, args.host, args.action, hdd)
generate_answerfile(tmp_local_path, installer, args.host, args.target_hostname, args.action, hdd)
generate_boot_conf(tmp_local_path, installer, args.action)
logging.info('Copy files to the pxe server')
copy_files_to_pxe(mac_address, tmp_local_path)
Expand Down

0 comments on commit 3644204

Please sign in to comment.