From 2416d8739870a45ddb9ac671d6c7f30bbcf0638d Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sun, 27 Sep 2020 22:25:09 +0530 Subject: [PATCH] container: Handle edge cases with container update Fix update command failing when image files do not exist. Signed-off-by: Joseph Nuthalapati Reviewed-by: Veiko Aasa --- container | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/container b/container index b82276da2..72d565845 100755 --- a/container +++ b/container @@ -286,11 +286,11 @@ def _get_systemd_nspawn_version(): def _download_file(url, target_file, force=False): """Download a file from remote URL.""" - if target_file.exists() and not force: - return - - if force: - os.remove(target_file) + if target_file.exists(): + if force: + os.remove(target_file) + else: + return partial_file = target_file.with_suffix(target_file.suffix + '.partial') @@ -805,8 +805,12 @@ def _get_latest_image_timestamp(distribution): def _is_update_required(distribution): """Compare local image timestamp against the latest image timestamp.""" - filename = URLS[distribution].split('/')[-1] - local_image_timestamp = os.path.getmtime(work_directory / filename) + file_path = work_directory / URLS[distribution].split('/')[-1] + + if not file_path.exists(): + return True + + local_image_timestamp = os.path.getmtime(file_path) one_day = datetime.timedelta(days=1).total_seconds() latest_image_timestamp = _get_latest_image_timestamp(distribution) return latest_image_timestamp - local_image_timestamp > one_day