This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmewdeko-runner
executable file
·190 lines (170 loc) · 5.48 KB
/
mewdeko-runner
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
#!/bin/bash
#
# Start Mewdeko in a specified run mode.
#
# Comment key:
# A.1. - Used in conjunction with 'systemctl'.
# B.1. - Used in the text output.
#
########################################################################################
#### [ Variables ]
### Indicate which actions to be performed on Mewdeko's service.
if [[ $_CODENAME = "MewdekoRun" ]]; then
lower="disable" # A.1.
upper="Disabling" # B.1.
else
lower="enable" # A.1.
upper="Enabling" # B.1.
fi
systemd_version_tmp=$(systemd --version)
# shellcheck disable=SC2206
systemd_version_tmp=($systemd_version_tmp)
systemd_version=${systemd_version_tmp[1]}
## The contents of Mewdeko's service.
## NOTE: 'StandardOutput' and 'StandardError' no longer supports 'syslog' starting in
## version 246 of systemd.
if ((systemd_version >= 246)); then
service_content="[Unit]
Description=Mewdeko service
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=2
[Service]
Type=simple
User=$USER
WorkingDirectory=$_WORKING_DIR
ExecStart=/bin/bash MewdekoRun
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=Mewdeko
[Install]
WantedBy=multi-user.target"
else
service_content="[Unit]
Description=Mewdeko service
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=2
[Service]
Type=simple
User=$USER
WorkingDirectory=$_WORKING_DIR
ExecStart=/bin/bash MewdekoRun
Restart=on-failure
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=Mewdeko
[Install]
WantedBy=multi-user.target"
fi
#### End of [ Variables ]
########################################################################################
#### [ Main ]
# Check if the service exists.
if [[ -f $_SERVICE ]]; then echo "Updating '$_SERVICE_NAME'..."
else echo "Creating '$_SERVICE_NAME'..."
fi
{
# Create/update the service.
echo "$service_content" | sudo tee "$_SERVICE" &>/dev/null \
&& sudo systemctl daemon-reload
} || {
echo "${_RED}Failed to create '$_SERVICE_NAME'" >&2
echo "${_CYAN}This service must exist for Mewdeko to work${_NC}"
read -rp "Press [Enter] to return to the installer menu"
exit 3
}
## Disable/enable the service.
echo "$upper '$_SERVICE_NAME'..."
sudo systemctl "$lower" "$_SERVICE_NAME" || {
echo "${_RED}Failed to $lower '$_SERVICE_NAME'" >&2
echo "${_CYAN}This service must be ${lower}d in order to use this run mode${_NC}"
read -rp "Press [Enter] to return to the installer menu"
exit 3
}
# Check if 'MewdekoRun' exists.
if [[ -f MewdekoRun ]]; then
echo "Updating 'MewdekoRun'..."
## Create 'MewdekoRun' if it doesn't exist.
else
echo "Creating 'MewdekoRun'..."
touch MewdekoRun
sudo chmod +x MewdekoRun
fi
## Add the code required to run Mewdeko in the background, to 'MewdekoRun'.
if [[ $_CODENAME = "MewdekoRun" ]]; then
printf '%s\n' \
"#!/bin/bash" \
"" \
"_code_name_=\"MewdekoRun\"" \
"" \
"echo \"Running Mewdeko in the background\"" \
"echo \"Starting Mewdeko...\"" \
"cd $_WORKING_DIR/Mewdeko/src/Mewdeko" \
"dotnet run -c Release || {" \
" echo \"An error occurred when trying to start Mewdeko\"" \
" echo \"Exiting...\"" \
" exit 1" \
"}" \
"echo \"Stopping Mewdeko...\"" \
"cd $_WORKING_DIR" > MewdekoRun
## Add code required to run Mewdeko in the background with auto restart, to
## 'MewdekoRun'.
else
printf '%s\n' \
"#!/bin/bash" \
"" \
"_code_name_=\"MewdekoRunAR\"" \
"" \
"echo \"Running Mewdeko in the background with auto restart\"" \
"echo \"Starting Mewdeko...\"" \
"" \
"while true; do" \
" if [[ -d $_WORKING_DIR/Mewdeko/src/Mewdeko ]]; then" \
" cd $_WORKING_DIR/Mewdeko/src/Mewdeko || {" \
" echo \"Failed to change working directory to '$_WORKING_DIR/Mewdeko/src/Mewdeko'\" >&2" \
" echo \"Ensure that the working directory inside of '/etc/systemd/system/mewdeko.service' is correct\"" \
" echo \"Exiting...\"" \
" exit 1" \
" }" \
" else" \
" echo \"'$_WORKING_DIR/Mewdeko/src/Mewdeko' doesn't exist\"" \
" exit 1" \
" fi" \
"" \
" dotnet run -c Release || {" \
" echo \"An error occurred when trying to start Mewdeko\"" \
" echo \"Exiting...\"" \
" exit 1" \
" }" \
"" \
" echo \"Waiting for 5 seconds...\"" \
" sleep 5" \
" echo \"Restarting Mewdeko...\"" \
"done" \
"" \
"echo \"Stopping Mewdeko...\"" > MewdekoRun
fi
## Restart the service if it is currently running.
if [[ $_SERVICE_STATUS = "active" ]]; then
echo "Restarting '$_SERVICE_NAME'..."
sudo systemctl restart "$_SERVICE_NAME" || {
echo "${_RED}Failed to restart '$_SERVICE_NAME'${_NC}" >&2
read -rp "Press [Enter] to return to the installer menu"
exit 3
}
## Start the service if it is NOT currently running.
else
echo "Starting '$_SERVICE_NAME'..."
sudo systemctl start "$_SERVICE_NAME" || {
echo "${_RED}Failed to start '$_SERVICE_NAME'${_NC}" >&2
read -rp "Press [Enter] to return to the installer menu"
exit 3
}
fi
_WATCH_SERVICE_LOGS "runner"
#### End of [ Variables ]
########################################################################################