From bad5b288482fe6ac22c51f067159760b11948220 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 23 Jul 2017 23:27:51 +0200 Subject: [PATCH] [bigupdate] systemd new management --- clone/clone | 100 +++++++------- clone/utility/sats-boot | 50 +++++++ clone/utility/sats-install | 203 ++++++++++++----------------- clone/utility/sats-install.service | 4 +- clone/utility/sats-loop.service | 4 +- clone/utility/sats-sync.path | 9 ++ clone/utility/sats-sync.target | 6 + clone/utility/sats-update.service | 4 +- 8 files changed, 211 insertions(+), 169 deletions(-) create mode 100644 clone/utility/sats-boot create mode 100644 clone/utility/sats-sync.path create mode 100644 clone/utility/sats-sync.target diff --git a/clone/clone b/clone/clone index 104328e..b1db5a3 100755 --- a/clone/clone +++ b/clone/clone @@ -283,35 +283,50 @@ step7(){ # (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 " - /lib/systemd/system"; 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 " - /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 " - /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 # - echo " (.) Create sats-dwc@ service"; + # (5) Create sats-sync target # + echo " (.) Create sats-sync target"; echo " - /lib/systemd/system"; - sudo cp ./utility/sats-dwc@.service /mnt/lib/systemd/system/sats-dwc@.service; + sudo cp ./utility/sats-sync.target /mnt/lib/systemd/system/sats-sync.target - # (5) Create firewall@ service # - echo " (.) Create firewall@ service"; + # (6) Create sats-sync path # + echo " (.) Create sats-sync path"; echo " - /lib/systemd/system"; - sudo cp ./utility/firewall@.service /mnt/lib/systemd/system/firewall@.service; + sudo cp ./utility/sats-sync.path /mnt/lib/systemd/system/sats-sync.path; - # (6) Create sats-update timer # + # (7) 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; + sudo cp ./utility/sats-update.timer /mnt/lib/systemd/system/sats-update.timer; + + # (8) Create sats-dwc@ service # + echo " (.) Create sats-dwc@ service"; + echo " - /lib/systemd/system"; + sudo cp ./utility/sats-dwc@.service /mnt/lib/systemd/system/sats-dwc@.service; + + # (9) Create firewall@ service # + echo " (.) Create firewall@ service"; + echo " - /lib/systemd/system"; + sudo cp ./utility/firewall@.service /mnt/lib/systemd/system/firewall@.service; # (3) Install services @@ -323,38 +338,46 @@ step7(){ 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; - # (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\`"; 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\`"; 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 #--------------------------------------------------------# - # (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"; sudo cp ./utility/sats-install /mnt/service/sats-install; - # (2) Create sats-update script # + # (3) Create sats-update script # echo " (.) Create sats-update script"; 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"; 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"; 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"; sudo mkdir -p /mnt/opt/firewall; cat ./utility/default.fw | sudo tee /mnt/opt/firewall/default.fw > /dev/null; @@ -365,29 +388,14 @@ step7(){ echo " (.) Set up permissions"; # (1) Services scripts # - echo " - sats-install (owner: pi)"; - sudo chown 1000:1000 /mnt/service/sats-install; - sudo chmod 770 /mnt/service/sats-install; + echo " - /service (sats:pi 750)"; + sudo chown -R 666:1000 /mnt/service/*; + sudo chmod -R 750 /mnt/service/*; - echo " - sats-update (ownder: sats)"; - sudo chown 666:666 /mnt/service/sats-update; - sudo chmod 770 /mnt/service/sats-update; - - 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; + # (2) target dir # + echo " - /target (sats:pi 750)"; + sudo chown -R 666:1000 /mnt/target; + sudo chmod -R 750 /mnt/target; echo "<<< done"; diff --git a/clone/utility/sats-boot b/clone/utility/sats-boot new file mode 100644 index 0000000..bdfcf78 --- /dev/null +++ b/clone/utility/sats-boot @@ -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; \ No newline at end of file diff --git a/clone/utility/sats-install b/clone/utility/sats-install index 2128753..8618bf6 100644 --- a/clone/utility/sats-install +++ b/clone/utility/sats-install @@ -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 #### ################################################ -if [ ! -e /target/sync ]; then - - # (x) If no network -> exit # - slog " * 4. Checking connectivity"; - test $(systemctl is-active network-online.target) = "active" \ - && slog " > done" \ - || failexit; - - - # [1] Installation - #========================================================# - # (1) Try to install necessary packages - slog " * 5. Updating packages"; - sudo apt-get update; - test $? -eq 0 \ - && slog " > done" \ - || failexit; - - slog " * 6. Installing necessary packages"; - sudo apt-get -y install git php5 php5-cli php5-curl python-dev; - test $? -eq 0 \ - && slog " > done" \ - || failexit; - - - BRANCH=$(sudo cat /home/sats/satsd/conf/machine.branch) > /dev/null; - - # (2) With git if installed # - slog " * 7. Cloning source"; - if `sudo -u sats test -d /home/sats/satsd/source`; then - slog " > done (already cloned)"; - else - sudo -u sats git clone -b $BRANCH ssh://smmp-server/satsd/git /home/sats/satsd/source \ - && slog " > done" \ - || failexit; - fi; - - - - # (2) Enable SPI device # - echo " * 8. Enabling spi device"; - echo "dtparam=spi=on" | sudo tee -a /boot/config.txt > /dev/null \ - || failexit; - echo "dtoverlay=spi-bcm2708" | sudo tee -a /boot/config.txt > /dev/null \ - || failexit; - slog " > done"; - - # (3) Clone SPI python library # - slog " * 9. Cloning 'SPI-Py' lib"; - if [ -d /home/pi/spi-lib ]; then - slog " > done (already cloned)"; - else - git clone https://github.com/lthiery/SPI-Py.git /home/pi/spi-lib \ - && slog " > done" \ - || 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; - +if [ -e /target/sync ]; then + slog " ** sync target already exists"; + exit 127; fi; + +# (x) If no network -> exit # +slog " * 4. Checking connectivity"; +test $(systemctl is-active network-online.target) = "active" \ + && slog " > done" \ + || failexit; + + +# [1] Installation +#========================================================# +# (1) Try to install necessary packages +slog " * 5. Updating packages"; +sudo apt-get update; +test $? -eq 0 \ + && slog " > done" \ + || failexit; + +slog " * 6. Installing necessary packages"; +sudo apt-get -y install git php5 php5-cli php5-curl python-dev; +test $? -eq 0 \ + && slog " > done" \ + || failexit; + + +BRANCH=$(sudo cat /home/sats/satsd/conf/machine.branch) > /dev/null; + +# (2) With git if installed # +slog " * 7. Cloning source"; +if `sudo -u sats test -d /home/sats/satsd/source`; then + slog " > done (already cloned)"; +else + sudo -u sats git clone -b $BRANCH ssh://smmp-server/satsd/git /home/sats/satsd/source \ + && slog " > done" \ + || failexit; +fi; + + + +# (2) Enable SPI device # +echo " * 8. Enabling spi device"; +echo "dtparam=spi=on" | sudo tee -a /boot/config.txt > /dev/null \ + || failexit; +echo "dtoverlay=spi-bcm2708" | sudo tee -a /boot/config.txt > /dev/null \ + || failexit; +slog " > done"; + +# (3) Clone SPI python library # +slog " * 9. Cloning 'SPI-Py' lib"; +if [ -d /home/pi/spi-lib ]; then + slog " > done (already cloned)"; +else + git clone https://github.com/lthiery/SPI-Py.git /home/pi/spi-lib \ + && slog " > done" \ + || 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; \ No newline at end of file diff --git a/clone/utility/sats-install.service b/clone/utility/sats-install.service index 6d3b24c..1aed882 100644 --- a/clone/utility/sats-install.service +++ b/clone/utility/sats-install.service @@ -1,7 +1,7 @@ [Unit] Description=[::SATS::] Install Process -Requires=sats-dwc@wlan0.service network-online.target -After=sats-dwc@wlan0.service network-online.target +Requires=sats-dwc@wlan0.service sats-boot.service network-online.target +After=sats-boot.service sats-dwc@wlan0.service [Service] Type=oneshot diff --git a/clone/utility/sats-loop.service b/clone/utility/sats-loop.service index b6d50f4..bb93ca7 100644 --- a/clone/utility/sats-loop.service +++ b/clone/utility/sats-loop.service @@ -1,7 +1,7 @@ [Unit] Description=[::SATS::] Daemon -Requires=sats-install.service -After=sats-install.service +Requires=sats-sync.target +After=sats-sync.target [Service] Type=simple diff --git a/clone/utility/sats-sync.path b/clone/utility/sats-sync.path new file mode 100644 index 0000000..083993d --- /dev/null +++ b/clone/utility/sats-sync.path @@ -0,0 +1,9 @@ +[Unit] +Description=[::SATS::] Path watcher + +[Path] +PathExists=/target/sync +Unit=sats-sync.target + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/clone/utility/sats-sync.target b/clone/utility/sats-sync.target new file mode 100644 index 0000000..5407394 --- /dev/null +++ b/clone/utility/sats-sync.target @@ -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 \ No newline at end of file diff --git a/clone/utility/sats-update.service b/clone/utility/sats-update.service index 1cea3c3..861e6f1 100644 --- a/clone/utility/sats-update.service +++ b/clone/utility/sats-update.service @@ -1,8 +1,8 @@ [Unit] Description=[::SATS::] Update Process Requires=sats-dwc@wlan0.service network-online.target -After=sats-dwc@wlan0.service network-online.target -Requisite=sats-install.service +After=sats-dwc@wlan0.service +Requisite=sats-sync.target [Service] Type=oneshot