-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·79 lines (69 loc) · 2.66 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
#!/bin/sh
# check for root (user id of 0)
[ `id -u` -eq 0 ] && echo "this script should not be run as root" && exit 1
# get discord token from user
echo "get the bot's token from the discord dev portal and enter it here: "
read DISCORDTOKEN
# make a config file for inportant variables
echo "creating config file 'config.py'"
cat > config.py <<CONFIGFILE
class Settings():
PREFIX = "%"
DISCORD_TOKEN = "$DISCORDTOKEN"
BOT_DIR = "$PWD/"
class Messages():
# time in seconds for what interval to refresh server info
REFRESH_TIME = 60
class Categories():
REFRESH_TIME = 300
class Topics():
REFRESH_TIME = 300
class Status():
REFRESH_TIME = 60
# if supplied, the address to be tracked for the bot's status
SERVER_ADDRESS = None
CONFIGFILE
# change permisssions of config file so that other users may not read it
chmod 660 config.py
### install requirements
echo "installing pip with apt. . ."
sudo apt update && sudo apt install python3-pip python3.8-venv -y || { echo "failed to install pip and venv, aborting. . ."; exit 1; }
# make a new venv for the bot
python3.8 -m venv bot_venv
# install python packages within the venv
echo "installing requirements with pip3. . ."
./bot_venv/bin/python3 -m pip install -r requirements.txt || { echo "failed to install pip packages, aborting. . ."; exit 1; }
### use systemd to run playerCountBot on system startup
# ask the user if they want run on startup
printf 'Should playercountBot run on system startup [y/N]? '
read SHOULDSTARTUP
[ "$SHOULDSTARTUP" != "${SHOULDSTARTUP#[Yy]}" ] && echo "Setting up to run on startup. . ." || { echo "Installation complete."; exit 0; }
# make a unit file for this systemd service
echo "creating unit file 'playerCountBot.service'"
cat > playerCountBot.service <<UNITFILE
[Unit]
Description=Runs playerCountBot script on startup
Wants=network-online.target
After=network-online.target
[Service]
User=$(whoami)
Group=$(id -gn)
WorkingDirectory=$PWD/
ExecStart="$PWD/bot_venv/bin/python3" "$PWD/main.py"
[Install]
WantedBy=multi-user.target
UNITFILE
# put the unitfile in its place w/ systemd
sudo mv playerCountBot.service /etc/systemd/system/playerCountBot.service
# reload systemd so it can find this newly created service
echo "reloding systemctl daemon"
sudo systemctl daemon-reload
# enable this service in systemd
echo "enabling playerCountBot.service"
sudo systemctl enable playerCountBot.service
echo "playerCountBot service has been added and enabled"
echo "playerCountBot will be automatically run on startup from now on"
echo ""
echo "to dissable running on startup type 'sudo systemctl disable playerCountBot.service'"
echo ""
exit 0