-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
98 lines (73 loc) · 2.56 KB
/
setup.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
#!/bin/bash
#The first time setup script
chmod +x *
# Check if the script is being run as root
if [ "$(id -u)" -eq 0 ]; then
echo "Script is running as root."
else
echo "Script is not running as root. Please run the script with sudo or as root."
exit 1
fi
#ask if config is set
echo "If config.json hasn't been set, now is a good time to do so"
echo "Continue (Y/N):"
read response
# Convert the response to uppercase for case-insensitive comparison
response=$(echo "$response" | tr '[:lower:]' '[:upper:]')
if [ "$response" = "Y" ]; then
echo "Continuing..."
# Add commands to execute when the answer is Yes (Y)
elif [ "$response" = "N" ]; then
echo "Exiting..."
# Add commands to execute when the answer is No (N)
else
echo "Invalid response. Please enter Y or N."
fi
#Ensure depencies
echo "Installing depencies"
sudo apt update
sudo apt upgrade -y
sudo apt install -y python3 wget unzip python3-pip
echo "installing pip3 depencies"
sudo rm -rf /usr/lib/python3.11/EXTERNALLY-MANAGED
pip3 install requests gpiozero discord_webhook
#create the service
echo "Creating systemd service"
current_directory=$(pwd)
cat >/etc/systemd/system/ruokalista-aanestyspaate.service <<EOL
[Unit]
Description=ruokalista-aanestyspaate
[Service]
WorkingDirectory=${current_directory}
ExecStart=python3 -u ${current_directory}/main.py
Restart=on-failure
RestartSec=100
[Install]
WantedBy=multi-user.target
EOL
sudo systemctl daemon-reload
#enable and start the service
echo "setting service to autostart and starting service"
sudo systemctl enable ruokalista-aanestyspaate.service
sudo systemctl start ruokalista-aanestyspaate.service
#add cronjobs
echo "Adding updateToken cronjob"
./addUpdateTokenCronjob.sh
echo "Adding Uptime cronjob"
./addUptimeCronjob.sh
echo "Adding Service restart cronjob"
./addServiceRestartCronjob.sh
#add permissions for everyone to start and stop the service
# Add an entry to sudoers file using visudo
echo "Adding entries to sudoers file..."
echo "ALL ALL=(ALL) NOPASSWD: /bin/sudo systemctl start ruokalista-aanestyspaate.service" >> /etc/sudoers
echo "ALL ALL=(ALL) NOPASSWD: /bin/sudo systemctl stop ruokalista-aanestyspaate.service" >> /etc/sudoers
echo "ALL ALL=(ALL) NOPASSWD: /bin/sudo systemctl status ruokalista-aanestyspaate.service" >> /etc/sudoers
# Check for any syntax errors in the sudoers file
echo "Checking sudoers file for syntax errors..."
visudo -c
if [ $? -eq 0 ]; then
echo "Entry added to sudoers file successfully."
else
echo "There was an error in the sudoers file. Please check and correct the syntax."
fi