---
- name: Docker | Download latest docker
  win_shell: |
    $ErrorActionPreference = "Stop"
    Find-PackageProvider -Name "Nuget" | Install-PackageProvider -Force
    Install-Module -Name "DockerMsftProvider" -Repository "PSGallery" -Force
    Install-Package -Name "Docker" -ProviderName "DockerMsftProvider" -Force -RequiredVersion "{{ docker_version }}"

# Remove the existing default Docker Windows service because we'll recreate it
# later on using the Windows service wrapper.This will allow us to capture
# the Docker logs to a file instead of using the event logs (the default
# logging behavior for Docker).
- name: Docker | Remove existing Docker Windows service
  win_service:
    name: Docker
    state: absent

- name: Docker | Create the service wrapper config file
  win_lineinfile:
    path: '{{ docker_info.install_dir }}\dockerd-servicewrapper-config.ini'
    create: yes
    line: |-
      service-name=Docker
      service-command={{ docker_info.install_dir }}/dockerd.exe
      log-file={{ docker_info.install_dir }}/dockerd.log

- name: Docker | Create the Docker Windows service using the service wrapper
  win_service:
    name: Docker
    display_name: "Docker Windows Agent"
    path: >-
      "{{ install_path }}\servicewrapper.exe" --config "{{ docker_info.install_dir }}\dockerd-servicewrapper-config.ini"
    state: stopped
    start_mode: disabled

- name: Docker | Create Docker config directory
  win_file:
    path: "{{ docker_info.config_dir }}"
    state: directory

- name: Docker | Disable docker default network
  win_lineinfile:
    path: "{{ docker_info.config_dir }}/daemon.json"
    create: yes
    line: '{ "bridge" : "none" }'
    newline: unix

- name: Docker | Remove current default nat network
  win_shell: |
    Get-HNSNetwork | Where-Object { $_.Name -eq "nat" } | Remove-HNSNetwork

- name: Docker | Set Docker service failure command
  win_shell: >-
    sc.exe failure Docker reset=40 actions=restart/0/restart/0/run/30000
    command="powershell.exe Move-Item
    \\\`"{{ docker_info.install_dir }}/dockerd.log\\\`"
    \\\`"{{ docker_info.install_dir }}/dockerd.log_`$(Get-Date -f yyyy-MM-dd-hh-mm-ss)\\\`";
    Restart-Service Docker"

- name: Docker | Enable Docker service failure flags
  win_shell: sc.exe failureflag Docker 1

- name: Docker | Get the Dockerd version
  win_command: >
    "{{ docker_info.install_dir }}\dockerd.exe" --version
  register: dockerd_version

# On Windows, we need to explicitly set the DOCKER_API_VERSION environment variable
# to avoid failures when spawning Docker tasks.
# More info here: https://github.com/Azure/acs-engine/issues/4118
- name: Docker | Set the DOCKER_API_VERSION system environment variable
  block:
    - name: Docker | Check if Docker 17.06 is found
      set_fact:
        docker_api_version: >
          "1.30"
      when: dockerd_version.stdout.startswith("Docker version 17.06")

    - name: Docker | Check if Docker 18.03 is found
      set_fact:
        docker_api_version: >
          "1.37"
      when: dockerd_version.stdout.startswith("Docker version 18.03")

    - name: Docker | Check if other Docker version is found
      set_fact:
        docker_api_version: >
          $null
      when: (not dockerd_version.stdout.startswith("Docker version 17.06") and
             not dockerd_version.stdout.startswith("Docker version 18.03"))

    - name: Set or clear the DOCKER_API_VERSION system environment variable
      win_shell: >
        [System.Environment]::SetEnvironmentVariable('DOCKER_API_VERSION', {{ docker_api_version }}, [System.EnvironmentVariableTarget]::Machine)
  when: dockerd_version.stdout != ""

- name: Docker | Start the Docker service
  win_service:
    name: Docker
    start_mode: auto
    state: started
