-
Notifications
You must be signed in to change notification settings - Fork 4
/
uyuni-docs-helper
executable file
·251 lines (222 loc) · 8.52 KB
/
uyuni-docs-helper
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
#!/bin/bash
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
# Language (force it so getopt messages are always in english, as the script)
LANG=en_EN
# Get script name
SCRIPT=$(basename ${0})
# By default, do not serve the doc using HTTP
SERVE=0
PROJECT='uyuni-project'
# Default tag
TAG='main'
# Default Git repository
GITREPO='https://github.com/uyuni-project/uyuni-docs.git'
# Default Git reference
GITREF='master'
# use gnu-getopt on macOS
getopt_for_macos() {
if [ "$(uname -s)" == 'Darwin' ]; then
if [ ! -f /usr/local/opt/gnu-getopt/bin/getopt ]; then
if [ "${SHELL}" == "/usr/local/bin/fish" ]; then
print_error "The fish shell is not supported (yet), please use bash or zsh for now"
exit 1
fi
print_error "Please install gnu-getopt via Homebrew with: brew install gnu-getopt"
exit 1
else
export PATH="$(brew --prefix gnu-getopt)/bin:$PATH"
fi
fi
}
print_info() {
echo -e "\033[1;36m[INFO] ${1}\033[0m"
}
print_error() {
echo -e "\033[1;31m[ERROR] ${1}\033[0m"
}
print_ok() {
echo -e "\033[1;32m[INFO] ${1}\033[0m"
}
print_incorrect_syntax() {
print_error "Incorrect syntax: ${1} (use ${SCRIPT} -h for help)"
exit 1
}
get_dir_abs_path() {
(
cd "$(dirname ${1})"
echo "${PWD}/$(basename $1)"
)
}
print_help() {
echo ""
echo "Build the Uyuni doc using a container and (optionally) serve it for verification via HTTP"
echo ""
echo "Syntax: "
echo ""
echo "${SCRIPT} <ARGUMENTS>"
echo ""
echo "Mandatory arguments:"
echo ""
echo "-p|--product <uyuni|suma> Build the documentation for either Uyuni or SUSE Manager"
echo "-c|--command <make command> Use the desired command to build html/pdf, html&pdf, etc..."
echo " for example: 'all-uyuni'. You can use 'help' to list all"
echo " possible commands. Be aware you still need to specify"
echo " one source for this to work"
echo ""
echo "${SCRIPT} can build documentation from two sources. Different parameters apply for each case."
echo "If both a local git repository and a remote git repository are specified, the local git"
echo "repository is used"
echo ""
echo "Remote Git repository:"
echo "-g|--gitrepo <REPOSITORY> A path to a HTTP git repository where the code for the"
echo " documentation is."
echo " If the value does not start with 'https://'"
echo " then the value is considered as a GitHUb user/organization and"
echo " the URL in the form https://github.com/<REPOSITORY>/uyuni-docs.git"
echo " will be used"
echo " If not specified at all, the default value will be"
echo " ${GITREPO}"
echo "-r|--gitref <REFERENCE> A git branch (${GITREF} by default)"
echo "-o|--output <PATH> An existing path to store the output of the build. A directory"
echo " 'build' will be created inside. Optional"
echo ""
echo "Local Git repository:"
echo "-l|--localclone <GIT_CLONE> A path to a local git repository. The output of the build will"
echo " be placed inside this path as a 'build' directory"
echo ""
echo "Optional arguments:"
echo "-t|--tag Use a secific tag for the container image. Can be useful if you"
echo " want to build an old documentation using an old version of the"
echo " toolchain. (${TAG} used by default)"
echo "-j|--project Specify a GitHub project. It can be useful if you want to use"
echo " a container image in a forked repository in your personal project"
echo "-s|--serve Start a HTTP server at the end of the build so the"
echo " documentation can be inspected (disabled by default)"
}
# adjustments for macOS
getopt_for_macos
# read the options
ARGS=$(getopt -o hg:r:l:o:p:c:t:j:s --long help,gitrepo:,gitref:,localclone:,output:,product:,command:,tag:,project:,serve -n "${SCRIPT}" -- "$@")
if [ $? -ne 0 ];
then
print_incorrect_syntax "Invalid getopt call, notify this to the script maintainers"
fi
eval set -- "${ARGS}"
# extract options and their arguments into variables
while true ; do
case "${1}" in
-h|--help) print_help; exit 1;;
-g|--gitrepo) GITREPO="${2}"; shift 2;;
-r|--gitref) GITREF="${2}"; shift 2;;
-l|--localclone) LOCALCLONE="${2}"; shift 2;;
-o|--output) OUTPUT="${2}"; shift 2;;
-p|--product) PRODUCT="${2}"; shift 2;;
-c|--command) COMMAND="${2}"; shift 2;;
-t|--tag) TAG="${2}"; shift 2;;
-j|--project) PROJECT="${2}"; shift 2;;
-s|--serve) SERVE=1; PORTS='-p 8000:8000'; shift 1;;
--) shift ; break ;;
*) print_incorrect_syntax "Invalid arguements in the call"; exit 1;;
esac
done
# Image
IMAGE="ghcr.io/${PROJECT}/uyuni-docs-helper:${TAG}"
# Command is mandatory
if [ -z ${COMMAND} ]; then
print_incorrect_syntax "-c is mandatory"
fi
# Print a special error for the help command
if [ "${COMMAND}" == "help" ]; then
if [ -z ${LOCALCLONE} ] && [ -z ${GITREPO} ]; then
print_incorrect_syntax "A source (a remote or local git repository) is needed for this."
# Define a default product, if none is available, so we can show the help
elif [ -z ${PRODUCT} ]; then
PRODUCT='suma'
fi
fi
# Product is mandatory
if [ -z ${PRODUCT} ]; then
print_incorrect_syntax "Product (-p) is mandatory"
fi
# Check if SELinux is present and Enforcing
if [ -f /etc/selinux/config ]; then
if [ selinuxenabled ]; then
print_info "Enabling SELinux flags for volume"
SELINUXMOUNT=':Z'
fi
fi
# Either a remote source or a local one must be used
if [ -z ${LOCALCLONE} ]; then
SOURCE="-e GITREPO=${GITREPO} -e GITREF=${GITREF}"
if [ ! -z ${OUTPUT} ]; then
if [ ! -d ${OUTPUT} ]; then
print_error "${OUTPUT} is not a directory or does not exist"
exit 2
else
OUTPUT="$(get_dir_abs_path ${OUTPUT})"
SOURCE="${SOURCE} -v ${OUTPUT}:/tmp/output${SELINUXMOUNT} --userns=keep-id"
if [ "${COMMAND}" != "help" ]; then
print_info "Output will be stored at ${OUTPUT}/build"
fi
OUTDIR="${OUTPUT}/build"
fi
fi
else
if [ ! -z ${OUTPUT} ]; then
print_incorrect_syntax "Using -o together with -l is invalid"
fi
if [ ! -d ${LOCALCLONE} ]; then
print_error "${LOCALCLONE} is not a directory or does not exist"
exit 2
elif [ ! -d ${LOCALCLONE}/.git ]; then
print_error "${LOCALCLONE} is not a Git repository"
exit 2
else
SOURCE="-v ${LOCALCLONE}:/tmp/uyuni-docs${SELINUXMOUNT} --userns=keep-id"
if [ "${COMMAND}" != "help" ]; then
print_info "Output will be stored at ${LOCALCLONE}/build"
fi
OUTDIR="${LOCALCLONE}/build"
fi
fi
# Guess the GitHub URL
if [[ ! "${GITREPO}" =~ ^https:// ]]; then
GITREPO="https://github.com/${GITREPO}/uyuni-docs.git"
fi
if [ -z ${LOCALCLONE} ]; then
SOURCE="${SOURCE} -e GITREPO=${GITREPO} -e GITREF=${GITREF}"
fi
print_info "Pulling the latest container image ${IMAGE}..."
podman pull ${IMAGE}
if [ ${SERVE} -eq 1 ] && [ "${COMMAND}" != "help" ]; then
print_info "The documentation will be served via HTTP. To stop the container when you are done, use CTRL+C"
fi
if [ "${COMMAND}" == "help" ]; then
print_info "Showing the help..."
else
print_info "Bulding the doc..."
fi
podman run -ti --rm ${SOURCE} -e PRODUCT=${PRODUCT} -e COMMAND=${COMMAND} -e SERVE=${SERVE} ${PORTS} ${IMAGE}
RET=${?}
if [ ${RET} -eq 0 ]; then
if [ ! -z ${OUTDIR} ] && [ "${COMMAND}" != "help" ]; then
print_info "You can find find the build output at ${OUTDIR}"
fi
else
print_error "There were errors! Please review the log!"
if [ ! -z ${OUTDIR} ] && [ "${COMMAND}" != "help" ]; then
print_error "Depending on the error, there could also be build outputs at ${OUTDIR}"
fi
fi
exit ${RET}