Skip to content

Commit 73c4dfc

Browse files
committed
Scripts: improve environment variable handling and fix script paths
1 parent bb62996 commit 73c4dfc

16 files changed

+135
-72
lines changed

.github/workflows/publish-monorepo-test.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ jobs:
7878
run: rm -rf __tests__ app pages components coverage data helpers hooks moonraker env recoil server utils zods test-setup.ts vitest.config.mts tsconfig.vitest.json copy-files-from-to.json components.json postcss.config.js prettier.config.mjs tailwind.config.ts
7979

8080
- name: Rename src/ to app/
81-
working-directory: ./src
81+
working-directory: ./
8282
run: mv src app
8383

84-
- name: Push
85-
uses: s0/git-publish-subdir-action@develop
86-
env:
87-
REPO: self
88-
BRANCH: monorepo-test-deployment # The branch name where you want to push the assets
89-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # GitHub will automatically add this - you don't need to bother getting a token
90-
MESSAGE: "Build: ({sha}) {msg}" # The commit message
84+
- name: Publish to monorepo-test-deployment
85+
uses: JamesIves/github-pages-deploy-action@v4
86+
with:
87+
folder: .
88+
git-config-name: Mikkel Schmidt
89+
git-config-email: [email protected]
90+
branch: monorepo-test-deployment # The branch name where you want to push the assets

configuration/scripts/beacon-update.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if [ "$EUID" -ne 0 ]
44
exit
55
fi
66

7-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
7+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
88
echo "##### Running beacon firmware update script"
99
# shellcheck source=./configuration/scripts/ratos-common.sh
1010
source "$SCRIPT_DIR"/ratos-common.sh

configuration/scripts/environment.sh

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,89 @@
11
#!/bin/bash
22

3-
# create ~/.ratos.env if file does not exist using sane defaults on a ratos pi image.
43
# performed outside of a function so that other scripts sourcing this in will run this by default
5-
if [ ! -f ~/.ratos.env ]; then
6-
echo "No .ratos.env file found in $HOME, determining default values..."
7-
if [ "$(id -u)" -eq 0 ]; then
8-
echo "Running as root, defaulting to pi as RATOS_USER and RATOS_USERGROUP..."
9-
RATOS_USER="pi";
10-
else
11-
echo "Running as non-root user, defaulting to $USER as RATOS_USER and RATOS_USERGROUP..."
12-
RATOS_USER=$USER;
13-
fi
14-
HOME_DIR="/home/$RATOS_USER";
15-
echo "Creating ~/.ratos.env file with default values..."
16-
17-
cat <<EOF > ~/.ratos.env
18-
RATOS_USERNAME="${RATOS_USER}"
19-
RATOS_USERGROUP="${RATOS_USER}"
20-
RATOS_PRINTER_DATA_DIR="${HOME_DIR}/printer_data"
21-
MOONRAKER_DIR="${HOME_DIR}/moonraker"
22-
KLIPPER_DIR="${HOME_DIR}/klipper"
23-
KLIPPER_ENV="${HOME_DIR}/klippy-env"
24-
BEACON_DIR="${HOME_DIR}/beacon"
4+
5+
# Get the real user (not root) when script is run with sudo
6+
7+
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
8+
REAL_USER=$SUDO_USER
9+
REAL_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
10+
elif [ "$EUID" -ne 0 ]; then
11+
REAL_USER=$USER
12+
REAL_HOME=$HOME
13+
else
14+
REAL_USER="pi"
15+
REAL_HOME="/home/pi"
16+
fi
17+
18+
if [ "$REAL_USER" = "root" ]; then
19+
echo "Fatal Error: Unable to determine non-root user, please run as a normal user or use sudo, exiting..." >&2
20+
exit 1
21+
fi
22+
23+
envFile="/usr/local/etc/.ratos.env"
24+
userEnvFile="${REAL_HOME}/.ratos.env"
25+
26+
# create $envFile if file does not exist using sane defaults on a ratos pi image.
27+
if [ ! -f "$envFile" ]; then
28+
echo "$envFile not found, determining default values..."
29+
CMD="tee"
30+
[ "$EUID" -ne 0 ] && CMD="sudo tee"
31+
RATOS_USER=$REAL_USER
32+
33+
$CMD "$envFile" > /dev/null <<EOF
34+
RATOS_USERNAME=${RATOS_USER}
35+
RATOS_USERGROUP=${RATOS_USER}
36+
RATOS_PRINTER_DATA_DIR=${REAL_HOME}/printer_data
37+
MOONRAKER_DIR=${REAL_HOME}/moonraker
38+
KLIPPER_DIR=${REAL_HOME}/klipper
39+
KLIPPER_ENV=${REAL_HOME}/klippy-env
40+
BEACON_DIR=${REAL_HOME}/beacon
2541
EOF
42+
chmod a+r "$envFile"
43+
echo "Created $envFile with default values:"
44+
cat "$envFile"
45+
echo "You can create $userEnvFile to override these values for $RATOS_USER or modify $envFile to change them for all users."
2646
fi
2747

