---
# tasks file for bootstrap
- name: wait for the host
  wait_for:
    port: "{{ ansible_port | default('22') }}"
    host: "{{ (ansible_ssh_host | default(ansible_host) | default(inventory_hostname)) }}"
  connection: local
  become: no
  when:
    - ansible_connection is defined
    - ansible_connection != "docker"
    - bootstrap_wait_for_host

- name: test connection
  wait_for_connection:
    timeout: "{{ bootstrap_timeout }}"
  register: bootstrap_connect
  changed_when: no
  ignore_errors: yes

- name: register temporary remote user
  set_fact:
    bootstrap_remote_user: "{{ (ansible_user | default(omit, true))
                            if bootstrap_connect is succeeded else bootstrap_user }}"
  changed_when: no

- name: find package manager
  remote_user: "{{ bootstrap_remote_user | default(omit) }}"
  become: no
  check_mode: no
  block:

    - name: register apk
      raw: apk --version
      register: apk
      changed_when: no
      failed_when: no

    - name: register apt_get
      raw: apt-get --version
      register: apt_get
      changed_when: no
      failed_when: no

    - name: register dnf
      raw: dnf --version
      register: dnf
      changed_when: no
      failed_when: no

    - name: register emerge
      raw: emerge --version
      register: emerge
      changed_when: no
      failed_when: no

    - name: register pacman
      raw: pacman --version
      register: pacman
      changed_when: no
      failed_when: no

    - name: register pkg
      raw: pkg --version
      register: pkg
      changed_when: no
      failed_when: no

    - name: register pkg_add
      raw: pkg_info
      register: pkg_add
      changed_when: no
      failed_when: no

    - name: register yum
      raw: yum --version
      register: yum
      changed_when: no
      failed_when: no

    - name: register zypper
      raw: zypper --version
      register: zypper
      changed_when: no
      failed_when: no

- name: install using found package manager
  remote_user: "{{ bootstrap_remote_user | default(omit) }}"
  block:

    - name: install software with apk
      raw: "apk update ; apk add {{ bootstrap_apk_packages }}"
      when:
        - apk.rc == 0
      register: apkresult
      changed_when:
        - "'Installing' in apkresult.stdout"
      until: apkresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with apt-get
      raw: "apt-get update ; apt-get -y install {{ bootstrap_apt_packages }}"
      when:
        - apt_get.rc == 0
        - zypper.rc != 0
      register: apt_getresult
      changed_when:
        - "' 0 newly installed' not in apt_getresult.stdout"
      until: apt_getresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with dnf
      raw: "dnf -y install {{ bootstrap_dnf_packages }}"
      when:
        - dnf.rc == 0
      register: dnfresult
      changed_when:
        - "'Nothing' not in dnfresult.stdout"
      until: dnfresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with emerge
      raw: "{{ bootstrap_emerge_command }}"
      when:
        - emerge.rc == 0
      register: emergeresult
      changed_when:
        - "'changed' in emergeresult.stdout"
      until: emergeresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with pacman
      raw: "pacman -Sy ; pacman -T {{ bootstrap_pacman_packages }} || pacman -S --noconfirm {{ bootstrap_pacman_packages }}"
      when:
        - pacman.rc == 0
      register: pacmanresult
      changed_when:
        - "' installing python' in pacmanresult.stdout"
      until: pacmanresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with pkg
      raw: >
        ASSUME_ALWAYS_YES=YES
        pkg install {{ bootstrap_pkg_packages }}
        ; ln -s /usr/local/bin/python /usr/bin/python
        || exit 0
      when:
        - pkg.rc == 0
      register: pkgresult
      changed_when:
        - "'Extracting python' in pkgresult.stdout"
      until: pkgresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with pkg_add
      raw: pkg_add -Iz {{ bootstrap_pkg_add_packages }}
      when:
        - pkg_add.rc == 0
      register: pkg_add_getresult
      changed_when:
        - "': ok' in pkg_add_getresult.stdout"
      until: pkg_add_getresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with yum
      raw: "yum -y install {{ bootstrap_yum_packages }}"
      when:
        - yum.rc == 0
      register: yumresult
      changed_when:
        - "'Nothing' not in yumresult.stdout"
      until: yumresult is succeeded
      retries: "{{ bootstrap_retries }}"

    - name: install software with zypper
      raw: "zypper -n install {{ bootstrap_zypper_packages }}"
      when:
        - zypper.rc == 0
      register: zypperresult
      changed_when:
        - "'Nothing' not in zypperresult.stdout"
      failed_when: no
      until: zypperresult is succeeded
      retries: "{{ bootstrap_retries }}"

- name: gather facts
  setup:
  become: no

- name: add community repository for alpine
  lineinfile:
    path: /etc/apk/repositories
    line: "{{ bootstrap_alpine_community_repo }}"
  when:
    - ansible_distribution == "Alpine"

- name: install bootstrap packages
  package:
    name: "{{ bootstrap_packages }}"
    state: "{{ bootstrap_package_state }}"
  register: packageresult
  until: packageresult is succeeded

- name: install software to support stable modules
  package:
    name: "{{ bootstrap_stable_packages }}"
    state: "{{ bootstrap_package_state }}"
  when:
    - bootstrap_stable_packages is defined
  register: packagestableresult
  until: packagestableresult is succeeded
  notify:
    - gather facts

- name: install software to support preview modules
  package:
    name: "{{ bootstrap_preview_packages }}"
    state: "{{ bootstrap_package_state }}"
  when:
    - bootstrap_preview
    - bootstrap_preview_packages is defined
  register: packagepreviewresult
  until: packagepreviewresult is succeeded
  notify:
    - gather facts

- name: flush handlers
  meta: flush_handlers
