From db872ae533f454d153c94af38921b03ccb3fc5a6 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Fri, 21 Apr 2023 22:05:54 +0700 Subject: [PATCH] refactoting services and remove upload by timer --- files/pullgit.service | 9 ----- files/pullgit.sh | 6 ---- files/pullgit.timer | 10 ------ files/pushgit.service | 9 ----- files/pushgit.sh | 11 ------ files/pushgit.timer | 10 ------ files/pushgit/pushgit.sh | 7 ++++ files/{ => pushgit}/pushgit_inotify.service | 4 +-- files/pushgit/pushgit_inotify.sh | 12 +++++++ files/pushgit/pushgit_inotify_special.service | 12 +++++++ files/pushgit/pushgit_run_on_start.timer | 9 +++++ files/pushgit_inotify.sh | 14 -------- files/vestasync.js | 35 +++---------------- vestasync.py | 32 +++++++++++------ 14 files changed, 68 insertions(+), 112 deletions(-) delete mode 100644 files/pullgit.service delete mode 100644 files/pullgit.sh delete mode 100644 files/pullgit.timer delete mode 100644 files/pushgit.service delete mode 100644 files/pushgit.sh delete mode 100644 files/pushgit.timer create mode 100644 files/pushgit/pushgit.sh rename files/{ => pushgit}/pushgit_inotify.service (51%) create mode 100644 files/pushgit/pushgit_inotify.sh create mode 100644 files/pushgit/pushgit_inotify_special.service create mode 100644 files/pushgit/pushgit_run_on_start.timer delete mode 100644 files/pushgit_inotify.sh diff --git a/files/pullgit.service b/files/pullgit.service deleted file mode 100644 index 9eec674..0000000 --- a/files/pullgit.service +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description=Pull git changes - -[Service] -Type=oneshot -ExecStart=/usr/local/bin/pullgit.sh - -[Install] -WantedBy=multi-user.target diff --git a/files/pullgit.sh b/files/pullgit.sh deleted file mode 100644 index 40f8d2e..0000000 --- a/files/pullgit.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env sh -git config --global user.name vestasync_wb_$(hostname) -git config --global user.email "vestasync@fake.mail" -cd /mnt/data/etc/ -git fetch > /dev/null 2>&1 || true -git pull > /dev/null 2>&1 || true diff --git a/files/pullgit.timer b/files/pullgit.timer deleted file mode 100644 index e6a4f29..0000000 --- a/files/pullgit.timer +++ /dev/null @@ -1,10 +0,0 @@ -[Unit] -Description=Run pullgit.service every 10 min - -[Timer] -OnCalendar=*:0/10 -Persistent=true -Unit=pullgit.service - -[Install] -WantedBy=timers.target diff --git a/files/pushgit.service b/files/pushgit.service deleted file mode 100644 index fc0353b..0000000 --- a/files/pushgit.service +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description=Push git changes - -[Service] -Type=oneshot -ExecStart=/usr/local/bin/pushgit.sh - -[Install] -WantedBy=multi-user.target diff --git a/files/pushgit.sh b/files/pushgit.sh deleted file mode 100644 index 7e63d55..0000000 --- a/files/pushgit.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env sh -apt-mark showmanual > /mnt/data/etc/vestasync/packages -echo $(hostname) > /mnt/data/etc/vestasync/hostname -export GIT_AUTHOR_NAME="vestasync_wb_$(hostname)" -export GIT_COMMITTER_NAME="vestasync_wb_$(hostname)" -export LC_TIME=en_GB.UTF-8 -cd /mnt/data/etc/ > /dev/null 2>&1 || true -git add . > /dev/null 2>&1 || true -git commit -m "$(date +"%Y-%m-%d %H:%M:%S %z %Z")" > /dev/null 2>&1 || true -git push -u origin master > /dev/null 2>&1 || true - diff --git a/files/pushgit.timer b/files/pushgit.timer deleted file mode 100644 index 5949c08..0000000 --- a/files/pushgit.timer +++ /dev/null @@ -1,10 +0,0 @@ -[Unit] -Description=Run pushgit.service daily - -[Timer] -OnCalendar=daily -Persistent=true -Unit=pushgit.service - -[Install] -WantedBy=timers.target diff --git a/files/pushgit/pushgit.sh b/files/pushgit/pushgit.sh new file mode 100644 index 0000000..e9af62f --- /dev/null +++ b/files/pushgit/pushgit.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +apt-mark showmanual > /mnt/data/etc/vestasync/packages +echo $(hostname) > /mnt/data/etc/vestasync/hostname +cd /mnt/data/etc/ +git add . +git commit -m "$(date +"%Y-%m-%d %H:%M:%S %z %Z")" +git push -u origin master diff --git a/files/pushgit_inotify.service b/files/pushgit/pushgit_inotify.service similarity index 51% rename from files/pushgit_inotify.service rename to files/pushgit/pushgit_inotify.service index 4a9bc2c..280347c 100644 --- a/files/pushgit_inotify.service +++ b/files/pushgit/pushgit_inotify.service @@ -1,9 +1,9 @@ [Unit] -Description=Script triggered by file changes in a folder (including subfolders) +Description=Main pushgit [Service] +Type=simple ExecStart=/usr/local/bin/pushgit_inotify.sh -Restart=always [Install] WantedBy=multi-user.target diff --git a/files/pushgit/pushgit_inotify.sh b/files/pushgit/pushgit_inotify.sh new file mode 100644 index 0000000..99e13f6 --- /dev/null +++ b/files/pushgit/pushgit_inotify.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +export GIT_AUTHOR_NAME="vestasync_wb_$(hostname)" +export GIT_COMMITTER_NAME="vestasync_wb_$(hostname)" +export LC_TIME=en_GB.UTF-8 +EXCLUDE_PATTERN='(^|\/)(\.git|packages|hostname|wb-mqtt-mbgate\.conf|resolv\.conf)($|\/)' +inotifywait -m -r -e close_write,move,create,delete --exclude "$EXCLUDE_PATTERN" --format '%w%f' /mnt/data/etc | while read FILE +do + /usr/local/bin/pushgit.sh +done + + + diff --git a/files/pushgit/pushgit_inotify_special.service b/files/pushgit/pushgit_inotify_special.service new file mode 100644 index 0000000..fb44c44 --- /dev/null +++ b/files/pushgit/pushgit_inotify_special.service @@ -0,0 +1,12 @@ +[Unit] +Description=Script triggered by file changes in a folder (including subfolders) + +[Service] +ExecStartPre=systemctl stop pushgit_inotify.service +ExecStart=systemctl start pushgit_inotify.service +ExecStop=systemctl stop pushgit_inotify.service +Type=oneshot +RemainAfterExit=true + +[Install] +WantedBy=multi-user.target diff --git a/files/pushgit/pushgit_run_on_start.timer b/files/pushgit/pushgit_run_on_start.timer new file mode 100644 index 0000000..1e14f71 --- /dev/null +++ b/files/pushgit/pushgit_run_on_start.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run pushgit.service on start + +[Timer] +OnBootSec=180 +Unit=pushgit_inotify_special.service + +[Install] +WantedBy=timers.target diff --git a/files/pushgit_inotify.sh b/files/pushgit_inotify.sh deleted file mode 100644 index 0566d64..0000000 --- a/files/pushgit_inotify.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -EXCLUDE_PATTERN='(^|/)(\.git($|/)|wb-mqtt-mbgate\.conf$|resolv\.conf$)' - -inotifywait -m -r -e close_write,move,create,delete --exclude "$EXCLUDE_PATTERN" --format '%w%f' /mnt/data/etc | while read FILE -do - echo $(hostname) > /mnt/data/etc/vestasync/hostname - export GIT_AUTHOR_NAME="vestasync_wb_$(hostname)_inotify" - export GIT_COMMITTER_NAME="vestasync_wb_$(hostname)_inotify" - export LC_TIME=en_GB.UTF-8 - cd /mnt/data/etc/ - git add . - git commit -m "$(date +"%Y-%m-%d %H:%M:%S %z %Z")" # > /dev/null 2>&1 || true - git push -u origin master -done diff --git a/files/vestasync.js b/files/vestasync.js index ed66e9e..234eb87 100644 --- a/files/vestasync.js +++ b/files/vestasync.js @@ -16,11 +16,6 @@ defineVirtualDevice("vestasync", { value: false, title: "Auto push on files changed" }, - autopush_timer: { - type: "switch", - value: false, - title: "Auto push every day" - }, push_now: { type: "pushbutton", value: false, @@ -70,15 +65,7 @@ function _update_vestasync() { } }); - runShellCommand("systemctl is-active pushgit_inotify.service", { - captureOutput: true, - exitCallback: function (exitCode, capturedOutput) { - var isEnabled = capturedOutput.trim() === "active"; - getControl("vestasync/autopush_inotify").setValue({ value: isEnabled, notify: false }) - } - }); - - runShellCommand("systemctl is-active pushgit.timer", { + runShellCommand("systemctl is-active pushgit_inotify_special.service", { captureOutput: true, exitCallback: function (exitCode, capturedOutput) { var isEnabled = capturedOutput.trim() === "active"; @@ -100,28 +87,16 @@ function _update_vestasync() { }; -defineRule("_vestasync_autopush_timer", { - whenChanged: "vestasync/autopush_timer", - then: function (newValue, devName, cellName) { - if (dev.vestasync.autopush_timer) { - runShellCommand("systemctl stop pushgit.timer ; systemctl disable pushgit.timer ; systemctl daemon-reload ; systemctl enable pushgit.timer ; systemctl start pushgit.timer"); - _update_vestasync(); - } else { - runShellCommand("systemctl stop pushgit.timer ; systemctl disable pushgit.timer"); - _update_vestasync(); - } - } -}); defineRule("_vestasync_autopush_inotify", { whenChanged: "vestasync/autopush_inotify", then: function (newValue, devName, cellName) { if (dev.vestasync.autopush_inotify) { - runShellCommand("systemctl stop pushgit_inotify.service ; systemctl disable pushgit_inotify.service ; systemctl daemon-reload ; systemctl enable pushgit_inotify.service ; systemctl start pushgit_inotify.service"); + runShellCommand("systemctl start pushgit_inotify_special.service"); _update_vestasync(); } else { - runShellCommand("systemctl stop pushgit_inotify.service ; systemctl disable pushgit_inotify.service"); + runShellCommand("systemctl stop pushgit_inotify_special.service"); _update_vestasync(); } } @@ -133,9 +108,7 @@ defineRule("_vestasync_push", { whenChanged: "vestasync/push_now", then: function (newValue, devName, cellName) { if (dev.vestasync.push_now) { - runShellCommand("systemctl start pushgit.service"); - dev.vestasync.push_now = false; - _update_vestasync(); + runShellCommand("/usr/local/bin/pushgit.sh"); } } }); diff --git a/vestasync.py b/vestasync.py index e3243f0..6d191ad 100755 --- a/vestasync.py +++ b/vestasync.py @@ -141,35 +141,47 @@ def create_automac_systemd(c): def create_autogit_systemd(c): #disable - for service in ['pushgit.timer', 'pushgit_inotify.service']: + print("Autogit: stop and disable services") + for service in ['pushgit.timer', + 'pushgit_inotify_special.service', + 'pushgit_inotify.service', + 'pushgit_run_on_start.timer' ]: c.run(f'systemctl stop {service}', warn=True) c.run(f'systemctl disable {service}', warn=True) + print("Autogit: Remove old files") + c.run(f'rm /etc/systemd/system/pushgit*', warn=True) + c.run(f'rm /usr/local/bin/pushgit*', warn=True) - #delete old files, copy new files, chmod +x + + print("Autogit: copy new files, chmod +x") file_paths = { #local path: remote path - './files/pushgit.sh': '/usr/local/bin/pushgit.sh', - './files/pushgit_inotify.sh': '/usr/local/bin/pushgit_inotify.sh', - './files/pushgit_inotify.service': '/etc/systemd/system/pushgit_inotify.service', - './files/pushgit.service': '/etc/systemd/system/pushgit.service', - './files/pushgit.timer': '/etc/systemd/system/pushgit.timer' + './files/pushgit/pushgit.sh': '/usr/local/bin/pushgit.sh', + './files/pushgit/pushgit_inotify.sh': '/usr/local/bin/pushgit_inotify.sh', + './files/pushgit/pushgit_inotify.service': '/etc/systemd/system/pushgit_inotify.service', + './files/pushgit/pushgit_run_on_start.timer': '/etc/systemd/system/pushgit_run_on_start.timer', + './files/pushgit/pushgit_inotify_special.service': '/etc/systemd/system/pushgit_inotify_special.service', } for local_path, remote_path in file_paths.items(): c.put(local_path, remote_path) c.run(f"chmod +x {remote_path}") - #reload + print("Autogit: reload configs") c.run("systemctl daemon-reload") #enable and start - for service in ['pushgit.timer', 'pushgit_inotify.service']: + print("Autogit: enable run on start") + for service in ['pushgit_run_on_start.timer']: c.run(f'systemctl enable {service}') + + print("Autogit: start inotify") + for service in ['pushgit_inotify_special.service']: c.run(f'systemctl start {service}') #check statuses - for service in ['pushgit.timer', 'pushgit_inotify.service']: + for service in ['pushgit_run_on_start.timer', 'pushgit_inotify.service', 'pushgit_inotify_special.service']: active = c.run(f'systemctl is-active {service} || true', hide=True).stdout.strip() enabled = c.run(f'systemctl is-enabled {service} || true', hide=True).stdout.strip() print(f"{service}: {active}, {enabled}")