-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ubuntu.sh
198 lines (174 loc) · 5.73 KB
/
update.ubuntu.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
#!/bin/bash
# Copyright 2021 aqz/tamaina, joinmisskey
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
version="3.2.0";
tput setaf 2;
echo "Check: root user;";
if [ "$(whoami)" != 'root' ]; then
tput setaf 1;
echo " NG. This script must be run as root.";
exit 1;
else
tput setaf 7;
echo " OK. I am root user.";
fi
tput setaf 3;
echo "Process: import environment and detect method;";
tput setaf 7;
if [ -f "/root/.misskey.env" ]; then
. "/root/.misskey.env";
if [ -f "/home/$misskey_user/.misskey.env" ]; then
. "/home/$misskey_user/.misskey.env";
method=systemd;
elif [ -f "/home/$misskey_user/.misskey-docker.env" ]; then
. "/home/$misskey_user/.misskey-docker.env";
else
misskey_user=misskey;
misskey_directory=misskey;
misskey_localhost=localhost;
method=systemd;
echo "use default";
fi
else
misskey_user=misskey;
misskey_directory=misskey;
misskey_localhost=localhost;
method=systemd;
echo "use default";
fi
echo "method: $method / user: $misskey_user / dir: $misskey_directory / $misskey_localhost:$misskey_port";
if [ $method == "systemd" ]; then
#region systemd
tput setaf 3;
echo "Process: update (systemd);";
tput setaf 7;
#region work with misskey user
su "$misskey_user" << MKEOF
set -eu;
cd ~/$misskey_directory;
tput setaf 3;
echo "Process: git pull;";
tput setaf 7;
git pull;
git submodule update --init;
MKEOF
#endregion
tput setaf 3;
echo "Process: stop daemon;";
tput setaf 7;
systemctl stop "$host"
#region work with misskey user
su "$misskey_user" << MKEOF
set -eu;
cd ~/$misskey_directory;
tput setaf 3;
echo "Process: clean;";
tput setaf 7;
pnpm run clean;
tput setaf 3;
echo "Process: pnpm install;";
tput setaf 7;
NODE_ENV=production pnpm install --frozen-lockfile;
tput setaf 3;
echo "Process: build misskey;";
tput setaf 7;
NODE_OPTIONS=--max_old_space_size=3072 NODE_ENV=production pnpm run build;
tput setaf 3;
echo "Process: migrate db;";
tput setaf 7;
NODE_OPTIONS=--max_old_space_size=3072 pnpm run migrate;
MKEOF
#endregion
if [ $# == 1 ] && [ $1 == "-r" ]; then
tput setaf 3;
echo "Process: apt upgrade;";
tput setaf 7;
apt update -y;
DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none apt full-upgrade -y;
tput setaf 3;
echo "reboot;";
tput setaf 7;
reboot;
else
tput setaf 3;
echo "Process: start daemon;";
tput setaf 7;
systemctl start "$host";
fi
#endregion
else
m_uid=$(id -u "$misskey_user");
oldid=$(sudo -iu "$misskey_user" XDG_RUNTIME_DIR=/run/user/$m_uid DOCKER_HOST=unix:///run/user/$m_uid/docker.sock docker images --no-trunc --format "{{.ID}}" $docker_repository);
if [ $method == "docker" ]; then
tput setaf 3;
echo "Process: docker build;";
tput setaf 7;
if [ $# == 1 ]; then
docker_repository="$1";
else
docker_repository="local/misskey:latest";
fi
sudo -iu "$misskey_user" XDG_RUNTIME_DIR=/run/user/$m_uid DOCKER_HOST=unix:///run/user/$m_uid/docker.sock docker build -t $docker_repository "/home/$misskey_user/$misskey_directory";
else
tput setaf 3;
echo "Process: docker pull;";
tput setaf 7;
if [ $# == 1 ]; then
docker_repository="$1";
else
docker_repository="ghcr.io/transfem-org/sharkey:stable";
fi
sudo -iu "$misskey_user" XDG_RUNTIME_DIR=/run/user/$m_uid DOCKER_HOST=unix:///run/user/$m_uid/docker.sock docker pull "$docker_repository";
fi
tput setaf 3;
echo "Process: docker run;";
tput setaf 7;
docker_new_container=$(sudo -iu "$misskey_user" XDG_RUNTIME_DIR=/run/user/$m_uid DOCKER_HOST=unix:///run/user/$m_uid/docker.sock docker run -d -p $misskey_port:$misskey_port --add-host=$misskey_localhost:$docker_host_ip -v /home/$misskey_user/$misskey_directory/files:/misskey/files -v "/home/$misskey_user/$misskey_directory/.config/default.yml":/misskey/.config/default.yml:ro --restart unless-stopped -t "$docker_repository");
tput setaf 3;
echo "Process: docker rm container;";
tput setaf 7;
sudo -iu "$misskey_user" XDG_RUNTIME_DIR=/run/user/$m_uid DOCKER_HOST=unix:///run/user/$m_uid/docker.sock docker rm -f "$docker_container";
tput setaf 3;
echo "Process: docker image prune;";
tput setaf 7;
sudo -iu "$misskey_user" XDG_RUNTIME_DIR=/run/user/$m_uid DOCKER_HOST=unix:///run/user/$m_uid/docker.sock docker image prune -f;
su "$misskey_user" <<-MKEOF
set -eu;
cd ~;
tput setaf 3;
echo "Process: create .misskey-docker.env;"
tput setaf 7;
cat > ".misskey-docker.env" << _EOF
method="$method"
host="$host"
misskey_port=$misskey_port
misskey_directory="$misskey_directory"
misskey_localhost="$misskey_localhost"
docker_host_ip=$docker_host_ip
docker_repository="$docker_repository"
docker_container="$docker_new_container"
version="$version"
_EOF
MKEOF
tput setaf 3;
echo "Process: docker remove image;";
tput setaf 7;
sudo -iu $misskey_user XDG_RUNTIME_DIR=/run/user/$m_uid DOCKER_HOST=unix:///run/user/$m_uid/docker.sock docker rmi "$oldid"
fi