-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_create.sh
72 lines (47 loc) · 1.55 KB
/
aws_create.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/sh
progress_bar() {
duration="$1"
bar_length=75
sleep_duration=$(echo "$duration / $bar_length" | bc)
i=0
while [ "$i" -le "$bar_length" ]; do
printf "\r["
j=0
while [ "$j" -lt "$i" ]; do
printf "="
j=$((j+1))
done
printf ">"
j=$((i+1))
while [ "$j" -lt "$bar_length" ]; do
printf " "
j=$((j+1))
done
printf "] %d%%" "$((i*100/bar_length))"
sleep "$sleep_duration"
i=$((i+1))
done
printf "\n"
}
INSTANCENAME=docker_jenkins
echo "Creating $INSTANCENAME server"
INSTANCETYPE=t2.medium
AMI_ID=ami-0f5ee92e2d63afc18
ZONE=subnet-058dfc6e7a1c552f2
COUNTS=1
INSTANCE_ID=$(aws ec2 run-instances --image-id $AMI_ID --count $COUNTS --instance-type $INSTANCETYPE --key-name filinta --security-group-ids sg-04d7cf1bb1a5f765d --subnet-id $ZONE --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value='$INSTANCENAME'}]' --query 'Instances[0].InstanceId' --output text)
progress_bar 75
echo "$INSTANCENAME Server Created Successfully!"
PUBLICIP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[].Instances[].PublicIpAddress' | cut -d "[" -f2 | cut -d "]" -f1 | tr -d '" ')
PCLINE="[$INSTANCENAME]
$PUBLICIP ansible_user=ubuntu"
PHLINE="[$INSTANCENAME]\n\n$PUBLICIP ansible_user=ubuntu"
PATH1="/var/lib/jenkins/workspace/$INSTANCENAME"
PATH2="/root/.jenkins/workspace/$INSTANCENAME"
if [ "$(echo "$PWD")" = "$PATH1" ]; then
echo "$PCLINE" > hosts
elif [ "$(echo "$PWD")" = "$PATH2" ]; then
echo "$PCLINE" > hosts
else
echo "$PHLINE" > hosts
fi