diff sync/sync.py @ 326:5b88b38f2471

huge reorg, reog toplevel functions in preparation of a ui with nice task lists
author drewp@bigasterisk.com
date Mon, 20 Jan 2025 21:55:08 -0800
parents sync.py@58d8e6072dcc
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sync/sync.py	Mon Jan 20 21:55:08 2025 -0800
@@ -0,0 +1,57 @@
+from pathlib import Path
+
+from pyinfra.context import host
+from pyinfra.facts.server import Command
+from pyinfra.operations import apt, files, server, systemd
+
+
+def host_running_version(user: str, v: str):
+    # if this command fails, we go in to error state and run nothing else #bugreport
+    out = host.get_fact(Command, f"runuser -u {user} syncthing serve --version || exit 0")
+    if out is None:
+        return False
+    _, sv, *_ = out.split()
+    return sv == v
+
+
+def install_syncthing(user, version, os='linux', arch='amd64'):
+    tmpdir = Path('/tmp/syncthing_install')
+    dl_name = f'syncthing-{os}-{arch}-{version}'
+    url = f'https://github.com/syncthing/syncthing/releases/download/{version}/{dl_name}.tar.gz'
+    files.directory(path=tmpdir)
+    files.download(src=url, dest=str(tmpdir / f'{dl_name}.tgz'))  # bugreport
+    server.shell(commands=[f'cd {tmpdir}; aunpack {dl_name}.tgz'])
+
+    systemd.service(service=f'syncthing@{user}', running=False)
+
+    user_svc_template = '/lib/systemd/system/syncthing@.service'
+    server.shell(commands=[
+        f'cp -a {tmpdir}/{dl_name}/{s} {d}' for s, d in [
+            ('syncthing', '/usr/bin'),
+            ('etc/linux-systemd/system/syncthing@.service', user_svc_template),
+        ]
+    ])
+    files.link(path=f'/etc/systemd/system/multi-user.target.wants/syncthing@{user}.service', target=user_svc_template)
+    systemd.service(service=f'syncthing@{user}', enabled=True, restarted=True, daemon_reload=True)
+
+def syncthing():
+    # also see /my/serv/filesync/syncthing/deploy.yaml for the container one
+    version = 'v1.27.10'
+
+    # primary instance is in k8s (/my/serv/filesync/syncthing); the rest are run with systemd.
+    # Configs are in ~/.config/syncthing/ on each box
+    if host.data.get('syncthing'):
+        apt.packages(packages=['syncthing'], present=False)
+        user = 'ari' if host.name == 'dot' else 'drewp'
+
+        if not host_running_version(user, version):
+            install_syncthing(user, version)
+
+    # something above has broken devnull #bugreport
+    server.shell(commands=['chmod a+w /dev/null'])
+
+    # also consider https://github.com/Martchus/syncthingtray tray status viewer on dtops
+
+operations = [
+    syncthing,
+]
\ No newline at end of file