From 0e9e40f154ca3aa1b524db5d105034cde2809b5e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 8 May 2017 16:22:11 +0200 Subject: [PATCH] [bigupdate] Updated /service and /target folders + renames sats-boot to sats-install + whole new service hierarchy + 2 states in /target --- clone/clone | 96 +++++++++++-------- clone/utility/interfaces | 2 +- clone/utility/{sats-boot => sats-install} | 11 ++- ...sats-boot.service => sats-install.service} | 6 +- clone/utility/sats-loop | 19 +--- clone/utility/sats-loop.service | 10 +- clone/utility/sats-update | 9 ++ clone/utility/sats-update.service | 5 +- 8 files changed, 87 insertions(+), 71 deletions(-) rename clone/utility/{sats-boot => sats-install} (84%) rename clone/utility/{sats-boot.service => sats-install.service} (71%) create mode 100644 clone/utility/sats-update diff --git a/clone/clone b/clone/clone index 245de7d..dca5784 100755 --- a/clone/clone +++ b/clone/clone @@ -16,7 +16,6 @@ echo "<<< done"; # [!] Check parameter : device file #========================================================# - echo ">>> [!] Checking parameter : device"; # (1) Check parameter existence # test $# -lt 1 && echo "Missing parameter : device" && exit; @@ -48,7 +47,7 @@ step1(){ test -z "$mounted_partitions" && echo "<<< done" && step2; for mounted in $mounted_partitions; do - + read -p " (!) umount $mounted (y/n) [n]" unmount; test -n "$unmount" && test $unmount = "y" && sudo umount $mounted 2> /dev/null > /dev/null && echo " > unmounted"; @@ -100,7 +99,7 @@ step3(){ # (2) Burning image into disk # # if GZIP if [ "IMAGE_ZIP" = "1" ]; then - + echo " - using gunzip"; @@ -112,7 +111,7 @@ step3(){ ( dd if=$IMAGE_FILE | pv -s $(du -bs $IMAGE_FILE | awk '{print $1}') | sudo dd of=$DEV bs=4M ) \ || $( echo "<<< error: dd command failed" && exit ); - + fi; @@ -199,7 +198,7 @@ step6(){ # (3) Create ssh file system # echo " (.) Init ssh folder (/home/sats/.ssh)"; sudo mkdir -p /mnt/home/sats/.ssh; - + # (4) Add both keys to sats files # echo " (.) Add keys to ssh folder"; @@ -231,7 +230,7 @@ step6(){ echo -e "Host\tsmmp-server\n\tHostname\t$SERVER_HOSTNAME\n\tUser\t$SERVER_USERNAME\n\tIdentityFile\t~/.ssh/id_ecdsa\n\tStrictHostKeyChecking\tno" | sudo tee /mnt/home/sats/.ssh/config > /dev/null; sudo chown 666:666 /mnt/home/sats/.ssh/config; sudo chmod 444 /mnt/home/sats/.ssh/config; - + echo "<<< done"; @@ -251,35 +250,64 @@ step7(){ echo -e "\n>>> [7] Set up systemd background"; + # (0) Create useful folders # + echo " (.) Create useful folder"; + echo " - /service"; + sudo mkdir /mnt/service; + echo " - /target"; + sudo mkdir /mnt/target; + sudo chown 666:666 /mnt/service/*; + sudo chown 666:666 /mnt/target; + sudo chmod 777 /mnt/service/*; + sudo chmod 777 /mnt/target; + # (1) Create link in order to be handled # echo " (.) Emulate \`systemctl set-default multi-user.target\`"; sudo ln -fs /lib/systemd/system/multi-user.target /mnt/etc/systemd/system/default.target; - #echo " (.) Set autologin for tty1.service"; - #sudo mkdir -pv /mnt/etc/systemd/system/getty@tty1.service.d; - #sudo touch /mnt/etc/systemd/system/getty@tty1.service.d/autologin.conf; - #cat ./utility/autologin_overload.conf | sudo tee /mnt/etc/systemd/system/getty@tty1.service.d/autologin.conf > /dev/null; - - # (2) Install sats-boot service # - echo " (.) Create sats-boot service"; + # (2) Install sats-install service # + echo " (.) Create sats-install service"; echo " - /lib/systemd/system"; - sudo cp ./utility/sats-boot.service /mnt/lib/systemd/system/sats-boot.service; - - - echo " (.) Create sats-boot script"; - sudo cp ./utility/sats-boot /mnt/home/pi/sats-boot; - - sudo touch /mnt/home/pi/first_boot; - sudo chown 1000:1000 /mnt/home/pi/sats-boot; - sudo chown 1000:1000 /mnt/home/pi/first_boot; - sudo chmod 770 /mnt/home/pi/sats-boot; - sudo chmod 770 /mnt/home/pi/first_boot; + sudo cp ./utility/sats-install.service /mnt/lib/systemd/system/sats-install.service; # (3) Install sats-boot service # echo " (.) Create sats-update service"; echo " - /lib/systemd/system"; sudo cp ./utility/sats-update.service /mnt/lib/systemd/system/sats-update.service; + # (4) Install 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; + + # (5) Enable startup service unit # + echo " (.) Emulate \`systemctl enable sats-loop.service\`"; + sudo mkdir -p /mnt/etc/systemd/system/multi-user.target.wants; + sudo ln -fs /lib/systemd/system/sats-loop.service /mnt/etc/systemd/system/multi-user.target.wants/sats-loop.service; + + # (6) Create sats-install script # + echo " (.) Create sats-install script"; + sudo cp ./utility/sats-install /mnt/service/sats-install; + + # (8) Create sats-update script # + echo " (.) Create sats-update script"; + cat ./utility/sats-update | sudo tee /mnt/service/sats-update > /dev/null; + + # (8) Create sats-loop script # + echo " (.) Create sats-loop script"; + cat ./utility/sats-loop | sudo tee /mnt/service/sats-loop > /dev/null; + + # (9) Set up permissions # + echo " (.) Set up permissions"; + echo " - sats-install @pi"; + sudo chown 1000:1000 /mnt/service/sats-install; + sudo chmod 770 /mnt/service/sats-install; + echo " - sats-update @sats"; + sudo chown 666:666 /mnt/service/sats-update; + sudo chmod 770 /mnt/service/sats-update; + echo " - sats-loop @sats"; + sudo chown 666:666 /mnt/service/sats-loop; + sudo chmod 770 /mnt/service/sats-loop; echo "<<< done"; @@ -347,27 +375,13 @@ step9(){ echo "$WAREHOUSE_TOKEN" | sudo tee /mnt/home/sats/satsd/conf/warehouse.token > /dev/null; echo "$MACHINE_ID;$UNLOCK_CODE" | tee ./server/created > /dev/null; - - # (3) Set up SATS botting script # - echo " (.) Set up SATS booting script"; - cat ./utility/sats-loop | sudo tee /mnt/home/sats/satsd/sats-loop > /dev/null; - - # (4) Set up service unit # - echo " (.) Set up service unit (sats-loop.service)"; - cat ./utility/sats-loop.service | sudo tee /mnt/lib/systemd/system/sats-loop.service > /dev/null; - - # (5) Enable startup service unit # - echo " (.) Emulate \`systemctl enable sats-loop.service\`"; - sudo mkdir -p /mnt/etc/systemd/system/multi-user.target.wants; - sudo ln -fs /lib/systemd/system/sats-loop.service /mnt/etc/systemd/system/multi-user.target.wants/sats-loop.service; - - # (6) Adjust permissions # + # (3) Adjust permissions # echo " (.) Adjust permissions"; sudo chown -R 666:666 /mnt/home/sats/satsd; sudo chmod -R 770 /mnt/home/sats/satsd; - - # (7) Add entry in /etc/hosts if LOCAL_TEST not empty # + + # (4) Add entry in /etc/hosts if LOCAL_TEST not empty # test -n "$LOCAL_TEST" && echo -e "\n$LOCAL_TEST\n" | sudo tee -a /mnt/etc/hosts > /dev/null; echo "<<< done"; diff --git a/clone/utility/interfaces b/clone/utility/interfaces index 626861b..e0c57ad 100644 --- a/clone/utility/interfaces +++ b/clone/utility/interfaces @@ -17,4 +17,4 @@ iface eth0 inet manual allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf - up /bin/systemctl start sats-update.service + up /bin/systemctl start sats-update.service diff --git a/clone/utility/sats-boot b/clone/utility/sats-install similarity index 84% rename from clone/utility/sats-boot rename to clone/utility/sats-install index 95d9635..b9aca6a 100644 --- a/clone/utility/sats-boot +++ b/clone/utility/sats-install @@ -23,20 +23,20 @@ sudo apt-get -y install git php5 php5-cli php5-curl python-dev; ################################################ #### ONLY ON FIRST BOOT #### ################################################ -if [ -e /home/pi/first_boot ]; then +if [ ! -e /target/install ]; then dpkg -s git 2>/dev/null >/dev/null && gitinstalled=1 || gitinstalled=0; BRANCH=$(sudo cat /home/sats/satsd/conf/machine.branch); # (1) With git if installed # - if [ $gitinstalled -eq 1 ]; then + if [ $gitinstalled -eq 1 ]; then - sudo -u sats git clone -b $BRANCH ssh://smmp-server/satsd/git /home/sats/satsd/source \ - && rm /home/pi/first_boot \ + sudo -u sats git clone -b $BRANCH ssh://smmp-server/satsd/git /home/sats/satsd/source \ + && sudo -u sats touch /target/install \ || exit; - fi; + fi; @@ -53,6 +53,7 @@ if [ -e /home/pi/first_boot ]; then sudo python setup.py install; # (4) Reboot to activate SPI # + sudo -u sats touch /target/install; sudo reboot; fi; diff --git a/clone/utility/sats-boot.service b/clone/utility/sats-install.service similarity index 71% rename from clone/utility/sats-boot.service rename to clone/utility/sats-install.service index f70df75..55b48ba 100644 --- a/clone/utility/sats-boot.service +++ b/clone/utility/sats-install.service @@ -5,11 +5,11 @@ After=network-online.target network.target Before=sats-loop.service [Service] -Type=oneshot +Type=simple RemainAfterExit=yes User=pi Group=pi -ExecStart=/bin/sh /home/pi/sats-boot +ExecStart=/bin/sh /home/pi/sats-install [Install] -WantedBy=multi-user.target +WantedBy=multi-user.target \ No newline at end of file diff --git a/clone/utility/sats-loop b/clone/utility/sats-loop index 3a26d31..7e911e4 100644 --- a/clone/utility/sats-loop +++ b/clone/utility/sats-loop @@ -1,18 +1,9 @@ #!/bin/sh -# [0] Exit if no argument -test $# -lt 1 && exit; - -# [1] Propagate script to DAEMON -if [ "$1" = "daemon" ]; then - - - /bin/bash /home/sats/satsd/source/daemon 2>> /home/sats/satsd/log/daemon.log >> /home/sats/satsd/log/daemon.log; - -# [2] Propagate script to UPDATE -elif [ "$1" = "update" ]; then - - /bin/bash /home/sats/satsd/source/update 2>> /home/sats/satsd/log/update.log >> /home/sats/satsd/log/update.log; - +# [0] Check installed first +if [ ! -e /target/install ]; then + exit 1; fi; +# [1] Propagate script to DAEMON +/bin/bash /home/sats/satsd/source/daemon 2>> /home/sats/satsd/log/daemon.log >> /home/sats/satsd/log/daemon.log; \ No newline at end of file diff --git a/clone/utility/sats-loop.service b/clone/utility/sats-loop.service index f721570..7a3f78c 100644 --- a/clone/utility/sats-loop.service +++ b/clone/utility/sats-loop.service @@ -1,14 +1,14 @@ [Unit] -Description=SATS (Secure Authentication and Tracking SYSTEM) Daemon -Wants=sats-boot.service +Description=SATS Daemon +Wants=sats-install.service +OnFailure=sats-install.service [Service] Type=simple User=sats Group=sats -ExecStart=/bin/sh /home/sats/satsd/sats-loop daemon +ExecStart=/bin/sh /service/sats-loop Restart=always - [Install] -WantedBy=multi-user.target +WantedBy=multi-user.target \ No newline at end of file diff --git a/clone/utility/sats-update b/clone/utility/sats-update new file mode 100644 index 0000000..9520f76 --- /dev/null +++ b/clone/utility/sats-update @@ -0,0 +1,9 @@ +#!/bin/sh + +# [0] Check synced first +if [ ! -e /target/sync ]; then + exit 1; +fi; + +# [1] Propagate script to UPDATE +/bin/bash /home/sats/satsd/source/update 2>> /home/sats/satsd/log/update.log >> /home/sats/satsd/log/update.log; diff --git a/clone/utility/sats-update.service b/clone/utility/sats-update.service index 1ad8943..624b73e 100644 --- a/clone/utility/sats-update.service +++ b/clone/utility/sats-update.service @@ -1,10 +1,11 @@ [Unit] Description=SATS Update Process -Requires=network-online.target network.target sats-loop.service sats-boot.service +Requires=network-online.target network.target +OnFailure=sats-loop.service [Service] RemainAfterExit=yes Type=oneshot User=sats Group=sats -ExecStart=/bin/bash /home/sats/satsd/sats-loop update +ExecStart=/bin/bash /service/sats-update \ No newline at end of file