---
version: 2.1

commands:
  dry_run:
    parameters:
      version:
        type: string
      python:
        type: string
    steps:
      - run: ansible-playbook -i ./ci_test/inventory/ci.ini "./ci_test/install_agent_<<parameters.version>>.yaml" -e 'ansible_python_interpreter=/usr/bin/<<parameters.python>>' --check
      - run: ansible-playbook -i ./ci_test/inventory/ci.ini "./ci_test/downgrade_to_5.yaml" -e 'ansible_python_interpreter=/usr/bin/<<parameters.python>>' --check

  install_agent_5:
    parameters:
      python:
        type: string
    steps:
      - run: ansible-playbook -i ./ci_test/inventory/ci.ini "./ci_test/install_agent_5.yaml" -e 'ansible_python_interpreter=/usr/bin/<<parameters.python>>'
      - run: dd-agent info || true
      - run: ps aux | grep -v grep | grep datadog-agent

  install_agent:
    parameters:
      version:
        type: string
      python:
        type: string
    steps:
      - run: ansible-playbook -i ./ci_test/inventory/ci.ini "./ci_test/install_agent_<<parameters.version>>.yaml" -e 'ansible_python_interpreter=/usr/bin/<<parameters.python>>'
      - run: datadog-agent version

  test_install_no_manage_config:
    parameters:
      version:
        type: string
      python:
        type: string
    steps:
      - run: ansible-playbook -i ./ci_test/inventory/ci.ini "./ci_test/install_agent_<<parameters.version>>.yaml" -e '{"ansible_python_interpreter":"/usr/bin/<<parameters.python>>","datadog_manage_config":false}'
      - run: bash -c '[ -f /etc/datadog-agent/datadog.yaml.example ] || [ -f /etc/dd-agent/datadog.conf.example ]'
      - run: bash -c '[ ! -f /etc/datadog-agent/datadog.yaml ] && [ ! -f /etc/datadog-agent/system-probe.yaml ] && [ ! -f /etc/datadog-agent/security-agent.yaml ] && [ ! -f /etc/dd-agent/datadog.conf ]'

  downgrade_agent_5_23_0:
    parameters:
      python:
        type: string
    steps:
      - run: ansible-playbook -i ./ci_test/inventory/ci.ini "./ci_test/downgrade_to_5.yaml" -e 'ansible_python_interpreter=/usr/bin/<<parameters.python>>'
      - run: dd-agent info || true
      - run: ps aux | grep -v grep | grep datadog-agent

  test_agent_install_downgrade:
    parameters:
      version:
        type: string
      python:
        type: string
    steps:
      - checkout
      - dry_run:
          version: "<<parameters.version>>"
          python: "<<parameters.python>>"
      - install_agent_5:
          python: "<<parameters.python>>"
      - when:
          condition:
            not:
              equal: [<<parameters.version>>, "5"]
          steps:
            - install_agent:
                version: "<<parameters.version>>"
                python: "<<parameters.python>>"
      - downgrade_agent_5_23_0:
          python: "<<parameters.python>>"

  test_agent_install:
    parameters:
      version:
        type: string
      python:
        type: string
    steps:
      - checkout
      - install_agent:
          version: "<<parameters.version>>"
          python: "<<parameters.python>>"

jobs:
  ansible_lint:
    docker:
      - image: datadog/docker-library:ansible_debian_2_10
    steps:
      - checkout
      - run: pip install ansible-lint
      - run: ansible-lint -v .

  test_install_downgrade:
    parameters:
      ansible_version:
        type: string
      agent_version:
        type: string
      os:
        type: string
      python:
        type: string
    docker:
      - image: datadog/docker-library:ansible_<<parameters.os>>_<<parameters.ansible_version>>
    steps:
      - checkout
      - test_agent_install_downgrade:
          version: "<<parameters.agent_version>>"
          python: "<<parameters.python>>"

  test_install:
    parameters:
      ansible_version:
        type: string
      agent_version:
        type: string
      os:
        type: string
      python:
        type: string
    docker:
      - image: datadog/docker-library:ansible_<<parameters.os>>_<<parameters.ansible_version>>
    steps:
      - checkout
      - test_agent_install:
          version: "<<parameters.agent_version>>"
          python: "<<parameters.python>>"

  test_install_no_manage_config:
    parameters:
      ansible_version:
        type: string
      agent_version:
        type: string
      os:
        type: string
      python:
        type: string
    docker:
      - image: datadog/docker-library:ansible_<<parameters.os>>_<<parameters.ansible_version>>
    steps:
      - checkout
      - test_install_no_manage_config:
          version: "<<parameters.agent_version>>"
          python: "<<parameters.python>>"

workflows:
  version: 2
  test_datadog_role:
    jobs:
      - ansible_lint
      - test_install_downgrade:
          matrix:
            parameters:
              ansible_version: ["2_6", "2_7", "2_8"]
              agent_version: ["5", "6", "7"]
              os: ["debian"]
              python: ["python2", "python3"]

      # Newer debian images only have Pythpn 3 installed
      - test_install_downgrade:
          matrix:
            parameters:
              ansible_version: ["2_9", "2_10"]
              agent_version: ["5", "6", "7"]
              os: ["debian"]
              python: ["python3"]

      # Newer debian images only have Pythpn 3 installed
      - test_install_no_manage_config:
          matrix:
            parameters:
              ansible_version: ["2_10"]
              agent_version: ["5", "7"]
              os: ["debian"]
              python: ["python3"]

      # centos = CentOS 7. CentOS <= 7 + Python3 is not supported,
      # as the yum module is Python2-only.
      - test_install_downgrade: 
          matrix:
            parameters:
              ansible_version: ["2_6", "2_7", "2_8", "2_9", "2_10"]
              agent_version: ["5", "6", "7"]
              os: ["centos"]
              python: ["python2"]

      # We want to check that the dnf path works with CentOS 8
      # Newer CentOS images only have Pythpn 3 installed
      - test_install:
          matrix:
            parameters:
              ansible_version: ["2_8", "2_9", "2_10"]
              agent_version: ["6", "7"]
              os: ["centos8"]
              python: ["python3"]

      # Newer suse images only have Python 3 installed
      - test_install:
          matrix:
            parameters:
              ansible_version: ["2_8", "2_9", "2_10"]
              agent_version: ["6", "7"]
              os: ["suse"]
              python: ["python3"]
