-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·565 lines (452 loc) · 14.1 KB
/
build.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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/bin/bash
# TODO ADD EXECUTION ERROR VALIDATIONS
VM='AMZN2-test'
VERSION="2.0.20211223.0"
FILENAME=amzn2-virtualbox-${VERSION}-x86_64.xfs.gpt.vdi
AWS_VDI=${PWD}/images/${FILENAME}
VDI_LINK="https://cdn.amazonlinux.com/os-images/${VERSION}/virtualbox/amzn2-virtualbox-${VERSION}-x86_64.xfs.gpt.vdi"
function usage_description {
echo "#################################################################################################\n"
echo "# Modo de usar: #\n"
echo "# ./build.sh [options] #\n"
echo "# Options: #\n"
echo "# --dry-run: Don't publish version to Vagrant Cloud #\n"
echo "# --headless: Don't show Virtualbox VM window #\n"
echo "# --import-local: Import Created box to local Box list #\n"
echo "# --test-local: Test the created image locally #\n"
echo "# --debug-mode: Stop the process after the end of configuration and open an SSH console #\n"
echo "# #\n"
echo "# #\n"
echo "#################################################################################################\n"
}
DRY="0"
HEADLESS="0"
LOCAL="0"
TEST="0"
DEBUG="0"
for var in "$@"
do
case $var in
"--dry-run" )
echo " -> Dry run ON"
DRY="1"
;;
"--headless" )
echo " -> Headless ON"
HEADLESS=" --type headless "
;;
"--import-local" )
echo " -> Import local ON"
LOCAL="1"
;;
"--test-local" )
echo " -> Test local ON"
TEST="1"
LOCAL="1"
;;
"--debug-mode" )
echo " -> Debug mode ON"
DEBUG="1"
;;
--help )
usage_description
exit 0
;;
esac
done
function clean {
echo ""
echo "---"
echo "Cleaning old work files..."
echo "---"
echo ""
VBoxManage controlvm $VM poweroff
sleep 10s
VBoxManage unregistervm $VM --delete
sleep 10s
rm -rf build
mkdir build
wget \
https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant \
-O build/insecure_key
chmod 600 build/insecure_key
echo ""
echo "---"
echo "Old work files cleaned."
echo "---"
echo ""
}
function get_base_vdi {
echo ""
echo "---"
echo "Downloading Amazon Linux 2 image..."
echo "---"
echo ""
FILE=${PWD}/work/${FILENAME}
if [ ! -f $FILE ]; then
echo "amzn2-virtualbox-${VERSION}-x86_64.xfs.gpt" > .imageVersion
## CHANGE_HERE If you want to use a newer version of AWS provided base image
wget \
"$VDI_LINK" \
-O $FILE
fi
cp $FILE $AWS_VDI
echo ""
echo "---"
echo "Amazon Linux 2 image downloaded."
echo "---"
echo ""
}
function generate_seed {
echo ""
echo "---"
echo "Generating seed image..."
echo "---"
echo ""
genisoimage -input-charset utf-8 -output build/seed.iso -volid cidata -joliet -rock seedconfig/user-data seedconfig/meta-data
echo ""
echo "---"
echo "Seed image generated."
echo "---"
echo ""
}
function create_machine {
echo ""
echo ""
echo "#####################################"
echo "#####################################"
echo "---"
echo "Create VM '$VM'"
echo ""
VBoxManage createvm --name $VM --ostype "Linux_64" --register
echo "---"
echo ""
echo "#####################################"
echo "#####################################"
echo ""
echo "#####################################"
echo "#####################################"
echo "---"
echo "Add Main Drive Media"
echo "---"
echo ""
VBoxManage storagectl $VM \
--name "main_disk" \
--add sata \
--controller IntelAHCI
VBoxManage storageattach $VM \
--storagectl "main_disk" \
--port 0 \
--device 0 \
--type hdd \
--medium $AWS_VDI
echo ""
echo "#####################################"
echo "#####################################"
echo "---"
echo "Creating storage controller for DVD ROM"
echo "---"
echo ""
VBoxManage storagectl $VM \
--name "cdrom" \
--add ide
echo ""
echo "#####################################"
echo "#####################################"
echo ""
echo "#####################################"
echo "#####################################"
echo "---"
echo "Add User Data Media"
echo "---"
echo ""
VBoxManage storageattach $VM \
--storagectl "cdrom" \
--port 0 \
--device 0 \
--type dvddrive \
--medium ${PWD}/build/seed.iso
echo ""
echo "#####################################"
echo "#####################################"
echo ""
echo ""
echo "#####################################"
echo "#####################################"
echo "---"
echo "Configuring RAM and VRAM"
echo "---"
echo ""
VBoxManage modifyvm $VM --memory 1024 --vram 128
echo ""
echo "#####################################"
echo "#####################################"
echo "#####################################"
echo "#####################################"
echo "---"
echo "Configuring Network"
echo "---"
echo ""
#VBoxManage modifyvm $VM --nic1 hostonly --hostonlyadapter1 vboxnet0 --ostype RedHat_64
vboxmanage showvminfo --details $VM
VBoxManage modifyvm $VM --natpf1 "guestssh,tcp,,9999,,22"
echo ""
echo "#####################################"
echo "#####################################"
echo "#####################################"
echo "#####################################"
echo "---"
echo "Configuring SSH port forward"
echo "---"
echo ""
#VBoxManage modifyvm $VM --natpf2 "guestssh,tcp,,9999,,22"
#VBoxManage natnetwork add --netname natnet2 --network "192.168.15.0/24" --enable --dhcp on
#VBoxManage modifyvm $VM natpf2 "guestssh,tcp,,9999,,22"
#VBoxManage natnetwork modify \
# --netname natnet2 --port-forward-4 "ssh:tcp:[]:9999:[192.168.15.5]:22"
#VBoxManage natnetwork start --netname natnet2
echo ""
echo "#####################################"
echo "#####################################"
}
function wait_boot_finishes {
STATE=$( ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" ls -la )
while [ $? -ne 0 ]
do
echo "---"
echo "waiting boot fish..."
echo "---"
sleep 10
STATE=$( ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" ls -la )
done
}
function wait_first_setup_finishes {
STATE=$(ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" cat /home/vagrant/state_file)
while [ "$STATE" != "FINISHED" ]
do
echo "---"
echo "waiting startup setup..."
echo "---"
sleep 10
STATE=$(ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" cat /home/vagrant/state_file)
done
}
function setup_guest_adition {
echo "#####################################"
echo "#####################################"
echo "---"
echo "Add Guest Additions Media"
echo "---"
echo ""
VBoxManage storageattach $VM \
--storagectl "cdrom" \
--port 0 \
--device 0 \
--type dvddrive \
--forceunmount \
--medium emptydrive
sleep 5
VBoxManage storageattach $VM \
--storagectl "cdrom" \
--port 0 \
--device 0 \
--type dvddrive \
--forceunmount \
--medium /usr/share/virtualbox/VBoxGuestAdditions.iso
sleep 5
echo ""
echo "#####################################"
echo "#####################################"
echo ""
ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" << SSH
echo "----------------"
echo " => UPDATING KERNEL PACKAGES"
yum update kernel* -y
echo "----------------"
reboot
SSH
wait_boot_finishes
ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" << SSH
KERN_DIR=/usr/src/kernels/`uname -r`/build
sudo yum update -y
echo "----------------"
echo " => CREATING MOUNTING POINT"
sudo mkdir -p /media/cdrom1
echo " => MOUNTING GUEST ADITIONS CD"
sudo mount -t iso9660 -o ro /dev/sr0 /media/cdrom1
cd /media/cdrom1/
echo " => STARTING INSTALLATION"
sudo ./VBoxLinuxAdditions.run
echo "----------------"
echo "TESTING VBOX GUEST ADDITIONS PRESENCE"
echo "test result: \$( lsmod | grep vboxguest )"
echo "----------------"
sudo reboot
SSH
#ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3"
VBoxManage storageattach $VM \
--storagectl "cdrom" \
--port 0 \
--device 0 \
--type dvddrive \
--forceunmount \
--medium emptydrive
wait_boot_finishes
ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" << SSH
echo "----------------"
echo "[AGAIN] TESTING VBOX GUEST ADDITIONS PRESENCE"
echo "test result: \$( lsmod | grep vboxguest )"
echo "----------------"
SSH
test_vbox_guest_aditions
}
function publish_version {
echo ""
echo "---"
echo "Publishing image to Vagrant Cloud..."
echo "---"
echo ""
if [ "${DRY}" -eq "0" ];then
vagrant cloud \
publish \
--version-description "Based on the image \`${VERSION}\`.\n From ${VDI_LINK}" \
--force \
--release \
Eldius/linux-amzn2 \
"$VERSION" \
virtualbox \
build/amazonlinux2.box
else
echo "---"
echo "Dry run. Not publishing..."
echo "---"
fi
echo ""
echo "---"
echo "Box published at Vagrant Cloud."
echo "---"
echo ""
}
function test_error {
echo "---"
echo "Ooops! Something went wrong here... Take a look!"
echo "---"
exit 1
}
function test_generated_box {
echo ""
echo "---"
echo "Starting a test instance..."
echo "---"
echo ""
if [ "${TEST}" -eq "1" ];then
cd test
vagrant destroy -f && \
vagrant up --debug && \
echo "Successfully tested the new Box" || \
test_error
fi
echo ""
echo "---"
echo "Box published at Vagrant Cloud."
echo "---"
echo ""
}
function package_box {
echo ""
echo "---"
echo "Packaging box..."
echo "---"
echo ""
vagrant package --base $VM --output build/amazonlinux2.box
echo ""
echo "---"
echo "Box package finished"
echo "---"
echo ""
}
function import_local {
if [ "${LOCAL}" -eq "1" ];then
echo ""
echo "---"
echo "Importing box to local repository..."
echo "---"
echo ""
vagrant box add Eldius/linux-amzn2 build/amazonlinux2.box --force
echo ""
echo "---"
echo "Finished box import to local repository."
echo "---"
echo ""
fi
}
function test_vbox_guest_aditions {
wait_boot_finishes
TEST_RESULT=$( ssh \
-i build/insecure_key vagrant@localhost \
-p 9999 \
-o "StrictHostKeyChecking no" \
-o "UserKnownHostsFile=/dev/null" \
-o "ConnectTimeout=3" \
echo " --\> test result: \$( sudo lsmod | grep vboxguest ) \<-- "
)
echo "----------------"
echo "[AGAIN] TESTING VBOX GUEST ADDITIONS PRESENCE"
echo "test result: ${TEST_RESULT}"
echo "----------------"
}
function get_cloudinit_log {
sftp -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" << SFTP
get /var/log/cloud-init*
SFTP
}
function clean_image {
ssh -i build/insecure_key vagrant@localhost -p 9999 -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -o "ConnectTimeout=3" << SSH
#sudo yum clean all
sudo rm -rfv /var/cache/yum
sudo rm /home/vagrant/state_file
cat /dev/null > ~/.bash_history && history -c
SSH
}
function debug_virtual_machine {
if [ "${DEBUG}" -eq "1" ];then
echo "---"
echo "## ENTERING DEBUG MODE ###"
echo "---"
ssh \
-i build/insecure_key \
vagrant@localhost \
-p 9999 \
-o "StrictHostKeyChecking no" \
-o "UserKnownHostsFile=/dev/null" \
-o "ConnectTimeout=3"
echo "## EXITING DEBUG MODE ###"
fi
}
clean
get_base_vdi
generate_seed
create_machine
VBoxManage startvm $VM $HEADLESS
#vboxmanage showvminfo $VM
sleep 60
wait_first_setup_finishes
setup_guest_adition
debug_virtual_machine
#read -p "Press enter to continue"
echo ""
echo "####################"
echo "####################"
echo "## FINISHED! ###"
echo "####################"
echo "####################"
echo ""
notify-send --urgency=low "hey you!" "Finished VM creation..."
#get_cloudinit_log
clean_image
package_box
import_local
test_generated_box
publish_version
clean
notify-send --urgency=low "hey you!" "Finished Vagrant Box creation."