# Copyright 2018 The Kubernetes Authors.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---
- include_tasks: aws.yml
  when: packer_builder_type.startswith('amazon')

- include_tasks: azure.yml
  when: packer_builder_type.startswith('azure')

- include_tasks: outscale.yml
  when: packer_builder_type.startswith('outscale')

- include_tasks: vmware.yml
  when: packer_builder_type is search('vmware') or
    packer_builder_type is search('vsphere')

- include_tasks: googlecompute.yml
  when: packer_builder_type.startswith('googlecompute')

- include_tasks: oci.yml
  when: packer_builder_type.startswith('oracle-oci')

- include_tasks: qemu.yml
  when: packer_builder_type is search('qemu') and
    build_target is not search('raw')

- include_tasks: cloudstack.yml
  when: packer_builder_type is search('qemu') and
    provider is defined and
    provider is search('cloudstack')

- include_tasks: raw.yml
  when: packer_builder_type is search('qemu') and
    build_target is search('raw')

- include_tasks: nutanix.yml
  when: packer_builder_type is search('nutanix')

# Create a boot order configuration
# b/w containerd and cloud final, cloud config services

- name: Creates unit file directory for cloud-final
  file:
    path: /etc/systemd/system/cloud-final.service.d
    state: directory

- name: Create cloud-final boot order drop in file
  copy:
    dest: /etc/systemd/system/cloud-final.service.d/boot-order.conf
    src: etc/systemd/system/cloud-final.service.d/boot-order.conf
    owner: root
    group: root
    mode: "0755"

- name: Creates unit file directory for cloud-config
  file:
    path: /etc/systemd/system/cloud-config.service.d
    state: directory

- name: Create cloud-final boot order drop in file
  copy:
    dest: /etc/systemd/system/cloud-config.service.d/boot-order.conf
    src: etc/systemd/system/cloud-config.service.d/boot-order.conf
    owner: root
    group: root
    mode: "0755"

# Some OS might disable cloud-final service on boot (rhel 7).
# Enable all cloud-init services on boot.
- name: Make sure all cloud init services are enabled
  service:
    name: "{{ item }}"
    enabled: yes
  with_items:
    - cloud-final
    - cloud-config
    - cloud-init
    - cloud-init-local
  when: ansible_os_family != "Flatcar"

- name: Create cloud-init config file
  copy:
    src: files/etc/cloud/cloud.cfg.d/05_logging.cfg
    dest: /etc/cloud/cloud.cfg.d/05_logging.cfg
    owner: root
    group: root
    mode: 0644
  when: ansible_os_family != "Flatcar"

- name: set cloudinit feature flags
  copy:
    src: usr/lib/python3/dist-packages/cloudinit/feature_overrides.py
    dest: /usr/lib/python3/dist-packages/cloudinit/feature_overrides.py
    owner: root
    group: root
    mode: 0644
  when: ansible_os_family == "Debian"

- name: Ensure chrony is running
  systemd:
    enabled: yes
    state: started
    daemon_reload: yes
    name: chronyd
  when: (packer_builder_type.startswith('amazon') or
    packer_builder_type.startswith('azure') or
    packer_builder_type is search('vmware') or
    packer_builder_type is search('vsphere')) and
    ansible_os_family != "Flatcar"