-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.bash
247 lines (211 loc) · 6.78 KB
/
installer.bash
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
#!/bin/bash/env bash
#
# Filename: installer.bash
# Description: Manages deploy-VFIO binaries and files.
# Author(s): Alex Portell <github.com/portellam>
# Maintainer(s): Alex Portell <github.com/portellam>
#
# <params>
declare -gr SCRIPT_NAME=$( basename "${0}" )
declare -gr MAIN_EXECUTABLE="deploy-vfio"
# <summary>Execution Flags</summary>
declare -g DO_INSTALL=true
# <summary>
# Color coding
# Reference URL: 'https://www.shellhacks.com/bash-colors'
# </summary>
declare -g SET_COLOR_GREEN='\033[0;32m'
declare -g SET_COLOR_RED='\033[0;31m'
declare -g SET_COLOR_YELLOW='\033[0;33m'
declare -g RESET_COLOR='\033[0m'
# <summary>Append output</summary>
declare -gr PREFIX_PROMPT="${SCRIPT_NAME}: "
declare -gr PREFIX_ERROR="${PREFIX_PROMPT}${SET_COLOR_YELLOW}An error occurred:${RESET_COLOR} "
declare -gr PREFIX_FAIL="${PREFIX_PROMPT}${SET_COLOR_RED}Failure:${RESET_COLOR} "
declare -gr PREFIX_PASS="${PREFIX_PROMPT}${SET_COLOR_GREEN}Success:${RESET_COLOR} "
# </params>
# <functions>
function main
{
if [[ $( whoami ) != "root" ]]; then
print_error_to_log "User is not sudo or root."
return 1
fi
get_option "${1}" || return 1
local -r source_subfolder="${MAIN_EXECUTABLE}.d"
local -r source_path=$( pwd )"/${source_subfolder}/"
local -r bin_path="/usr/local/bin/"
local -r bin_target_path="${bin_path}${source_subfolder}/"
local -r bin_source_path="${source_path}/bin/"
local -r etc_target_path="/usr/local/etc/${source_subfolder}/"
local -r etc_source_path="${source_path}/etc/"
if "${DO_INSTALL}"; then
if ! install; then
print_fail_to_log "Could not install deploy-VFIO."
return 1
else
print_pass_to_log "Installed deploy-VFIO."
fi
else
if ! uninstall; then
print_fail_to_log "Could not uninstall deploy-VFIO."
return 1
else
print_pass_to_log "Uninstalled deploy-VFIO."
fi
fi
}
# <summary>Checks</summary>
function do_binaries_exist
{
if [[ ! -e "${MAIN_EXECUTABLE}" ]]; then
print_error_to_log "Missing main executable."
return 1
fi
local -r lwd=$( pwd )
cd "${bin_source_path}" || return 1
if [[ ! -e "args_common" ]] \
|| [[ ! -e "args_compatibility" ]] \
|| [[ ! -e "args_database" ]] \
|| [[ ! -e "args_main-setup" ]] \
|| [[ ! -e "args_post-setup" ]] \
|| [[ ! -e "args_pre-setup" ]] \
|| [[ ! -e "args_usage" ]] \
|| [[ ! -e "logic_common" ]] \
|| [[ ! -e "logic_compatibility" ]] \
|| [[ ! -e "logic_database" ]] \
|| [[ ! -e "logic_main-setup" ]] \
|| [[ ! -e "logic_post-setup" ]] \
|| [[ ! -e "logic_pre-setup" ]] \
|| [[ ! -e "sources" ]]; then
print_error_to_log "Missing project binaries."
return 1
fi
if ! cd "${lwd}"; then
print_error_to_log "Failed to return to last working directory."
return 1
fi
}
function do_files_exist
{
local -r lwd=$( pwd )
cd "${etc_source_path}" || return 1
if [[ ! -e "custom" ]] \
|| [[ ! -e "grub" ]] \
|| [[ ! -e "initramfs-tools" ]] \
|| [[ ! -e "modules" ]] \
|| [[ ! -e "pci-blacklists.conf" ]] \
|| [[ ! -e "vfio.conf" ]]; then
print_error_to_log "Missing project files."
return 1
fi
if ! cd "${lwd}"; then
print_error_to_log "Failed to return to last working directory."
return 1
fi
}
function does_target_path_exist
{
if [[ ! -d "${bin_target_path}" ]] \
&& ! sudo mkdir --parents "${bin_target_path}"; then
print_error_to_log "Could not create directory '${bin_target_path}'."
return 1
fi
if [[ ! -d "${etc_target_path}" ]] \
&& ! sudo mkdir --parents "${etc_target_path}"; then
print_error_to_log "Could not create directory '${etc_target_path}'."
return 1
fi
}
# <summary>Execution</summary>
function copy_sources_to_targets
{
if ! sudo cp --force "${MAIN_EXECUTABLE}" "${bin_path}${MAIN_EXECUTABLE}" &> /dev/null; then
print_error_to_log "Failed to copy main executable."
return 1
fi
if ! sudo cp --force --recursive "${bin_source_path}"* "${bin_target_path}" &> /dev/null; then
print_error_to_log "Failed to copy project binaries."
return 1
fi
if ! sudo cp --force --recursive "${etc_source_path}"* "${etc_target_path}" &> /dev/null; then
print_error_to_log "Failed to copy project file(s)."
return 1
fi
}
function install
{
do_binaries_exist || return 1
do_files_exist || return 1
does_target_path_exist || return 1
copy_sources_to_targets || return 1
set_target_file_permissions || return 1
}
function set_target_file_permissions
{
if ! sudo chown --recursive root:root "${bin_target_path}" &> /dev/null \
|| ! sudo chmod --recursive +x "${bin_target_path}" &> /dev/null \
|| ! sudo chown --recursive root:root "${etc_target_path}" &> /dev/null; then
print_error_to_log "Failed to set file permissions."
return 1
fi
}
function uninstall
{
if ! rm --force "${bin_path}${MAIN_EXECUTABLE}" &> /dev/null; then
print_error_to_log "Failed to delete main executable."
return 1
fi
if ! rm --force --recursive "${bin_target_path}" &> /dev/null; then
print_error_to_log "Failed to delete project binaries."
return 1
fi
if ! rm --force --recursive "${etc_target_path}" &> /dev/null; then
print_error_to_log "Failed to delete project file(s)."
return 1
fi
}
# <summary>Loggers/summary>
function print_error_to_log
{
echo -e "${PREFIX_ERROR}${1}" >&2
}
function print_fail_to_log
{
echo -e "${PREFIX_FAIL}${1}" >&2
}
function print_pass_to_log
{
echo -e "${PREFIX_PASS}${1}" >&1
}
# <summary>Usage</summary>
function get_option
{
case "${1}" in
"-u" | "--uninstall" )
DO_INSTALL=false ;;
"-i" | "--install" )
DO_INSTALL=true ;;
"-h" | "--help" | * )
print_usage
return 1 ;;
esac
}
function print_usage
{
IFS=$'\n'
local -ar output=(
"Usage:\tbash installer.bash [OPTION]"
"Manages ${MAIN_EXECUTABLE} binaries and files.\n"
" -h, --help\t\tPrint this help and exit."
" -i, --install\t\tInstall ${MAIN_EXECUTABLE} to system."
" -u, --uninstall\tUninstall ${MAIN_EXECUTABLE} from system."
)
echo -e "${output[*]}"
unset IFS
}
# </functions>
# <code>
main "${1}"
exit "${?}"
# </code>