/home/zuul/src/opendev.org/openstack/openstack-ansible-os_glance/tasks/glance_install.yml
---
# Copyright 2014, 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: Record the installation method
  ini_file:
    dest: "/etc/ansible/facts.d/openstack_ansible.fact"
    section: "glance"
    option: "install_method"
    value: "{{ glance_install_method }}"

- name: Refresh local facts to ensure the glance section is present
  setup:
    filter: ansible_local
    gather_subset: "!all"

- name: create the system group
  group:
    name: "{{ glance_system_group_name }}"
    gid: "{{ glance_system_group_gid | default(omit) }}"
    state: "present"
    system: "yes"

- name: Create the glance system user
  user:
    name: "{{ glance_system_user_name }}"
    uid: "{{ glance_system_user_uid | default(omit) }}"
    group: "{{ glance_system_group_name }}"
    comment: "{{ glance_system_comment }}"
    shell: "{{ glance_system_shell }}"
    system: "yes"
    createhome: "yes"
    home: "{{ glance_system_user_home }}"

- name: Create glance NFS mount point(s)
  file:
    path: "{{ item.local_path }}"
    state: directory
    mode: "0755"
    owner: "{{ item.owner | default(glance_system_user_name) }}"
    group: "{{ item.group | default(glance_system_group_name) }}"
  with_items: "{{ glance_nfs_client }}"

# NOTE(cloudnull): During an upgrade the local directory may exist on a source
#                  install. If the directory does exist it will need to be
#                  removed. This is required on source installs because the
#                  config directory is a link.
- name: Source config block
  block:
    - name: Stat config directory
      stat:
        path: "{{ glance_etc_dir }}"
      register: glance_conf_dir_stat

    - name: Remove the config directory
      file:
        path: "{{ glance_etc_dir }}"
        state: absent
      when:
        - glance_conf_dir_stat.stat.isdir is defined and
          glance_conf_dir_stat.stat.isdir
  when:
    - glance_install_method == 'source'

- name: Create glance directories
  file:
    path: "{{ item.path | default(omit) }}"
    src: "{{ item.src | default(omit) }}"
    dest: "{{ item.dest | default(omit) }}"
    state: "{{ item.state | default('directory') }}"
    owner: "{{ item.owner|default(glance_system_user_name) }}"
    group: "{{ item.group|default(glance_system_group_name) }}"
    mode: "{{ item.mode | default(omit) }}"
    force: "{{ item.force | default(omit) }}"
  when:
    - (item.condition | default(true)) | bool
    - (item.dest | default(item.path)) not in glance_mount_points
  with_items:
    - path: "/openstack"
      mode: "0755"
      owner: "root"
      group: "root"
    - path: "{{ (glance_install_method == 'distro') | ternary(glance_etc_dir, (glance_bin | dirname) + glance_etc_dir) }}"
      mode: "0755"
    # NOTE(cloudnull): The "src" path is relative. This ensures all files remain
    #                  within the host/container confines when connecting to
    #                  them using the connection plugin or the root filesystem.
    - dest: "{{ glance_etc_dir }}"
      src: "{{ glance_bin | dirname | regex_replace('^/', '../') }}/etc/glance"
      state: link
      force: true
      condition: "{{ glance_install_method == 'source' }}"
    - path: "{{ glance_etc_dir }}/rootwrap.d"
      owner: "root"
      group: "root"
    - path: "/var/cache/glance"
    - path: "{{ glance_system_user_home }}"
    - path: "{{ glance_system_user_home }}/cache"
    - path: "{{ glance_system_user_home }}/cache/api"
      mode: "0700"
    - path: "{{ glance_system_user_home }}/cache/registry"
    - path: "{{ glance_system_user_home }}/scrubber"
    - path: "{{ glance_system_user_home }}/{{ glance_nfs_local_directory }}"
      mode: "0755"

- name: Install distro packages
  package:
    name: "{{ glance_package_list }}"
    state: "{{ glance_package_state }}"
    update_cache: "{{ (ansible_pkg_mgr in ['apt', 'zypper']) | ternary('yes', omit) }}"
    cache_valid_time: "{{ (ansible_pkg_mgr == 'apt') | ternary(cache_timeout, omit) }}"
  register: install_packages
  until: install_packages is success
  retries: 5
  delay: 2

- name: Install the python venv
  import_role:
    name: "python_venv_build"
  vars:
    venv_python_executable: "{{ glance_venv_python_executable }}"
    venv_build_constraints: "{{ glance_git_constraints }}"
    venv_build_distro_package_list: "{{ glance_devel_distro_packages }}"
    venv_install_destination_path: "{{ glance_bin | dirname }}"
    venv_install_distro_package_list: "{{ glance_distro_packages }}"
    venv_pip_install_args: "{{ glance_pip_install_args }}"
    venv_pip_packages: >-
      {{ glance_pip_packages |
           union(glance_user_pip_packages) |
             union(((glance_oslomsg_amqp1_enabled | bool) | ternary(glance_optional_oslomsg_amqp1_pip_packages, []))) }}
    venv_facts_when_changed:
      - section: "glance"
        option: "need_service_restart"
        value: True
      - section: "glance"
        option: "venv_tag"
        value: "{{ glance_venv_tag }}"
  when: glance_install_method == 'source'

- name: Record the need for a service restart
  ini_file:
    dest: "/etc/ansible/facts.d/openstack_ansible.fact"
    section: "glance"
    option: "need_service_restart"
    value: true
  when: (install_packages is changed) or
        ('need_service_restart' not in ansible_local['openstack_ansible']['glance'])

- name: Run the systemd service role
  include_role:
    name: systemd_service
  vars:
    systemd_user_name: "{{ glance_system_user_name }}"
    systemd_group_name: "{{ glance_system_group_name }}"
    systemd_tempd_prefix: openstack
    systemd_slice_name: glance
    systemd_lock_path: /var/lock/glance
    systemd_CPUAccounting: true
    systemd_BlockIOAccounting: true
    systemd_MemoryAccounting: true
    systemd_TasksAccounting: true
    systemd_services:
      - service_name: "{{ service_var.service_name }}"
        enabled: yes
        state: started
        execstarts: "{{ service_var.execstarts }}"
        execreloads: "{{ service_var.execreloads | default([]) }}"
        config_overrides: "{{ service_var.init_config_overrides }}"
  with_items: "{{ filtered_glance_services }}"
  loop_control:
    loop_var: service_var
  tags:
    - glance-config
    - systemd-service