- name: Install sbuild
  apt: package=sbuild state=present
- name: Install lintian-brush dependencies
  apt:
    package:
      - python3-ruamel.yaml
      - python3-iniparse
      - python3-toml
      - python3-docutils
      - python3-markdown
      - python3-lxml
      - python3-semver
      - devscripts
      - quilt
      - gnome-pkg-tools
      - schroot
      - python3-bs4
    state: present
- stat:
    path: "{{ janitor_chroot_base }}/{{ item['chroot'] }}"
  register: sbuild
  with_items: "{{ janitor_distributions }}"
- name: Create schroot
  command: "{{ janitor_code_path }}/create-sbuild-chroot.py --user={{ janitor_user }} {% if 'include' in item.item %} --include={% for include in item.item['include'] %}{{ include }}{{ ',' if not loop.last }}{% endfor %}{% endif %} {{ item.item['name'] }}"
  when: not item.stat.exists
  with_items: "{{ sbuild.results }}"
- name: Update schroot
  command: sbuild-update -udcar "{{ item.item['chroot'] }}"
  with_items: "{{ sbuild.results }}"
  when: item.stat.exists
- name: Install requirement-parser
  pip:
    name: requirements-parser
- name: Update schroot from cron job
  cron:
    name: "update schroot for {{ item['name'] }}"
    special_time: daily
    job: sbuild-update -udcar "{{ item['chroot'] }}"
  with_items: "{{ janitor_distributions }}"
- name: janitor in sbuild group
  user:
    name: '{{ janitor_user }}'
    groups: sbuild
    append: yes
- name: sbuild configuration
  file:
    state: link
    src: "{{ janitor_code_path }}/sbuildrc"
    dest: "{{ janitor_home }}/.sbuildrc"
- name: Runner systemd installed
  template:
    src: worker@.service
    dest: /etc/systemd/system/janitor-worker@.service
- name: Enable systemd service file
  systemd:
    name: janitor-worker@{{ item }}.service
    state: started
    enabled: yes
    daemon_reload: yes
  loop: "{{ range(janitor_runner_concurrency) }}"
- name: Credentials file
  template:
    src: credentials.json
    dest: "{{ janitor_home }}/credentials.json"
    owner: "{{ janitor_user }}"
    mode: '0600'
  vars:
    username: "{{ inventory_hostname }}"
    password: "{{ lookup('password', 'worker-credentials.txt') }}"
