---
- name: check/create instance
  hosts: localhost
  user: copr
  gather_facts: False

  vars:
  - keypair: buildsys
  - image: ami-0000000e
  - instance_type: m1.builder
  - security_group: builder

  tasks:
  - name: spin it up
    local_action: ec2 keypair=${keypair} image=${image} type=${instance_type} wait=true group=${security_group}
    register: inst_res

  - name: get its internal ip b/c openstack is sometimes stupid
    local_action: shell euca-describe-instances ${inst_res.instances[0].id} | grep INSTANCE | cut -f 18
    register: int_ip

  - name: add it to the special group
    local_action: add_host hostname=${int_ip.stdout} groupname=builder_temp_group

  - name: wait for the host to be hot
    local_action: wait_for host=${int_ip.stdout} port=22 delay=5 timeout=600


- hosts: builder_temp_group
  user: root
  vars:
   - files: files/
  
  tasks:
  - name: edit hostname to be instance name
    action: shell hostname  `curl -s http://169.254.169.254/2009-04-04/meta-data/instance-id`
  
  - name: add repos
    action: copy src=$files/$item dest=/etc/yum.repos.d/$item
    with_items:
    - builder.repo
    - epel6.repo

  - name: install pkgs
    action: yum state=present pkg=$item
    with_items:
    - mock
    - createrepo
    - yum-utils
    - rsync
    - openssh-clients

  - name: make sure newest rpm
    action: yum name=rpm state=latest

  - name: mockbuilder user   
    action: user name=mockbuilder groups=mock

  - name: mockbuilder .ssh
    action: file state=directory path=/home/mockbuilder/.ssh mode=0700 owner=mockbuilder group=mockbuilder

  - name: mockbuilder authorized_keys
    action: authorized_key user=mockbuilder key='$FILE(${files}/buildsys.pub)'

  - name: put updated mock configs into /etc/mock
    action: copy src=$files/mock/$item dest=/etc/mock
    with_items:
    - site-defaults.cfg
    - epel-5-x86_64.cfg
    - epel-5-i386.cfg

  - name: put updated   mockchain into /usr/bin
    action: copy src=$files/mockchain dest=/usr/bin/mockchain mode=0755 owner=root group=root


