-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall.sh
executable file
·217 lines (188 loc) · 5.84 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
# This script does all the small things needed to install mulchd and
# mulch-proxy on a Linux system. Have a look to main() function for
# important stuff if you need to create a package.
# See also install/ directory for basic/demo installations.
# defaults (see --help)
ETC="/etc/mulch"
VAR_DATA="/var/lib/mulch"
VAR_STORAGE="/srv/mulch"
FORCE="false"
SOURCE=$(dirname "$0")
# TODO:
# check storage accessibility (minimum: --x) for libvirt? (RH/Fedora)
# setfacl -m g:qemu:x /home/mulch/
# setfacl -m g:libvirt-qemu:x /home/mulch/
function main() {
parse_args "$@"
check_noroot # show warning if UID 0
check_libvirt_access
is_dir_writable "$ETC"
is_dir_writable "$VAR_DATA"
is_dir_writable "$VAR_STORAGE"
check_if_existing_config
copy_config
update_config_path
gen_services
infos_next
}
function check() {
if [ $1 -ne 0 ]; then
echo "error, exiting"
exit $1
fi
}
function parse_args() {
while (( "$#" )); do
case $1 in
-e|--etc)
shift
ETC="$1"
shift
;;
-d|--data)
shift
VAR_DATA="$1"
shift
;;
-s|--storage)
shift
VAR_STORAGE="$1"
shift
;;
-f|--force)
FORCE="true"
shift
;;
-h|--help)
echo ""
echo "** Helper script: install mulchd and mulch-proxy **"
echo ""
echo "Note: mulch client is not installed/configured by this script."
echo ""
echo "Options and defaults (short options available too):"
echo " --etc $ETC (-e, config files)"
echo " --data $VAR_DATA (-d, state [small] databases)"
echo " --storage $VAR_STORAGE (-s, disks storage)"
echo " --force (-f, erase old install)"
echo ""
echo "Sample install:"
echo "mkdir -p ~/mulch/etc ~/mulch/data ~/mulch/storage"
echo "./install.sh --etc ~/mulch/etc/ --data ~/mulch/data/ --storage ~/mulch/storage/"
echo ""
echo "For a quick demo install for Ubuntu, see install/ubuntu_autoinstall.sh"
exit 1
;;
*)
echo "Unknown option $1"
exit 2
;;
esac
done
}
function is_dir_writable() {
echo "checking if $1 is writable…"
if [ ! -d "$1" ]; then
echo "error: directory $1 does not exists"
exit 10
fi
test_file="$1/.wtest"
touch "$test_file"
check $?
rm -f "$test_file"
}
function check_noroot() {
uid=$(id -u)
if [ "$uid" -eq 0 ]; then
echo "ROOT PRIVILEGES NOT REQUIRED!"
fi
}
function check_if_existing_config() {
if [ -f "$ETC/mulchd.toml" ]; then
echo "Existing configuration found!"
if [ $FORCE == "false" ]; then
echo "This script is intentend to do a new install, not to upgrade an existing one."
echo "If you know what you are doing, you may use --force option."
echo "Exiting."
exit 1
fi
fi
}
function copy_config() {
echo "copying config…"
cp -Rp $SOURCE/etc/* "$ETC/"
check $?
mv "$ETC/mulchd.sample.toml" "$ETC/mulchd.toml"
check $?
}
function check_libvirt_access() {
echo "checking libvirt access…"
virsh -c qemu:///system version
ret=$?
if [ "$ret" -ne 0 ]; then
echo "Failed."
echo " - check that libvirtd is running:"
echo " - systemctl status libvirtd"
echo " - check that $USER is allowed to connect to qemu:///system URL:"
echo " - check that your user is in 'libvirt' group"
echo " - sudo usermod -aG libvirt \$USER"
echo " - you may have to disconnect / reconnect your user"
fi
check $ret
}
function update_config_path() {
sed -i'' "s|^data_path =.*|data_path = \"$VAR_DATA\"|" "$ETC/mulchd.toml"
check $?
sed -i'' "s|^storage_path =.*|storage_path = \"$VAR_STORAGE\"|" "$ETC/mulchd.toml"
check $?
}
function infos_next() {
echo ""
echo "Install OK."
echo ""
echo "Now, you can:"
echo " - update $ETC/mulchd.toml"
echo " - test manually mulchd and mulch-proxy"
echo " - $mulchd_bin -path \"$ETC\""
echo " - $proxy_bin -path \"$ETC\""
echo " - install+start services (root)"
echo " - sudo cp mulchd.service mulch-proxy.service /etc/systemd/system/ (no symlink)"
echo " - sudo systemctl daemon-reload"
echo " - sudo systemctl enable --now mulchd"
echo " - sudo systemctl enable --now mulch-proxy"
echo " - get API key(s) in $VAR_DATA/mulch-api-keys.db"
echo " - have fun with mulch client"
}
function gen_services() {
echo "generating systemd unit service files…"
go_bin=$(go env GOBIN)
if [ -z "$go_bin" ]; then
go_bin="$(go env GOPATH)/bin"
fi
# should apply systemd-escape ?
mulchd_bin="$go_bin/mulchd"
proxy_bin="$go_bin/mulch-proxy"
if [ ! -x "$mulchd_bin" ]; then
echo "Unable to find $mulchd_bin (compilation was OK?)"
check 20
fi
if [ ! -x "$proxy_bin" ]; then
echo "Unable to find $proxy_bin (compilation was OK?)"
check 20
fi
cp -p "$SOURCE/install/mulchd.sample.service" "$SOURCE/mulchd.service"
check $?
cp -p "$SOURCE/install/mulch-proxy.sample.service" "$SOURCE/mulch-proxy.service"
check $?
sed -i'' "s|{USER}|$USER|" "$SOURCE/mulchd.service"
check $?
sed -i'' "s|{USER}|$USER|" "$SOURCE/mulch-proxy.service"
check $?
sed -i'' "s|{MULCHD_START}|$mulchd_bin -path \"$ETC\"|" "$SOURCE/mulchd.service"
check $?
sed -i'' "s|{MULCH_PROXY_START}|$proxy_bin -path \"$ETC\"|" "$SOURCE/mulch-proxy.service"
check $?
sed -i'' "s|{MULCH_PROXY}|$proxy_bin|" "$SOURCE/mulch-proxy.service"
check $?
}
main "$@"