/home/zuul/src/opendev.org/openstack/openstack-ansible-lxc_hosts/tasks/lxc_cache_prestage.yml
---
# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- 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_date_time.epoch }}"

- name: Legacy image url fetch
  block:
    - name: Create legacy image URL fetch
      template:
        src: "lxc-image-fetch-url.py.j2"
        dest: "/usr/local/bin/lxc-image-fetch-url"
        owner: "root"
        group: "root"
        mode: "0755"

    - name: Fetch legacy container image url
      command: "/usr/local/bin/lxc-image-fetch-url {{ lxc_cache_map.distro }} {{ lxc_cache_map.release }} {{ lxc_cache_map.arch }} {{ lxc_cache_default_variant }}"
      register: legacy_image_url
      retries: 3
      delay: 1
      until: legacy_image_url is success

    - name: Set LXC cache fact(s) (legacy)
      set_fact:
        lxc_hosts_container_image_url: "{{ legacy_image_url.stdout.strip() }}"
  when:
    - lxc_hosts_container_image_download_legacy | bool

- name: Set LXC cache basename
  set_fact:
    cache_basename: "{{ lxc_hosts_container_image_url | basename }}"

- name: Create aria2c output script
  template:
    src: aria2c-download-helper.sh.j2
    dest: /usr/local/bin/aria2c-download-helper.sh
    owner: "root"
    group: "root"
    mode: "0755"

- name: Pre-stage the LXC image on the system
  shell: >
    aria2c
    --max-connection-per-server=4
    --allow-overwrite=true
    --dir=/tmp
    --out={{ cache_basename }}
    --check-certificate={{ (lxc_hosts_validate_certs | bool) | lower }}
    --follow-metalink=mem
    --on-download-complete=/usr/local/bin/aria2c-download-helper.sh
    {{ lxc_hosts_container_image_url }}
    > /var/log/aria2c-image-prestage.log 2>&1
  args:
    warn: no
  register: prestage_image
  async: "{{ lxc_cache_prep_timeout | int // 5 }}"
  poll: 0
  tags:
    - skip_ansible_lint