Skip to content

Commit

Permalink
Scripts: hopefully fix remaining issues
Browse files Browse the repository at this point in the history
  • Loading branch information
miklschmidt committed Nov 23, 2024
1 parent 9663cb5 commit 82be6f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
15 changes: 6 additions & 9 deletions configuration/scripts/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
# performed outside of a function so that other scripts sourcing this in will run this by default

# Get the real user (not root) when script is run with sudo
# Debug output
echo "Debug: SUDO_USER=$SUDO_USER"
echo "Debug: USER=$USER"
echo "Debug: id -u=$(id -u)"
echo "Debug: EUID=$EUID"

echo -e "\n\n###### Loading RatOS environment"

if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
echo "Case 1: Running with sudo as non-root user"
echo "Running with sudo as non-root user"
REAL_USER=$SUDO_USER
REAL_HOME=$(getent passwd "$SUDO_USER" | cut -d: -f6)
elif [ "$EUID" -ne 0 ]; then
echo "Case 2: Running as non-root without sudo"
echo "Running as non-root without sudo"
REAL_USER=$USER
REAL_HOME=$HOME
else
echo "Case 3: Running as root directly"
echo "Running as root directly, defaulting to pi user."
REAL_USER="pi"
REAL_HOME="/home/pi"
fi

if [ "$REAL_USER" = "root" ]; then
echo "Fatal Error: Running as root, this is not supported, exiting..." >&2
echo "Fatal Error: Unable to determine non-root user, please run as a normal user or use sudo, exiting..." >&2
exit 1
fi

Expand Down
22 changes: 13 additions & 9 deletions src/scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ __EOF

patch_log_rotation() {
if [ -e /etc/logrotate.d/ratos-configurator ]; then
if grep -q "/printer_data/logs/configurator.log" /etc/logrotate.d/ratos-configurator; then
if grep -q "${RATOS_PRINTER_DATA_DIR}/logs/ratos-configurator.log" /etc/logrotate.d/ratos-configurator; then
report_status "Patching log rotation"
sudo sed -i 's|rotate 4|rotate 3|g' /etc/logrotate.d/ratos-configurator
sudo sed -i 's|/printer_data/logs/configurator.log"|/printer_data/logs/ratos-configurator.log"|g' /etc/logrotate.d/ratos-configurator
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
fi
else
install_logrotation
Expand All @@ -145,16 +145,17 @@ patch_log_rotation() {

symlink_configuration() {
report_status "Symlinking configuration"
[ -z "$RATOS_PRINTER_DATA_DIR" ] && { echo "Error: RATOS_PRINTER_DATA_DIR not set"; return 1; }
[ -z "$BASE_DIR" ] && { echo "Error: BASE_DIR not set"; return 1; }
[ -z "$RATOS_PRINTER_DATA_DIR" ] && { echo "Error: RATOS_PRINTER_DATA_DIR not set" >&2; return 1; }
[ -z "$BASE_DIR" ] && { echo "Error: BASE_DIR not set" >&2; return 1; }

sudo=""
[ "$EUID" -ne 0 ] && sudo="sudo"

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

install_udev_rule()
{
report_status "Installing udev rule"

sudo=""
[ "$EUID" -ne 0 ] && sudo="sudo"

if [ ! -e /etc/udev/rules.d/97-ratos.rules ]; then
if [ ! -L /etc/udev/rules.d/97-ratos.rules ]; then
report_status "Installing RatOS udev rule"
$sudo rm -f /etc/udev/rules.d/97-ratos.rules
$sudo ln -s "$SCRIPT_DIR/ratos.rules" /etc/udev/rules.d/97-ratos.rules
fi
if [ ! -e /etc/udev/rules.d/97-vaoc.rules ]; then
if [ ! -L /etc/udev/rules.d/97-vaoc.rules ]; then
report_status "Installing VAOC udev rule"
$sudo rm -f /etc/udev/rules.d/97-vaoc.rules
$sudo ln -s "$SCRIPT_DIR/vaoc.rules" /etc/udev/rules.d/97-vaoc.rules
fi
}
Expand Down

0 comments on commit 82be6f8

Please sign in to comment.