---
- hosts: dkr
  vars:
    # CGRateS vars
    cgrates_dir: "/home/{{ user }}/go/src/github.com/cgrates/cgrates"
    cgrates_branch: "master"
    cgrates_distribution: "nightly"
    docker_tag: "latest"
    registry_site: "dkr.cgrates.org"      # the web site
    registry_name: "registry"             # the name of the registry container
    users_filename: ""                    # the file containing the htpasswd users
    registry_port: 5000

    dependencies:
      - build-essential
      - git
      - devscripts
      - apt-transport-https
      - ca-certificates
      - gnupg2
      - software-properties-common
      - nginx
      - python3
      - python3-pip
      - virtualenv
      - python3-setuptools
      - apache2-utils

    build_execs:
      - cgr-engine
      - cgr-console
      - cgr-loader
      - cgr-migrator
      - cgr-tester

    rootUser : root
    
  remote_user: '{{ user }}'
  tasks:
    - name: set cgrates distribution
      set_fact:
        cgrates_distribution: "{{ cgrates_branch }}"
      when: cgrates_branch != "master" or cgrates_branch != "1.0"
###########################################################################################################################
###########################################################################################################################
# install dependencies
    - name: Install dependencies
      become: yes
      apt: name={{ dependencies }} state=present
###########################################################################################################################
###########################################################################################################################
# Prepare for configuration
    - name: Check if NGINX needs to be configured
      become: true
      shell: "ls /etc/nginx/sites-enabled | grep '{{ registry_site }}.vhost'"
      ignore_errors: true
      register: nginxConfig

    - name: Configure NGINX server
      include_tasks: nginx.yaml
      when: nginxConfig.stdout_lines|length < 1 or users_filename|length > 0

    - name: Enable and start nginx
      systemd:
        name: nginx
        state: started
        masked: no
        enabled: yes
      
    - name: Configure docker 
      import_tasks: docker.yaml

###########################################################################################################################
###########################################################################################################################
# Install Go
    - name: Install Go
      import_role:
        name: ../roles/go

###########################################################################################################################
###########################################################################################################################
# Install CGRateS
    - name: create cgrates directory
      become: yes
      file:
        state: absent
        dest: '{{ cgrates_dir }}'
      become_user: "{{ user }}"

    - name: create cgrates directory
      become: yes
      file:
        state: directory
        mode: 'u=rwx,go=rx'
        owner: "{{ user }}"
        group: "{{ user }}"
        dest: '{{ cgrates_dir }}'
      become_user: "{{ user }}"

    - name: git clone cgrates
      git:
        repo: https://github.com/cgrates/cgrates.git
        dest: '{{ cgrates_dir }}'
        update: yes
        force: yes
        version: "{{ cgrates_branch }}"
      become: yes
      become_user: "{{ user }}"

    - name: clean go cache
      become: yes
      shell: "go clean --cache"
      environment:
        PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}"

    - name: clean go modcache
      become: yes
      shell: "go clean --modcache"
      environment:
        PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}"

    - name:  build cgrates for docker
      shell: "sh {{ cgrates_dir }}/data/docker/scratch/build.sh"
      environment:
        PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}"
      args:
        chdir: '{{ cgrates_dir }}/data/docker/scratch'

    - name: clean go cache after build
      become: yes
      shell: "go clean --cache"
      environment:
        PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}"

    - name: clean go modcache after build
      become: yes
      shell: "go clean --modcache"
      environment:
        PATH: "{{ lookup('env','PATH') }}:/home/{{ user }}/go/bin:/usr/local/go/bin:{{ ansible_env.PATH }}"

    - name: copy certificate authority
      copy:
        remote_src: yes
        src: "/etc/ssl/certs/ca-certificates.crt"
        dest: "{{ cgrates_dir }}/data/docker/scratch/ca-certificates.crt"

    - name: get git tag
      shell: "git tag -l --points-at HEAD"
      args:
        chdir: '{{ cgrates_dir }}'
      register: gitTagVar

    - name: get commit
      shell: git log -n1 --format=format:%h
      args:
        chdir: '{{ cgrates_dir }}'
      register: gitCommit 
      when: cgrates_branch == "master" or cgrates_branch == "1.0" or gitTagVar.stdout_lines|length == 0
   
    - name: get commit
      shell: date +%Y%m%d%H%M%S --date="@$(git log -n1 --format=format:%ct)"
      args:
        chdir: '{{ cgrates_dir }}'
      register: gitDate 
      when: cgrates_branch == "master" or cgrates_branch == "1.0" or gitTagVar.stdout_lines|length == 0

    - name: set cgrates docker_tag
      set_fact:
        docker_tag: "{{ gitDate.stdout }}{{ gitCommit.stdout }}"
      when: cgrates_branch == "master" or cgrates_branch == "1.0" or gitTagVar.stdout_lines|length == 0

    - name: build Docker image
      become: yes
      shell: 
        docker build -t 127.0.0.1:{{ registry_port }}/{{ cgrates_distribution }}/{{ item }}:{{ docker_tag }} -f="{{ item }}.doker" {{ cgrates_dir }}/data/docker/scratch/;
      args:
        chdir: '{{ cgrates_dir }}/data/docker/scratch'
      with_items: "{{ build_execs }}"

    - name: push docker image to repo
      become: yes
      shell: 
        docker image push 127.0.0.1:{{ registry_port }}/{{ cgrates_distribution }}/{{ item }}:{{ docker_tag }};
      with_items: "{{ build_execs }}"

    - name: tag docker master image
      become: yes
      shell: 
        docker tag 127.0.0.1:{{ registry_port }}/{{ cgrates_distribution }}/{{ item }}:{{ docker_tag }} 127.0.0.1:{{ registry_port }}/{{ cgrates_distribution }}/{{ item }}:latest;
      when: cgrates_branch == "master" or cgrates_branch == "1.0" 
      with_items: "{{ build_execs }}"

    - name: push docker master image to repo
      become: yes
      shell: 
        docker image push 127.0.0.1:{{ registry_port }}/{{ cgrates_distribution }}/{{ item }}:latest;
      when: cgrates_branch == "master" or cgrates_branch == "1.0" 
      with_items: "{{ build_execs }}"

    - name: clean binar files
      file:
        path: "{{ cgrates_dir }}/data/docker/scratch/{{ item }}"
        state: absent
      with_items: "{{ build_execs }}"

    - name: remove local docker images
      become: yes
      shell: 
        docker image prune -af

    - name:  docker_tag is
      debug:
        msg: "{{ docker_tag }}"
      when: docker_tag != ""