[bigupdate] systemd new management

This commit is contained in:
xdrm-brackets 2017-07-23 23:27:51 +02:00
parent 98615df45f
commit bad5b28848
8 changed files with 211 additions and 169 deletions

View File

@ -283,36 +283,51 @@ step7(){
# (2) Create .service lib files () # (2) Create .service lib files ()
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Create sats-install service # # (1) Create sats-boot service #
echo " (.) Create sats-boot service";
echo " - /lib/systemd/system";
sudo cp ./utility/sats-boot.service /mnt/lib/systemd/system/sats-boot.service;
# (2) Create sats-install service #
echo " (.) Create sats-install service"; echo " (.) Create sats-install service";
echo " - /lib/systemd/system"; echo " - /lib/systemd/system";
sudo cp ./utility/sats-install.service /mnt/lib/systemd/system/sats-install.service; sudo cp ./utility/sats-install.service /mnt/lib/systemd/system/sats-install.service;
# (2) Create sats-update service # # (3) Create sats-update service #
echo " (.) Create sats-update service"; echo " (.) Create sats-update service";
echo " - /lib/systemd/system"; echo " - /lib/systemd/system";
sudo cp ./utility/sats-update.service /mnt/lib/systemd/system/sats-update.service; sudo cp ./utility/sats-update.service /mnt/lib/systemd/system/sats-update.service;
# (3) Create sats-loop service # # (4) Create sats-loop service #
echo " (.) Create sats-loop service"; echo " (.) Create sats-loop service";
echo " - /lib/systemd/system"; echo " - /lib/systemd/system";
sudo cp ./utility/sats-loop.service /mnt/lib/systemd/system/sats-loop.service; sudo cp ./utility/sats-loop.service /mnt/lib/systemd/system/sats-loop.service;
# (4) Create sats-dwc@ service # # (5) Create sats-sync target #
echo " (.) Create sats-sync target";
echo " - /lib/systemd/system";
sudo cp ./utility/sats-sync.target /mnt/lib/systemd/system/sats-sync.target
# (6) Create sats-sync path #
echo " (.) Create sats-sync path";
echo " - /lib/systemd/system";
sudo cp ./utility/sats-sync.path /mnt/lib/systemd/system/sats-sync.path;
# (7) Create sats-update timer #
echo " (.) Create sats-update timer";
echo " - /lib/systemd/system";
sudo cp ./utility/sats-update.timer /mnt/lib/systemd/system/sats-update.timer;
# (8) Create sats-dwc@ service #
echo " (.) Create sats-dwc@ service"; echo " (.) Create sats-dwc@ service";
echo " - /lib/systemd/system"; echo " - /lib/systemd/system";
sudo cp ./utility/sats-dwc@.service /mnt/lib/systemd/system/sats-dwc@.service; sudo cp ./utility/sats-dwc@.service /mnt/lib/systemd/system/sats-dwc@.service;
# (5) Create firewall@ service # # (9) Create firewall@ service #
echo " (.) Create firewall@ service"; echo " (.) Create firewall@ service";
echo " - /lib/systemd/system"; echo " - /lib/systemd/system";
sudo cp ./utility/firewall@.service /mnt/lib/systemd/system/firewall@.service; sudo cp ./utility/firewall@.service /mnt/lib/systemd/system/firewall@.service;
# (6) Create sats-update timer #
echo " (.) Create sats-update timer";
echo " - /lib/systemd/system";
cat ./utility/sats-update.timer | sudo tee /mnt/lib/systemd/system/sats-update.timer > /dev/null;
# (3) Install services # (3) Install services
#--------------------------------------------------------# #--------------------------------------------------------#
@ -323,38 +338,46 @@ step7(){
echo " (.) Emulate \`systemctl enable sats-loop.service\`"; echo " (.) Emulate \`systemctl enable sats-loop.service\`";
sudo ln -fs /lib/systemd/system/sats-loop.service /mnt/etc/systemd/system/multi-user.target.wants/sats-loop.service; sudo ln -fs /lib/systemd/system/sats-loop.service /mnt/etc/systemd/system/multi-user.target.wants/sats-loop.service;
# (3) Enable sats-dwc@wlan0 at startup # # (3) Enable sats-sync path at startup #
echo " (.) Emulate \`systemctl enable sats-sync.path\`";
sudo ln -fs /lib/systemd/system/sats-sync.path /mnt/etc/systemd/system/multi-user.target.wants/sats-sync.path;
# (4) Enable sats-update timer at startup #
echo " (.) Emulate \`systemctl enable sats-update.timer\`";
sudo ln -fs /lib/systemd/system/sats-update.timer /mnt/etc/systemd/system/multi-user.target.wants/sats-update.timer;
# (5) Enable sats-dwc@wlan0 at startup #
echo " (.) Emulate \`systemctl enable sats-dwc@wlan0.service\`"; echo " (.) Emulate \`systemctl enable sats-dwc@wlan0.service\`";
sudo ln -fs /lib/systemd/system/sats-dwc@.service /mnt/etc/systemd/system/multi-user.target.wants/sats-dwc@wlan0.service; sudo ln -fs /lib/systemd/system/sats-dwc@.service /mnt/etc/systemd/system/multi-user.target.wants/sats-dwc@wlan0.service;
# (4) Enable firewall@default at startup # # (6) Enable firewall@default at startup #
echo " (.) Emulate \`systemctl enable firewall@default.service\`"; echo " (.) Emulate \`systemctl enable firewall@default.service\`";
sudo ln -fs /lib/systemd/system/firewall@.service /mnt/etc/systemd/system/multi-user.target.wants/firewall@default.service; sudo ln -fs /lib/systemd/system/firewall@.service /mnt/etc/systemd/system/multi-user.target.wants/firewall@default.service;
# (5) Enable sats-update timer at startup #
echo " (.) Emulate \`systemctl enable sats-update.timer\`";
sudo ln -fs /lib/systemd/system/sats-update.timer /mnt/etc/systemd/system/multi-user.target.wants/sats-update.timer;
# (4) Create target scripts # (4) Create target scripts
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Create sats-install script # # (1) Create sats-boot script #
echo " (.) Create sats-boot script";
sudo cp ./utility/sats-boot /mnt/service/sats-boot;
# (2) Create sats-install script #
echo " (.) Create sats-install script"; echo " (.) Create sats-install script";
sudo cp ./utility/sats-install /mnt/service/sats-install; sudo cp ./utility/sats-install /mnt/service/sats-install;
# (2) Create sats-update script # # (3) Create sats-update script #
echo " (.) Create sats-update script"; echo " (.) Create sats-update script";
cat ./utility/sats-update | sudo tee /mnt/service/sats-update > /dev/null; cat ./utility/sats-update | sudo tee /mnt/service/sats-update > /dev/null;
# (3) Create sats-loop script # # (4) Create sats-loop script #
echo " (.) Create sats-loop script"; echo " (.) Create sats-loop script";
cat ./utility/sats-loop | sudo tee /mnt/service/sats-loop > /dev/null; cat ./utility/sats-loop | sudo tee /mnt/service/sats-loop > /dev/null;
# (4) Create sats-dwc@wlan0 script # # (5) Create sats-dwc@wlan0 script #
echo " (.) Create sats-dwc@wlan0 script"; echo " (.) Create sats-dwc@wlan0 script";
cat ./utility/wlan0.dwc | sed "s/\*\*\*SALT\*\*\*/$WIFI_SALT/" | sed "s/\*\*\*PEPPER\*\*\*/$WIFI_PEPPER/" | sudo tee /mnt/etc/wpa_supplicant/wlan0.dwc > /dev/null; cat ./utility/wlan0.dwc | sed "s/\*\*\*SALT\*\*\*/$WIFI_SALT/" | sed "s/\*\*\*PEPPER\*\*\*/$WIFI_PEPPER/" | sudo tee /mnt/etc/wpa_supplicant/wlan0.dwc > /dev/null;
# (5) Create firewall@default script # # (6) Create firewall@default script #
echo " (.) Create firewall@default script"; echo " (.) Create firewall@default script";
sudo mkdir -p /mnt/opt/firewall; sudo mkdir -p /mnt/opt/firewall;
cat ./utility/default.fw | sudo tee /mnt/opt/firewall/default.fw > /dev/null; cat ./utility/default.fw | sudo tee /mnt/opt/firewall/default.fw > /dev/null;
@ -365,29 +388,14 @@ step7(){
echo " (.) Set up permissions"; echo " (.) Set up permissions";
# (1) Services scripts # # (1) Services scripts #
echo " - sats-install (owner: pi)"; echo " - /service (sats:pi 750)";
sudo chown 1000:1000 /mnt/service/sats-install; sudo chown -R 666:1000 /mnt/service/*;
sudo chmod 770 /mnt/service/sats-install; sudo chmod -R 750 /mnt/service/*;
echo " - sats-update (ownder: sats)"; # (2) target dir #
sudo chown 666:666 /mnt/service/sats-update; echo " - /target (sats:pi 750)";
sudo chmod 770 /mnt/service/sats-update; sudo chown -R 666:1000 /mnt/target;
sudo chmod -R 750 /mnt/target;
echo " - sats-loop (ownder: sats)";
sudo chown 666:666 /mnt/service/sats-loop;
sudo chmod 770 /mnt/service/sats-loop;
echo " - sats-dwc@wlan0 (ownder: pi)";
sudo chown 1000:1000 /mnt/etc/wpa_supplicant/wlan0.dwc;
sudo chmod 770 /mnt/etc/wpa_supplicant/wlan0.dwc;
echo " - /service (ownder: sats)";
sudo chown 666:666 /mnt/service/*;
sudo chmod 775 /mnt/service/*;
echo " - /target (ownder: sats)";
sudo chown 666:666 /mnt/target;
sudo chmod 775 /mnt/target;
echo "<<< done"; echo "<<< done";

50
clone/utility/sats-boot Normal file
View File

@ -0,0 +1,50 @@
#!/bin/sh
#@@@@#
LOGFILE="/home/sats/satsd/log/install.log";
plog(){
sudo -u sats tee -a /home/sats/satsd/log/install.log > /dev/null;
}
slog(){
echo "$1" | plog;
}
failexit(){
slog " > failed";
exit 127;
}
# [1] Notify boot (first or normal) #
echo;
test ! -e /target/sync && echo "First Boot" | plog;
test -e /target/sync && echo "Normal Boot" | plog;
slog "============";
# [2] Start ssh service
slog " * 1. Starting ssh service";
sudo systemctl start ssh \
&& slog " > done" \
|| failexit;
# [3] Set random passwords for 'sats'
echo " * 2. Changing sats password" | plog;
RANDOM_PASS=$(tr -cd A-Za-z0-9_ < /dev/urandom | head -c 256);
echo -ne "$RANDOM_PASS\n$RANDOM_PASS\n" | sudo passwd sats;
test $? -eq 0 \
&& slog " > done" \
|| failexit;
# [4] Set random passwords for 'pi' itself
echo " * 3. Changing pi password" | plog;
RANDOM_PASS=$(tr -cd A-Za-z0-9_ < /dev/urandom | head -c 256);
echo -ne "$RANDOM_PASS\n$RANDOM_PASS\n" | sudo passwd pi;
test $? -eq 0 \
&& slog " > done" \
|| failexit;

View File

@ -20,124 +20,93 @@ failexit(){
# [1] Notify boot (first or normal) #
echo;
test ! -e /target/sync && echo "First Boot" | plog;
test -e /target/sync && echo "Normal Boot" | plog;
slog "============";
# [2] Start ssh service
slog " * 1. Starting ssh service";
sudo systemctl start ssh \
&& slog " > done" \
|| failexit;
# [3] Set random passwords for 'sats'
echo " * 2. Changing sats password" | plog;
RANDOM_PASS=$(tr -cd A-Za-z0-9_ < /dev/urandom | head -c 256);
echo -ne "$RANDOM_PASS\n$RANDOM_PASS\n" | sudo passwd sats;
test $? -eq 0 \
&& slog " > done" \
|| failexit;
# [4] Set random passwords for 'pi' itself
echo " * 3. Changing pi password" | plog;
RANDOM_PASS=$(tr -cd A-Za-z0-9_ < /dev/urandom | head -c 256);
echo -ne "$RANDOM_PASS\n$RANDOM_PASS\n" | sudo passwd pi;
test $? -eq 0 \
&& slog " > done" \
|| failexit;
################################################ ################################################
#### ONLY ON FIRST BOOT #### #### ONLY ON FIRST BOOT ####
################################################ ################################################
if [ ! -e /target/sync ]; then if [ -e /target/sync ]; then
slog " ** sync target already exists";
exit 127;
fi;
# (x) If no network -> exit # # (x) If no network -> exit #
slog " * 4. Checking connectivity"; slog " * 4. Checking connectivity";
test $(systemctl is-active network-online.target) = "active" \ test $(systemctl is-active network-online.target) = "active" \
&& slog " > done" \ && slog " > done" \
|| failexit; || failexit;
# [1] Installation # [1] Installation
#========================================================# #========================================================#
# (1) Try to install necessary packages # (1) Try to install necessary packages
slog " * 5. Updating packages"; slog " * 5. Updating packages";
sudo apt-get update; sudo apt-get update;
test $? -eq 0 \ test $? -eq 0 \
&& slog " > done" \ && slog " > done" \
|| failexit; || failexit;
slog " * 6. Installing necessary packages"; slog " * 6. Installing necessary packages";
sudo apt-get -y install git php5 php5-cli php5-curl python-dev; sudo apt-get -y install git php5 php5-cli php5-curl python-dev;
test $? -eq 0 \ test $? -eq 0 \
&& slog " > done" \ && slog " > done" \
|| failexit; || failexit;
BRANCH=$(sudo cat /home/sats/satsd/conf/machine.branch) > /dev/null; BRANCH=$(sudo cat /home/sats/satsd/conf/machine.branch) > /dev/null;
# (2) With git if installed # # (2) With git if installed #
slog " * 7. Cloning source"; slog " * 7. Cloning source";
if `sudo -u sats test -d /home/sats/satsd/source`; then if `sudo -u sats test -d /home/sats/satsd/source`; then
slog " > done (already cloned)"; slog " > done (already cloned)";
else else
sudo -u sats git clone -b $BRANCH ssh://smmp-server/satsd/git /home/sats/satsd/source \ sudo -u sats git clone -b $BRANCH ssh://smmp-server/satsd/git /home/sats/satsd/source \
&& slog " > done" \ && slog " > done" \
|| failexit; || failexit;
fi; fi;
# (2) Enable SPI device # # (2) Enable SPI device #
echo " * 8. Enabling spi device"; echo " * 8. Enabling spi device";
echo "dtparam=spi=on" | sudo tee -a /boot/config.txt > /dev/null \ echo "dtparam=spi=on" | sudo tee -a /boot/config.txt > /dev/null \
|| failexit; || failexit;
echo "dtoverlay=spi-bcm2708" | sudo tee -a /boot/config.txt > /dev/null \ echo "dtoverlay=spi-bcm2708" | sudo tee -a /boot/config.txt > /dev/null \
|| failexit; || failexit;
slog " > done"; slog " > done";
# (3) Clone SPI python library # # (3) Clone SPI python library #
slog " * 9. Cloning 'SPI-Py' lib"; slog " * 9. Cloning 'SPI-Py' lib";
if [ -d /home/pi/spi-lib ]; then if [ -d /home/pi/spi-lib ]; then
slog " > done (already cloned)"; slog " > done (already cloned)";
else else
git clone https://github.com/lthiery/SPI-Py.git /home/pi/spi-lib \ git clone https://github.com/lthiery/SPI-Py.git /home/pi/spi-lib \
&& slog " > done" \ && slog " > done" \
|| failexit; || failexit;
fi;
# (4) Install SPI python library #
slog " * 10. Installing 'SPI-Py' lib into the system";
cd /home/pi/spi-lib;
sudo python setup.py build \
|| failexit;
sudo python setup.py install \
|| failexit;
slog " > done";
# [2] Synchronization
#========================================================#
# (1) Process sync #
slog " * 11. Synchronizing the SATS with SMMP's server";
test "$(sudo -u sats /home/sats/satsd/source/lib/api/sync)" = "0" \
&& slog " > done" \
|| failexit;
# (2) Create target file #
slog " * 12. Creating target file 'sync'";
sudo -u sats touch /target/sync \
&& slog " > done" \
|| failexit;
# (3) Reboot to activate SPI #
slog " * 13. Launching first reboot";
sudo reboot;
fi; fi;
# (4) Install SPI python library #
slog " * 10. Installing 'SPI-Py' lib into the system";
cd /home/pi/spi-lib;
sudo python setup.py build \
|| failexit;
sudo python setup.py install \
|| failexit;
slog " > done";
# [2] Synchronization
#========================================================#
# (1) Process sync #
slog " * 11. Synchronizing the SATS with SMMP's server";
test "$(sudo -u sats /home/sats/satsd/source/lib/api/sync)" = "0" \
&& slog " > done" \
|| failexit;
# (2) Create target file #
slog " * 12. Creating target file 'sync'";
sudo -u sats touch /target/sync \
&& slog " > done" \
|| failexit;
# (3) Reboot to activate SPI #
slog " * 13. Launching first reboot";
sudo reboot;

View File

@ -1,7 +1,7 @@
[Unit] [Unit]
Description=[::SATS::] Install Process Description=[::SATS::] Install Process
Requires=sats-dwc@wlan0.service network-online.target Requires=sats-dwc@wlan0.service sats-boot.service network-online.target
After=sats-dwc@wlan0.service network-online.target After=sats-boot.service sats-dwc@wlan0.service
[Service] [Service]
Type=oneshot Type=oneshot

View File

@ -1,7 +1,7 @@
[Unit] [Unit]
Description=[::SATS::] Daemon Description=[::SATS::] Daemon
Requires=sats-install.service Requires=sats-sync.target
After=sats-install.service After=sats-sync.target
[Service] [Service]
Type=simple Type=simple

View File

@ -0,0 +1,9 @@
[Unit]
Description=[::SATS::] Path watcher
[Path]
PathExists=/target/sync
Unit=sats-sync.target
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,6 @@
[Unit]
Description=[::SATS::] Sync target
Requires=sats-boot.service
Wants=sats-install.service
After=sats-boot.service sats-install.service
AssertPathExists=/target/sync

View File

@ -1,8 +1,8 @@
[Unit] [Unit]
Description=[::SATS::] Update Process Description=[::SATS::] Update Process
Requires=sats-dwc@wlan0.service network-online.target Requires=sats-dwc@wlan0.service network-online.target
After=sats-dwc@wlan0.service network-online.target After=sats-dwc@wlan0.service
Requisite=sats-install.service Requisite=sats-sync.target
[Service] [Service]
Type=oneshot Type=oneshot