/home/zuul/src/opendev.org/openstack/openstack-ansible-repo_server/tasks/repo_pre_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: create the system group
  group:
    name: "{{ repo_service_group_name }}"
    state: "present"
    system: "yes"

- name: Create the nginx system user
  user:
    name: "{{ repo_service_user_name }}"
    group: "{{ repo_service_group_name }}"
    comment: "Nginx repo user"
    shell: "/bin/bash"
    system: "yes"
    createhome: "yes"
    home: "{{ repo_service_home_folder }}"

- name: File and directory setup (non-root user)
  file:
    path: "{{ item.path }}"
    state: "{{ item.state | default('directory') }}"
    owner: "{{ repo_service_user_name }}"
    group: "{{ repo_service_group_name }}"
    mode: "{{ item.mode | default('02755') }}"
  with_items:
    - path: "{{ repo_service_home_folder }}"
    - path: "{{ repo_service_home_folder }}/.ssh"
      mode: "02700"
    - path: "{{ repo_service_home_folder }}/repo"
    - path: "{{ repo_service_home_folder }}/repo/links"
    - path: "{{ repo_service_home_folder }}/repo/os-releases"
    - path: "{{ repo_service_home_folder }}/repo/os-releases/{{ openstack_release }}"
    - path: "{{ repo_service_home_folder }}/repo/pools"
    - path: "{{ repo_service_home_folder }}/repo/venvs"
    - path: "/var/lib/nginx"
    - path: "/var/log/nginx"
      mode: "0775"

- name: Drop repo pre/post command script
  template:
    src: repo_prepost_cmd.sh.j2
    dest: "{{ repo_service_home_folder }}/repo/repo_prepost_cmd.sh"
    mode: "0750"
    owner: "root"
    group: "root"

# TODO(evrardjp):
# The functionality provided by the following two tasks is
# deprecated in Queens and should be removed in Rocky. Apply
# the changes as done in https://review.openstack.org/521750
- name: Check if the git folder exists already
  stat:
    path: "{{ repo_git_cache_dir }}"
  register: _git_folder

- name: Git service data folder setup
  file:
    path: "{{ (_git_folder.stat.exists and _git_folder.stat.islnk) | ternary(_git_folder.stat.lnk_source, repo_git_cache_dir) }}"
    state: "directory"
    owner: "{{ repo_service_user_name }}"
    group: "{{ repo_service_group_name }}"
    recurse: true

- name: File and directory setup (root user)
  file:
    path: "{{ item.path }}"
    state: "{{ item.state | default('directory') }}"
    owner: "root"
    group: "root"
    mode: "0755"
  with_items:
    - path: "/etc/lsyncd"
    - path: "/var/log/lsyncd"
    - path: "/etc/nginx/sites-enabled/default"
      state: "absent"
    - path: "/etc/nginx/conf.d"
    - path: "/etc/nginx/sites-available"
    - path: "/etc/nginx/sites-enabled"