---
# Generic EC2 VM setup
#
# Ran from config-ec2-prepare-db+web-vm.yml
#
# Copyright (c) 2013 Hubert Feyrer <hubert@feyrer.de>
#

    - name: ${security_group} | Launch new EC2 instance
      local_action: ec2
         keypair=${keypair}
         group=${security_group}
         instance_type=${instance_type}
         image=${image}
         wait=true
      register: ec2

    - name: ${security_group} | Give the system 30 seconds to boot up
      local_action: command sleep 30

    - name: ${security_group} | Get rid of SSH "Are you sure you want to continue connecting (yes/no)?" query
      local_action: command sh -c 'ssh-keyscan -t rsa ${ec2.instances[0].public_ip} >>$HOME/.ssh/known_hosts'

    - name: ${security_group} | Fix /usr/bootstrap.sh to run pkgin with -y
      local_action: command ssh root@${ec2.instances[0].public_ip} '${item.cmd}'
      with_items:
       - { cmd: "install /usr/bootstrap.sh /usr/bootstrap.sh.orig" }
       - { cmd: "chmod +w /usr/bootstrap.sh" }
       - { cmd: "sed \"s,bin/pkgin update,bin/pkgin -y update,\" </usr/bootstrap.sh.orig >/usr/bootstrap.sh" }
       - { cmd: "chmod -w /usr/bootstrap.sh" }

    - name: ${security_group} | Install pkgin via /usr/bootstrap.sh
      local_action: command ssh root@${ec2.instances[0].public_ip} '${item.cmd}'
      with_items:
       - { cmd: "env PATH=/usr/sbin:${PATH} /usr/bootstrap.sh binpkg" }

    - name: ${security_group} | Copy over Ansible binary package
      local_action: command scp files/ansible-1.0.tgz root@${ec2.instances[0].public_ip}:/tmp

    - name: ${security_group} | Install Ansible dependencies
      local_action: command ssh root@${ec2.instances[0].public_ip} '/usr/sbin/pkg_info -qn /tmp/ansible-1.0.tgz | sed -e "s,\[[^]]*\],," -e "s,[->][^->]*\$,," | xargs -n 1 echo /usr/pkg/bin/pkgin -y install | sh -vx'

    - name: ${security_group} | Install Ansible package (manually)
      local_action: command ssh root@${ec2.instances[0].public_ip} '/usr/sbin/pkg_add /tmp/ansible-1.0.tgz'

    - name: ${security_group} | Setup lame /usr/bin/python symlink
      local_action: command ssh root@${ec2.instances[0].public_ip} 'ln -s /usr/pkg/bin/python2.7   /usr/bin/python'
