Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add postrename module #128

Merged
merged 1 commit into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
export DIST_NAME=MainsailOS
export DIST_VERSION=0.6.1
export BASE_IMAGE_ENLARGEROOT=2500
export MODULES="base,pkgupgrade,mainsailos(network,piconfig,klipper,is_req_preinstall,moonraker,mainsail,crowsnest,sonar,password-for-sudo)"
export MODULES="base,pkgupgrade,mainsailos(network,piconfig,klipper,is_req_preinstall,moonraker,mainsail,crowsnest,sonar,password-for-sudo),postrename"
export BASE_RELEASE_COMPRESS=yes
export BASE_IMAGE_RESIZEROOT=600
12 changes: 12 additions & 0 deletions src/modules/postrename/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
#### Post Rename
####
#### Written by Stephan Wendel aka KwadFan <me@stephanwe.de>
#### Copyright 2021
#### https://github.com/mainsail-crew/MainsailOS
####
#### This File is distributed under GPLv3
####
# shellcheck disable=all

# Intentionaly left blank
195 changes: 195 additions & 0 deletions src/modules/postrename/filesystem/root/postrename
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#!/bin/bash
#### Post Rename
####
#### Written by Stephan Wendel aka KwadFan <me@stephanwe.de>
#### Copyright 2021
#### https://github.com/mainsail-crew/MainsailOS
####
#### This File is distributed under GPLv3
####

## Error handling
set -e

# Colored Output
RED="\e[31m"
YEL="\e[33m"
GRE="\e[32m"
WHITE="\e[97m"
NOC="\e[0m"

### Setup
SERVICES=(moonraker klipper nginx sonar crowsnest)
SYSTEMD_DIR="/etc/systemd/system"
DEFAULT_USER="$(grep "1000" /etc/passwd | awk -F ':' '{print $1}')"


## Helper funcs
### Mangle services
function stop_services {
for srv in "${SERVICES[@]}"; do
if [ "$(systemctl is-active "${srv}.service")" != "inactive" ]; then
systemctl stop "${srv}.service"
fi
done
}

function start_services {
for srv in "${SERVICES[@]}"; do
systemctl start "${srv}.service"
done
}

### Clean up
function cleanup {
# Cleanup self
rm -rf "${0}"
# Cleanup rc.local
sed -i '/\/postrename/d' /etc/rc.local
}

### Change nginx root
function change_www_root {
bash -c "
sed -i 's|/home/pi/mainsail|/home/${DEFAULT_USER}/mainsail|g' \
/etc/nginx/sites-available/mainsail
"
}


### change username in service files
function change_service_user {
### Filter nginx service first!
local -a servicefile
servicefile=( "${SERVICES[@]/nginx}" )

for i in "${servicefile[@]}"; do
if [ -n "${i}" ]; then
sed -i 's/pi/'"${DEFAULT_USER}"'/g' "${SYSTEMD_DIR}/${i}.service"
fi
done
}

function relocate_venv {
local -a venvs
venvs=(klippy-env moonraker-env)

for venv in "${venvs[@]}"; do
sudo -u "${DEFAULT_USER}" \
virtualenv --relocatable -p /usr/bin/python3 "/home/${DEFAULT_USER}/${venv}"
done
}

function patch_polkit_rules {
local polkit_dir polkit_legacy_dir polkit_usr_dir
polkit_legacy_dir="/etc/polkit-1/localauthority/50-local.d"
polkit_dir="/etc/polkit-1/rules.d"
polkit_usr_dir="/usr/share/polkit-1/rules.d"
if [ -f "${polkit_dir}/moonraker.rules" ]; then
sed -i 's/pi/'"${DEFAULT_USER}"'/g' "${polkit_dir}/moonraker.rules"
fi
if [ -f "${polkit_usr_dir}/moonraker.rules" ]; then
sed -i 's/pi/'"${DEFAULT_USER}"'/g' "${polkit_usr_dir}/moonraker.rules"
fi
if [ -f "${polkit_legacy_dir}/10-moonraker.pkla" ]; then
sed -i 's/pi/'"${DEFAULT_USER}"'/g' "${polkit_legacy_dir}/10-moonraker.pkla"
fi
}

function patch_cn_logrotate {
if [ -f "/etc/logrotate.d/crowsnest" ]; then
sed -i 's/pi/'"${DEFAULT_USER}"'/g' "/etc/logrotate.d/crowsnest"
fi
}

