---
- name: Install the systemd units
  import_tasks: systemd_units.yml

- name: Pull container images
  import_tasks: container_images.yml
  when: openshift_is_containerized | bool
- name: Create flexvolume directory when containerized
  file:
    state: directory
    path: "{{ openshift_flexvolume_container_directory_default }}/volume/exec"
    mode: '0750'
  when: openshift_is_containerized | bool

- name: Start and enable openvswitch service
  systemd:
    name: openvswitch.service
    enabled: yes
    state: started
    daemon_reload: yes
  when:
    - openshift_is_containerized | bool
    - openshift_node_use_openshift_sdn | default(true) | bool
  register: ovs_start_result
  until: not (ovs_start_result is failed)
  retries: 3
  delay: 30

- set_fact:
    ovs_service_status_changed: "{{ ovs_start_result is changed }}"

# If this tasks fails, you may have a malformed openshift_node_kubelet_args
# or openshift_node_labels.  Those variables must be dictionaries.
- name: Create kublet args config dir
  file:
    dest: "{{ l2_openshift_node_kubelet_args['config'] }}"
    state: directory
  when: ('config' in l2_openshift_node_kubelet_args) | bool

# TODO: add the validate parameter when there is a validation command to run
- name: Create the Node config
  template:
    dest: "{{ openshift.common.config_base }}/node/node-config.yaml"
    src: node.yaml.v1.j2
    backup: true
    owner: root
    group: root
    mode: 0600
  notify:
    - restart node

- name: Configure Node Environment Variables
  lineinfile:
    dest: /etc/sysconfig/{{ openshift_service_type }}-node
    regexp: "^{{ item.key }}="
    line: "{{ item.key }}={{ item.value }}"
    create: true
  with_dict: "{{ openshift_node_env_vars }}"
  notify:
    - restart node

- name: include aws provider credentials
  import_tasks: aws.yml
  when: not (openshift_node_use_instance_profiles | default(False))

# Necessary because when you're on a node that's also a master the master will be
# restarted after the node restarts docker and it will take up to 60 seconds for
# systemd to start the master again
- when:
    - openshift_is_containerized | bool
    - not openshift_node_bootstrap
  block:
    - name: Wait for master API to become available before proceeding
      # Using curl here since the uri module requires python-httplib2 and
      # wait_for port doesn't provide health information.
      command: >
        curl --silent --tlsv1.2 --max-time 2
        --cacert {{ openshift.common.config_base }}/node/ca.crt
        {{ openshift_node_master_api_url }}/healthz/ready
      args:
        # Disables the following warning:
        # Consider using get_url or uri module rather than running curl
        warn: no
      register: api_available_output
      until: api_available_output.stdout == 'ok'
      retries: 120
      delay: 1
      changed_when: false

    - name: Start and enable node dep
      systemd:
        daemon_reload: yes
        name: "{{ openshift_service_type }}-node-dep"
        enabled: yes
        state: started

- when: not openshift_node_bootstrap
  block:
    - name: Start and enable node
      systemd:
        name: "{{ openshift_service_type }}-node"
        enabled: yes
        state: started
        daemon_reload: yes
      register: node_start_result
      until: not node_start_result is failed
      retries: 1
      delay: 30
      ignore_errors: true

    - name: Dump logs from node service if it failed
      command: journalctl --no-pager -n 100 -u {{ openshift_service_type }}-node
      when: node_start_result is failed

    - name: Abort if node failed to start
      fail:
        msg: Node failed to start please inspect the logs and try again
      when: node_start_result is failed

    - set_fact:
        node_service_status_changed: "{{ node_start_result is changed }}"
