Skip to content

Instantly share code, notes, and snippets.

@j3tm0t0
Created November 21, 2012 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j3tm0t0/4122817 to your computer and use it in GitHub Desktop.
Save j3tm0t0/4122817 to your computer and use it in GitHub Desktop.
bulk AMI deploy
#!/bin/sh
if [ "$4" == "" ]
then
echo usage: $0 image-url image-name image-description image-arch
exit -1
fi
alias p="pssh -h hosts.txt -i -t 600"
workdir=/media/ephemeral0
echo setting region to .bashrc // this should be run once, but no harm if run multiple times
p "(echo -n export EC2_URL=http://ec2. ; ec2-metadata -z | perl -pe 's/.+\ //g ; chomp ; chop '; echo .amazonaws.com) >> .bashrc"
echo remove if image file exists
p "[ -f $workdir/image.bin ] && rm $workdir/image.bin ; [ -f $workdir/image.bin.gz ] && rm $workdir/image.bin.gz ; true"
echo download and decompress image file
p "wget -qc -t 0 -O $workdir/image.bin.gz $1 && gzip -d $workdir/image.bin.gz"
echo writing image to volume
p "dd if=$workdir/image.bin of=/dev/sdf"
echo checking AKI
p "ec2-describe-images -o amazon | grep pv-grub-hd0_1.03-$4.gz | cut -f 2 > aki"
echo taking snapshot
p "ec2-metadata -i | cut -f 2 -d \ | xargs ec2-describe-instances | grep /dev/sdf | cut -f 3 | xargs ec2-create-snapshot -d \"$2 root\" | cut -f 2 > snap"
sleep 180
echo register
p "ec2-register -a $4 -n \"$2\" -d \"$3\" -b /dev/sdb=ephemeral0 -b /dev/sdc=ephemeral1 -b /dev/sdd=ephemeral2 -b /dev/sde=ephemeral3 -s \`cat snap\` --kernel \`cat aki\` | cut -f 2 > ami"
#!/bin/sh
size=$1
if [ "$size" == "" ]
then
size=1
else
size=$1
fi
(
for region in `ec2dre | cut -f 2`
do
ami=`ec2dim -o amazon -F "name=amzn-ami-pv-*" -F "architecture=x86_64" -F "virtualization-type=paravirtual" -F "root-device-type=ebs" --region $region | grep ^IMAGE | grep -v beta | tail -1 | cut -f 2`
instanceid=`ec2-run-instances --instance-initiated-shutdown-behavior terminate -k KYEPAIR -p AMI -f userdata.txt -b /dev/sdf=:$1:true -b /dev/sdb=ephemeral0 --region $region $ami | tee /dev/stderr | grep INSTANCE | cut -f 2`
echo $region $instanceid
done
) | tee instances.txt
cat instances.txt | xargs -L 1 -P 1 ec2-describe-instances --region | grep INSTANCE | cut -f 4 | tee hosts.txt
#!/bin/sh
groupmems -g disk -a ec2-user
chmod 777 /media/ephemeral0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment