ambakshi
10/11/2015 - 6:48 AM

Amazon Linux cloud-init script

Amazon Linux cloud-init script

#!/bin/bash
#
# Amazon Linux cloud-init script
#
# Amit Bakshi
# 10/2014
#
if [ `id -u` -ne 0 ]; then
   sudo exec /bin/bash -x "$0" "$@"
fi
yum update -y

## Fix ssh keys
yum install -y curl jq
curl -sSL https://github.com/ambakshi.keys | grep -ow 'ssh-[rd]sa .*$' | tee -a /home/ec2-user/.ssh/authorized_keys
chown ec2-user:ec2-user /home/ec2-user/.ssh/authorized_keys

## Fix awscli
yum install -y python-pip
pip install -U pip
pip install -U awscli

INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
AZ=$(curl -sSL http://169.254.169.254/latest/meta-data/placement/availability-zone)
export AWS_DEFAULT_REGION=${AZ%%?}

get_tag () {
   aws ec2 describe-tags --filters Name=resource-type,Values=instance Name=resource-id,Values=$INSTANCE_ID Name=key,Values=$1 --output text | awk '{print $(NF)}'
}

aws ec2 describe-instances --instance-id ${INSTANCE_ID} > /root/${INSTANCE_ID}.json
ln -sfn ${INSTANCE_ID}.json /root/this.json
NAME=$(get_tag Name)
LOCAL_IPV4=$(jq -r '.Reservations[].Instances[].NetworkInterfaces[].PrivateIpAddresses[]|[.PrivateIpAddress][0]' < /root/this.json)
if [ "$NAME" != "" ]; then
   hostname ${NAME}.hpc.local
   echo "${LOCAL_IPV4}  ${NAME}.hpc.local ${NAME}" > /etc/hosts
   echo "127.0.0.1   localhost localhost.localdomain" >> /etc/hosts
   sed -i -e 's/^HOSTNAME=.*$/'${NAME}'.hpc.local/g' /etc/sysconfig/network
   service network restart
   service rsyslog restart
fi

yum install -y epel-release
yum localinstall -y https://yum.puppetlabs.com/puppetlabs-release-pc1-el-6.noarch.rpm
yum clean all
yum update -y
yum install -y puppet-agent
export PATH=/opt/puppetlabs/bin:$PATH
puppet  module install --target-dir /opt/puppetlabs/puppet/modules puppetlabs-stdlib
puppet  module install --target-dir /opt/puppetlabs/puppet/modules puppetlabs-inifile
puppet  module install --target-dir /opt/puppetlabs/puppet/modules saz-ssh

ROLE=$(get-tag Role)
if [ $? -eq 0 ] && [ -n "$ROLE" ]; then
    puppet apply --debug -t -e "node default { include $ROLE }"
else
   cat > /tmp/puppet.pp<<'EOF'
node default {
  File_line {
    ensure  => present,
    path    => '/etc/ssh/sshd_config',
    replace => true,
    notify  => Service['openssh'],
    require => Package['openssh'],
  }

  file_line { 'PermitRootLogin':
    line    => 'PermitRootLogin yes',
    match   => 'PermitRootLogin forced-commands-only',
  }
  file_line { 'Port':
    line    => 'Port 30000',
    match   => 'Port 22',
  }
  
  package { 'openssh':
    name   => 'openssh-server',
    ensure => latest,
  } ~>
  service { 'openssh':
    name   => 'sshd',
    ensure => 'running',
    enable => true,
  }
}
EOF
   puppet apply --debug -t /tmp/puppet.pp
fi


## Configure docker
set -o pipefail
curl -sSL https://get.docker.io | bash && service docker start && chkconfig docker on && gpasswd --add ec2-user docker

for tool in p4 p4p p4d; do
   curl -sSL http://cdist2.perforce.com/perforce/r15.2/bin.linux26x86_64/${tool} > /usr/local/bin/${tool}.tmp && \
   mv /usr/local/bin/${tool}.tmp /usr/local/bin/${tool} && \
   chmod +x /usr/local/bin/${tool}
done

## ZFS
yum install -y kernel-devel zlib-devel libuuid-devel libblkid-devel libselinux-devel parted lsscsi wget
yum localinstall -y --nogpgcheck http://archive.zfsonlinux.org/epel/zfs-release.el6.noarch.rpm
you clean all
yum update -y
yum install -y zfs

## Configure ec2-user
yum install -y ctags tmux vim-enhanced
yum groupinstall -y 'Development tools'
yum localinstall -y http://www.caliban.org/files/redhat/RPMS/noarch/bash-completion-20060301-1.noarch.rpm
curl -o /usr/bin/gosu -sSL "https://github.com/tianon/gosu/releases/download/1.6/gosu-amd64" && chmod +x /usr/bin/gosu
curl -o /usr/bin/devbootstrap -sSL "http://bit.ly/devbootstrap" && chmod +x /usr/bin/devbootstrap && gosu ec2-user /bin/bash -x /usr/bin/devbootstrap
echo >&2 "FINISHED"