From 7e75a083d29e0cbf7033c0a8e4c4a6c24f0a08f5 Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Wed, 12 Jul 2023 15:43:20 +0200 Subject: [PATCH] Refactor LXC image expiration Right now we write output of `date -d @{{ timestamp }} to the expiry file, and then attempt to comapre with timestamp. However, output of `date -d` is datetime and not timestamp, so these 2 things can not be properly compared. So image cache was valid forever. Change-Id: I42f5b43f09d3c530813dd7fd334eafce7a5eaf39 (cherry picked from commit a51291f87d44a126f432bafbfd68f6fe3cd1ba1d) --- defaults/main.yml | 4 ++++ .../fix_lxc_cache_expiration-1656b5758c4ec24e.yaml | 10 ++++++++++ tasks/lxc_cache.yml | 4 ++-- tasks/lxc_cache_create.yml | 11 ++--------- 4 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 releasenotes/notes/fix_lxc_cache_expiration-1656b5758c4ec24e.yaml diff --git a/defaults/main.yml b/defaults/main.yml index f49a75b7..96d0bc85 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -202,6 +202,10 @@ lxc_hosts_container_build_command: "{{ _lxc_hosts_container_build_command | defa # Local path to cached image lxc_image_cache_path: "/var/lib/machines/{{ lxc_container_base_name }}" +# Expiration timeout for the cached image +# Should be in format supported by the to_time_unit filter +lxc_image_cache_expiration: "1year" + # Set this option to true to pull a new cached image. lxc_image_cache_refresh: false diff --git a/releasenotes/notes/fix_lxc_cache_expiration-1656b5758c4ec24e.yaml b/releasenotes/notes/fix_lxc_cache_expiration-1656b5758c4ec24e.yaml new file mode 100644 index 00000000..3a580c75 --- /dev/null +++ b/releasenotes/notes/fix_lxc_cache_expiration-1656b5758c4ec24e.yaml @@ -0,0 +1,10 @@ +--- +features: + - | + Implemented variable ``lxc_image_cache_expiration`` that controlls for how + long cached LXC image will be valid. Default value is `1year`. Variable + format should be compatible with community.general.to_time_unit filter. +fixes: + - | + LXC image cache expiration mechanism has being fixed. Previously LXC images were + valid forever. diff --git a/tasks/lxc_cache.yml b/tasks/lxc_cache.yml index 60be2f40..fef2eed6 100644 --- a/tasks/lxc_cache.yml +++ b/tasks/lxc_cache.yml @@ -16,7 +16,7 @@ - name: Set LXC cache fact(s) set_fact: cache_path_fact: "{{ lxc_container_cache_path }}/{{ lxc_cache_map.distro }}/{{ lxc_cache_map.release }}/{{ lxc_cache_map.arch }}/{{ lxc_cache_default_variant }}" - cache_time: "{{ ansible_facts['date_time']['epoch'] }}" + cache_time: "{{ now().timestamp() }}" - name: Retrieve the expiry object slurp: @@ -32,7 +32,7 @@ set_fact: lxc_image_cache_refresh: true when: - cache_time >= (expiry.content|default('MQo=') | b64decode) + cache_time | int >= (expiry.content | default('MQo=') | b64decode | int) tags: - always diff --git a/tasks/lxc_cache_create.yml b/tasks/lxc_cache_create.yml index 8598740d..4748038d 100644 --- a/tasks/lxc_cache_create.yml +++ b/tasks/lxc_cache_create.yml @@ -51,21 +51,14 @@ - expiry - templates -- name: Set cache expiry - shell: "date -d @{{ (cache_time | int) + 31536000 }}" - changed_when: false - register: _cache_expiry - tags: - - skip_ansible_lint - - name: Set expiry copy: - content: "{{ _cache_expiry.stdout }}" + content: "{{ cache_time | int + lxc_image_cache_expiration | community.general.to_seconds | int }}" dest: "{{ cache_path_fact }}/expiry" - name: Set build ID copy: - content: "{{ cache_time }}" + content: "{{ cache_time | int }}" dest: "{{ cache_path_fact }}/build_id" - name: Create base container to use for {{ lxc_container_backing_store }}-backed containers