-
Notifications
You must be signed in to change notification settings - Fork 0
/
source-aliases
155 lines (138 loc) · 4.18 KB
/
source-aliases
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# alias incomplete_di='aws ec2 describe-instances --filter 'Name=state-reason-code,Values='
unset -f getsgs
unset -f get_sgid
unset -f dsg
unset -f dabb
unset -f instids
unset -f terminsts
unset -f listem
unset -f getssh
unset -f getapps
unset -f get_latest_ubuntu
unset -f addfren
unset -f blog
export WORKDIR="$PWD/$BASH_SOURCE"
export QH_VPC_ID='vpc-03209b91ef8b01496'
export PATH=$PATH:$(pwd)/dist
function getsgs() {
local sgs=($(aws ec2 describe-security-groups --filter Name='vpc-id',Values="$QH_VPC_ID" | jq -r '.SecurityGroups[].GroupName'))
sgs=(${sgs[@]/'default'/})
echo "${sgs[*]}"
}
function get_sgid() {
local sg=$(aws ec2 describe-security-groups --filter Name='vpc-id',Values="$QH_VPC_ID" Name=group-name,Values="$1" | jq -r '.SecurityGroups[].GroupId')
echo "${sg}"
}
function dsg () {
printf "deleting security group '$1'..."
local f=$(mktemp)
aws ec2 delete-security-group --group-id "$1" 2>&1 > "$f"
if [ "$?" -eq 0 ]; then
echo Done
else
cat "$f" | tr -d '\n'
fi
rm "$f"
}
function dabb() {
echo "$1"
local sgid=$(get_sgid "$1")
echo "$sgid"
return
dsg "$sgid"
printf "deleting key pair '$1'..."
local f=$(mktemp)
#aws ec2 delete-key-pair --key-name "$1" 2>&1 > "$f"
aws ec2 delete-key-pair --key-name "$1"
if [ "$?" -eq 0 ]; then
echo Done
else
cat "$f" | tr -d '\n'
fi
rm "$1.pem"
rm "$f"
}
function instids () {
sgname=${1:-quickhost}
echo "instances tagged with name '$sgname':"
aws ec2 describe-instances \
--filter 'Name=instance-state-name,Values=running' \
--filter "Name=instance.group-name,Values=$sgname" \
--query 'Reservations[].Instances[].{id:InstanceId}' \
--output text
}
function terminsts () {
[ -z "$1" ] && echo "usage terminsts NAME" && return 1
instances=($(aws ec2 describe-instances \
--filter 'Name=instance-state-name,Values=running' \
--filter "Name=vpc-id,Values=$QH_VPC_ID" \
--query 'Reservations[].Instances[].{id:InstanceId}' \
--output text))
aws ec2 terminate-instances --instance-id ${instances[@]}
#--filter "Name=instance.group-name,Values=$1" \
}
function listem() {
aws ec2 describe-instances \
--filter 'Name=instance-state-name,Values=running' \
--filter "Name=vpc-id,Values=$QH_VPC_ID" \
--query 'Reservations[].Instances[].{id:InstanceId}' \
--output text
}
function getssh () {
sgname=${1:-quickhost}
local keyname="$1.pem"
# user ubuntu for ubuntu...
local host_user=ec2-user
echo "ssh commands for '$sgname':"
ips=($(aws ec2 describe-instances \
--filter 'Name=instance-state-name,Values=running' \
--filter "Name=instance.group-name,Values=$sgname" \
--query 'Reservations[].Instances[].{id:PublicIpAddress}' \
--output text))
for i in "${ips[@]}"; do
printf "ssh -i %s %s@%s\n" "$keyname" "$host_user" "$i"
done
}
function getapps () {
local tagkey=${1:-app}
# belongs in python
#tagval=${1:-quickhost}
#filter_tag="--filter 'Name=tag:${tagkey},Values=${tagval}'"
#filter_all="--filter Name=tag-key,Values=$tagkey"
echo "Getting hosts with tag app: $tagval"
aws ec2 describe-instances \
--filter "Name=tag-key,Values=app" \
--filter 'Name=instance-state-name,Values=running' \
--query 'Reservations[].Instances[].Tags[]' \
--output text
}
# figure this one out and build into make_instances.py
function get_latest_ubuntu () {
aws ssm get-parameters --names \
/aws/service/canonical/ubuntu/server/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id \
--query 'Parameters[0].Value' --output text
}
function addfren () {
[ -z "$1" -o -z "$2" ] && echo "usage addfren app_name fren_ip" && return 1
host=$1
fren_ip=$2
aws ec2 authorize-security-group-ingress --group-name "$1" --ip-permissions IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges="[{CidrIp=$2/32}]"
}
function blog () {
local blog_dir="${WORKDIR%/*}/blog"
vim "$blog_dir/blog-$(date -I).md"
}
#export -f getsgs
#export -f get_sgid
#export -f dsg
#export -f dabb
#export -f instids
#export -f terminsts
#export -f listem
#export -f getssh
#export -f getapps
#export -f get_latest_ubuntu
#export -f addfren
#export -f blog
#
#aws ec2 describe-instances --query 'Reservations[].Instances[].Tags[]'