forked from vmware-samples/packer-examples-for-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.tmpl
179 lines (161 loc) · 4.55 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
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
#!/usr/bin/env bash
# Copyright 2023 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" == "--help" ] || [ "$1" == "-h" ]; then
echo "Usage: script.sh [OPTIONS] [CONFIG_PATH]"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit."
echo " -d, --debug Run builds in debug mode."
echo ""
echo "Arguments:"
echo " CONFIG_PATH Path to the configuration directory."
echo ""
echo "Examples:"
echo " ./build.sh"
echo " ./build.sh --help"
echo " ./build.sh --debug"
echo " ./build.sh config"
echo " ./build.sh us-west-1"
echo " ./build.sh --debug config"
echo " ./build.sh --debug us-west-1"
exit 0
fi
if [ "$1" == "--debug" ] || [ "$1" == "-d" ]; then
debug_mode=true
debug_option="-debug"
shift
else
debug_mode=false
debug_option=""
fi
SCRIPT_PATH=$(realpath "$(dirname "$(follow_link "$0")")")
if [ -n "$1" ]; then
CONFIG_PATH=$(realpath "$1")
else
CONFIG_PATH=$(realpath "${SCRIPT_PATH}/config")
fi
menu_banner=$(cat << "EOF"
____ __ ____ _ __ __
/ __ \____ ______/ /_____ _____ / __ )__ __(_) /___/ /____
/ /_/ / __ / ___/ //_/ _ \/ ___/ / __ / / / / / / __ / ___/
/ ____/ /_/ / /__/ ,< / __/ / / /_/ / /_/ / / / /_/ (__ )
/_/ \__,_/\___/_/|_|\___/_/ /_____/\__,_/_/_/\__,_/____/
EOF
)
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 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 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_banner"
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