-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinstall.sh
executable file
·182 lines (157 loc) · 5.01 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
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
#!/bin/bash
SCRIPT=$(realpath "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
OLD_SERVICE_FILE="/etc/systemd/system/OpenNept4une.service"
SERVICE_FILE="/etc/systemd/system/display.service"
SCRIPT_PATH="$SCRIPTPATH/display.py"
VENV_PATH="$SCRIPTPATH/venv"
LOG_FILE="/var/log/display.log"
MOONRAKER_ASVC="$HOME/printer_data/moonraker.asvc"
R=$'\e[1;91m' # Red ${R}
G=$'\e[1;92m' # Green ${G}
Y=$'\e[1;93m' # Yellow ${Y}
M=$'\e[1;95m' # Magenta ${M}
C=$'\e[96m' # Cyan ${C}
NC=$'\e[0m' # No Color ${NC}
reset_env() {
sudo rm -rf "$SCRIPTPATH/venv"
rm -rf "$SCRIPTPATH/__pycache__"
}
install_env() {
sudo apt update
sudo apt install python3-venv -y
# Create and activate a Python virtual environment
echo "Creating and activating a virtual environment..."
python3 -m venv "$SCRIPTPATH/venv"
source "$SCRIPTPATH/venv/bin/activate"
# Install required Python packages
echo "Installing required Python packages..."
pip install -r "$SCRIPTPATH/requirements.txt"
# Deactivate the virtual environment
echo "Deactivating the virtual environment..."
deactivate
echo "Environment setup completed."
}
stop_service() {
if systemctl is-active --quiet OpenNept4une; then
# Stop the service silently
sudo service OpenNept4une stop >/dev/null 2>&1
# Disable the service silently
sudo service OpenNept4une disable >/dev/null 2>&1
sudo rm -f $OLD_SERVICE_FILE
fi
if systemctl is-active --quiet display; then
# Stop the service silently
sudo service display stop >/dev/null 2>&1
fi
}
disable_service() {
echo "Disabling the service..."
sudo systemctl disable display.service
}
create_service() {
# Create the systemd service file
echo "Creating systemd service file at $SERVICE_FILE..."
cat <<EOF | sudo tee $SERVICE_FILE > /dev/null
[Unit]
Description=OpenNept4une TouchScreen Display Service
After=klipper.service klipper-mcu.service moonraker.service
Wants=klipper.service moonraker.service
Documentation=man:display(8)
[Service]
ExecStartPre=/bin/sleep 10
ExecStart=$SCRIPTPATH/venv/bin/python $SCRIPTPATH/display.py
WorkingDirectory=$SCRIPTPATH
Restart=on-failure
RestartSec=10
User=$USER
ProtectSystem=full
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd to read new service file
echo "Reloading systemd..."
sudo systemctl daemon-reload
# Enable and start the service
echo "Enabling and starting the service..."
sudo systemctl enable display.service
sudo systemctl start display.service
}
give_moonraker_control() {
echo "Allowing Moonraker to control display service"
grep -qxF 'display' $MOONRAKER_ASVC || echo 'display' >> $MOONRAKER_ASVC
sudo service moonraker restart
}
moonraker_update_manager() {
update_selection="display"
config_file="$HOME/printer_data/config/moonraker.conf"
if [ "$update_selection" = "OpenNept4une" ]; then
new_lines="[update_manager $update_selection]\n\
type: git_repo\n\
primary_branch: $current_branch\n\
path: $OPENNEPT4UNE_DIR\n\
is_system_service: False\n\
origin: $OPENNEPT4UNE_REPO"
elif [ "$update_selection" = "display" ]; then
current_display_branch=$(git -C "$DISPLAY_CONNECTOR_DIR" symbolic-ref --short HEAD 2>/dev/null)
new_lines="[update_manager $update_selection]\n\
type: git_repo\n\
primary_branch: $current_display_branch\n\
path: $DISPLAY_CONNECTOR_DIR\n\
virtualenv: $DISPLAY_CONNECTOR_DIR/venv\n\
requirements: requirements.txt\n\
origin: $DISPLAY_CONNECTOR_REPO"
else
echo -e "${R}Invalid argument. Please specify either 'OpenNept4une' or 'display_connector'.${NC}"
return 1
fi
# Check if the lines exist in the config file
if grep -qF "[update_manager $update_selection]" "$config_file"; then
# Lines exist, update them
perl -pi.bak -e "BEGIN{undef $/;} s|\[update_manager $update_selection\].*?((?:\r*\n){2}\|$)|$new_lines\$1|gs" "$config_file"
sync
else
# Lines do not exist, append them to the end of the file
echo -e "\n$new_lines" >> "$config_file"
fi
}
if [ ! -f "$SCRIPT_PATH" ]; then
echo "${R}Error: Script $SCRIPT_PATH not found.${NC}"
exit 1
fi
HELP="Please specify either 'full', 'env', 'service-install', 'service-disable' or 'moonraker'."
if [ "$1" ]; then
case "$1" in
"full")
reset_env
install_env
stop_service
create_service
give_moonraker_control
;;
"env")
reset_env
install_env
;;
"service-install")
stop_service
create_service
;;
"service-disable")
stop_service
disable_service
;;
"moonraker")
give_moonraker_control
moonraker_update_manager
;;
*)
echo "${R}Invalid argument $1. ${Y}$HELP${NC}"
exit 1
;;
esac
else
echo "${R}No arguments provided. ${Y}$HELP${NC}"
exit 1
fi