Last active
November 6, 2015 10:13
-
-
Save alemazz/c9bc5d753d590f2ccd8b to your computer and use it in GitHub Desktop.
jenkins EC2 instance switchon cronjob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BACKUPLOG="/var/tmp/backuplogaws" | |
SENDTO="webmaster@example.com" | |
echo "start setup jenkinsCI" > ${BACKUPLOG} 2>&1 | |
AMI_ID_OLD=$(aws ec2 describe-images --owner ***** --query 'Images[?starts_with(Name, `jenkinsAMI`) == `true`]|[0:5].[CreationDate,ImageId,Name]' --profile **** --output text | sort -k1 | tail -n1 | awk '{print $2}') | |
sleep 20 | |
echo "found ami id : $AMI_ID_OLD" >> ${BACKUPLOG} 2>&1 | |
SNAPSHOT_ID=$(aws ec2 describe-images --image-id $AMI_ID_OLD --query 'Images[*].BlockDeviceMappings[0].Ebs.SnapshotId' --profile **** --output text) | |
sleep 40 | |
echo "found snapshot id : $AMI_ID_OLD" >> ${BACKUPLOG} 2>&1 | |
cp /root/mapping_default.json /root/mapping.json | |
echo "copied mapping for delete on termination" >> ${BACKUPLOG} 2>&1 | |
sed -i "s/snap-****/$SNAPSHOT_ID/g" /root/mapping.json | |
aws ec2 run-instances --instance-type c4.xlarge --key jenkinsci --image-id $AMI_ID_OLD --sub subnet-**** --security-group-ids sg-****--block-device-mappings file://mapping.json --count 1 --profile ***** | |
echo "started ec2 instance " >> ${BACKUPLOG} 2>&1 | |
sleep 200 | |
echo "run ec2 instance" >> ${BACKUPLOG} 2>&1 | |
INSTANCE_ID=$(aws ec2 describe-instances --filters "Name=image-id,Values=$AMI_ID_OLD" --query 'Reservations[*].Instances[*].{ID:InstanceId}' --profile ***** --output text) | |
echo "found instance id : $INSTANCE_ID" >> ${BACKUPLOG} 2>&1 | |
aws ec2 associate-address --instance-id $INSTANCE_ID --public-ip **** --profile *** | |
echo "associated ip for : $INSTANCE_ID " >> ${BACKUPLOG} 2>&1 | |
mail -s "JENKINS AWS switched on" ${SENDTO} | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment