-
-
Notifications
You must be signed in to change notification settings - Fork 615
/
functions.sh
262 lines (210 loc) · 5.74 KB
/
functions.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
AXIOM_PATH="$HOME/.axiom"
LOG="$AXIOM_PATH/log.txt"
# takes no arguments, outputs JSON object with instances
instances() {
doctl compute droplet list -o json
}
# takes one argument, name of instance, returns raw IP address
instance_ip() {
name="$1"
instances | jq -r ".[] | select(.name==\"$name\") | .networks.v4[].ip_address"
}
# takes no arguments, creates an fzf menu
instance_menu() {
instances | jq -r '.[].name' | fzf
}
quick_ip() {
data="$1"
ip=$(echo $droplets | jq -r ".[] | select(.name == \"$name\") | .networks.v4[].ip_address")
echo $ip
}
instance_pretty() {
data=$(instances)
i=0
for f in $(echo $data | jq -r '.[].size.price_monthly'); do new=$(expr $i + $f); i=$new; done
(echo "Instance,IP,Region,Memory,\$/M" && echo $data | jq -r '.[] | [.name, .networks.v4[].ip_address, .region.slug, .size_slug, .size.price_monthly] | @csv' && echo "_,_,Total,\$$i") | sed 's/"//g' | column -t -s, | perl -pe '$_ = "\033[0;37m$_\033[0;34m" if($. % 2)'
}
# identifies the selected instance/s
selected_instance() {
cat "$AXIOM_PATH/selected.conf"
}
get_image_id() {
query="$1"
images=$(doctl compute snapshot list -o json)
name=$(echo $images | jq -r ".[].name" | grep "$query" | tail -n 1)
id=$(echo $images | jq -r ".[] | select(.name==\"$name\") | .id")
echo $id
}
#deletes instance, if the second argument is set to "true", will not prompt
delete_instance() {
name="$1"
force="$2"
if [ "$force" == "true" ]
then
doctl compute droplet delete -f "$name"
else
doctl compute droplet delete "$name"
fi
}
# TBD
instance_exists() {
instance="$1"
}
list_regions() {
doctl compute region list
}
regions() {
doctl compute region list -o json
}
instance_sizes() {
doctl compute size list -o json
}
# List DNS records for domain
list_dns() {
domain="$1"
doctl compute domain records list "$domain"
}
list_domains_json() {
doctl compute domain list -o json
}
# List domains
list_domains() {
doctl compute domain list
}
list_subdomains() {
domain="$1"
doctl compute domain records list $domain -o json | jq '.[]'
}
# get JSON data for snapshots
snapshots() {
doctl compute snapshot list -o json
}
delete_record() {
domain="$1"
id="$2"
doctl compute domain records delete $domain $id
}
delete_record_force() {
domain="$1"
id="$2"
doctl compute domain records delete $domain $id -f
}
# Delete a snapshot by its name
delete_snapshot() {
name="$1"
snapshot_data=$(snapshots)
snapshot_id=$(echo $snapshot_data | jq -r ".[] | select(.name==\"$snapshot\") | .id")
doctl compute snapshot delete "$snapshot_id" -f
}
add_dns_record() {
subdomain="$1"
domain="$2"
ip="$3"
doctl compute domain records create $domain --record-type A --record-name $subdomain --record-data $ip
}
msg_success() {
echo -e "${BGreen}$1${Color_Off}"
echo "SUCCESS $(date):$1" >> $LOG
}
msg_error() {
echo -e "${BRed}$1${Color_Off}"
echo "ERROR $(date):$1" >> $LOG
}
msg_neutral() {
echo -e "${Blue}$1${Color_Off}"
echo "INFO $(date): $1" >> $LOG
}
# takes any number of arguments, each argument should be an instance or a glob, say 'omnom*', returns a sorted list of instances based on query
# $ query_instances 'john*' marin39
# Resp >> john01 john02 john03 john04 nmarin39
query_instances() {
droplets="$(instances)"
selected=""
for var in "$@"; do
if [[ "$var" =~ "*" ]]
then
var=$(echo "$var" | sed 's/*/.*/g')
selected="$selected $(echo $droplets | jq -r '.[].name' | grep "$var")"
else
if [[ $query ]];
then
query="$query\|$var"
else
query="$var"
fi
fi
done
if [[ "$query" ]]
then
selected="$selected $(echo $droplets | jq -r '.[].name' | grep -w "$query")"
else
if [[ ! "$selected" ]]
then
echo -e "${Red}No instance supplied, use * if you want to delete all instances...${Color_Off}"
exit
fi
fi
selected=$(echo "$selected" | tr ' ' '\n' | sort -u)
echo -n $selected
}
# take no arguments, generate a SSH config from the current Digitalocean layout
generate_sshconfig() {
droplets="$(instances)"
echo -n "" > $AXIOM_PATH/.sshconfig.new
for name in $(echo "$droplets" | jq -r '.[].name')
do
ip=$(echo "$droplets" | jq -r ".[] | select(.name==\"$name\") | .networks.v4[].ip_address")
echo -e "Host $name\n\tHostName $ip\n\tUser op\n\tPort 2266\n" >> $AXIOM_PATH/.sshconfig.new
done
mv $AXIOM_PATH/.sshconfig.new $AXIOM_PATH/.sshconfig
}
# create an instance, name, image_id (the source), sizes_slug, or the size (e.g 1vcpu-1gb), region, boot_script (this is required for expiry)
create_instance() {
name="$1"
image_id="$2"
size_slug="$3"
region="$4"
boot_script="$5"
doctl compute droplet create "$name" --image "$image_id" --size "$size" --region "$region" --wait --user-data-file "$boot_script" 2>&1 >>/dev/null &
}
# Function used for splitting $src across $instances and rename the split files.
lsplit() {
src="$1"
instances=$2
total=$(echo $instances| tr ' ' '\n' | wc -l | awk '{ print $1 }')
orig_pwd=$(pwd)
lines=$(wc -l $src | awk '{ print $1 }')
lines_per_file=$(bc <<< "scale=2; $lines / $total" | awk '{print int($1+0.5)}')
id=$(echo "$instances" | md5sum | awk '{ print $1 }' | head -c5)
split_dir="$AXIOM_PATH/tmp/$id"
rm -rf $split_dir >> /dev/null 2>&1
mkdir -p $split_dir
cp $src $split_dir
cd $split_dir
split -l $lines_per_file $src
rm $src
a=1
for f in $(ls | grep x)
do
mv $f $a.txt
a=$((a+1))
done
i=1
for instance in $(echo $instances | tr ' ' '\n')
do
mv $i.txt $instance.txt
i=$((i+1))
done
cd $orig_pwd
echo -n $split_dir
}
# Check if host is in .sshconfig, and if it's not, regenerate sshconfig
conf_check() {
instance="$1"
l="$(cat "$AXIOM_PATH/.sshconfig" | grep "$instance" | wc -l | awk '{ print $1 }')"
if [[ $l -lt 1 ]]
then
generate_sshconfig
fi
}