## Fix broken links
function fix_broken_links {
local -a brokenlinks
local path
brokenlinks=(crowsnest sonar)
path="/usr/local/bin"

for bl in "${brokenlinks[@]}"; do
if [ -h "${path}/${bl}" ]; then
rm -rf "${path:?}/${bl}"
fi
find "/home/${DEFAULT_USER}" -type f -iname "${bl}" -print0 | \
xargs -0 -I {} ln -s {} "${path}/${bl}"
done
}

function main {
local cmdltxt
cmdltxt="/boot/cmdline.txt"

## Make sure init_resize.sh and firstrun.sh are finished
if [ "$(grep -c "init" "${cmdltxt}")" != "0" ] && \
[ "$(grep -c "systemd.run.*" "${cmdltxt}")" != "0" ]; then
echo -e "[${RED}ERROR${NOC}] \
${WHITE}Sdcard resize and firtrun are not finished yet ... [ABORT]${NOC}"
## Make sure not failing rc.local
return 0
fi
## Abort if user = pi
if [ "${DEFAULT_USER}" == "pi" ]; then
echo -e "[${YEL}SKIPPED${NOC}] \
${WHITE}User is ${DEFAULT_USER}! Nothing to do ...${NOC}"
cleanup
exit 0
fi

echo -e "\n\t${WHITE}Trying to fix user rename ...${NOC}"
## Stop services
echo -en "${WHITE}Stopping all related services ...${NOC}\r"
stop_services
echo -e "${WHITE}Stopping all related services ...${NOC}[${GRE}OK${NOC}]"
## Change www root
echo -en "${WHITE}Try changing path of nginx root ...${NOC}\r"
change_www_root
echo -e "${WHITE}Try changing path of nginx root ...${NOC}[${GRE}OK${NOC}]"
## patch service files
echo -en "${WHITE}Patching service files ...${NOC}\r"
change_service_user
echo -e "${WHITE}Patching service files ...${NOC}[${GRE}OK${NOC}]"
## relocate venvs
echo -e "${WHITE}Trying to relocate venv's ...${NOC}"
relocate_venv
echo -e "${WHITE}Trying to relocate venv's ...${NOC}[${GRE}OK${NOC}]"
## patch polkit rules
echo -e "${WHITE}Patching moonraker's polkit rules ...${NOC}"
patch_polkit_rules
echo -e "${WHITE}Patching moonraker's polkit rules ...${NOC}[${GRE}OK${NOC}]"
## patch crowsnest logrotate
echo -e "${WHITE}Patching crowsnest logrotate ...${NOC}"
patch_cn_logrotate
echo -e "${WHITE}Patching crowsnest logrotate ...${NOC}[${GRE}OK${NOC}]"
## fix broken links
echo -en "${WHITE}Fix broken symlinks ...${NOC}\r"
fix_broken_links
echo -e "${WHITE}Fix broken symlinks ...${NOC}[${GRE}OK${NOC}]"
## do a short break
sleep 2
## reload daemons
echo -en "${WHITE}Apply service file changes ...${NOC}\r"
systemctl daemon-reload
echo -e "${WHITE}Apply service file changes ...${NOC}[${GRE}OK${NOC}]"
## do a short break
sleep 2
## restart services
echo -e "${WHITE}Trying to restart services ...${NOC}"
start_services
## wait 30sec to come up
echo -e "${WHITE}Waiting for service restart completed ...${NOC}"
sleep 30
## Cleanup
echo -e "${WHITE}Cleanup files, remove postrename ...${NOC}"
cleanup
sleep 2
## reboot system
echo -e "${WHITE}Reboot system ...${NOC}"
reboot
}

### MAIN
main
exit 0
27 changes: 27 additions & 0 deletions src/modules/postrename/start_chroot_script
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
#!/bin/bash
#### Post Rename
####
#### Written by Stephan Wendel aka KwadFan <me@stephanwe.de>
#### Copyright 2021
#### https://github.com/mainsail-crew/MainsailOS
####
#### This File is distributed under GPLv3
####
# shellcheck disable=all

# Source error handling, leave this in place
set -xe

# Source CustomPIOS common.sh
source /common.sh
install_cleanup_trap

## unpack postrename
unpack filesystem/root / root
## set executable
sudo chmod 0755 /postrename

## run postrename via rc.local
sed -i 's@exit 0@@' /etc/rc.local
echo -e "/postrename\nexit 0\n" | tee -a /etc/rc.local