-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarmen-pill-init-dev.script
executable file
·230 lines (166 loc) · 8.57 KB
/
karmen-pill-init-dev.script
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
set -e
_log () {
echo "
>==============================================================================
$1
<==============================================================================
"
}
# check run as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
###############################################################################
# LOCALE - set system locale to en_US.UTF-8
###############################################################################
_log "Setting system locale to en_US.UTF-8..."
localectl set-locale LANG=en_US.UTF-8
###############################################################################
# VARIABLES - TODO: add more variables from this script
###############################################################################
KEYS_OUT_FILE=/boot/KARMEN_KEYS.TXT
KEY_RESPONSE="" # will be set later in script
OCTOPRINT_API_KEY="" # will be set later in script
SSH_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13 ; echo '') # new password to be assigned to pi user
OCTOPRINT_KARMEN_USER_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13 ; echo '') # password to be assigned to karmen octoprint user
###############################################################################
# Read KEY_RESPONSE from user input
###############################################################################
_log "Karmen websocket proxy needs key to communicate with the server."
echo "<<<<<<<<<<<<<<< Please provide your Device Key"
read -p "Karmen Device Key: " KEY_RESPONSE
###############################################################################
# Upgrade system
###############################################################################
_log "Upgrading system..."
apt update
apt upgrade -y
###############################################################################
# Enable SPI and install python3-rpi.gpio - for karmen LEDs
###############################################################################
_log "Enabling SPI..."
sed -i "s/#dtparam=spi=on/dtparam=spi=on/" /boot/config.txt
###############################################################################
# Upgrade PIP - system and on octoprint virtualenv
###############################################################################
_log "Upgrading PIP in system..."
python -m pip install --upgrade pip
_log "Upgrading PIP in Octoprint virtualenv..."
/home/pi/oprint/bin/python -m pip install --upgrade pip
###############################################################################
# Install Karmen Octoprint Plugin and LED Plugin
###############################################################################
_log "Installing Karmen proxy plugin..."
runuser -l pi -c '/home/pi/oprint/bin/pip install "https://github.com/fragaria/karmen-octoprint-plugin/archive/refs/heads/main.zip"'
_log "Installing Karmen LED plugin..."
runuser -l pi -c '/home/pi/oprint/bin/pip install "https://github.com/fragaria/karmen-led-octoprint-plugin/archive/refs/heads/main.zip"'
###############################################################################
# Camera resolution
###############################################################################
_log "Changing camera resolution..."
cp /boot/octopi.txt /boot/octopi.txt.bck
tee /boot/octopi.txt > /dev/null <<EOT
camera="usb"
camera_usb_options="-fps 10 -r 1296x972 -quality 15"
EOT
_log "Restarting webcamd..."
systemctl restart webcamd
sleep 5
###############################################################################
# Restart Octoprint
###############################################################################
_log "Restarting Octoprint..."
systemctl restart octoprint.service
sleep 30
###############################################################################
# Run octoprint configuration wizzard
###############################################################################
OCTOPRINT_API_KEY=$(grep -r " key: " /home/pi/.octoprint/config.yaml | awk '{print $2}')
# Access Control - create account:
curl 'http://localhost/plugin/corewizard/acl' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"user":"karmen","pass1":"'$OCTOPRINT_KARMEN_USER_PASSWORD'","pass2":"'$OCTOPRINT_KARMEN_USER_PASSWORD'"}'
# Configure the connectivity check
curl 'http://localhost/api/settings' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"server":{"onlineCheck":{"enabled":true,"interval":15,"host":"1.1.1.1","port":53}}}'
# Disable tracking
curl 'http://localhost/api/settings' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"plugins":{"tracking":{"enabled":false}}}'
# Enable plugin blacklist processing
curl 'http://localhost/api/settings' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"server":{"pluginBlacklist":{"enabled":true}}}'
# Final click
curl 'http://localhost/api/printerprofiles/_default' \
-X 'PATCH' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"profile":{"id":"_default","name":"Default","color":"default","model":"Generic RepRap Printer","volume":{"width":200,"depth":200,"height":200,"formFactor":"rectangular","origin":"lowerleft","custom_box":false},"heatedBed":true,"heatedChamber":false,"extruder":{"count":1,"offsets":[[0,0]],"nozzleDiameter":0.4,"sharedNozzle":false},"axes":{"x":{"speed":6000,"inverted":false},"y":{"speed":6000,"inverted":false},"z":{"speed":200,"inverted":false},"e":{"speed":300,"inverted":false}}}}'
curl 'http://localhost/api/settings' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"temperature":{"profiles":[{"name":"ABS","extruder":210,"bed":100,"chamber":null},{"name":"PLA","extruder":180,"bed":60,"chamber":null}]}}'
curl 'http://localhost/api/setup/wizard' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"handled":["backup","corewizard","tracking"]}'
# Add more nozzles to work with Prusa MMU2
curl 'http://localhost/api/printerprofiles/_default' \
-X 'PATCH' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"profile":{"id":"_default","extruder":{"count":5,"offsets":[[0,0],[0,0],[0,0],[0,0],[0,0]],"sharedNozzle":true}}}'
# Setup webcam api snapshot url
curl 'http://localhost/api/settings' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"webcam":{"snapshotUrl":"http://127.0.0.1/webcam/?action=snapshot"}}'
# Disable octoprint unsupported hardware notiffication
curl 'http://localhost/api/settings' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"plugins":{"pi_support":{"ignore_unrecommended_model":true}}}'
# Set Karmen Device Key
curl 'http://localhost/api/settings' \
-H 'Content-Type: application/json; charset=UTF-8' \
-H 'X-Api-Key: '$OCTOPRINT_API_KEY \
--data-raw '{"plugins":{"karmen":{"karmen_key":"'$KEY_RESPONSE'"}}}'
###############################################################################
# Stop Octoprint
###############################################################################
_log "Stopping Octoprint..."
sleep 20
systemctl stop octoprint.service
sleep 10
###############################################################################
# Change PI user password
###############################################################################
_log "Changing PI user password..."
usermod --password $(echo $SSH_PASSWORD | openssl passwd -1 -stdin) pi
###############################################################################
# Generate keys file
###############################################################################
tee $KEYS_OUT_FILE > /dev/null <<EOT
DEVICE KEY: $KEY_RESPONSE
OCTOPRINT API KEY: $OCTOPRINT_API_KEY
OCTOPRINT USER: karmen
OCTOPRINT PASSWORD: $OCTOPRINT_KARMEN_USER_PASSWORD
PI SSH USER DEFAULT PASSWORD: $SSH_PASSWORD
EOT
# show content $KEYS_OUT_FILE content to user before reboot, so one can assign printer key in Karmen immediatelly
_log "Your secrets from $KEYS_OUT_FILE (don't share with anyone):"
cat $KEYS_OUT_FILE
###############################################################################
# All done, reboot system
###############################################################################
_log "All done. Raspberry will reboot. Happy Karming!"
sleep 5
sudo reboot