forked from pry0cc/axiom
-
Notifications
You must be signed in to change notification settings - Fork 20
/
axiom-build
executable file
·451 lines (405 loc) · 26.2 KB
/
axiom-build
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/bin/bash
###########################################################################################################
# Header
#
AXIOM_PATH="$HOME/.axiom"
source "$AXIOM_PATH/interact/includes/vars.sh"
source "$AXIOM_PATH/interact/includes/system-notification.sh"
############################################################################################################
# Variable Declarations
#
region="$(jq -r '.region' "$AXIOM_PATH"/axiom.json)"
builder="$(jq -r '.provider' "$AXIOM_PATH"/axiom.json)"
start_time=$(date +%s)
op_ssh_password=$(cat /dev/urandom| base64 | head -c 128 | tr -d '+=-' | tr -d '\n' | tr -d /)
account_path=$(ls -la $AXIOM_PATH/axiom.json | rev | cut -d " " -f 1 | rev)
provider="$(cat "$AXIOM_PATH/axiom.json" | jq -r '.provider')"
BASEOS="$(uname)"
custom_ssh_key=false
extension=pkr.hcl
###########################################################################################################
# Display axiom banner and authors
#
banner() {
cat << EOF >&2
█████╗ ██╗ ██╗ ██████╗ ██╗ ██╗██╗██╗ ██████╗
██╔══██╗╚██╗██╔╝ ██╔══██╗██║ ██║██║██║ ██╔══██╗
███████║ ╚███╔╝ ██████╔╝██║ ██║██║██║ ██║ ██║
██╔══██║ ██╔██╗ ██╔══██╗██║ ██║██║██║ ██║ ██║
██║ ██║██╔╝ ██╗ ██████╔╝╚██████╔╝██║███████╗██████╔╝
╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝╚══════╝╚═════╝
Maintainer: 0xtavian
EOF
echo ''
echo '
"𝓲𝓷𝓼𝓹𝓲𝓻𝓮𝓭 𝓫𝔂 𝓪𝔁𝓲𝓸𝓶: 𝓽𝓱𝓮 𝓭𝔂𝓷𝓪𝓶𝓲𝓬 𝓲𝓷𝓯𝓻𝓪𝓼𝓽𝓻𝓾𝓬𝓽𝓾𝓻𝓮 𝓯𝓻𝓪𝓶𝓮𝔀𝓸𝓻𝓴 𝓯𝓸𝓻 𝓮𝓿𝓮𝓻𝔂𝓫𝓸𝓭𝔂! - @𝓹𝓻𝔂0𝓬𝓬 @0𝔁𝓽𝓪𝓿𝓲𝓪𝓷"
"𝓽𝓱𝓮 𝓬𝓸𝓷𝓽𝓲𝓷𝓾𝓪𝓽𝓲𝓸𝓷 𝓸𝓯 𝓪𝔁𝓲𝓸𝓶: 𝓽𝓱𝓮 𝓭𝔂𝓷𝓪𝓶𝓲𝓬 𝓲𝓷𝓯𝓻𝓪𝓼𝓽𝓻𝓾𝓬𝓽𝓾𝓻𝓮 𝓯𝓻𝓪𝓶𝓮𝔀𝓸𝓻𝓴 𝓯𝓸𝓻 𝓮𝓿𝓮𝓻𝔂𝓫𝓸𝓭𝔂! - @𝓹𝓻𝔂0𝓬𝓬 @0𝔁𝓽𝓪𝓿𝓲𝓪𝓷"
"𝓬𝓸𝓷𝓽𝓲𝓷𝓾𝓮𝓭 𝓯𝓻𝓸𝓶: 𝓪𝔁𝓲𝓸𝓶: 𝓽𝓱𝓮 𝓭𝔂𝓷𝓪𝓶𝓲𝓬 𝓲𝓷𝓯𝓻𝓪𝓼𝓽𝓻𝓾𝓬𝓽𝓾𝓻𝓮 𝓯𝓻𝓪𝓶𝓮𝔀𝓸𝓻𝓴 𝓯𝓸𝓻 𝓮𝓿𝓮𝓻𝔂𝓫𝓸𝓭𝔂! - @𝓹𝓻𝔂0𝓬𝓬 @0𝔁𝓽𝓪𝓿𝓲𝓪𝓷"
"𝓫𝓪𝓼𝓲𝓬𝓪𝓵𝓵𝔂, 𝓪𝔁𝓲𝓸𝓶: 𝓽𝓱𝓮 𝓭𝔂𝓷𝓪𝓶𝓲𝓬 𝓲𝓷𝓯𝓻𝓪𝓼𝓽𝓻𝓾𝓬𝓽𝓾𝓻𝓮 𝓯𝓻𝓪𝓶𝓮𝔀𝓸𝓻𝓴 𝓯𝓸𝓻 𝓮𝓿𝓮𝓻𝔂𝓫𝓸𝓭𝔂! - @𝓹𝓻𝔂0𝓬𝓬 @0𝔁𝓽𝓪𝓿𝓲𝓪𝓷 "
"𝓶𝓲𝓰𝓱𝓽 𝓪𝓼 𝔀𝓮𝓵𝓵 𝓫𝓮 𝓪𝔁𝓲𝓸𝓶: 𝓽𝓱𝓮 𝓭𝔂𝓷𝓪𝓶𝓲𝓬 𝓲𝓷𝓯𝓻𝓪𝓼𝓽𝓻𝓾𝓬𝓽𝓾𝓻𝓮 𝓯𝓻𝓪𝓶𝓮𝔀𝓸𝓻𝓴 𝓯𝓸𝓻 𝓮𝓿𝓮𝓻𝔂𝓫𝓸𝓭𝔂! - @𝓹𝓻𝔂0𝓬𝓬 @0𝔁𝓽𝓪𝓿𝓲𝓪𝓷"
"𝓪𝓵𝓻𝓲𝓰𝓱𝓽, 𝔂𝓸𝓾 𝓰𝓸𝓽 𝓶𝓮, 𝓲𝓽𝓼 𝓳𝓾𝓼𝓽 𝓪𝔁𝓲𝓸𝓶: 𝓽𝓱𝓮 𝓭𝔂𝓷𝓪𝓶𝓲𝓬 𝓲𝓷𝓯𝓻𝓪𝓼𝓽𝓻𝓾𝓬𝓽𝓾𝓻𝓮 𝓯𝓻𝓪𝓶𝓮𝔀𝓸𝓻𝓴 𝓯𝓸𝓻 𝓮𝓿𝓮𝓻𝔂𝓫𝓸𝓭𝔂! - @𝓹𝓻𝔂0𝓬𝓬 @0𝔁𝓽𝓪𝓿𝓲𝓪𝓷"
"𝓼𝓽𝓸𝓵𝓮𝓷 𝓯𝓻𝓸𝓶: 𝓪𝔁𝓲𝓸𝓶: 𝓽𝓱𝓮 𝓭𝔂𝓷𝓪𝓶𝓲𝓬 𝓲𝓷𝓯𝓻𝓪𝓼𝓽𝓻𝓾𝓬𝓽𝓾𝓻𝓮 𝓯𝓻𝓪𝓶𝓮𝔀𝓸𝓻𝓴 𝓯𝓸𝓻 𝓮𝓿𝓮𝓻𝔂𝓫𝓸𝓭𝔂! - @𝓹𝓻𝔂0𝓬𝓬 @0𝔁𝓽𝓪𝓿𝓲𝓪𝓷"
' | xargs shuf -n1 -e
echo ''
}
banner
###########################################################################################################
# Help Menu:
#
function help() {
echo -e "${BWhite}Description:${Color_Off}"
echo -e " axiom-build supports both legacy ${BGreen}Packer JSON${Color_Off} files as well as the new ${BGreen}HCL format${Color_Off}"
echo -e " axiom-build combines a cloud provider's ${BGreen}Builder${Color_Off} (~/.axiom/images/\$format/builders) with a ${BGreen}Provisioner ${Color_Off}(~/.axiom/images/\$format/provisioners)"
echo -e " to generate the final ${BGreen}Packer file${Color_Off} (~/.axiom/images/\$format/axiom.\$format)"
echo -e " Using ${BGreen}Packer${Color_Off}, axiom-build creates an image with the tools pre-installed, as defined by the chosen Provisioner"
echo -e " Choose between multiple Provisioners (barebones, default, reconftw, and extras) or bring your own (custom)"
echo -e ""
echo -e " axiom-build adds your${BGreen} SSH key${Color_Off} to the image, allowing you to connect to instances created using the image"
echo -e " By default, it creates and uses an SSH key located at ~/.ssh/axiom_ssh.pub, but you can specify a ${BGreen}custom SSH key${Color_Off} if needed"
echo -e "${BWhite}Examples:${Color_Off}"
echo -e " ${BGreen}axiom-build --run ${Color_Off}# Kick off the interactive build wizard"
echo -e " ${BGreen}axiom-build default${Color_Off} # Build a new Packer image using the Default Provisioner"
echo -e " ${BGreen}axiom-build barebones --debug ${Color_Off}# Build a new Packer image using the Barebones Provisioner, run with --debug"
echo -e " ${BGreen}axiom-build --ssh-key ~/.ssh/id_rsa.pub ${Color_Off}# Set a custom SSH key (default is SSH key in ~/.ssh/axiom_ssh.pub)"
echo -e "${BWhite}Usage:${Color_Off}"
echo -e " <provisioner> (optional)"
echo -e " Name of provisioner to build, options are barebones, default, reconftw, extras or custom (provisioner must be located in ~/.axiom/images/\$format/provisioners)"
echo -e " --run (optional)"
echo -e " Start axiom-build setup wizard"
echo -e " --legacy (optional)"
echo -e " Use legacy Packer JSON Builder and Provisioner instead of pkr.hcl"
echo -e " --ssh-key <file to pub key> (optional)"
echo -e " Build the image using a custom SSH key (instead of using the default ssh key axiom creates ~/.ssh/axiom_ssh.pub)"
echo -e " --debug (optional)"
echo -e " Enable debug mode (VERY VERBOSE!)"
echo -e " --help (optional)"
echo -e " Display this help menu"
}
###########################################################################################################
# Parse command line arguments
#
if [[ "$@" == "--help" ]] || [[ "$@" == "-h" ]] || [[ "$@" == "" ]]; then
help
exit
fi
###########################################################################################################
# Parse command line arguments
#
i=0
for arg in "$@"
do
i=$((i+1))
if [[ ! " ${pass[@]} " =~ " ${i} " ]]; then
set=false
if [[ "$arg" == "--debug" ]]; then
set -xv
export PACKER_LOG=1
set=true
pass+=($i)
fi
if [[ "$arg" == "--help" ]] || [[ "$arg" == "-h" ]]; then
usage
exit
set=true
pass+=($i)
fi
if [[ "$arg" == "--run" ]] || [[ "$arg" == "--setup" ]]; then
setup=true
set=true
pass+=($i)
fi
if [[ "$arg" == "--legacy" ]]; then
extension=json
set=true
pass+=($i)
fi
if [[ "$arg" == "--ssh-key" ]]|| [[ "$arg" == "--sshkey" ]] ; then
n=$((i+1))
custom_ssh_key=$(echo ${!n})
set=true
pass+=($i)
pass+=($n)
fi
if [[ "$set" != "true" ]]; then
provisioner="$arg"
fi
fi
done
############################################################################################################
# Get the provisioner file
#
if [ -z "$provisioner" ]; then
echo -e '\033[1mTime to pick your provisioner. Options: default, reconftw, barebones, custom\033[0m'
echo -e "${Yellow}Default: is our latest iteration of axiom provisioners. It comes with all tools installed and has been optimized for readability. Recommended for axiom-fleet${Color_Off}"
echo -e "${Green}ReconFTW: includes all tools in ReconFTW and works with most modules. Recommended for ReconFTW.${Color_Off}"
echo -e "${Yellow}Barebones: a decently secure base install, comes with Docker, Go-lang, Interlace and nmap installed. Recommended for templating.${Color_Off}"
echo -e "${Green}Extras: more tools installed compared to default${Color_Off}"
echo -e "${Yellow}Custom: custom Packer provisioner you provide.${Color_Off}"
echo -e -n "${BGreen}Which axiom provisioner would you like to use: default, reconftw, barebones, custom? (option: press enter to select Default) \n>>${Color_Off}"
read ans
if [[ $ans == "default" ]] || [[ $ans == "Default" ]]; then
echo -e "${Green}Selected provisioner option 'Default'${Color_Off}"
provisioner="default"
elif [[ $ans == "reconftw" ]] || [[ $ans == "Reconftw" ]] || [[ $ans == "ReconFTW" ]]; then
echo -e "${Green}Selected provisioner option 'ReconFTW'${Color_Off}"
provisioner="reconftw"
elif [[ $ans == "barebones" ]] || [[ $ans == "Barebones" ]]; then
echo -e "${Green}Selected provisioner option 'Barebones'${Color_Off}"
provisioner="barebones"
elif [[ $ans == "Extras" ]] || [[ $ans == "extras" ]] || [[ $ans == "extra" ]]; then
echo -e "${Green}Selected provisioner option 'Extras'${Color_Off}"
provisioner="extras"
elif [[ $ans == "custom" ]] || [[ $ans == "Custom" ]]; then
echo -e "${Red}Your Packer provosioner must be located in "$AXIOM_PATH/images/$extension/provisioners/". Make sure to move it.${Color_Off}"
echo -e "${Green}Listing all provisioners available in "$AXIOM_PATH/images/$extension/provisioners/" ...${Color_Off}"
ls -1 $AXIOM_PATH/images/$extension/provisioners/ | sort -u | sed -e 's/\.json$//' | sed -e 's/\.pkr.hcl$//'
echo -e -n "${Green}Enter the provisioner filename. \n>>${Color_Off}"
read ans
provisioner=$ans
echo -e "${Green}Selected provisioner option 'Custom', using $ans ${Color_Off}"
elif [[ $ans == "" ]]; then
echo -e "${Green}Selected default provisioner option 'Default'${Color_Off}"
provisioner="default"
else
provisioner=$ans
fi
fi
############################################################################################################
# check the provisioner and bulder exist
#
packer_provisioner_path="$AXIOM_PATH/images/$extension/provisioners/$provisioner.$extension"
packer_builder_path="$AXIOM_PATH/images/$extension/builders/$builder.$extension"
if [ ! -f "$packer_provisioner_path" ]; then
echo -e "${Yellow}Warning: cant find $extension Packer Provisioner in $AXIOM_PATH/images/$extension/provisioners/$provisioner.$extension"
echo -e "Checking for legacy Packer JSON Provisioner in $AXIOM_PATH/images/json/provisioners/$provisioner.json${Color_Off}"
if [ -f "$AXIOM_PATH/images/json/provisioners/$provisioner.json" ]; then
echo -e "${BGreen}Provisioner found! $AXIOM_PATH/images/json/provisioners/$provisioner.json!${Color_Off}"
packer_provisioner_path="$AXIOM_PATH/images/json/provisioners/$provisioner.json"
extension=json
else
echo -e "${Yellow}Warning: can not find legacy Packer JSON in $AXIOM_PATH/images/json/provisioners/$provisioner.json"
echo -e "${Yellow}Looking in $AXIOM_PATH/images/provisioners/$provisioner.json just in case...${Color_Off}"
if [ -f "$AXIOM_PATH/images/provisioners/$provisioner.json" ]; then
echo -e "${BGreen}Provisioner found! Moving it to $AXIOM_PATH/images/json/provisioners/$provisioner.json${Color_Off}"
mv $AXIOM_PATH/images/provisioners/$provisioner.json $AXIOM_PATH/images/json/provisioners/$provisioner.json
packer_provisioner_path="$AXIOM_PATH/images/$extension/provisioners/$provisioner.$extension"
extension=json
else
echo -e "${Red}Error: unfortunately, can not find Packer Provisioner.."
echo -e "${Red}Make sure your provisioner is located in $AXIOM_PATH/images/$extension/provisioners/$provisioner.$extension and run again"
echo -e "${Red}exiting..${Color_Off}"
exit
fi
fi
fi
if [ ! -f "$packer_builder_path" ]; then
if [ ! -f "$AXIOM_PATH/images/$extension/builders/$builder.json" ]; then
extension=json
else
echo -e ""
echo -e "${Red}Error: unfortunately, can not find Packer Builder...${Color_Off}"
echo -e "${Red}Make sure your Packer Builder is located in $AXIOM_PATH/images/$extension/builders/$builder.$extension and run again"
echo -e "${Red}exiting..${Color_Off}"
exit
fi
fi
############################################################################################################
# Generate the Packer final provisioner by combining the Cloud Provider builder and provisioner file
#
snapshot_name=axiom-$provisioner-$(date +%s)
image_path="$AXIOM_PATH/images/$extension/axiom.$extension"
$AXIOM_PATH/interact/axiom-provider "$provider"
mv $image_path $image_path.tmp > /dev/null 2>&1
"$AXIOM_PATH"/interact/generate_packer "$builder" "$provisioner" "$extension"
if [ ! -f "$image_path" ]; then
echo -e "${Red}Error: axiom build failed to generate final Packer provsioner...${Color_Off}"
echo -e "${Red}exiting..${Color_Off}"
mv $image_path.tmp $image_path > /dev/null 2>&1
exit
else
rm $image_path.tmp > /dev/null 2>&1
fi
#############################################################################################################
# Add SSH Key in account.json to $AXIOM_PATH/configs/authorized_keys
# If SSH key in account.json doesnt exists, create axiom_rsa SSH key and add that to $AXIOM_PATH/configs/authorized_keys
# TIPS: if you want to swap out the SSH key axiom uses, just edit the account.json.
# For example: change key:value pair "sshkey": "id_rsa" to "sshkey": "axiom_rsa" and rerun axiom-build.
#
if [[ "$custom_ssh_key" == false ]]; then
account_path=$(ls -la $AXIOM_PATH/axiom.json | rev | cut -d " " -f 1 | rev)
sshkey="$(cat "$AXIOM_PATH/axiom.json" | jq -r '.sshkey')"
# If custom SSH key is found in "$AXIOM_PATH/axiom.json", add it to Packer builder
if [ -f ~/.ssh/$sshkey ] ; then
echo -e "${BGreen}Adding $sshkey SSH key to Packer Builder${Color_Off}"
cat ~/.ssh/$sshkey.pub >$AXIOM_PATH/configs/authorized_keys
# If axiom_rsa key is already created but not in "$AXIOM_PATH/axiom.json", add it to $AXIOM_PATH/axiom.json" and Packer builder
elif [ -f ~/.ssh/axiom_rsa ] ; then
echo -e "${BGreen}Adding axiom_rsa SSH key to Packer Builder${Color_Off}"
jq '.sshkey="'axiom_rsa'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
cat ~/.ssh/axiom_rsa.pub >$AXIOM_PATH/configs/authorized_keys
sshkey="$(cat "$AXIOM_PATH/axiom.json" | jq -r '.sshkey')"
# If axiom_rsa is not created, create it and then add it to "$AXIOM_PATH/axiom.json" and Packer builder
else
echo -e "${BGreen}Creating axiom SSH key in ~/.ssh/axiom_rsa.pub and adding it to Packer Builder${Color_Off}"
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"
cat ~/.ssh/axiom_rsa.pub >$AXIOM_PATH/configs/authorized_keys
sshkey="$(cat "$AXIOM_PATH/axiom.json" | jq -r '.sshkey')"
fi
if [[ ! -f "$AXIOM_PATH/configs/authorized_keys" ]]
then
echo -e "${BRed}You have no authorized keys file at $AXIOM_PATH/configs/authorized_keys"
echo -e "Please make sure an SSH key exists in ~/.axiom/axiom.json or rerun axiom-configure."
echo -e "Then run axiom-build..${Color_Off}"
exit
fi
else
if [ -f $custom_ssh_key ] ; then
if [ "${custom_ssh_key: -4}" == ".pub" ]; then
echo -e "${BGreen}Adding $custom_ssh_key SSH key to Packer Builder${Color_Off}"
sshkey=$(basename $custom_ssh_key | rev | cut -d . -f 2- |rev)
cat $custom_ssh_key >$AXIOM_PATH/configs/authorized_keys
jq '.sshkey="'$sshkey'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
else
echo -e "${Red}Error: the ssh key provided does not end in .pub. You must provide a public SSH key..exiting..."
exit 1
fi
else
echo -e "${Red}Error: can not find custom ssh key at $custom_ssh_key... exiting"
exit 1
fi
fi
#############################################################################################################
# If axiom_path isnt set, exit
#
cd "$AXIOM_PATH"/ || exit
#############################################################################################################
# change password or fail
#
if [ -z "$op_ssh_password" ]; then
echo -e "Unable to generate random password for Packer image.. exiting"
exit 0
fi
#############################################################################################################
# Display account.json one last time, since its been updated
#
echo -e "${Green}If this build completes successfully, the following updated axiom.json data will be stored in $account_path${Color_Off}"
if [[ "$provider" == "aws" ]]; then
cat "$account_path" | jq '.aws_secret_access_key = "*************************************"'| \
jq '.op = "***************************************************************************************************************************"' |
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
elif [[ "$provider" == "azure" ]]; then
cat "$account_path" | jq '.client_secret = "*************************************"'| \
jq '.op = "***************************************************************************************************************************"' |
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
elif [[ "$provider" == "do" ]]; then
cat "$account_path" | jq '.do_key = "************************************************************************"'| \
jq '.op = "***************************************************************************************************************************"' | \
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
elif [[ "$provider" == "ibm" ]]; then
cat "$account_path" | jq '.sl_key = "************************************************************************" | .ibm_cloud_api_key = "***************************************"'| \
jq '.op = "***************************************************************************************************************************"' |
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
elif [[ "$provider" == "linode" ]]; then
cat "$account_path" | jq '.linode_key = "*******************************************************"'| \
jq '.op = "***************************************************************************************************************************"' |
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
elif [[ "$provider" == "hetzner" ]]; then
cat "$account_path" |jq '.hetzner_key = "****************************************************************"'| \
jq '.op = "***************************************************************************************************************************"' |
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
elif [[ "$provider" == "scaleway" ]]; then
cat "$account_path" |jq '.secret_key = "************************************"'| \
jq '.op = "***************************************************************************************************************************"' |
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
elif [[ "$provider" == "gcp" ]]; then
cat "$account_path" | \
jq '.op = "***************************************************************************************************************************"' |
jq '.imageid = "'$snapshot_name'"' | jq '.provisioner = "'$provisioner'"' | jq '.provisioner = "'$provisioner'"'| jq '.sshkey = "'$sshkey'"'
else
cat "$account_path"
fi
############################################################################################################
# opportunity to cancel axiom-build
#
echo -e "${Red}Now running ${Color_Off}[ ${BBlue}axiom-build $provisioner ${Color_Off}]${Red}"
echo "The Packer Build will take about 25 minutes, spin up a temporary instance, then take a snapshot and use it for our future instances"
echo -e "Starting Packer Build in 5 seconds... Ctrl+C to quit, remember, you can just run ${Color_Off}[ ${BBlue}axiom-build --run ${Color_Off}] ${Red}at any time${Color_Off}"
sleep 8
#############################################################################################################
# Change packer version for IBM
#
if [[ "$provider" == "ibm" ]]; then
if [[ "$extension" == "json" ]]; then
mkdir -p ~/.packer.d/plugins/
mkdir -p /tmp/packer-ibm/
if [[ ! -f /tmp/packer-ibm/packer ]] || [[ ! -f "$HOME/.packer.d/plugins/packer-builder-ibmcloud" ]]; then
if [[ $BASEOS == "Linux" ]]; then
wget -q -O /tmp/packer.zip https://releases.hashicorp.com/packer/1.5.6/packer_1.5.6_linux_amd64.zip && unzip -qq /tmp/packer.zip -d /tmp/packer-ibm/ && rm /tmp/packer.zip
wget -q https://github.com/IBM/packer-plugin-ibmcloud/releases/download/v1.0.1/packer-builder-ibmcloud_1.0.1_linux_64-bit.tar.gz -O - | tar -xz -C ~/.packer.d/plugins/
elif [[ $BASEOS == "Darwin" ]]; then
wget -q -O /tmp/packer.zip https://releases.hashicorp.com/packer/1.5.6/packer_1.5.6_darwin_amd64.zip && unzip -qq /tmp/packer.zip -d /tmp/packer-ibm/ && rm /tmp/packer.zip
wget -q https://github.com/IBM/packer-plugin-ibmcloud/releases/download/v1.0.1/packer-builder-ibmcloud_1.0.1_macOS_64-bit.tar.gz -O - | tar -xz -C ~/.packer.d/plugins/
fi
fi
BASEOS="$(uname)"
case $BASEOS in
'Darwin')
PATH="/tmp/packer-ibm/:$(brew --prefix coreutils)/libexec/gnubin:$PATH"
;;
'Linux')
PATH="/tmp/packer-ibm:$PATH"
;;
*) ;;
esac
fi
fi
# ###########################################################################################################
# Execute the Packer binary to build the final image
#
counter=0
echo -e "${BWhite}Building image ${Color_Off}[ ${BBlue}$provisioner ${Color_Off}]${BWhite} for provider ${Color_Off}[ ${BBlue}$builder ${Color_Off}]${BWhite} using Packer... This can take up to 25 minutes so please be patient!\nIf this fails, verify the account is properly configured and/or try a different region.${Color_Off}"
if [[ $provisioner == "azure" ]]; then
cp $AXIOM_PATH/axiom.json $AXIOM_PATH/axiom_bck.json
sed -i "/\"region\"/d" > $AXIOM_PATH/axiom.json
fi
if packer build -var-file "$AXIOM_PATH"/axiom.json -var "golang_version=${GolangVersion}" -var "variant=$provisioner" -var "op_random_password=$op_ssh_password" -var "snapshot_name=$snapshot_name" "$image_path";
then
end_time=$(date +%s)
seconds="$(expr $end_time - $start_time)"
time=""
if [[ "$seconds" -gt 59 ]]
then
minutes=$(expr $seconds / 60)
time="$minutes minutes"
else
time="$seconds seconds"
fi
if [[ $provisioner == "azure" ]]; then
mv "$AXIOM_PATH/axiom_bck.json" "$AXIOM_PATH/axiom.json"
fi
echo -e "${BGreen}Your build completed successfully in $time! You should run axiom-update && axiom-build weekly to get the latest and greatest updates! ${Color_Off}"
echo -e "${Green}To initialize an instance, run axiom-init${Color_Off}"
"$NOTIFY_CMD" "Axiom Info" "Build completed successfully!"
else
echo -e "${BRed}Your build failed :( Please take a look at the errors!${Color_Off}"
if [[ "$provider" == "linode" ]]; then
echo -e "${Yellow}This can happen sometimes, if you have a [404] error while using Linode, this is likely because you need to request support for a larger image size${Color_Off}"
echo -e "${Yellow}If you are using the default provisioner you need to request an increase to 45GB or higher! ${Color_Off}"
echo -e "${Yellow}Please open a Linode support ticket saying 'Hello! I have recently installed the Ax Framework https://github.com/attacksurge/ax and would like to request an image increase to 75GB please for the purposes of bulding the packer image. Thank you have a great day!'${Color_Off}"
echo -e "${Yellow}You could try with another provisioner (reconftw is 14 GB in size) ${Color_Off}"
echo -e "${Yellow}Use 'axiom-images ls' and delete any image before trying to build again ${Color_Off}"
fi
echo -e "${Yellow}Verify the account is properly configured and/or try a different region.${Color_Off}"
echo -e "${Yellow}For support, take a screenshot of the last 20 lines and open a issue at https://github.com/attacksurge/ax/issues${Color_Off}"
echo -e "${Green}Sometimes just running axiom-build again can work, so please try at least once: 'axiom-build', thanks!${Color_Off}"
echo -e "${Green}To debug Packer run again with ${Color_Off}[ ${BBlue}axiom-build $provisioner --debug ${Color_Off}]"
"$NOTIFY_CMD" "Axiom Info" "Build failed :("
exit 1
fi
############################################################################################################
# add data to account.json i.e account_path
#
jq '.op="'$op_ssh_password'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
jq '.imageid="'$snapshot_name'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"
jq '.provisioner="'$provisioner'"' <"$account_path">"$AXIOM_PATH"/tmp.json ; mv "$AXIOM_PATH"/tmp.json "$account_path"