- name: create slave
  sudo: true
  sudo_user: "{{ slave_user }}"
  command: creates={{ slave_dir }} buildslave create-slave {{ slave_dir }} {{ buildmaster }} buildslave passwd

- name: generate slave config
  sudo: true
  sudo_user: "{{ slave_user }}"
  template: src=buildbot.tac.j2 dest={{ slave_dir }}/buildbot.tac mode=0600 owner={{ slave_user }} group={{ slave_user }}

- name: generate slave info
  sudo: true
  sudo_user: "{{ slave_user }}"
  template: src={{ item }}.j2 dest={{ slave_dir }}/info/{{ item }} mode=0644 owner={{ slave_user }} group={{ slave_user }}
  with_items:
    - admin
    - host

- name: create slave ssh directory
  when: buildslave_public_sshkey_file is defined
  file: path={{ slave_home }}/.ssh owner={{ slave_user }} group={{ slave_user }} mode=0700 state=directory

- name: install slave ssh private key
  when: buildslave_private_sshkey_file is defined
  copy: src={{ private }}/files/taskotron/{{ buildslave_private_sshkey_file }} dest={{ slave_home }}/.ssh/id_rsa owner={{ slave_user }} group={{ slave_user }} mode=0600

- name: install slave ssh public key
  when: buildslave_public_sshkey_file is defined
  copy: src={{ private }}/files/taskotron/{{ buildslave_public_sshkey_file }} dest={{ slave_home }}/.ssh/id_rsa.pub owner={{ slave_user }} group={{ slave_user }} mode=0644

- name: make sure master is in known_hosts
  when: buildslave_public_sshkey_file is defined
  lineinfile: dest=/home/{{ slave_user }}/.ssh/known_hosts regexp='{{ buildmaster }}' line='{{ buildmaster }} {{ buildmaster_pubkey }}' create=yes owner={{ slave_user }} group={{ slave_user }}

- name: generate buildslave service file
  template: src=buildslave.service.j2 dest=/lib/systemd/system/buildslave.service owner=root group=root mode=0744

- name: start and enable buildslave service
  service: name=buildslave enabled=yes state=started

