# 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.
---
- name: Put templated sources.list in place
  template:
    src: etc/apt/sources.list.j2
    dest: /etc/apt/sources.list
    mode: 0644
  # OCI Base images have the required apt sources list embedded inside the image, adding the sources list
  # from this repo leads to build failures(especially in Arm), hence ignoring the step.
  when: packer_builder_type != "oracle-oci"

- name: Put templated apt.conf.d/90proxy in place when defined
  template:
    src: etc/apt/apt.conf.d/90proxy
    dest: /etc/apt/apt.conf.d/90proxy
    mode: 0644
  when: http_proxy is defined or https_proxy is defined

- name: Ensure cloud-final is in a running state
  service:
    name: cloud-final
    state: started
  check_mode: yes
  register: cloudfinalstatus
  until: cloudfinalstatus.status.ActiveState == "active"
  retries: 5
  delay: 10
  when: packer_builder_type == "oracle-oci" and extra_repos != ""

- name: Find existing repo files
  find:
    depth: 1
    paths:
      - /etc/apt
      - /etc/apt/sources.list.d
    patterns: '*.list'
  register: repo_files
  when: disable_public_repos|bool

- name: Disable repos
  command: "mv {{ item.path }} {{ item.path }}.disabled"
  loop: "{{ repo_files.files }}"
  when: disable_public_repos|bool

- name: Install extra repos
  copy:
    src: "{{ item }}"
    dest: "/etc/apt/sources.list.d/{{ item | basename }}"
    mode: 0644
  loop: "{{ extra_repos.split() }}"
  when: extra_repos != ""

- name: perform a dist-upgrade
  apt:
    force_apt_get: True
    update_cache: True
    upgrade: dist
  register: apt_lock_status
  until: apt_lock_status is not failed
  retries: 5
  delay: 10

- name: install baseline dependencies
  apt:
    force_apt_get: True
    update_cache: True
    name: "{{ debs }}"
    state: latest
  register: apt_lock_status
  until: apt_lock_status is not failed
  retries: 5
  delay: 10
  
- name: install extra debs
  apt:
    force_apt_get: True
    name: "{{ extra_debs.split() }}"
    state: latest
  register: apt_lock_status
  until: apt_lock_status is not failed
  retries: 5
  delay: 10
  
- name: install pinned debs
  apt:
    force_apt_get: True
    name: "{{ pinned_debs }}"
    state: present
    force: yes
  register: apt_lock_status
  until: apt_lock_status is not failed
  retries: 5
  delay: 10