- name: check/create instance for jenkins-master
  hosts: 209.132.184.153
  user: root
  gather_facts: False
  tags:
   - jenkins_master

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - ${private}/vars.yml

  tasks:
  - include: $tasks/persistent_cloud.yml
  - include: $tasks/growroot_cloud.yml

- name: provision master
  hosts: 209.132.184.153
  user: root
  gather_facts: True
  tags:
   - jenkins_master

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - ${private}/vars.yml
   - ${vars}/${ansible_distribution}.yml

  tasks:
  - include: $tasks/cloud_setup_basic.yml
  - include: $tasks/postfix_basic.yml

  - name: make the jenkins path
    action: file state=directory path=/var/lib/jenkins
  
  - name: mount our persistent space
    action: mount name=/var/lib/jenkins src='LABEL=jenkins' fstype=ext4 state=mounted

  - name: poke firewall holes
    action: command lokkit $item
    with_items:
    - --service=ssh
    - --service=https
    - --service=http

  - name: install pkgs for jenkins
    action: yum state=installed pkg=$item
    with_items:
    - vim
    - dejavu-s\*
    - fontconfig
    - java-1.6.0-openjdk
    - httpd
    - openssh-clients
    tags:
    - packages

  - name: add jenkins proxy config file for apache
    action: copy src=$files/jenkins/master/jenkins-apache.conf dest=/etc/httpd/conf.d/jenkins-apache.conf owner=root group=root mode=0644
    notify:
    - restart httpd
    tags:
    - config
 
  - name: enable apache
    action: service name=httpd state=running enabled=true
    
  - name: add jenkins upstream repo 
    action: copy src=$files/jenkins/master/jenkins.repo dest=/etc/yum.repos.d/jenkins.repo  owner=root group=root
    tags:
     - config

  - name: import jenkins upstream gpg key
    action: copy src=$files/jenkins/master/jenkins-ci.org.key dest=/etc/pki/rpm-gpg/RPM-GPG-KEY-jenkins-ci.org owner=root group=root
    tags:
     - config

  - name: install pkgs for jenkins
    action: yum state=installed pkg=$item
    with_items:
    - jenkins
    tags:
    - packages

  - name: set the hostname to jenkins-osversion
    action: command hostname jenkins-master-${dist_tag}
    tags:
    - config

  - name: mkdir dir for jenkins data
    action: file state=directory path=/var/lib/jenkins/plugins/ owner=jenkins group=jenkins

  - name: import jenkins plugins
    action: copy src=$item owner=jenkins group=jenkins dest=/var/lib/jenkins/plugins/
    with_fileglob: $files/jenkins/master/plugins/*.hpi
    tags:
     - config

  - name: import jenkins configuration files
    action: copy src=$item owner=jenkins group=jenkins dest=/var/lib/jenkins/
    with_fileglob: $files/jenkins/master/*.xml
    tags:
     - config

  - name: add jenkins ssh priv key so it can connect to clients
    action: copy src=$private/files/jenkins/ssh/jenkins_master dest=/var/tmp/jenkins_master_id_rsa mode=600 owner=jenkins group=jenkins
    tags:
    - config

  - name: start jenkins itself
    action: service name=jenkins state=running

  
  handlers:
  - include: $handlers/restart_services.yml

###################################################
# jenkins slaves

- name: check/create instance for jenkins-slaves
  hosts: jenkins-slaves
  user: root
  gather_facts: False
  tags:
   - jenkins_workers

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - ${private}/vars.yml

  tasks:
  - include: $tasks/persistent_cloud.yml
  - include: $tasks/growroot_cloud.yml

- name: provision workers
  hosts: jenkins-slaves
  user: root
  gather_facts: True
  tags:
   - jenkins_workers

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - ${private}/vars.yml
   - ${vars}/${ansible_distribution}.yml

  tasks:
  - include: $tasks/cloud_setup_basic.yml

  - include: $tasks/cloud_setup_basic.yml
  - name: install pkgs for jenkins
    action: yum state=installed pkg=$item
    with_items:
    - vim
    - java-1.7.0-openjdk
    - subversion
    - bzr
    - git
    - rpmlint
    - rpmdevtools
    - mercurial
    - mock
    - gcc
    - gcc-c++
    - python-bugzilla
    - python-straight-plugin
    - python-pip
    - python-virtualenv 
    - python-coverage
    - pylint
    - python-argparse
    - python-nose
    - python-BeautifulSoup
    - python-fedora
    - python-unittest2
    - python-pep8
    - docbook-style-xsl  # Required by gimp-help-2
    - make               # Required by gimp-help-2
    - automake           # Required by gimp-help-2
    - libcurl-devel      # Required by blockerbugs
    tags:
    - packages
  
  - name: install pkgs for jenkins for fedora systems
    action: yum state=installed pkg=$item
    only_if: '$is_fedora'
    with_items:
    - python3
    - python-nose-cover3
    - python3-nose-cover3
    tags:
    - packages
  
  - name: set the hostname to jenkins-osversion
    action: command hostname jenkins-${dist_tag}
    tags:
    - config
  
  - name: setup jenkins_slave user
    action: user name=jenkins_slave state=present createhome=yes system=no
    tags:
    - jenkinsuser  

  - name: setup jenkins_slave ssh key
    action: authorized_key user=jenkins_slave key='$FILE($private/files/jenkins/ssh/jenkins_master.pub)'

  - name: jenkins_slave to mock group
    action: user name=jenkins_slave groups=mock
  
  - name: add .gitconfig for jenkins_slave user
    action: copy src=$files/jenkins/gitconfig dest=/home/jenkins_slave/.gitconfig owner=jenkins_slave group=jenkins_slave mode=664
    tags:
     - config
  
  - name: template sshd_config
    action: copy src=$item dest=/etc/ssh/sshd_config mode=0600 owner=root group=root
    first_available_file:
      - $files/jenkins/sshd_config_slave.$ansible_distribution
      - $files/jenkins/sshd_config_slave
    notify:
    - restart sshd
    tags:
     - config
  
  - name: mkdir dir for jenkins data
    action: file state=directory path=/mnt/jenkins owner=jenkins_slave group=jenkins_slave

  
  handlers:
  - include: $handlers/restart_services.yml

