Skip to content

Commit e83bc73

Browse files
committed
WIP move generic pxe-boot code to new pxe.py
improvements: - PXE_CONFIG_SERVER is checked only once at parse time - don't over-handle exceptions FIXME: - test - find a proper way to get "restore" not to block reboot (disabled the workaround to understand the problem)
1 parent ec659ba commit e83bc73

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

lib/pxe.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from lib.commands import ssh, scp, SSHCommandFailed
2+
3+
PXE_CONFIG_DIR = "/pxe/configs/custom"
4+
5+
try:
6+
from data import PXE_CONFIG_SERVER
7+
assert PXE_CONFIG_SERVER
8+
except ImportError:
9+
raise Exception('No address for the PXE server found in data.py (`PXE_CONFIG_SERVER`)')
10+
11+
def generate_boot_conf(directory, installer):
12+
if True:
13+
rt = ""
14+
else:
15+
# (because of the restore case), we disable the text ui from
16+
# the installer completely, to workaround a bug that leaves us
17+
# stuck on a confirmation dialog at the end of the operation.
18+
rt = 'rt=1'
19+
with open(f'{directory}/boot.conf', 'w') as bootfile:
20+
bootfile.write(f"""
21+
answerfile=custom
22+
installer={installer}
23+
is_default=1
24+
{rt}
25+
""")
26+
27+
def server_push_config(mac_address, tmp_local_path):
28+
remote_dir = f'{PXE_CONFIG_DIR}/{mac_address}/'
29+
clean_files_on_pxe(mac_address)
30+
ssh(PXE_CONFIG_SERVER, ['mkdir', '-p', remote_dir])
31+
scp(PXE_CONFIG_SERVER, f'{tmp_local_path}/boot.conf', remote_dir)
32+
scp(PXE_CONFIG_SERVER, f'{tmp_local_path}/answerfile.xml', remote_dir)
33+
34+
def server_remove_config(mac_address):
35+
assert mac_address # protection against deleting the whole parent dir!
36+
remote_dir = f'{PXE_CONFIG_DIR}/{mac_address}/'
37+
ssh(PXE_CONFIG_SERVER, ['rm', '-rf', remote_dir])
38+
39+
def server_remove_bootconf(mac_address):
40+
assert mac_address
41+
distant_file = f'{PXE_CONFIG_DIR}/{mac_address}/boot.conf'
42+
ssh(PXE_CONFIG_SERVER, ['rm', '-rf', distant_file])
43+
44+
def arp_addresses_for(mac_address):
45+
output = ssh(
46+
PXE_CONFIG_SERVER,
47+
['arp', '-n', '|', 'grep', mac_address, '|', 'awk', '\'{ print $1 }\'']
48+
)
49+
candidate_ips = output.splitlines()
50+
return candidate_ips

scripts/install_xcpng.py

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from packaging import version
1616

1717
sys.path.append(f"{os.path.abspath(os.path.dirname(__file__))}/..") # noqa
18+
from lib import pxe
1819
from lib.commands import ssh, scp, SSHCommandFailed
1920
from lib.common import wait_for, is_uuid
2021
from lib.host import host_data
@@ -23,25 +24,8 @@
2324

2425
logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO)
2526

26-
PXE_CONFIG_DIR = "/pxe/configs/custom"
27-
2827
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
4529

4630
def generate_answerfile(directory, installer, hostname_or_ip, target_hostname, action, hdd, netinstall_gpg_check):
4731
pxe = pxe_address()
@@ -89,37 +73,16 @@ def generate_answerfile(directory, installer, hostname_or_ip, target_hostname, a
8973
raise Exception(f"Unknown action: `{action}`")
9074

9175
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)
9977

10078
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)
10580

10681
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)
11483

11584
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)
12386

12487
def is_ip_active(ip):
12588
return not os.system(f"ping -c 3 -W 10 {ip} > /dev/null 2>&1")
@@ -191,8 +154,6 @@ def main():
191154

192155
# *** "fail early" checks
193156

194-
pxe = pxe_address() # raises if not defined
195-
196157
if not is_uuid(args.vm_uuid):
197158
raise Exception(f'The provided VM UUID is invalid: {args.vm_uuid}')
198159

@@ -237,7 +198,7 @@ def main():
237198
hdd = 'nvme0n1' if vm.is_uefi else 'sda'
238199
generate_answerfile(tmp_local_path, installer, args.host, args.target_hostname, args.action, hdd,
239200
netinstall_gpg_check)
240-
generate_boot_conf(tmp_local_path, installer, args.action)
201+
pxe.generate_boot_conf(tmp_local_path, installer)
241202
logging.info('Copy files to the pxe server')
242203
copy_files_to_pxe(mac_address, tmp_local_path)
243204
atexit.register(lambda: clean_files_on_pxe(mac_address))

0 commit comments

Comments
 (0)