diff --git a/ansible/playbooks/roles/image_registry/defaults/main.yml b/ansible/playbooks/roles/image_registry/defaults/main.yml new file mode 100644 index 0000000000..ecb1a8903c --- /dev/null +++ b/ansible/playbooks/roles/image_registry/defaults/main.yml @@ -0,0 +1,4 @@ +--- +epiphany_registry: + container_name: epiphany-registry + volume_name: epiphany-registry diff --git a/ansible/playbooks/roles/image_registry/tasks/main.yml b/ansible/playbooks/roles/image_registry/tasks/main.yml index 28a11672db..ef16833b7b 100644 --- a/ansible/playbooks/roles/image_registry/tasks/main.yml +++ b/ansible/playbooks/roles/image_registry/tasks/main.yml @@ -17,10 +17,10 @@ - name: Install and configure Docker import_role: name: docker - when: - - check_docker.rc != 0 + when: check_docker.rc != 0 - name: Reconfigure Docker if necessary + when: check_docker.rc == 0 block: - name: Include get-registries.yml from docker role # this sets result include_role: @@ -33,37 +33,65 @@ tasks_from: configure-docker when: - not image_registry_address in result.stdout - when: - - check_docker.rc == 0 - - name: Check if previous version registry is running and stop it if needed - become: true + - name: Migrate registry volume to named one + when: is_upgrade_run block: - - name: Get current running registry image name - command: "{% raw %}docker ps --format '{{ .Image }}' -f 'name=registry' -f 'status=running'{% endraw %}" - register: running_registry_image + - name: Get image name of registry container + command: >- + docker ps --all --format {% raw %}'{{ .Image }}'{% endraw %} -f name={{ epiphany_registry.container_name }} + changed_when: false + register: container_image - - name: Get current running registry container names - command: "{% raw %}docker ps --format '{{ .Names }}' -f 'name=registry' -f 'status=running'{% endraw %}" - register: running_registry_containers + - name: Perform migration when: - - running_registry_image.stdout - - specification.registry_image.name != running_registry_image.stdout - - - set_fact: - container_name: "{{ running_registry_containers.stdout }}" - when: running_registry_containers.changed - - - block: - - name: Stop previous version running registry container - command: docker stop '{{ container_name }}' - - - name: Kill previous version running registry container - command: docker rm '{{ container_name }}' - when: - - container_name is defined - - container_name | length > 0 - when: is_upgrade_run + - container_image.stdout | count > 0 # container exists + - container_image.stdout != specification.registry_image.name + block: + - name: Inspect old version registry container + command: docker inspect {{ epiphany_registry.container_name }} + changed_when: false + register: inspect_registry_container + + - name: Parse information on registry container + set_fact: + container_facts: "{{ inspect_registry_container.stdout | from_json | first }}" + + - name: Check if named volume exists + command: docker volume ls -f name={{ epiphany_registry.volume_name }} --quiet + changed_when: false + register: volume_ls + + - name: Create named volume if not exists + command: docker volume create {{ epiphany_registry.volume_name }} + when: volume_ls.stdout | count == 0 + + - name: Set facts on old version registry volume + set_fact: + old_volume: "{{ container_facts.Mounts | selectattr('Destination', '==' ,'/var/lib/registry') + | first }}" + + - name: Stop old version registry container + command: docker stop {{ epiphany_registry.container_name }} + when: container_facts.State.Status == 'running' + + # There is no 'docker volume rename' command + - name: Move data from old volume + shell: mv {{ old_volume.Source }}/* {{ _new_volume_path }} + args: + removes: "{{ old_volume.Source }}" + vars: + _new_volume_path: >- + {{ old_volume.Source | replace('/' + old_volume.Name + '/', + '/' + epiphany_registry.volume_name + '/') }} + + - name: Remove old version registry container + command: docker rm {{ epiphany_registry.container_name }} + + # Has to be run after container is removed + - name: Remove old version volume + command: docker volume rm {{ old_volume.Name }} + when: old_volume.Name != epiphany_registry.volume_name - name: Load registry image include_tasks: load-image.yml @@ -71,18 +99,28 @@ docker_image: "{{ specification.registry_image }}" - name: Check if registry is running - become: true - command: docker ps -q -f 'ancestor={{ specification.registry_image.name }}' -f 'status=running' - register: current_registry_up_check + command: docker ps -f name={{ epiphany_registry.container_name }} -f status=running --quiet + register: registry_up_check changed_when: false - # todo run registry with SSL - generate/copy certs, mount it to registry container - name: Run registry - become: true - command: >- - docker run -d -e REGISTRY_HTTP_ADDR=0.0.0.0:5000 -p 5000:5000 --restart=always - --name epiphany-registry {{ specification.registry_image.name }} - when: current_registry_up_check.stdout | length == 0 + when: registry_up_check.stdout | count == 0 # not running + block: + - name: Check if named volume exists + command: docker volume ls -f name={{ epiphany_registry.volume_name }} --quiet + changed_when: false + register: volume_ls + + - name: Create named volume if not exists + command: docker volume create {{ epiphany_registry.volume_name }} + when: volume_ls.stdout | count == 0 + + # todo run registry with SSL - generate/copy certs, mount it to registry container + - name: Run registry + command: >- + docker run -d -e REGISTRY_HTTP_ADDR=0.0.0.0:5000 -p 5000:5000 --restart=always + --name {{ epiphany_registry.container_name }} -v {{ epiphany_registry.volume_name }}:/var/lib/registry + {{ specification.registry_image.name }} - name: Set images to load set_fact: