-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathinstall.sh
executable file
·193 lines (168 loc) · 5.03 KB
/
install.sh
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
#!/bin/bash
#
# Install a tool from debian-server-tools.
#
# VERSION :0.5.0
# DATE :2018-07-26
# AUTHOR :Viktor Szépe <[email protected]>
# LICENSE :The MIT License (MIT)
# URL :https://github.com/szepeviktor/debian-server-tools
# BASH-VERSION :4.2+
Die()
{
local -i RET="$1"
shift
echo -e "$*" 1>&2
exit "$RET"
}
#####################################################
# Parses out a meta value from a script
# Arguments:
# FILE
# META
#####################################################
Get_meta()
{
# Defaults to self
local FILE="${1:-$0}"
# Defaults to "VERSION"
local META="${2:-VERSION}"
local VALUE
VALUE="$(head -n 30 "$FILE" | grep -m 1 "^# ${META}\\s*:" | cut -d ":" -f 2-)"
if [ -z "$VALUE" ]; then
VALUE="(unknown)"
fi
echo "$VALUE"
}
#####################################################
# Parses out all meta values from a script
# Arguments:
# FILE
# META
#####################################################
Get_meta_all()
{
# Defaults to self
local FILE="${1:-$0}"
# Defaults to "VERSION"
local META="${2:-VERSION}"
local VALUE
VALUE="$(head -n 30 "$FILE" | grep "^# ${META}\\s*:" | cut -d ":" -f 2-)"
if [ -z "$VALUE" ]; then
VALUE="(unknown)"
fi
echo "$VALUE"
}
#####################################################
# Copies files in place, sets permissions and owner
# Arguments:
# FILE
#####################################################
Do_install()
{
local FILE="$1"
local OWNER
local PERMS
local SYMLINK
local SCRIPT
local PKG
if [ ! -f "$FILE" ]; then
Die 1 "File does not exist (${FILE})"
fi
SCRIPT="$(Get_meta "$FILE" LOCATION)"
if [ -z "$SCRIPT" ] || [ "$SCRIPT" == "(unknown)" ]; then
Die 2 "Invalid location (${SCRIPT})"
fi
OWNER="$(Get_meta "$FILE" OWNER)"
if [ -z "$OWNER" ] || [ "$OWNER" == "(unknown)" ]; then
OWNER="root:staff"
fi
PERMS="$(Get_meta "$FILE" PERMISSION)"
if [ -z "$PERMS" ] || [ "$PERMS" == "(unknown)" ]; then
PERMS="0755"
fi
# Check for existence
if [ -f "$SCRIPT" ]; then
if diff -q "$FILE" "$SCRIPT" &> /dev/null; then
echo "Already up-to-date ${SCRIPT}"
return
fi
echo "Updating ${SCRIPT}"
else
echo "Installing ${FILE} to ${SCRIPT}"
fi
# Install
install -v -D --no-target-directory --preserve-timestamps \
--owner="${OWNER%:*}" --group="${OWNER#*:}" --mode "$PERMS" \
"$FILE" "$SCRIPT" \
|| Die 11 "Installation failure (${FILE})"
# Create symlink
head -n 30 "$FILE" | grep '^# SYMLINK\s*:' | cut -d ":" -f 2- \
| while read -r SYMLINK; do
echo -n "Symlinking "
ln -s -v -f "$SCRIPT" "$SYMLINK" || Die 12 "Symbolic link creation failure (${SYMLINK})"
done
# Cron jobs
if head -n 30 "$FILE" | grep -q -i '^# CRON'; then
"$(dirname "$0")/install-cron.sh" "$FILE" || Die 13 "Cron installation failure (${FILE})"
fi
# Display dependencies
Get_meta_all "$FILE" DEPENDS
# Check APT dependencies
Get_meta_all "$FILE" DEPENDS | sed -n -e 's/^apt-get install \(.\+\)$/\1 /p' \
| while read -r -d " " PKG; do
if [ "$(dpkg-query --showformat="\${Status}" --show "$PKG" 2> /dev/null)" != "install ok installed" ]; then
echo "MISSING DEPENDENCY: apt-get install ${PKG}" 1>&2
fi
done
# Check PyPA dependencies
Get_meta_all "$FILE" DEPENDS | sed -n -e 's/^pip install \(.\+\)$/\1 /p' \
| while read -r -d " " PKG; do
if ! pip show "$PKG" | grep -q -x 'Version: \S\+'; then
echo "MISSING DEPENDENCY: pip install ${PKG}" 1>&2
fi
done
Get_meta_all "$FILE" DEPENDS | sed -n -e 's/^pip3 install \(.\+\)$/\1 /p' \
| while read -r -d " " PKG; do
if ! pip3 show "$PKG" | grep -q -x 'Version: \S\+'; then
echo "MISSING DEPENDENCY: pip3 install ${PKG}" 1>&2
fi
done
# Check file dependencies
Get_meta_all "$FILE" DEPENDS | sed -n -e 's|^\(/.\+\)$|\1|p' \
| while read -r PKG; do
test -x "$PKG" || echo "MISSING DEPENDENCY: Install '${PKG}'" 1>&2
done
}
#####################################################
# Process all files in a directory NOT recursively
# Arguments:
# DIR
#####################################################
Do_dir()
{
local DIR="$1"
local FILE
find "$DIR" -maxdepth 1 -type f \
| while read -r FILE; do
Do_install "$FILE"
done
}
#########################################################
INSTALL_PATH="$1"
# Check user
if [[ $EUID -ne 0 ]]; then
Die 1 "Only root is allowed to install"
fi
# Display version
if [ "$INSTALL_PATH" == "--version" ]; then
Get_meta
exit 0
fi
if [ -d "$INSTALL_PATH" ]; then
Do_dir "$INSTALL_PATH"
elif [ -f "$INSTALL_PATH" ]; then
Do_install "$INSTALL_PATH"
else
Die 2 "Specified path does not exist (${INSTALL_PATH})"
fi