-
Notifications
You must be signed in to change notification settings - Fork 17
/
metroae
executable file
·1488 lines (1343 loc) · 45.9 KB
/
metroae
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -e
METROAE_VERSION="5.5.0"
SCRIPT_VERSION="1.5.2"
MENU=()
#################################################################################
# CONTAINER COMMANDS #
#################################################################################
MENU+=(',container' 'Manage the Old MetroAE container' 'container' '' '')
MENU+=(',container,destroy' 'Destroy the Old MetroAE container' 'container' 'destroy' ',container,destroy')
#################################################################################
# VARIABLES #
#################################################################################
# Switch
ANSIBLE_CONFIG_FILE="ansible.cfg"
CLI_ARGS=""
# Menu
MENU_STRIDE=5 # Menu steps
MENU_KEY=""
SUB_MENU=""
MATCH_MENU=""
EXTRA_ARGS=()
PLAYBOOK_MENU="menu"
# Common
LINUX=0
NON_LINUX=1
FALSE=0
TRUE=1
CONTAINER_ID=''
RUNNING_CONTAINER_ID=''
IMAGE_ID=''
if [[ -z $MAX_CONTAINER_VERSION ]]; then
MAX_CONTAINER_VERSION='current'
fi
DOCKER_REPO="nuagenetworks"
METRO_AE_IMAGE="$DOCKER_REPO/metroae"
if [[ ! -z $IMAGE_NAME ]]; then
METRO_AE_IMAGE="$DOCKER_REPO/$IMAGE_NAME"
fi
OS_RELEASE=$LINUX
SETUP_USER_DATA_PATH=""
ENVIRONMENT_FILTERS=(PWD PATH HOME USER SHELL MAIL SSH_CONNECTION LOGNAME OLDPWD LESSOPEN _ XDG_RUNTIME_DIR HISTCONTROL)
CONTAINER_TAR_FILE=metroaecontainer.tar
if [[ ! -z $IMAGE_NAME ]]; then
CONTAINER_TAR_FILE="$IMAGE_NAME.tar"
fi
CONTAINER_HOME=/source
UUID_FILE=.uuid
if [[ -z $UUID ]]; then
UUID=""
fi
if [[ -z $SKIP_UUID ]]; then
SKIP_UUID=$FALSE
fi
PHONE_HOME_URL="https://nuagemetroae-487c.restdb.io/rest/usage"
PHONE_HOME_API_KEY="ce4d6cfa136a9cf10b46d92fd613eb8c50f4d"
CONTAINER_MOUNT_POINT=/metroae_data
INSTALL_FOLDER=/opt/metroae
if [[ -z $SCRIPT_LOG_FILE ]]; then
SCRIPT_LOG_FILE=/opt/metroae/metroae.log
fi
if [[ -z $SETUP_FILE ]]; then
SETUP_FILE=/opt/metroae/.metroae
fi
TAB_COMPLETION_SCRIPT=tab-completion-metroae.sh
COMPLETION_DIR=/etc/bash_completion.d/
CURRENT_DIR=`pwd`
PLAYBOOK_DIR=$CURRENT_DIR/src/playbooks
PLAYBOOK_WITH_BUILD_DIR=$CURRENT_DIR/src/playbooks/with_build
ROLES_DIR=$CURRENT_DIR/src/roles
SCHEMA_DIR=$CURRENT_DIR/schemas
DEPLOYMENTS_BASE_DIR=$CURRENT_DIR/deployments
DEPLOYMENT_DIR=$DEPLOYMENTS_BASE_DIR/default
INVENTORY_DIR=$CURRENT_DIR/src/inventory
PLUGIN_DIR=$CURRENT_DIR/src/metro_plugins
VAULT_ENV_FILE=$CURRENT_DIR/src/vault-env
ENCRYPTED_TOKEN=\$ANSIBLE_VAULT
LOGS_DIRECTORY=logs
ANSIBLE_LOG=$LOGS_DIRECTORY/ansible.log
AUDIT_LOG=$LOGS_DIRECTORY/audit.log
ORIGINAL_ARGS=""
SKIP_BUILD=0
SKIP_PASSWORD=0
POSITIONAL=()
CONTAINER_RUN_MODE="CONTAINER"
REPO_RUN_MODE="REPO"
INSIDE_RUN_MODE="INSIDE"
CONFIG_SETUP_TYPE="c"
DEPLOY_SETUP_TYPE="d"
BOTH_SETUP_TYPE="b"
CONTAINER_STOPPED=0
CONTAINER_DELETED=0
TEMPLATE_TAR_LOCATION="https://metroae-config-templates.s3.amazonaws.com/metroae_config.tar"
VSD_SPECIFICATIONS_LOCATION="https://vsd-api-specifications.s3.us-east-2.amazonaws.com/specifications.tar"
TEMPLATE_DIR="standard-templates"
SPECIFICATION_DIR="vsd-api-specifications"
function print_write_permission_warning {
debug ${FUNCNAME[0]}
echo ""
echo "WARNING: This user does not have Write permissions for MetroAE log file: $SCRIPT_LOG_FILE"
echo "Ensure that the MetroAE install directory $INSTALL_FOLDER and its contents belongs to"
echo "group 'docker' and that group write permissions are set. Continuing..."
echo ""
}
function debug {
if [[ ! -z $METROAE_DEBUG ]]; then
write_to_screen_and_script_log " DEBUG: $1"
fi
}
function write_to_script_log {
if [[ -f $SCRIPT_LOG_FILE ]]; then
if [ -w "$SCRIPT_LOG_FILE" ]; then
echo "$(date): $1" >> $SCRIPT_LOG_FILE
else
if [ -z $METROAE_WRITE_WARNING_PRINTED ]; then
print_write_permission_warning
METROAE_WRITE_WARNING_PRINTED=1
fi
fi
fi
}
function write_to_screen_and_script_log {
echo "$1"
write_to_script_log "$1"
}
function check_run_mode {
debug ${FUNCNAME[0]}
if [[ -z $RUN_MODE ]]; then
if [[ -f $ANSIBLE_CONFIG_FILE ]]; then
RUN_MODE=$REPO_RUN_MODE
else
RUN_MODE=$CONTAINER_RUN_MODE
fi
fi
debug "${FUNCNAME[0]}: RUN_MODE is $RUN_MODE"
}
function phone_home {
if [[ $UUID == "" ]]; then
debug "${FUNCNAME[0]}: Skipping Phone home"
else
# append and create json data
json_data=""
while [[ $# -gt 0 ]]; do
json_data+="$1,"
shift
done
json_data=`echo $json_data | sed 's/.$//'`
debug "${FUNCNAME[0]}: curl -vvvv -k -H \"Content-Type: application/json\"\
-H \"x-apikey: $PHONE_HOME_API_KEY\"\
-X POST -d '{$json_data}'\
$PHONE_HOME_URL"
curl -vvvv -k -H "Content-Type: application/json"\
-H "x-apikey: $PHONE_HOME_API_KEY"\
-X POST -d "{$json_data}"\
$PHONE_HOME_URL >> /dev/null 2>&1 &
fi
}
function check_if_internal_user {
debug ${FUNCNAME[0]}
if [[ $SKIP_UUID -eq $TRUE ]]; then
debug "${FUNCNAME[0]}: Using existing UUID $UUID"
else
set +e
int_user_check=`ping -c 5 github.mv.usa.alcatel.com 2>&1`
SKIP_UUID=$?
set -e
debug "${FUNCNAME[0]} $int_user_check"
if [[ $SKIP_UUID -eq $FALSE ]]; then
echo ""
write_to_screen_and_script_log "We have detected that this setup of MetroAE is internal to Nokia."
write_to_screen_and_script_log "We would like to collect usage information to improve the tool,"
write_to_screen_and_script_log "no personally identifiable information will be collected as part of this process."
if [[ $RUN_MODE == $REPO ]]; then
write_to_screen_and_script_log "Just remove file src/$UUID_FILE to opt out later"
else
write_to_screen_and_script_log "Just remove file $METROAE_MOUNT_POINT/$UUID_FILE to opt out later"
fi
echo ""
declare -l continue_confirm
if [[ -z SKIP_PROMPTS ]]
then
continue_confirm="init"
else
continue_confirm=$SKIP_PROMPTS
fi
while [[ $continue_confirm != "y" ]] && [[ $continue_confirm != "n" ]] && [[ $continue_confirm != "" ]]
do
read -p " Would like to share usage data with MetroAE team? (y/N): " continue_confirm
done
if [[ $continue_confirm != "y" ]] ; then
SKIP_UUID=$TRUE
fi
else
SKIP_UUID=$TRUE
fi
fi
}
function generate_UUID {
debug ${FUNCNAME[0]}
UUID=`uuidgen`
debug "${FUNCNAME[0]}: UUID=$UUID"
}
function generate_and_save_UUID {
debug ${FUNCNAME[0]}
if [[ $SKIP_UUID -eq $TRUE ]]; then
debug "${FUNCNAME[0]}: Skipping UUID generation"
else
check_if_internal_user
debug "${FUNCNAME[0]}: RUN_MODE = $RUN_MODE"
if [[ $RUN_MODE == $INSIDE_RUN_MODE ]]; then
debug "${FUNCNAME[0]}: Creating $CONTAINER_MOUNT_POINT/$UUID_FILE"
touch $CONTAINER_MOUNT_POINT/$UUID_FILE
sudo chown root:docker $CONTAINER_MOUNT_POINT/$UUID_FILE
if [[ $SKIP_UUID -eq $FALSE ]]; then
generate_UUID
echo UUID=$UUID > $CONTAINER_MOUNT_POINT/$UUID_FILE
SKIP_UUID=$TRUE
fi
debug "${FUNCNAME[0]}: SKIP_UUID = $SKIP_UUID"
echo SKIP_UUID=$SKIP_UUID >> $CONTAINER_MOUNT_POINT/$UUID_FILE
elif [[ $RUN_MODE == $REPO_RUN_MODE ]]; then
debug "${FUNCNAME[0]}: Creating src/$UUID_FILE"
touch src/$UUID_FILE
if [[ $SKIP_UUID -eq $FALSE ]]; then
generate_UUID
echo UUID=$UUID > src/$UUID_FILE
SKIP_UUID=$TRUE
fi
debug "${FUNCNAME[0]}: SKIP_UUID = $SKIP_UUID"
echo SKIP_UUID=$SKIP_UUID >> src/$UUID_FILE
else # RUN_MODE == $CONTAINER_RUN_MODE
debug "${FUNCNAME[0]}: Creating $METROAE_MOUNT_POINT/$UUID_FILE"
touch $METROAE_MOUNT_POINT/$UUID_FILE
sudo chown root:docker $METROAE_MOUNT_POINT/$UUID_FILE
if [[ $SKIP_UUID -eq $FALSE ]]; then
generate_UUID
echo UUID=$UUID > $METROAE_MOUNT_POINT/$UUID_FILE
SKIP_UUID=$TRUE
fi
debug "${FUNCNAME[0]}: SKIP_UUID = $SKIP_UUID"
echo SKIP_UUID=$SKIP_UUID >> $METROAE_MOUNT_POINT/$UUID_FILE
fi
fi
}
function source_UUID {
debug ${FUNCNAME[0]}
debug "${FUNCNAME[0]}: RUN_MODE = $RUN_MODE"
if [[ $RUN_MODE == $INSIDE_RUN_MODE ]]; then
if [[ -f $CONTAINER_MOUNT_POINT/$UUID_FILE ]]; then
source $CONTAINER_MOUNT_POINT/$UUID_FILE
debug "${FUNCNAME[0]}: source $CONTAINER_MOUNT_POINT/$UUID_FILE"
fi
elif [[ $RUN_MODE == $REPO_RUN_MODE ]]; then
if [[ -f src/$UUID_FILE ]]; then
source src/$UUID_FILE
debug "${FUNCNAME[0]}: source src/$UUID_FILE"
fi
else # RUN_MODE == $CONTAINER_RUN_MODE
if [[ -f $METROAE_MOUNT_POINT/$UUID_FILE ]]; then
source $METROAE_MOUNT_POINT/$UUID_FILE
debug "${FUNCNAME[0]}: source $METROAE_MOUNT_POINT/$UUID_FILE"
fi
fi
}
function source_menu {
debug ${FUNCNAME[0]}
debug "${FUNCNAME[0]}: RUN_MODE = $RUN_MODE"
if [[ $RUN_MODE == $INSIDE_RUN_MODE ]]; then
if [[ -f $CONTAINER_HOME/nuage-metroae/src/$PLAYBOOK_MENU ]]; then
source $CONTAINER_HOME/nuage-metroae/src/$PLAYBOOK_MENU
debug "${FUNCNAME[0]}: source $CONTAINER_HOME/nuage-metroae/src/$PLAYBOOK_MENU"
fi
if ls /source/nuage-metroae/src/metro_plugins/* 1> /dev/null 2>&1; then
source_plugin_menus /source/nuage-metroae/src/metro_plugins
fi
elif [[ $RUN_MODE == $REPO_RUN_MODE ]]; then
if [[ -f src/$PLAYBOOK_MENU ]]; then
source src/$PLAYBOOK_MENU
debug "${FUNCNAME[0]}: source src/$PLAYBOOK_MENU"
fi
if ls src/metro_plugins/* 1> /dev/null 2>&1; then
source_plugin_menus src/metro_plugins
fi
else # RUN_MODE == $CONTAINER_RUN_MODE
if [[ -f $METROAE_MOUNT_POINT/$PLAYBOOK_MENU ]]; then
source $METROAE_MOUNT_POINT/$PLAYBOOK_MENU
debug "${FUNCNAME[0]}: source $METROAE_MOUNT_POINT/$PLAYBOOK_MENU"
fi
if ls $METROAE_MOUNT_POINT/metro_plugins/* 1> /dev/null 2>&1; then
source_plugin_menus $METROAE_MOUNT_POINT/metro_plugins
fi
fi
}
function source_plugin_menus {
debug ${FUNCNAME[0]}
plugin_dir=$1
if [[ ! -d $plugin_dir ]]; then
write_to_screen_and_script_log "Could not find plugin directory $plugin_dir"
print_version_and_exit 1
fi
for plugin in $plugin_dir/*
do
menu_file=$plugin/menu
if [[ -f $menu_file ]]; then
source $menu_file
debug "${FUNCNAME[0]}: source $menu_file"
else
write_to_screen_and_script_log "Could not find required menu file for plugin $plugin"
print_version_and_exit 1
fi
done
}
function source_container_config {
debug ${FUNCNAME[0]}
if [[ -f $SETUP_FILE ]]; then
source $SETUP_FILE
else
METROAE_SETUP_TYPE=$DEPLOY_SETUP_TYPE
METROAE_MOUNT_POINT="./"
fi
debug "${FUNCNAME[0]}: METROAE_SETUP_TYPE = $METROAE_SETUP_TYPE"
debug "${FUNCNAME[0]}: METROAE_MOUNT_POINT = $METROAE_MOUNT_POINT"
}
function check_for_prerequisite {
debug ${FUNCNAME[0]}
debug "${FUNCNAME[0]}: RUN_MODE = $RUN_MODE"
if [[ $RUN_MODE == $INSIDE_RUN_MODE ]]; then
if [[ ! -f $CONTAINER_HOME/nuage-metroae/src/$PLAYBOOK_MENU ]]; then
write_to_screen_and_script_log "It looks like you are running MetroAE from inside the container, but we couldn't find the container's menu file in the data directory. Please run 'metroae container setup' and try again."
exit 1
fi
elif [[ $RUN_MODE == $REPO_RUN_MODE ]]; then
if [[ ! -f src/$PLAYBOOK_MENU ]]; then
write_to_screen_and_script_log "It looks like you are trying to run using a clone of MetroAE, but we couldn't find the menu file in the workspace. Please update your workspace and try again."
exit 1
fi
fi
}
function print_version_and_exit {
debug ${FUNCNAME[0]}
if [[ $1 == 0 ]]; then
echo ""
if [[ $RUN_MODE == $CONTAINER_RUN_MODE ]]; then
get_max_container_version
debug "${FUNCNAME[0]}: [MetroAE $METROAE_VERSION, script $SCRIPT_VERSION, container $MAX_CONTAINER_VERSION]"
elif [[ $RUN_MODE == $REPO_RUN_MODE ]]; then
if [[ ! ${MATCH_MENU[0]} =~ ",container*" ]] && [[ ! ${MATCH_MENU[0]} =~ ",config*" ]]; then
debug "${FUNCNAME[0]}: [MetroAE $METROAE_VERSION, script $SCRIPT_VERSION]"
fi
fi
fi
exit $1
}
#################################################################################
# COMMON #
#################################################################################
function get_max_container_version {
debug ${FUNCNAME[0]}
if [[ ! -z $IMAGE_TAG ]]; then
MAX_CONTAINER_VERSION=$IMAGE_TAG
else
versions=`docker images 2>/dev/null | grep $METRO_AE_IMAGE | awk '{ print $2}'`
debug "${FUNCNAME[0]}: versions = $versions"
MAX_CONTAINER_VERSION=''
for version in $versions
do
if [[ $version != "<none>" ]]; then
debug "${FUNCNAME[0]}: version = $version"
if [[ -z $MAX_CONTAINER_VERSION ]]; then
MAX_CONTAINER_VERSION=$version
fi
if [[ $MAX_CONTAINER_VERSION < $version ]]; then
MAX_CONTAINER_VERSION=$version
fi
fi
debug "${FUNCNAME[0]}: MAX_CONTAINER_VERSION = $MAX_CONTAINER_VERSION"
done
if [[ -z $MAX_CONTAINER_VERSION ]]; then
MAX_CONTAINER_VERSION='current'
fi
fi
debug "${FUNCNAME[0]}: Newest MetroAE container version found is $MAX_CONTAINER_VERSION"
}
function get_container_id {
debug ${FUNCNAME[0]}
CONTAINER_ID=`docker ps -a 2>/dev/null | grep $METRO_AE_IMAGE | awk '{ print $1}'`
debug "${FUNCNAME[0]}: CONTAINER_ID: $CONTAINER_ID"
}
function get_running_container_id {
debug ${FUNCNAME[0]}
RUNNING_CONTAINER_ID=`docker ps 2>/dev/null | grep $METRO_AE_IMAGE | awk '{ print $1}'`
debug "${FUNCNAME[0]}: RUNNING_CONTAINER_ID: $RUNNING_CONTAINER_ID"
}
function get_image_id {
debug ${FUNCNAME[0]}
if [[ -z $IMAGE_ID ]]; then
get_max_container_version
IMAGE_ID=`docker images 2>/dev/null | grep $METRO_AE_IMAGE | grep $MAX_CONTAINER_VERSION | awk '{ print $3}'`
fi
debug "${FUNCNAME[0]}: IMAGE_ID: $IMAGE_ID"
}
function stop_running_container {
debug ${FUNCNAME[0]}
echo ""
write_to_screen_and_script_log ">>> Stopping the MetroAE container"
echo ""
get_running_container_id
if [[ -z $RUNNING_CONTAINER_ID ]]; then
write_to_screen_and_script_log "The MetroAE container is not running. Nothing to do."
return 0
fi
set +e
docker stop $RUNNING_CONTAINER_ID
status=$?
if [[ $status -ne 0 ]]; then
write_to_screen_and_script_log "Attempt to stop MetroAE container failed"
else
write_to_screen_and_script_log "The MetroAE container was stopped"
CONTAINER_STOPPED=1
fi
set -e
return $status
}
function docker_exec_config {
debug ${FUNCNAME[0]}
docker_exec env /usr/bin/python3 $CONTAINER_HOME/nuage-metroae-config/metroae_config.py "$@"
}
function delete_container_id {
debug ${FUNCNAME[0]}
echo ""
write_to_screen_and_script_log ">>> Deleting the MetroAE container"
echo ""
get_container_id
if [[ -z $CONTAINER_ID ]]; then
write_to_screen_and_script_log "MetroAE container not found. Nothing to do."
return 0
fi
debug "${FUNCNAME[0]}: Execute 'docker rm' on the MetroAE container with id $CONTAINER_ID"
set +e
if [[ -f $SCRIPT_LOG_FILE ]]; then
if [ -w "$SCRIPT_LOG_FILE" ]; then
docker rm -v $CONTAINER_ID | tee -a $SCRIPT_LOG_FILE
else
if [ -z $METROAE_WRITE_WARNING_PRINTED ]; then
print_write_permission_warning
METROAE_WRITE_WARNING_PRINTED=1
fi
docker rm -v $CONTAINER_ID
fi
else
docker rm -v $CONTAINER_ID
fi
if [[ $? -ne 0 ]]; then
write_to_screen_and_script_log "Attempt to execute 'docker rm' on the MetroAE container failed"
return 1
fi
set -e
write_to_screen_and_script_log "Execute of 'docker rm' on the MetroAE container was successful"
CONTAINER_DELETED=1
}
function destroy {
debug ${FUNCNAME[0]}
declare -l confirmation
if [[ -z $1 ]]; then
confirmation="init"
echo ""
write_to_screen_and_script_log "It looks like you are about to destroy the MetroAE container."
echo "If you continue, the MetroAE container will be stopped, the MetroAE container image will be"
echo "removed from Docker, and your data on disk will be preserved. Your data will"
echo "not be destroyed. You will be able to use it again by pulling another MetroAE"
echo "container and running 'metroae container setup'."
while [[ $confirmation != "n" ]] && [[ $confirmation != "y" ]] && [[ $confirmation != "" ]]
do
echo ""
read -p "Do you really want to destroy the MetroAE container? (y/N): " confirmation
done
else
confirmation=$1
fi
if [[ $confirmation != "y" ]]; then
echo ""
write_to_screen_and_script_log "Destroy of metroae container was canceled"
echo ""
print_version_and_exit 0
fi
set +e
container_image=`docker ps 2>/dev/null | grep $METRO_AE_IMAGE | awk '{ print $2 }'`
IMAGE_TAG=${container_image##*:}
stop_running_container
if [[ $? -ne 0 ]]; then
return 1
fi
delete_container_id
if [[ $? -ne 0 ]]; then
return 1
fi
set -e
echo ""
write_to_screen_and_script_log ">>> Removing MetroAE image"
echo ""
get_image_id
if [[ -z $IMAGE_ID ]]; then
write_to_screen_and_script_log "MetroAE container image not found. Nothing to do."
return 0
fi
set +e
if [[ "$CONTAINER_STOPPED" -eq "1" ]] && [[ "$CONTAINER_DELETED" -eq "1" ]]; then
docker rmi -f $IMAGE_ID
if [[ $? -ne 0 ]]; then
echo ""
write_to_screen_and_script_log "Attempt to remove the MetroAE image failed"
echo ""
return 1
fi
set -e
echo ""
write_to_screen_and_script_log "The MetroAE container image was removed"
echo ""
return 0
fi
echo ""
write_to_screen_and_script_log "No container stopped and removed. No MetroAE container image needs to be removed"
echo ""
}
#################################################################################
# DEPLOYMENT #
#################################################################################
function list_workflows {
debug ${FUNCNAME[0]}
for file in $PLAYBOOK_DIR/*.yml
do
if [[ -f $file ]]; then
filename=$(basename "$file")
filename="${filename%.*}"
echo $filename
fi
done
for file in $PLAYBOOK_WITH_BUILD_DIR/*.yml
do
if [[ -f $file ]]; then
filename=$(basename "$file")
filename="${filename%.*}"
echo $filename
fi
done
}
function check_password_needed {
debug ${FUNCNAME[0]}
deployment_dir="$1"
encrypted_files=$(grep -Ril $ENCRYPTED_TOKEN $deployment_dir)
if [[ -z $METROAE_PASSWORD ]]; then
if [[ -z "$encrypted_files" ]]; then
SKIP_PASSWORD=1
else
SKIP_PASSWORD=0
fi
else
SKIP_PASSWORD=1
fi
}
function ask_password {
debug ${FUNCNAME[0]}
if [[ $SKIP_PASSWORD -ne 1 ]]; then
write_to_screen_and_script_log "The deployment contains encrypted content which requires a password to access."
echo "Enter the password, below, or add the environment variable METROAE_PASSWORD and retry."
echo ""
read -s -p "Enter password: " METROAE_PASSWORD
export METROAE_PASSWORD
fi
}
function write_audit_log_entry {
debug ${FUNCNAME[0]}
echo "`date` MetroAE $METROAE_VERSION $ORIGINAL_ARGS" >> $AUDIT_LOG
}
function audit_log_and_exit {
debug ${FUNCNAME[0]}
echo "`date` MetroAE $METROAE_VERSION exit code $1" >> $AUDIT_LOG
print_version_and_exit $1
}
function create_log_files {
debug ${FUNCNAME[0]}
mkdir $LOGS_DIRECTORY
touch $ANSIBLE_LOG
touch $AUDIT_LOG
}
function setup_log_files {
debug ${FUNCNAME[0]}
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
ln -s $ANSIBLE_LOG ansible.log.$WORKFLOW.$DEPLOYMENT.$current_time
}
function deployment_main {
generate_and_save_UUID
debug ${FUNCNAME[0]}
set +e
ORIGINAL_ARGS="$*"
create_log_files
#
# Parse arguments
#
SKIP_BUILD=0
SKIP_PASSWORD=0
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
main_help
print_version_and_exit 0
;;
-v|--version)
help_header
print_version_and_exit 0
;;
--ansible-help)
$(which ansible-playbook) --help
print_version_and_exit 0
;;
--list)
list_workflows
print_version_and_exit 0
;;
--set-group)
GROUP="$2"
chgrp $GROUP $ANSIBLE_LOG
chgrp $GROUP $AUDIT_LOG
shift # past argument
shift # past value
;;
--skip-build)
SKIP_BUILD=1
shift # past argument
;;
--skip-password)
SKIP_PASSWORD=1
shift # past argument
;;
destroy)
shift
if [[ -z $2 ]]; then
destroy
else
destroy $2
shift
fi
shift
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
# Missing workflow, show usage
if [[ $# -eq 0 ]] || [[ $1 == -* ]]; then
main_help
print_version_and_exit 1
fi
# <workflow> argument
WORKFLOW=$1
debug "${FUNCNAME[0]}: WORKFLOW = @WORKFLOW"
# Add .yml extension if needed
EXTENSION="${WORKFLOW##*.}"
if [[ "$EXTENSION" != "yml" ]]; then
WORKFLOW=${WORKFLOW}.yml
fi
if [[ ! -a $PLAYBOOK_DIR/$WORKFLOW ]] && [[ ! -a $PLAYBOOK_WITH_BUILD_DIR/$WORKFLOW ]]; then
write_to_screen_and_script_log "Requested MetroAE workflow ($1) could not be found"
print_version_and_exit 1
fi
shift
# [deployment] argument
if [[ $# -gt 0 ]]; then
if [[ $1 != -* ]]; then
DEPLOYMENT="$1"
shift
if [[ $DEPLOYMENT == *.csv ]]; then
filename=$(basename -- "$DEPLOYMENT")
deployment_name="${filename%.*}"
DEPLOYMENT_DIR="$DEPLOYMENTS_BASE_DIR/$deployment_name"
rm -f $DEPLOYMENT_DIR/*.yml
./convert_csv_to_deployment.py $DEPLOYMENT $deployment_name
elif [[ $DEPLOYMENT == *.xlsx ]]; then
filename=$(basename -- "$DEPLOYMENT")
deployment_name="${filename%.*}"
DEPLOYMENT_DIR="$DEPLOYMENTS_BASE_DIR/$deployment_name"
rm -f $DEPLOYMENT_DIR/*.yml
./convert_excel_to_deployment.py $DEPLOYMENT $deployment_name
elif [[ -d $DEPLOYMENT ]]; then
DEPLOYMENT_DIR=$DEPLOYMENT
elif [[ -d $DEPLOYMENTS_BASE_DIR/$DEPLOYMENT ]]; then
DEPLOYMENT_DIR=$DEPLOYMENTS_BASE_DIR/$DEPLOYMENT
else
write_to_screen_and_script_log "Could not find deployment '$DEPLOYMENT' under $DEPLOYMENTS_BASE_DIR"
print_version_and_exit 1
fi
fi
fi
setup_log_files $WORKFLOW $DEPLOYMENT
# Get password if needed
check_password_needed "$DEPLOYMENT_DIR"
ask_password
if [[ ! -z $METROAE_PASSWORD ]]; then
export ANSIBLE_VAULT_PASSWORD_FILE=$VAULT_ENV_FILE
fi
# Run playbooks
if [[ -a $PLAYBOOK_DIR/$WORKFLOW ]]; then
write_audit_log_entry
$(which ansible-playbook) -e deployment_dir=\'"$DEPLOYMENT_DIR"\' -e schema_dir=$SCHEMA_DIR $PLAYBOOK_DIR/$WORKFLOW "$@" || audit_log_and_exit $?
elif [[ -a $PLAYBOOK_WITH_BUILD_DIR/$WORKFLOW ]]; then
write_audit_log_entry
phone_home_args=("\"UUID\":\"${UUID}\"" "\"Actions\":\"${WORKFLOW}\"" "\"Mode\":\"${RUN_MODE}\"" "\"timestamp\":\"$(date)\"" "\"Version\":\"${METROAE_VERSION}\"")
phone_home "${phone_home_args[@]}"
if [[ $SKIP_BUILD -ne 1 ]]; then
$(which ansible-playbook) -e deployment_dir=\'"$DEPLOYMENT_DIR"\' -e schema_dir=$SCHEMA_DIR $PLAYBOOK_DIR/build.yml "$@" || audit_log_and_exit $?
if [[ $GROUP ]]; then chgrp -R $GROUP $INVENTORY_DIR; fi
fi
$(which ansible-playbook) $PLAYBOOK_WITH_BUILD_DIR/$WORKFLOW "$@" || audit_log_and_exit $?
else
write_to_screen_and_script_log "Requested MetroAE workflow ($WORKFLOW) could not be found"
print_version_and_exit 1
fi
audit_log_and_exit 0
set -e
}
#################################################################################
# Plugins #
#################################################################################
function version_compare {
debug ${FUNCNAME[0]}
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
function get_plugin_name {
debug ${FUNCNAME[0]}
set +e
local plugin_line=`grep plugin_name $1/plugin-cfg.yml`
if [[ $? -ne 0 ]]
then
write_to_screen_and_script_log "Plugin missing or corrupt plugin-cfg.yml"
print_version_and_exit 1
fi
plugin_name=`echo $plugin_line | awk '{ print $2 }'`
set -e
}
function check_plugin_version {
debug ${FUNCNAME[0]}
set +e
local plugin_line=`grep required_metro_version $1/plugin-cfg.yml`
if [[ $? -ne 0 ]]
then
write_to_screen_and_script_log "Plugin missing or corrupt plugin-cfg.yml"
print_version_and_exit 1
fi
local plugin_version=`echo $plugin_line | awk '{ print $2 }'`
version_compare $plugin_version ${METROAE_VERSION#"v"}
if [[ $? -eq 1 ]]
then
write_to_screen_and_script_log "Plugin requires MetroAE version $plugin_version"
print_version_and_exit 1
fi
set -e
}
function check_plugin_files {
debug ${FUNCNAME[0]}
for file in $1/*
do
#echo $file
if [[ -d $file ]]
then
check_plugin_files $file $2 $3
elif [[ -f $file ]]
then
local base_file=${file#$2/}
if [[ -f $3/$base_file ]]
then
write_to_screen_and_script_log "Plugin file $base_file conflicts with existing files"
print_version_and_exit 1
fi
fi
done
}
function check_single_plugin_file {
debug ${FUNCNAME[0]}
if [[ -f $3/$1 ]]
then
write_to_screen_and_script_log "Plugin file $1 conflicts with existing files"
print_version_and_exit 1
fi
if [[ ! -f $2/$1 ]]
then
write_to_screen_and_script_log "Plugin file $1 is missing"
print_version_and_exit 1
fi
}
function setup_plugin {
debug ${FUNCNAME[0]}
write_to_script_log "Writing $1/uninstall_files"
mkdir -p $1
touch $1/uninstall_files
uninstall_file=$1/uninstall_files
}
function copy_plugin_files {
debug ${FUNCNAME[0]}
for file in $1/*
do
if [[ -d $file ]]
then
copy_plugin_files $file $2 $3
elif [[ -f $file ]]
then
local base_file=${file#$2/}
write_to_script_log "Copying $file -> $3/$base_file"
local dir=`dirname $3/$base_file`
mkdir -p $dir
cp $file $3/$base_file
echo "UNINSTALL_FILES+=($3/$base_file)" >> $uninstall_file
fi
done
}
function copy_single_plugin_file {
debug ${FUNCNAME[0]}
write_to_script_log "Copying $2/$1 -> $3/$1"
mkdir -p $3
cp $2/$1 $3/$1
echo "UNINSTALL_FILES+=($3/$1)" >> $uninstall_file
}
function delete_plugin_files {
debug ${FUNCNAME[0]}
if [[ ! -f $PLUGIN_DIR/$1/uninstall_files ]]
then
echo "Plugin $1 uninstall_files missing"
print_version_and_exit 1
fi
UNINSTALL_FILES=()
source $PLUGIN_DIR/$1/uninstall_files
for file in ${UNINSTALL_FILES[@]}
do
delete_single_plugin_file $file
done
}
function delete_single_plugin_file {
debug ${FUNCNAME[0]}
write_to_script_log "Deleting $1"
if [[ -f $1 ]]
then
rm -f $1
local dir=`dirname $1`
set +e
rmdir $dir 2> /dev/null
set -e
else
write_to_script_log "Not found: $1"
fi
}
function install_plugin {
debug ${FUNCNAME[0]}
if [[ $1 == *.tar.gz ]]
then
untar_plugin $1
install_plugin_dir $PLUGIN_INSTALL_DIR
write_to_screen_and_script_log "Cleaning up: $PLUGIN_INSTALL_DIR"
rm -rf install_plugin/
else
install_plugin_dir $1
fi