Commit 367858ce authored by thiolliere's avatar thiolliere
Browse files

robustness + image resize fix

robustness:
when ansible failed then run installation abort and return the exitcode

image resize fix:
because when sd card we resized number was not converted from GiB to B
now everything is in B
parent c4215db8
......@@ -3,9 +3,6 @@ import json
# machine must provide write_file and exec_cmd functions
def run(machine, name, timezone, wifi_pwd, kalite, zim_install):
# machine.write_file("/etc/systemd/system.conf", systemd.system_conf)
# machine.write_file("/etc/systemd/user.conf", systemd.user_conf)
machine.exec_cmd("sudo apt-get update")
machine.exec_cmd("sudo apt-get install -y python-pip git python-dev libffi-dev libssl-dev gnutls-bin")
......@@ -64,4 +61,5 @@ def run(machine, name, timezone, wifi_pwd, kalite, zim_install):
ansible_pull_cmd += " --extra-vars \"%s\"" % extra_vars
ansible_pull_cmd += " main.yml"
machine.exec_cmd(ansible_pull_cmd)
code = machine.exec_cmd(ansible_pull_cmd)
return code
......@@ -63,14 +63,15 @@ class Emulator:
pipe_reader.readline()
pipe_reader.readline()
size_line = pipe_reader.readline()
matches = re.findall(r"virtual size: (.*)G", size_line)
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)
return float(matches[0])
return int(matches[0])
def resize_image(self, size):
output = subprocess.check_output([qemu_img_exe, "resize", "-f", "raw", self._image, "{}G".format(size)])
self._logger.raw_std(output.decode("utf-8", "ignore"))
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
def copy_image(self, device_name):
self._logger.step("copy image to sd card")
......@@ -223,8 +224,8 @@ class _RunningInstance:
self._client.close()
self._qemu.wait(timeout)
def exec_cmd(self, command, return_stdout=False):
if return_stdout:
def exec_cmd(self, command, capture_stdout=False):
if capture_stdout:
stdout_buffer = ""
self._logger.std(command)
......@@ -233,20 +234,25 @@ class _RunningInstance:
line = stdout.readline()
if line == "":
break
if return_stdout:
if capture_stdout:
stdout_buffer += line
self._logger.std(line.replace("\n", ""))
for line in stderr.readlines():
self._logger.err("STDERR: " + line.replace("\n", ""))
if return_stdout:
return stdout_buffer
return_code = stdout.channel.recv_exit_status()
if capture_stdout:
return return_code, stdout_buffer
else:
return return_code
def resize_fs(self):
self._logger.step("resize partition")
stdout = self.exec_cmd("sudo LANG=C fdisk -l /dev/mmcblk0", return_stdout=True)
code, stdout = self.exec_cmd("sudo LANG=C fdisk -l /dev/mmcblk0", capture_stdout=True)
lines = stdout.splitlines()
number_of_sector_match = []
......
......@@ -28,7 +28,7 @@ parser.add_argument("-t", "--timezone", help="timezone (Europe/Paris)", default=
parser.add_argument("-w", "--wifi-pwd", help="wifi password (Open)")
parser.add_argument("-k", "--kalite", help="install kalite (fr | en | ar | es)", choices=["fr", "en", "ar", "er"], nargs="*")
parser.add_argument("-z", "--zim-install", help="install zim", nargs="*")
parser.add_argument("-r", "--resize", help="resize image in GiB (5)", type=float, default=5)
parser.add_argument("-r", "--resize", help="resize image in B (5*2**30)", type=float, default=5*2**30)
parser.add_argument("-c", "--catalog", help="print zim catalog", action="store_true")
parser.add_argument("-s", "--sd", help="sd card device to put the image onto")
......
......@@ -241,7 +241,7 @@ class Application:
cancel_event=self.cancel_event,
sd_card=sd_card,
output_file=output_file,
done_callback=lambda : GLib.idle_add(self.installation_done))
done_callback=lambda code: GLib.idle_add(self.installation_done))
self.component.window.destroy()
self.component.run_window.show()
......
......@@ -19,14 +19,16 @@ def run_installation(name, timezone, wifi_pwd, kalite, zim_install, size, logger
if size < emulator.get_image_size():
logger.err("error: cannot decrease image size")
exit(1)
return done_callback(1)
emulator.resize_image(size)
return_code = emulator.resize_image(size)
if return_code != 0:
return done_callback(return_code)
with emulator.run(cancel_event) as emulation:
emulation.resize_fs()
logger.step("run ansiblecube")
ansiblecube.run(
ansible_exit_code = ansiblecube.run(
machine=emulation,
name=name,
timezone=timezone,
......@@ -34,6 +36,9 @@ def run_installation(name, timezone, wifi_pwd, kalite, zim_install, size, logger
kalite=kalite,
zim_install=zim_install)
if ansible_exit_code != 0:
return done_callback(ansible_exit_code)
if sd_card:
emulator.copy_image(sd_card)
......@@ -51,4 +56,4 @@ def run_installation(name, timezone, wifi_pwd, kalite, zim_install, size, logger
logger.step("done")
if done_callback:
done_callback()
return done_callback(0)
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