|
15 | 15 | from packaging import version
|
16 | 16 |
|
17 | 17 | sys.path.append(f"{os.path.abspath(os.path.dirname(__file__))}/..") # noqa
|
| 18 | +from lib import pxe |
18 | 19 | from lib.commands import ssh, scp, SSHCommandFailed
|
19 | 20 | from lib.common import wait_for, is_uuid
|
20 | 21 | from lib.host import host_data
|
|
23 | 24 |
|
24 | 25 | logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO)
|
25 | 26 |
|
26 |
| -PXE_CONFIG_DIR = "/pxe/configs/custom" |
27 |
| - |
28 | 27 | def pxe_address():
|
29 |
| - try: |
30 |
| - from data import PXE_CONFIG_SERVER |
31 |
| - return PXE_CONFIG_SERVER |
32 |
| - except ImportError: |
33 |
| - raise Exception('No address for the PXE server found in data.py (`PXE_CONFIG_SERVER`)') |
34 |
| - |
35 |
| -def generate_boot_conf(directory, installer, action): |
36 |
| - # in case of restore, we disable the text ui from the installer completely, |
37 |
| - # to workaround a bug that leaves us stuck on a confirmation dialog at the end of the operation. |
38 |
| - rt = 'rt=1' if action == 'restore' else '' |
39 |
| - with open(f'{directory}/boot.conf', 'w') as bootfile: |
40 |
| - bootfile.write(f"""answerfile=custom |
41 |
| -installer={installer} |
42 |
| -is_default=1 |
43 |
| -{rt} |
44 |
| -""") |
| 28 | + return pxe.PXE_CONFIG_SERVER |
45 | 29 |
|
46 | 30 | def generate_answerfile(directory, installer, hostname_or_ip, target_hostname, action, hdd, netinstall_gpg_check):
|
47 | 31 | pxe = pxe_address()
|
@@ -89,37 +73,16 @@ def generate_answerfile(directory, installer, hostname_or_ip, target_hostname, a
|
89 | 73 | raise Exception(f"Unknown action: `{action}`")
|
90 | 74 |
|
91 | 75 | def copy_files_to_pxe(mac_address, tmp_local_path):
|
92 |
| - assert mac_address |
93 |
| - pxe = pxe_address() |
94 |
| - remote_dir = f'{PXE_CONFIG_DIR}/{mac_address}/' |
95 |
| - clean_files_on_pxe(mac_address) |
96 |
| - ssh(pxe, ['mkdir', '-p', remote_dir]) |
97 |
| - scp(pxe, f'{tmp_local_path}/boot.conf', remote_dir) |
98 |
| - scp(pxe, f'{tmp_local_path}/answerfile.xml', remote_dir) |
| 76 | + pxe.server_push_config(mac_address, tmp_local_path) |
99 | 77 |
|
100 | 78 | def clean_files_on_pxe(mac_address):
|
101 |
| - assert mac_address # protection against deleting the whole parent dir! |
102 |
| - pxe = pxe_address() |
103 |
| - remote_dir = f'{PXE_CONFIG_DIR}/{mac_address}/' |
104 |
| - ssh(pxe, ['rm', '-rf', remote_dir]) |
| 79 | + pxe.server_remove_config(mac_address) |
105 | 80 |
|
106 | 81 | def clean_bootconf_on_pxe(mac_address):
|
107 |
| - assert mac_address |
108 |
| - pxe = pxe_address() |
109 |
| - distant_file = f'{PXE_CONFIG_DIR}/{mac_address}/boot.conf' |
110 |
| - try: |
111 |
| - ssh(pxe, ['rm', '-rf', distant_file]) |
112 |
| - except SSHCommandFailed as e: |
113 |
| - raise Exception('ERROR: failed to clean the boot.conf file.' + e) |
| 82 | + pxe.server_remove_bootconf(mac_address) |
114 | 83 |
|
115 | 84 | def get_candidate_ips(mac_address):
|
116 |
| - pxe = pxe_address() |
117 |
| - output = ssh( |
118 |
| - pxe, |
119 |
| - ['arp', '-n', '|', 'grep', mac_address, '|', 'awk', '\'{ print $1 }\''] |
120 |
| - ) |
121 |
| - candidate_ips = output.splitlines() |
122 |
| - return candidate_ips |
| 85 | + return pxe.arp_addresses_for(mac_address) |
123 | 86 |
|
124 | 87 | def is_ip_active(ip):
|
125 | 88 | return not os.system(f"ping -c 3 -W 10 {ip} > /dev/null 2>&1")
|
@@ -191,8 +154,6 @@ def main():
|
191 | 154 |
|
192 | 155 | # *** "fail early" checks
|
193 | 156 |
|
194 |
| - pxe = pxe_address() # raises if not defined |
195 |
| - |
196 | 157 | if not is_uuid(args.vm_uuid):
|
197 | 158 | raise Exception(f'The provided VM UUID is invalid: {args.vm_uuid}')
|
198 | 159 |
|
@@ -237,7 +198,7 @@ def main():
|
237 | 198 | hdd = 'nvme0n1' if vm.is_uefi else 'sda'
|
238 | 199 | generate_answerfile(tmp_local_path, installer, args.host, args.target_hostname, args.action, hdd,
|
239 | 200 | netinstall_gpg_check)
|
240 |
| - generate_boot_conf(tmp_local_path, installer, args.action) |
| 201 | + pxe.generate_boot_conf(tmp_local_path, installer) |
241 | 202 | logging.info('Copy files to the pxe server')
|
242 | 203 | copy_files_to_pxe(mac_address, tmp_local_path)
|
243 | 204 | atexit.register(lambda: clean_files_on_pxe(mac_address))
|
|
0 commit comments