-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens-api
executable file
·111 lines (96 loc) · 3.55 KB
/
tokens-api
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
#!/usr/bin/env bash
# systemd Service Installer Script
# Directory of this script (absolute path)
DIR=$(dirname $(realpath $0))
service="tokens-api"
exec_start="$DIR/main.mjs"
working_directory="$DIR"
# user=$USER # by default run service as user who's installing
user="root" # run service as root (need access to letsencrypt certs and port 443)
# override default user from argument to install command (if supplied)
if [ "$1" == "install" ]; then
[ -n "$2" ] && user=$2
fi
service_file="[Unit]
Description=Web API – Tokens for Climate Care
Documentation=https://github.com/studioprocess/tokens-api-node
After=network.target couchdb.service snap.couchdb.server.service
Wants=couchdb.service snap.couchdb.server.service
Conflicts=
[Service]
Environment=
Type=simple
User=$user
ExecStart=$exec_start
WorkingDirectory=$working_directory
Restart=always
[Install]
WantedBy=multi-user.target
"
usage() {
echo "Usage:"
echo " Install/Uninstall: $0 install|remove|uninstall"
echo " Enable/Disable: $0 enable|disable"
echo " Start/Stop/Restart: $0 start|stop|restart"
echo " Status: $0 status"
echo " Logs: $0 log[s] [live]"
echo " Reload Certificate: $0 reload-cert"
exit
}
if [ $# -eq 0 ]; then
usage
fi
# Check if node is installed via snap (needed for logs and status)
# Background: When using the node snap, journalctl won't show node's output for the unit, since snap fires up node in another unit ('snap.node.*')
# See: https://askubuntu.com/questions/1467887/journalctl-doesnt-show-logs-of-a-snap
[ "$(which node)" == "/snap/bin/node" ]
NODE_IS_SNAP=$?
[ $NODE_IS_SNAP -eq 0 ] && SNAP_CMDLINE="$(readlink -f /snap/node/current/bin/node) $DIR/main.mjs"
# PID of our process; 0 if not running (needed for status)
PID=$(systemctl show -p MainPID --value ${service}.service)
if [ $1 == "install" ] ; then
echo "[ Installing Script to /usr/local/bin ]"
sudo ln -sf $(realpath $0) /usr/local/bin/$(basename $0)
echo "[ Installing Service File ]"
echo "$service_file" | sudo tee /etc/systemd/system/${service}.service > /dev/null
echo "[ Enabling Service ]"
sudo systemctl enable $service
echo "Important: Service will be run as user: $user"
elif [ $1 == "remove" ] || [ $1 == "uninstall" ]; then
echo "[ Disabling Service ]"
sudo systemctl disable $service
echo "[ Removing Service File ]"
sudo rm -f /etc/systemd/system/${service}.service
echo "[ Removing Script from /usr/local/bin ]"
sudo rm /usr/local/bin/$(basename $0)
elif [ $1 == "enable" ] ; then
echo "[ Enabling Service ]"
sudo systemctl enable $service
elif [ $1 == "disable" ] ; then
echo "[ Disabling Service ]"
sudo systemctl disable $service
elif [ $1 == "start" ] ; then
echo "[ Starting Service ]"
sudo systemctl start $service
elif [ $1 == "stop" ] ; then
echo "[ Stopping Service ]"
sudo systemctl stop $service
elif [ $1 == "restart" ] ; then
echo "[ Restarting Service ]"
sudo systemctl restart $service
elif [ $1 == "status" ] ; then
echo "[ Service Status ]"
sudo systemctl status $service
[[ $NODE_IS_SNAP -eq 0 && $PID -ne 0 ]] && sudo systemctl status $PID
elif [[ $1 == "logs?" || $1 == "log" ]] ; then
echo "[ Service Logs ]"
args=("-u" "${service}.service" "-e") # use array so we can add args with spaces (_CMDLINE)
[ $NODE_IS_SNAP -eq 0 ] && args=("UNIT=${service}.service" "+" "_CMDLINE=$SNAP_CMDLINE" "-e")
[ "$2" == "live" ] && args+=("-f") # add "follow" argument
sudo journalctl "${args[@]}"
elif [ $1 == "reload-cert" ] ; then
echo "[ Reload Certificate ]"
sudo systemctl kill $service --signal=SIGUSR2
else
usage
fi