28-
if [ -f ~/.ratos.env ] ; then
29-
echo "Loading RatOS environment data from $(realpath ~/.ratos.env)"
30-
# shellcheck disable=SC1090
31-
set -a && source ~/.ratos.env && set +a
32-
else
33-
echo "Fatal Error: Unable to load RatOS environment data, exiting..." >&2
48+
profileLink="/etc/profile.d/ratos.sh"
49+
localProfileLink="$REAL_HOME/.profile.d/ratos.sh"
50+
# Create symlink in profile.d if directory exists
51+
if [ -d "$REAL_HOME/.profile.d" ]; then
52+
if [ ! -e "$localProfileLink" ]; then
53+
echo "Creating shell profile symlink $localProfileLink to $envFile"
54+
rm -f "$localProfileLink"
55+
ln -s "$envFile" "$localProfileLink" || echo "Warning: Failed to create profile.d symlink"
56+
fi
57+
fi
58+
# Create symlink in system profile.d if directory exists
59+
if [ -d "/etc/profile.d" ]; then
60+
if [ ! -e "$profileLink" ]; then
61+
echo "Creating shell profile symlink $profileLink to $envFile"
62+
sudo rm -f "$profileLink"
63+
sudo ln -s "$envFile" "$profileLink" || echo "Warning: Failed to create profile.d symlink"
64+
fi
65+
fi
66+
67+
# Function to load env files
68+
load_env() {
69+
local file="$1"
70+
if [ -f "$file" ]; then
71+
while IFS='=' read -r key value; do
72+
# Skip comments and empty lines
73+
[[ $key =~ ^[[:space:]]*# ]] && continue
74+
[[ -z "$key" ]] && continue
75+
76+
# Only set if not already defined
77+
if [ -z "${!key}" ]; then
78+
export "$key=$value"
79+
fi
80+
done < "$file"
81+
fi
82+
}
83+
84+
if [ ! -f "$envFile" ] && [ ! -f "$userEnvFile" ] ; then
85+
echo "Fatal Error: Unable to load RatOS environment, neither $envFile nor $userEnvFile found, exiting..." >&2
3486
exit 1
3587
fi
88+
[ -f "$envFile" ] && load_env "$envFile"
89+
[ "$EUID" -ne 0 ] && [ -f "$userEnvFile" ] && load_env "$userEnvFile"

configuration/scripts/generate-belt-tension-graph.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
set -e -u
3-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
44

55
# shellcheck source=./configuration/scripts/ratos-common.sh
66
source "$SCRIPT_DIR"/ratos-common.sh

configuration/scripts/generate-shaper-graph-x.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
44

55
# shellcheck source=./configuration/scripts/ratos-common.sh
66
source "$SCRIPT_DIR"/ratos-common.sh

configuration/scripts/generate-shaper-graph-y.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
44

55
# shellcheck source=./configuration/scripts/ratos-common.sh
66
source "$SCRIPT_DIR"/ratos-common.sh

configuration/scripts/moonraker-update.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
44
# shellcheck source=./configuration/scripts/moonraker-ensure-policykit-rules.sh
55
source "$SCRIPT_DIR"/moonraker-ensure-policykit-rules.sh
66
ensure_moonraker_policiykit_rules

configuration/scripts/ratos-common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
33

44
# shellcheck source=./configuration/scripts/environment.sh
55
source "$SCRIPT_DIR"/environment.sh

configuration/scripts/ratos-install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
PKGLIST="python3-numpy python3-matplotlib curl git"
55

6-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
77
# shellcheck source=./configuration/scripts/ratos-common.sh
88
source "$SCRIPT_DIR"/ratos-common.sh
99

configuration/scripts/ratos-post-install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
44
# shellcheck source=./configuration/scripts/ratos-common.sh
55
source "$SCRIPT_DIR"/ratos-common.sh
66

configuration/scripts/ratos-update.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if [ "$EUID" -ne 0 ]
44
exit
55
fi
66

7-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
7+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
88
# shellcheck source=./configuration/scripts/ratos-common.sh
99
source "$SCRIPT_DIR"/ratos-common.sh
1010
# shellcheck source=./configuration/scripts/moonraker-ensure-policykit-rules.sh
@@ -39,12 +39,6 @@ fix_klippy_env_ownership()
3939
fi
4040
}
4141

42-
restart_configurator()
43-
{
44-
report_status "Restarting configurator..."
45-
systemctl restart ratos-configurator
46-
}
47-
4842
symlink_extensions()
4943
{
5044
report_status "Symlinking klippy extensions"
@@ -74,4 +68,3 @@ register_ratos_kinematics
7468
unregister_vaoc_led
7569
symlink_extensions
7670
update_beacon_fw
77-
restart_configurator

scripts/post-merge.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
# This file is only here for backwards compatibility with the old location of the post-merge githook.
4+
# It will be removed in a future version.
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
6+
7+
sudo "$SCRIPT_DIR/update.sh"

scripts/update.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env bash
22

3-
# This file is only here for backwards compatibility with the old location of the update script (run via githook).
3+
# This file is only here for backwards compatibility with the old location of the post-merge githook.
44
# It will be removed in a future version.
5-
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "$(realpath -- "${BASH_SOURCE[0]}")" )" &> /dev/null && pwd )
66

7-
exec "$SCRIPT_DIR/app/scripts/update.sh"
7+
"$SCRIPT_DIR"/../app/scripts/update.sh
8+
# Important to run this after update.sh to ensure the CLI is up to date.
9+
"$SCRIPT_DIR"/../configuration/scripts/ratos-update.sh

src/scripts/common.sh

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env bash
22
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3-
GIT_DIR=$SCRIPT_DIR/../../.git
43
SRC_DIR=$(realpath "$SCRIPT_DIR/..")
5-
GIT_DIR=$(realpath "$GIT_DIR")
4+
BASE_DIR=$(realpath "$SRC_DIR/..")
5+
GIT_DIR=$BASE_DIR/.git
66

7-
source "$SRC_DIR/configuration/scripts/environment.sh"
7+
source "$BASE_DIR/configuration/scripts/environment.sh"
88

99
report_status()
1010
{
@@ -57,9 +57,9 @@ install_or_update_service_file()
5757
pnpm_install() {
5858
report_status "Installing pnpm dependencies..."
5959
pushd "$SRC_DIR" || exit 1
60-
if [ -d "$GIT_DIR/node_modules" ]; then
60+
if [ -d "$BASE_DIR/node_modules" ]; then
6161
report_status "Moving node_modules from git directory to src directory"
62-
mv "$GIT_DIR/node_modules" "$SRC_DIR"
62+
mv "$BASE_DIR/node_modules" "$SRC_DIR"
6363
fi
6464
if [ "$EUID" -eq 0 ]; then
6565
# Check if node_modules is owned by root and delete
@@ -133,10 +133,10 @@ __EOF
133133

134134
patch_log_rotation() {
135135
if [ -e /etc/logrotate.d/ratos-configurator ]; then
136-
if grep -q "/printer_data/logs/configurator.log" /etc/logrotate.d/ratos-configurator; then
136+
if grep -q "${RATOS_PRINTER_DATA_DIR}/logs/ratos-configurator.log" /etc/logrotate.d/ratos-configurator; then
137137
report_status "Patching log rotation"
138138
sudo sed -i 's|rotate 4|rotate 3|g' /etc/logrotate.d/ratos-configurator
139-
sudo sed -i 's|/printer_data/logs/configurator.log"|/printer_data/logs/ratos-configurator.log"|g' /etc/logrotate.d/ratos-configurator
139+
sudo sed -i "s|${RATOS_PRINTER_DATA_DIR}/logs/configurator.log|${RATOS_PRINTER_DATA_DIR}/logs/ratos-configurator.log|g" /etc/logrotate.d/ratos-configurator
140140
fi
141141
else
142142
install_logrotation
@@ -145,16 +145,17 @@ patch_log_rotation() {
145145

146146
symlink_configuration() {
147147
report_status "Symlinking configuration"
148-
[ -z "$RATOS_PRINTER_DATA_DIR" ] && { echo "Error: RATOS_PRINTER_DATA_DIR not set"; return 1; }
149-
[ -z "$GIT_DIR" ] && { echo "Error: GIT_DIR not set"; return 1; }
148+
[ -z "$RATOS_PRINTER_DATA_DIR" ] && { echo "Error: RATOS_PRINTER_DATA_DIR not set" >&2; return 1; }
149+
[ -z "$BASE_DIR" ] && { echo "Error: BASE_DIR not set" >&2; return 1; }
150150

151151
sudo=""
152152
[ "$EUID" -ne 0 ] && sudo="sudo"
153153

154154
target="${RATOS_PRINTER_DATA_DIR}/config/RatOS"
155-
if [ ! -L "$target" ] || [ ! "$(readlink "$target")" = "$GIT_DIR/configuration" ]; then
156-
$sudo rm -rf "$target" || { echo "Failed to remove old configuration"; return 1; }
157-
$sudo ln -s "$GIT_DIR/configuration" "$target" || { echo "Failed to create symlink"; return 1; }
155+
if [ ! -L "$target" ] || [ ! "$(readlink "$target")" = "$BASE_DIR/configuration" ]; then
156+
$sudo rm -rf "$target" || { echo "Failed to remove old configuration" >&2; return 1; }
157+
$sudo ln -s "$BASE_DIR/configuration" "$target" || { echo "Failed to create symlink" >&2; return 1; }
158+
$sudo chown -R "${RATOS_USERNAME}:${RATOS_USERGROUP}" "$target" || { echo "Failed to change ownership of configuration" >&2; return 1; }
158159
echo "Configuration symlink created successfully"
159160
fi
160161
}
@@ -184,15 +185,18 @@ verify_users()
184185

185186
install_udev_rule()
186187
{
187-
report_status "Installing udev rule"
188188

189189
sudo=""
190190
[ "$EUID" -ne 0 ] && sudo="sudo"
191191

192-
if [ ! -e /etc/udev/rules.d/97-ratos.rules ]; then
192+
if [ ! -L /etc/udev/rules.d/97-ratos.rules ]; then
193+
report_status "Installing RatOS udev rule"
194+
$sudo rm -f /etc/udev/rules.d/97-ratos.rules
193195
$sudo ln -s "$SCRIPT_DIR/ratos.rules" /etc/udev/rules.d/97-ratos.rules
194196
fi
195-
if [ ! -e /etc/udev/rules.d/97-vaoc.rules ]; then
197+
if [ ! -L /etc/udev/rules.d/97-vaoc.rules ]; then
198+
report_status "Installing VAOC udev rule"
199+
$sudo rm -f /etc/udev/rules.d/97-vaoc.rules
196200
$sudo ln -s "$SCRIPT_DIR/vaoc.rules" /etc/udev/rules.d/97-vaoc.rules
197201
fi
198202
}

src/scripts/setup.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ensure_pnpm_installation
2626
install_logrotation
2727
pnpm_install
2828
disable_telemetry
29-
install_or_update_service_file
3029
install_udev_rule
3130
install_cli
31+
symlink_configuration
32+
install_or_update_service_file

src/scripts/update.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ ensure_sudo_command_whitelisting
3232
ensure_pnpm_installation
3333
pnpm_install
3434
install_cli
35-
install_udev_rule root
36-
patch_log_rotation
35+
install_udev_rule
36+
patch_log_rotation
37+
symlink_configuration
38+
install_or_update_service_file

0 commit comments

Comments
 (0)