- name: Install {{appname}}
  include_tasks: tasks/install_without_docker.yml
  vars:
     app: "mingdao"

- name: Create a directory if it does not exist
  file:
    path: /data/apps/mingdao/installer
    state: directory
    recurse: yes
    
- name: Create MingDao link directory
  file:
    path: /usr/local/MDPrivateDeployment
    state: directory
    recurse: yes
    
- name: Create a file as the identity file and set env
  shell: |
    mkdir /data/apps/mingdao/src
    touch  /data/apps/mingdao/installer/first
    ln -sf /data/apps/mingdao/installer /usr/local/MDPrivateDeployment
    echo "vm.max_map_count = 262144" >> /etc/sysctl.conf
    sed -i "s/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g" /etc/sysctl.conf
    sysctl -p
    
- name: Pull mingdao docker image & change tag for automatic installation
  shell: |
    docker pull mdpublic/mingdaoyun-community
    docker pull mdpublic/mingdaoyun-doc
    docker tag mdpublic/mingdaoyun-community:latest registry.cn-hangzhou.aliyuncs.com/mdpublic/mingdaoyun-community:{{mingdao_version}}
    docker rmi mdpublic/mingdaoyun-community:latest
    docker tag mdpublic/mingdaoyun-doc:latest registry.cn-hangzhou.aliyuncs.com/mdpublic/mingdaoyun-doc:{{mingdao_doc_version}}
    docker rmi mdpublic/mingdaoyun-doc:latest
    
- name: Download mingdao installation tool from {{mingdao_download_url}}
  shell: |
    wget -N {{mingdao_download_url}} 
    tar -xzvf  mingdaoyun_private_deployment_captain_linux_amd64.tar.gz
    rm -f mingdaoyun_private_deployment_captain_linux_amd64.tar.gz
  args:
    chdir: /data/apps/mingdao/installer
    
- name: Copy mingdao service 
  copy:
    src: mingdao.service
    dest: /lib/systemd/system/mingdao.service

- name: Restart and Enable mingdao service
  systemd:
    state: restarted
    daemon_reload: yes
    name: mingdao
    enabled: yes

- name: Copy after_up.sh to src  
  copy:
    src: after_up.sh
    dest: /data/apps/mingdao/src
     
# display version 
- name: Check mingdao version
  shell: > 
    sudo echo "mingdao version:" $(docker images |grep mingdaoyun-community |awk '{print $2}') |tee -a /data/logs/install_version.txt
- name: Check mingdao doc version
  shell: >
    sudo echo "mingdao doc version:" $(docker images |grep mingdaoyun-doc |awk '{print $2}') |tee -a /data/logs/install_version.txt
