-
Notifications
You must be signed in to change notification settings - Fork 9
/
getdess.sh
283 lines (241 loc) · 7.01 KB
/
getdess.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash
# Enable job control
set -m
#* START SCRIPT GLOBALS
# Supported distros by type
debian_releases='ubuntu debian raspbian'
redhat_releases='centos fedora amzn rhel rocky'
arch_support='x86_64 amd64 aarch64 arm64 armv7l'
# Required base packages
packages="curl openssl qrencode"
# Docker compose link
compose_url="https://github.com/docker/compose/releases/download/1.29.2"
# Atsign user info
user_info="atsign, secondaries account, atsign.com"
# Atsign directories
atsign_dirs="/home/atsign/dess /home/atsign/base /home/atsign/atsign/var /home/atsign/atsign/etc /home/atsign/atsign/logs /home/atsign/tmp"
# Repository files
repo_url="https://raw.githubusercontent.com/atsign-foundation/dess/trunk"
atsign_files="base/.env base/docker-swarm.yaml base/setup.sh base/shepherd.yaml base/restart.sh"
dess_scripts="create reshowqr"
#* END SCRIPT GLOBALS
error_exit() {
exit_msg=""
case "$1" in
1)exit_msg="Not running with root priveleges";;
2)exit_msg="Could not detect /etc/os-release ID";;
4)exit_msg="certbot failed to install";;
50)exit_msg="docker daemon failed to start in time";;
51)exit_msg="docker-compose failed to install";;
52)exit_msg="docker stack failed to deploy";;
6)exit_msg="A dess script failed to install";;
*);;
esac
echo "ERROR: $exit_msg"
exit "$1"
}
command_exists () {
command -v "$@" > /dev/null 2>&1
}
# Helper function to check if user's release matches the list
# $1 = list of valid releases
# $2 = release to check
is_release () { [[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]]; }
pre_install () {
# Get the user's release
os_release=$(. /etc/os-release; echo "$ID")
os_id=$(. /etc/os-release; echo "$VERSION_ID")
if [ -z "$os_release" ]
then
error_exit 2
fi
echo "Detected release id: $os_release, version: $os_id"
echo "Detected architecture: $(uname -m)"
if ! is_release "$arch_support" "$(uname -m)"; then
echo "Your architecture ($(uname -m)) is currently unsupported by this script."
exit 0
fi
# get package manager
if is_release "$debian_releases" "$os_release"; then
pkg_man='apt-get'
elif is_release "$redhat_releases" "$os_release"; then
#Use dnf if available, fallback to yum on older distros
if [[ -n $(command -v 'dnf') ]]; then
pkg_man='dnf'
else
pkg_man='yum'
fi
else
echo 'Your distribution is currently unsupported by this script.'
exit 0
fi
echo "Detected package manager: $pkg_man"
}
# Functions below are run as root via do_install
install_dependencies () {
$pkg_man -y update
for pkg in $packages; do
if ! command_exists "$pkg"; then
$pkg_man -y install "$pkg"
fi
done
}
install_certbot () {
case "$os_release" in
centos|rocky) echo y | $pkg_man -y install epel-release;;
amzn) echo y | amazon-linux-extras install epel;;
rhel) echo y | $pkg_man -y install "https://dl.fedoraproject.org/pub/epel/epel-release-latest-$($os_id | awk -F. '{print $1}').noarch.rpm";;
*);;
esac
echo y | $pkg_man -y install certbot
CERTBOT_RESULT=$?
if [[ $CERTBOT_RESULT -gt 0 ]]; then
error_exit 4
fi
}
install_docker () {
# docker
if ! command_exists docker; then
case $os_release in
amzn) amazon-linux-extras install docker;;
rocky)
dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo;
dnf -y update;
dnf -y install docker-ce docker-ce-cli containerd.io;
systemctl enable --now docker.service;
;;
*) curl -fsSL https://get.docker.com | sh;;
esac
fi
# docker-compose
if ! command_exists docker-compose; then
case $(uname -m) in
x86_64|amd64) curl -fsSL "$compose_url/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose;;
*)
case "$os_release" in
amzn) $pkg_man install -y libffi libffi-devel openssl-devel python3 python3-pip python3-devel gcc;;
*) $pkg_man install -y python3 python3-pip;;
esac;
pip3 install docker-compose;
;;
esac
COMPOSE_RESULT=$?
echo "$COMPOSE_RESULT"
if [[ $COMPOSE_RESULT -gt 0 ]]; then
error_exit 51
fi
chown root:docker /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
fi
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
systemctl enable --now docker.service
}
mkdir_atsign () {
mkdir -p "$1"
echo "making $1"
chown atsign:atsign "$1"
}
curl_atsign_file () {
curl -fsSL "$repo_url/$1" -o "/home/atsign/$1"
echo "curling $1"
chown atsign:atsign "/home/atsign/$1"
}
setup_atsign_user () {
# add atsign user
if [[ $pkg_man == apt-get ]]; then
adduser -uid 1024 --disabled-password --disabled-login --gecos "$user_info" atsign
else
adduser --uid 1024 --comment "$user_info" atsign
fi
# link lets-encrypt folder
if [[ ! -d /home/atsign/atsign/etc ]]; then
tput setaf 2
echo "setting up certbot"
rm /etc/letsencrypt/*
rmdir /etc/letsencrypt/
ln -s /home/atsign/atsign/etc /etc/letsencrypt
tput setaf 7
else
tput setaf 1
echo 'saved you from destroying letsencrypt by running the script again :-)'
tput setaf 7
fi
# make ~atsign directories
tput setaf 2
echo "Creating some base directories for atsign"
tput setaf 7
for directory in $atsign_dirs; do
mkdir_atsign "$directory"
done
# curl the base files for atsign
for file in $atsign_files; do
curl_atsign_file "$file"
done
chown atsign:atsign /home/atsign
}
setup_docker () {
# wait for docker to startup
SECONDS=0
STATUS=1
until [[ $STATUS -ne 0 ]]; do
systemctl is-active --quiet docker.service
STATUS=$?
SECONDS=$SECONDS+1
sleep 1
if [[ $SECONDS -gt 120 ]]; then
error_exit 50
fi
done
# give atsign user docker permissions
usermod -aG docker atsign
# setup and deploy the swarm as atsign
docker swarm init
docker network create -d overlay secondaries
docker stack deploy -c /home/atsign/base/shepherd.yaml secondaries
STACK_RESULT=$?
if [[ $STACK_RESULT -gt 0 ]]; then
error_exit 52
fi
}
test_atsign_user () {
# check if docker works for atsign user
runuser -l atsign -c '/usr/bin/docker run hello-world'
RESULT=$?
if [[ $RESULT -eq 0 ]]; then
echo "Docker setup correctly for atsign user"
else
echo "Please check docker install, something went wrong"
fi
}
get_dess_scripts () {
# curl create and reshowqr from repo
# to /usr/local/bin
for script in $dess_scripts; do
curl -fsSL "$repo_url"/"$script".sh -o /usr/local/bin/dess-"$script"
DESS_SCRIPT_RESULT=$?
if [[ $DESS_SCRIPT_RESULT -gt 0 ]]; then
error_exit 6
fi
chmod 774 /usr/local/bin/dess-"$script"
ln -s /usr/local/bin/dess-"$script" /usr/bin/dess-"$script"
done
}
post_install() {
echo;
echo 'Dess installed, please move on to the sudo dess-create command.'
}
do_install () {
pre_install
if [[ $EUID -ne 0 ]]; then
error_exit 1
fi
install_dependencies
install_certbot
install_docker
setup_atsign_user
setup_docker
test_atsign_user
get_dess_scripts
post_install
}
do_install