#!/usr/bin/env ansible-playbook
---
#==============================================================#
# Init a local software repo
# all necessary rpm packages are downloaded to accelerate
# cluster initialization, or in case that target machine does
# not have internet access.
#
# RPM packages are downloaded to {{ repo_home }}/{{ repo_name }}.
# And it will skip download if all packages already exists
#
# Local repo url: {{ repo_endpoint }}/{{ repo_name }}/
#==============================================================#
# you can invoke repo tasks with infra.yml or deploy.yml
#
# infra.yml -t repo_url_pkg   # redownload weburl packages
# infra.yml -t repo_boot_pkg  # install bootstrap packages
# infra.yml -t repo_rpm_pkg   # download repo rpm packages
# infra.yml -t repo_create    # create local repo
# build without check: ./infra.yml -t repo_build -e repo_url_packages=[] -e repo_packages=[]
#
# infra.yml -t repo          # init repo
# infra.yml -t repo_prepare  # use existing repo if exists
# infra.yml -t repo_build    # (re)build local repo
# infra.yml -t repo_nginx    # setup temp nginx server for repo
# infra.yml -t repo_create   # recreate repo
# infra.yml -t repo_upstream # add upstream repo file
# infra.yml -t repo_build   -e repo_url_packages=[]    # build & skip url download
# infra.yml -t repo_build   -e repo_packages=[]        # build & skip rpm download
# infra.yml -t repo_build   -e repo_packages=[] -e repo_url_packages=[]  # skip download
# infra.yml -t repo_pkg     -e repo_packages=[xxxx]


#--------------------------------------------------------------#
# 1. Check whether repo exists?                     [repo_check]
#--------------------------------------------------------------#
# check flag file {{ repo_home }}/{{ repo_name }}/repo_complete existence
- name: check local repo exists
  tags: repo_check
  block:
    - name: check local repo exists
      stat: path={{ repo_home }}/{{ repo_name }}/repo_complete
      register: repo_cache
    - name: set repo_exists flag
      set_fact: { repo_exists: "{{ repo_cache.stat.exists }}" }


#--------------------------------------------------------------#
# 2. Use local repo (IF REPO EXISTS)              [repo_prepare]
#--------------------------------------------------------------#
# if local apt/yum cache exists, use it directly, done
- name: setup local repo if exists
  tags: repo_prepare
  when: repo_exists|bool and repo_enabled|bool
  shell: |
    {% if os_package == 'rpm' %}

    mkdir -p /etc/yum.repos.d/backup/
    {% if repo_remove|bool %}
    mv -f /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ || /bin/true
    yum clean all;
    {% endif %}
    cat > /etc/yum.repos.d/{{ repo_name }}-local.repo <<-'EOF'
    [{{ repo_name }}-local]
    name={{ repo_name }}-local $releasever - $basearch
    baseurl=file://{{ repo_home }}/{{ repo_name }}/
    enabled=1
    gpgcheck=0
    {% if os_version|int >= 8 %}
    module_hotfixes=1
    {% endif %}
    EOF
    yum makecache;
    yum install -y wget nginx sshpass createrepo_c;
    yum install -y yum-utils || /bin/true
    /bin/true;
    
    {% elif os_package == 'deb' %}
    
    mkdir -p /etc/apt/backup
    {% if repo_remove|bool %}
    mv -f /etc/apt/sources.list.d/* /etc/apt/backup/ 2> /dev/null || /bin/true
    mv -f /etc/apt/sources.list     /etc/apt/backup/ 2> /dev/null || /bin/true
    {% endif %}
    
    echo "deb [trusted=yes] file:{{ repo_home }}/{{ repo_name }}/ ./" > /etc/apt/sources.list.d/pigsty-local.list
    apt update
    apt install -y wget nginx sshpass dpkg-dev || /bin/true
    /bin/true
    {% endif %}
  args: { executable: /bin/bash }


#--------------------------------------------------------------#
# 3. Build repo (IF REPO NOT EXISTS)                [repo_build]
#--------------------------------------------------------------#
# if local repo cache not exists, download them and use it, done
- import_tasks: build.yml
  when: (repo_enabled|bool and not repo_exists|bool) or (repo_build|default(false)|bool)
  tags: repo_build
  ignore_errors: true


#--------------------------------------------------------------#
# 4. Repo Nginx                                     [repo_nginx]
#--------------------------------------------------------------#
# repo nginx will not be created if a nginx server is already running on admin node
# and this temp nginx will be overwritten by role `nginx`.
- import_tasks: nginx.yml
  when: repo_enabled|bool
  tags: repo_nginx


...