-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.tmpl
141 lines (125 loc) · 3.46 KB
/
build.tmpl
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
#!/usr/bin/env bash
# Copyright 2023-2024 Broadcom. All Rights Reserved.
# SPDX-License-Identifier: BSD-2
set -e
follow_link() {
FILE="$1"
while [ -h "$FILE" ]; do
# On Mac OS, readlink -f doesn't work.
FILE="$(readlink "$FILE")"
done
echo "$FILE"
}
if [ "$1" == "--debug" ] || [ "$1" == "-d" ]; then
debug_option="-debug"
shift
else
debug_option=""
fi
SCRIPT_PATH=$(realpath)
CONFIG_PATH="${SCRIPT_PATH}"/config
menu_message="Select a HashiCorp Packer build for VMware vSphere."
if [ "$debug_mode" = true ]; then
menu_message+=" \e[31m(Debug Mode)\e[0m"
fi
{{- $func_index := 0 }}
{{ range $menu := (ds "build").menu -}}
{{ range $submenu := $menu.submenu -}}
{{ $func_index = math.Add $func_index 1 }}
menu_option_{{ $func_index }}() {
INPUT_PATH="$SCRIPT_PATH"/{{ $submenu.build.path }}
{{ $message := "" -}}
{{- if (coll.Has $submenu "message") -}}
{{- $message = $submenu.message -}}
{{- else -}}
{{- $message = printf "a %s Template" $submenu.entry -}}
{{- end -}}
echo -e "\nCONFIRM: Build {{ $message }} for VMware vSphere?"
echo -e "\nContinue? (y/n)"
read -r REPLY
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
### Build {{ $message }} for VMware vSphere. ###
echo "Building {{ $message }} for VMware vSphere..."
### Initialize HashiCorp Packer and required plugins. ###
echo "Initializing HashiCorp Packer and required plugins..."
packer init "$INPUT_PATH"
### Start the Build. ###
echo "Starting the build...."
echo "packer build -force -on-error=ask $debug_option"
PACKER_LOG=1 packer build -force -on-error=ask $debug_option \
{{- if (coll.Has $submenu.build "only") }}
--only {{ join $submenu.build.only "," }} \
{{- end -}}
{{- if (coll.Has $menu.build "var_files") -}}
{{ range $menu.build.var_files }}
-var-file="$CONFIG_PATH/{{ . }}" \
{{- end -}}
{{- end -}}
{{- if (coll.Has $submenu.build "var_files") -}}
{{ range $submenu.build.var_files }}
-var-file="$CONFIG_PATH/{{ . }}" \
{{- end -}}
{{- end }}
"$INPUT_PATH"
### Build Complete. ###
echo "Build Complete."
}
{{ end }}
{{ end -}}
press_enter() {
cd "$SCRIPT_PATH"
echo -n "Press Enter to continue."
read -r
clear
}
info() {
echo "Copyright 2023-2024 Broadcom. All Rights Reserved."
echo "License: BSD-2"
echo ""
echo "GitHub Repository: github.com/vmware-samples/packer-examples-for-vsphere/"
read -r
}
incorrect_selection() {
echo "Invalid selection, please try again."
}
until [ "$selection" = "0" ]; do
clear
echo ""
echo -e "$menu_message"
echo ""
{{- $menu_index := 0 }}
{{- range $menu := (ds "build").menu }}
echo " {{ $menu.entry }}:"
echo ""
{{- range $submenu := $menu.submenu -}}
{{ $menu_index = math.Add $menu_index 1 }}
{{- if (lt $menu_index 10) }}
echo " {{ $menu_index }} - {{ $submenu.entry }}"
{{- else }}
echo " {{ $menu_index }} - {{ $submenu.entry }}"
{{- end }}
{{- end }}
echo ""
{{- end }}
echo " Other:"
echo ""
echo " I - Information"
echo " Q - Quit"
echo ""
read -r selection
echo ""
case $selection in
{{- $selection_index := 0 }}
{{- range $menu := (ds "build").menu }}
{{- range $submenu := $menu.submenu -}}
{{ $selection_index = math.Add $selection_index 1 }}
{{ $selection_index }} ) clear ; menu_option_{{ $selection_index }} ; press_enter ;;
{{- end }}
{{- end }}
i|I ) clear ; info ; press_enter ;;
q|Q ) clear ; exit ;;
* ) clear ; incorrect_selection ; press_enter ;;
esac
done