Skip to content

Instantly share code, notes, and snippets.

@alemazz alemazz/jenkins_on.sh
Last active Nov 6, 2015

Embed
What would you like to do?
jenkins EC2 instance switchon cronjob
#!/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
You can’t perform that action at this time.