Commit e3d66ba1 authored by thiolliere's avatar thiolliere
Browse files

better exception handling

use of exception in qemu.
exception are caught in run_installation and returned
parent a2a79c15
......@@ -61,5 +61,4 @@ def run(machine, name, timezone, wifi_pwd, kalite, zim_install):
ansible_pull_cmd += " --extra-vars \"%s\"" % extra_vars
ansible_pull_cmd += " main.yml"
code = machine.exec_cmd(ansible_pull_cmd)
return code
machine.exec_cmd(ansible_pull_cmd)
......@@ -17,11 +17,9 @@ else:
qemu_system_arm_exe = "qemu-system-arm"
qemu_img_exe = "qemu-img"
class QemuWaitSignalTimeoutError(Exception):
pass
class QemuInternalError(Exception):
pass
class QemuException(Exception):
def __init__(self, msg):
Exception(self, msg)
def generate_random_name():
r = ""
......@@ -65,13 +63,12 @@ class Emulator:
size_line = pipe_reader.readline()
matches = re.findall(r"virtual size: \S*G \((\d*) bytes\)", size_line)
if len(matches) != 1:
raise QemuInternalError("cannot get image %s size from qemu-img info" % self._image)
raise QemuException("cannot get image %s size from qemu-img info" % self._image)
return int(matches[0])
def resize_image(self, size):
completed_process = subprocess.run([qemu_img_exe, "resize", "-f", "raw", self._image, "{}".format(size)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
self._logger.raw_std(completed_process.stdout.decode("utf-8", "ignore"))
return completed_process.returncode
output = subprocess.check_output([qemu_img_exe, "resize", "-f", "raw", self._image, "{}".format(size)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
self._logger.raw_std(output.decode("utf-8", "ignore"))
def copy_image(self, device_name):
self._logger.step("copy image to sd card")
......@@ -172,7 +169,7 @@ class _RunningInstance:
self._logger.raw_std(decoded_buf)
if timeout_event.is_set():
raise QemuWaitSignalTimeoutError("signal %s" % signal)
raise QemuException("wait signal timeout: %s" % signal)
timer.cancel()
ring_buf.extend(buf)
......@@ -241,17 +238,17 @@ class _RunningInstance:
for line in stderr.readlines():
self._logger.err("STDERR: " + line.replace("\n", ""))
return_code = stdout.channel.recv_exit_status()
exit_status = stdout.channel.recv_exit_status();
if exit_status != 0:
raise QemuException("ssh command failed with status {}. cmd: {}".format(exit_status, command))
if capture_stdout:
return return_code, stdout_buffer
else:
return return_code
return stdout_buffer
def resize_fs(self):
self._logger.step("resize partition")
code, stdout = self.exec_cmd("sudo LANG=C fdisk -l /dev/mmcblk0", capture_stdout=True)
stdout = self.exec_cmd("sudo LANG=C fdisk -l /dev/mmcblk0", capture_stdout=True)
lines = stdout.splitlines()
......@@ -262,11 +259,11 @@ class _RunningInstance:
second_partition_match += re.findall(r"^/dev/mmcblk0p2 +(\d+) +(\d+) +\d+ +\S+ +\d+ +Linux$", line)
if len(number_of_sector_match) != 1:
raise QemuInternalError("cannot find the number of sector of disk")
raise QemuException("cannot find the number of sector of disk")
number_of_sector = int(number_of_sector_match[0])
if len(second_partition_match) != 1:
raise QemuInternalError("cannot find start and/or end of root partition of disk")
raise QemuException("cannot find start and/or end of root partition of disk")
second_partition_start = int(second_partition_match[0][0])
second_partition_end = int(second_partition_match[0][1])
......
......@@ -39,7 +39,7 @@ if args.catalog:
print(yaml.dump(catalog, default_flow_style=False, default_style=''))
exit(0)
run_installation(
error = run_installation(
name=args.name,
timezone=args.timezone,
wifi_pwd=args.wifi_pwd,
......@@ -50,3 +50,5 @@ run_installation(
cancel_event=CancelEvent(),
sd_card=args.sd,
output_file=not args.sd)
print("ERRROR: " + str(error))
......@@ -241,7 +241,7 @@ class Application:
cancel_event=self.cancel_event,
sd_card=sd_card,
output_file=output_file,
done_callback=lambda code: GLib.idle_add(self.installation_done, code))
done_callback=lambda error: GLib.idle_add(self.installation_done, error))
self.component.window.destroy()
self.component.run_window.show()
......@@ -295,9 +295,9 @@ class Application:
def done_window_ok_button_clicked(self, widget):
self.component.done_window.close()
def installation_done(self, code):
if code != 0:
self.component.done_label.set_text("Installation failed: " + str(code))
def installation_done(self, error):
if error != None:
self.component.done_label.set_text("Installation failed: " + str(error))
validate_label(self.component.done_label, False)
self.component.done_window.show()
self.component.run_abort_done_button.set_label("Close")
......
......@@ -11,57 +11,54 @@ def run_installation(name, timezone, wifi_pwd, kalite, zim_install, size, logger
os.makedirs(build_dir, exist_ok=True)
os.chdir(build_dir)
def clear_dir():
os.chdir(current_working_dir)
shutil.rmtree(build_dir)
try:
downloader = Downloader(logger)
vexpress_boot_kernel_path, vexpress_boot_dtb_path = downloader.download_vexpress_boot()
raspbian_image_path = downloader.download_raspbian()
downloader = Downloader(logger)
vexpress_boot_kernel_path, vexpress_boot_dtb_path = downloader.download_vexpress_boot()
raspbian_image_path = downloader.download_raspbian()
emulator = qemu.Emulator(vexpress_boot_kernel_path, vexpress_boot_dtb_path, raspbian_image_path, logger)
emulator = qemu.Emulator(vexpress_boot_kernel_path, vexpress_boot_dtb_path, raspbian_image_path, logger)
if size < emulator.get_image_size():
logger.err("cannot decrease image size")
raise ValueError("cannot decrease image size")
if size < emulator.get_image_size():
logger.err("error: cannot decrease image size")
clear_dir()
return done_callback(1)
emulator.resize_image(size)
return_code = emulator.resize_image(size)
if return_code != 0:
clear_dir()
return done_callback(return_code)
with emulator.run(cancel_event) as emulation:
emulation.resize_fs()
logger.step("run ansiblecube")
ansible_exit_code = ansiblecube.run(
machine=emulation,
name=name,
timezone=timezone,
wifi_pwd=wifi_pwd,
kalite=kalite,
zim_install=zim_install)
with emulator.run(cancel_event) as emulation:
emulation.resize_fs()
logger.step("run ansiblecube")
ansible_exit_code = ansiblecube.run(
machine=emulation,
name=name,
timezone=timezone,
wifi_pwd=wifi_pwd,
kalite=kalite,
zim_install=zim_install)
if sd_card:
emulator.copy_image(sd_card)
if ansible_exit_code != 0:
clear_dir()
return done_callback(ansible_exit_code)
if output_file:
filename = "pibox.img"
if os.path.exists(path.os.join(current_working_dir, filename)):
increment = 0
while os.path.exists(path.os.join(current_working_dir, filename)):
increment += 1
filename = "pibox({}).img".format(increment)
if sd_card:
emulator.copy_image(sd_card)
os.rename(raspbian_image_path, path.os.join(current_working_dir, filename))
if output_file:
filename = "pibox.img"
if os.path.exists(path.os.join(current_working_dir, filename)):
increment = 0
while os.path.exists(path.os.join(current_working_dir, filename)):
increment += 1
filename = "pibox({}).img".format(increment)
except Exception as e:
logger.step("failed")
error = e
else:
logger.step("done")
error = None
os.rename(raspbian_image_path, path.os.join(current_working_dir, filename))
os.chdir(current_working_dir)
shutil.rmtree(build_dir)
logger.step("done")
clear_dir()
if done_callback:
return done_callback(0)
return 0
done_callback(error)
return error
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment