Repository navigation
Expand file tree
/
Copy pathaxiom-init
More file actions
executable file
·324 lines (302 loc) · 12 KB
/
Copy pathaxiom-init
File metadata and controls
executable file
·324 lines (302 loc) · 12 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env bash
###########################################################################################################
# Header
#
AXIOM_PATH="$HOME/.axiom"
DOCTL_CONFIG_PATH="$HOME/.config/doctl/config.yaml"
source "$AXIOM_PATH/interact/includes/vars.sh"
source "$AXIOM_PATH/interact/includes/functions.sh"
source "$AXIOM_PATH/interact/includes/system-notification.sh"
starttime=$(date +"%F-TIME-%T")
start="$(pwd)"
BASEOS="$(uname)"
case $BASEOS in
'Darwin')
PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
;;
*) ;;
esac
###########################################################################################################
# Declare defaut variables
#
provider="$(jq -r '.provider' "$AXIOM_PATH"/axiom.json)"
region="$(jq -r '.region' "$AXIOM_PATH"/axiom.json)"
size="$(jq -r '.default_size' "$AXIOM_PATH"/axiom.json)"
image="$(jq -r '.imageid' "$AXIOM_PATH"/axiom.json)"
sshkey="$(jq -r '.sshkey' "$AXIOM_PATH"/axiom.json)"
box_name=""
connect_shell=false
deploy=false
expire=false
default_expiry=525600000
quiet=false
domain=false
no_select=false
manual_image_id=false
###########################################################################################################
# waitabit function
# TODO https://github.com/pry0cc/axiom/issues/175
#
waitabit() {
local -r pid="${1}"
local -r delay='0.38'
i=420
while ps a | awk '{print $1}' | grep -q "${pid}"; do
: $((i--))
sleep "$delay"
done
printf " \b\b\b\b"
}
###########################################################################################################
# DO Region Transfer
# Transfer image to region if requested in that region yet does not exist. DO only
#
region_transfer(){
if [[ "$provider" == "do" ]]; then
avail_image_id_regions=$(doctl compute image get "$image_id" -o json| jq -r '.[] | .regions[]')
requested_image_id_regions="$region"
if [[ "$avail_image_id_regions" != *"$requested_image_id_regions"* ]]; then
echo -e "${BYellow}You requested image in region ${BRed}$region${BYellow}, however the image ${BRed}$image${BYellow} only exists in ${BRed}$(echo $avail_image_id_regions | tr '\n' ',')"
echo -e "${BYellow}Axiom will auto transfer your image to new region. This can take a few minutes please be patient..${Color_Off}"
doctl compute image-action transfer $image_id --region $region --wait
sleep 3
echo -e "${BYellow}Image transfer completed... if the above command errored, you might need to pick a different region. Check if image is available in new region with 'axiom-images ls'${Color_Off}"
echo -e "${BWhite}Waiting 90 seconds before continuing...${Color_Off}"
sleep 90
fi
fi
}
###########################################################################################################
# Help Menu
#
function usage() {
echo -e "${BWhite}Description:${Color_Off}"
echo -e " Initialize one axiom instance with differnet options, such as image, region, size and axiom deployment profiles"
echo -e "${BWhite}Examples:${Color_Off}"
echo -e " ${Green}axiom-init --run${Color_Off} # provision instance with random name"
echo -e " ${Green}axiom-init --deploy desktop ${Color_Off}# provision instance with random name, then deploy axiom profile 'desktop'"
echo -e " ${Green}axiom-init testy01${Color_Off} # provision instance named testy01"
echo -e " ${Green}axiom-init stok01 --region nyc3 --image axiom-default-1635920849 --size s-1vcpu-2gb --deploy desktop --shell${C
10BC8
olor_Off}"
echo -e "${BWhite}Usage:${Color_Off}"
echo -e " <name> string (optional)"
echo -e " Name of the instance, supplied as a positional first argument"
echo -e " --run (optional)"
echo -e " Create one instance with random name"
echo -e " --image <image name>"
echo -e " Manually set the image to use (default is imageid in ~/.axiom/axiom.json)"
echo -e " --region <region>"
echo -e " User specified region to use (default is region in ~/.axiom/axiom.json)"
echo -e " --deploy <profile>"
echo -e " Deploy a profile after initialization (e.g desktop, openvpn, bbrf, wireguard)"
echo -e " --shell (optional)"
echo -e " Connect to instance after initialization"
echo -e " --size <vm size>"
echo -e " VM size to use (default is size in ~/.axiom/axiom.json)"
echo -e " --no-select (optional)"
echo -e " Dont select instance after initialization (default is to select instance)"
echo -e " --debug (optional)"
echo -e " Run with set -xv, warning: very verbose"
echo -e " --help"
echo -e " Display this help menu"
}
###########################################################################################################
# Parse command line arguments
#
if [ $# -eq 0 ]; then
usage
exit 0
fi
i=0
for arg in "$@"
do
if [[ "$arg" == "--help" ]] || [[ "$arg" == "-h" ]]; then
usage
exit
fi
i=$((i+1))
if [[ ! " ${pass[@]} " =~ " ${i} " ]]; then
set=false
if [[ "$arg" == "--debug" ]]; then
set -xv
set=true
pass+=($i)
fi
if [[ "$arg" == "--shell" ]]; then
shell=true
set=true
pass+=($i)
fi
if [[ "$arg" == "--quiet" ]]; then
quiet=true
set=true
pass+=($i)
fi
if [[ "$arg" == "--run" ]]; then
run=true
set=true
pass+=($i)
fi
if [[ "$arg" == "--no-select" ]]; then
no_select=true
set=true
pass+=($i)
fi
if [[ "$arg" == "--region" ]] || [[ "$arg" == "-r" ]] ; then
n=$((i+1))
region=$(echo ${!n})
set=true
pass+=($i)
pass+=($n)
fi
if [[ "$arg" == "--deploy" ]]; then
n=$((i+1))
deploy=$(echo ${!n})
set=true
pass+=($i)
pass+=($n)
fi
if [[ "$arg" == "--size" ]] ; then
n=$((i+1))
size=$(echo ${!n})
set=true
pass+=($i)
pass+=($n)
fi
if [[ "$arg" == "--image" ]]; then
n=$((i+1))
image=$(echo ${!n})
set=true
pass+=($i)
pass+=($n)
fi
if [[ "$arg" == "--image-id" ]]; then
n=$((i+1))
manual_image_id=$(echo ${!n})
set=true
pass+=($i)
pass+=($n)
fi
if [[ "$set" != "true" ]]; then
space=" "
if [[ $arg =~ $space ]]; then
args="$args \"$arg\""
else
args="$args $arg"
fi
fi
fi
done
###########################################################################################################
# Generate name
#
if [ -z ${args+x} ]; then
name="${names[$RANDOM % ${#names[@]}]}$((10 + RANDOM % 20))"
else
name=$(echo "$args" | tr -d ' ')
fi
###########################################################################################################
# Get image_id from $image ( default is from axiom.json ) or from user supplied manual image id param
#
if [ "$manual_image_id" != "false" ]
then
image_id="$manual_image_id"
else
image_id="$(get_image_id "$image")"
if [ -z "$image_id" ]; then
echo -e "${BRed}ERROR: image ${Color_Off}[ ${BBlue}$image ${Color_Off}]${BRed} not found. you may need to run ${Color_Off}[ ${BBlue}axiom-build --setup ${Color_Off}]${BRed} to build a new image."
echo -e "${BRed}if you've already built an image, list all images with ${Color_Off}[ ${BBlue}axiom-images ls ${Color_Off}]${BRed} and select it with ${Color_Off}[ ${BBlue}axiom-images select \$image_name${Color_Off}]"
echo -e "${BRed}exiting...${Color_Off}"
exit 1
fi
fi
###########################################################################################################
# Check if ssh key is specified in axiom.json
#
if [ "$sshkey" == "" ] || [ "$sshkey" == "null" ]; then
echo -e "${BYellow}WARNING: sshkey not found in ~/.axiom/axiom.json. Adding ${Color_Off}[ ${BBlue}axiom_rsa ${Color_Off}] ${BYellow}key as a backup."
echo -e "${BYellow}If ${Color_Off}[ ${BBlue}axiom_rsa ${Color_Off}] ${BYellow}was not used to build the initial image ${Color_Off}[ ${BBlue}$image_id ${Color_Off}]${BYellow}, you may have to manually edit ~/.axiom/axiom.json to set the correct sshkey${Color_Off}"
account_path=$(ls -la "$AXIOM_PATH"/axiom.json | rev | cut -d " " -f 1 | rev)
if [ -f ~/.ssh/axiom_rsa ] ; then
jq '.sshkey="'axiom_rsa'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
else
ssh-keygen -b 2048 -t rsa -f ~/.ssh/axiom_rsa -q -N ""
jq '.sshkey="'axiom_rsa'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path" >> /dev/null 2>&1
fi
fi
###########################################################################################################
# Function called by trap
#
clean_up() {
tput setaf 1
echo ''
echo -e "${BRed}Exiting..."
echo -e "${Color_Off}You may need to delete the instance if it was created"
echo -e "run [ ${BGreen}ax ls ${Color_Off}] to list all instance and [ ${BGreen}ax rm $name ${Color_Off}] to delete it"
echo "Thank you for using ax :) "
exit
}
###########################################################################################################
# quiet flag
#
if [ $quiet == "true" ]
then
trap 'clean_up' SIGINT
create_instance "$name" "$image_id" "$size" "$region" &
PID="$!"
waitabit "$PID"
sleep 20
ip="$(instance_ip "$name")"
ssh-keygen -R "[$ip]:2266" >> /dev/null 2>&1
>&2 echo -e "${BWhite}Initialized instance '${BGreen}$name${Color_Off}${BWhite}' at '${BGreen}$ip${BWhite}'!${Color_Off}"
else
###########################################################################################################
# Create instance
#
echo -e "${BWhite}Initializing '${BGreen}$name${Color_Off}${BWhite}' at '${BGreen}$region${Color_Off}${BWhite}' with image '${BGreen}$image${Color_Off}${BWhite}'"
# echo deployment profile when needed
if [ "$deploy" != false ]; then
echo -e "${BWhite}Deploying '${BGreen}$deploy${Color_Off}${BWhite}' after init${Color_Off}"
fi
echo -e "${BWhite}INITIALIZING IN 5 SECONDS, CTRL+C to quit... ${Color_Off}"
sleep 5
region_transfer
mkdir -p "$AXIOM_PATH/tmp/"
trap 'clean_up' SIGINT
create_instance "$name" "$image_id" "$size" "$region" &
PID="$!"
"$NOTIFY_CMD" "Axiom Info" "Initializing '$name'..."
secs=$((280))
while [ $secs -gt 0 ]; do
echo -ne "${BWhite}>> T-Minus $secs to full initialization...${Color_Off}\r"
sleep 1
: $((secs--))
done
ip="$(instance_ip "$name")"
ssh-keygen -R "[$ip]:2266" >> /dev/null 2>&1
>&2 echo -e "${BWhite}Initialized instance '${BGreen}$name${Color_Off}${BWhite}' at '${BGreen}$ip${BWhite}'!"
>&2 echo -e "${BWhite}To connect, run '${BGreen}axiom-ssh $name${Color_Off}'${BWhite}${Color_Off}"
"$NOTIFY_CMD" "Axiom Info" "$name successfully initialized at $ip!"
fi
###########################################################################################################
# no_select flag
#
if [[ "$no_select" != "true" ]]; then
echo "$name" > "$AXIOM_PATH"/selected.conf
fi
###########################################################################################################
# deploy flag
#
if [ "$deploy" != false ]; then
"$AXIOM_PATH"/interact/axiom-deploy "$deploy" "$name"
fi
###########################################################################################################
# add axiom-init stats to stats.log
#
echo "{\"init\":\"$name\",\"ip\":\"$ip\",\"time\":\"$starttime\",\"region\":\"$region\",\"size\":\"$size\",\"image\":\"$image\",\"deploy\":\"$deploy\"}" >> $AXIOM_PATH/stats.log
###########################################################################################################
# connect shell flag
#
if [[ $shell == "true" ]];
then
axiom-ssh $name -o StrictHostKeyChecking=no
fi