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 a51291f87d)
This commit is contained in:
Dmitriy Rabotyagov 2023-07-12 15:43:20 +02:00 committed by Dmitriy Rabotyagov
parent 5b209f9db1
commit 7e75a083d2
4 changed files with 18 additions and 11 deletions

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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