Mercurial > code > home > repos > infra
changeset 152:4f1f39f81bc1
fetch and install newer syncthing
author | drewp@bigasterisk.com |
---|---|
date | Sat, 11 Mar 2023 12:27:08 -0800 |
parents | 258865bd7059 |
children | 03ea89f9013a |
files | sync.py |
diffstat | 1 files changed, 46 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/sync.py Sat Mar 11 12:25:02 2023 -0800 +++ b/sync.py Sat Mar 11 12:27:08 2023 -0800 @@ -1,15 +1,55 @@ +from pathlib import Path + from pyinfra import host -from pyinfra.operations import apt, systemd +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(tmpdir) + files.download(url, str(tmpdir / 'archive.tgz')) # bugreport + server.shell([f'cd {tmpdir}; aunpack archive.tgz']) + + systemd.service(service=f'syncthing@{user}', running=False) + + user_svc_template = '/lib/systemd/system/syncthing@.service' + server.shell([ + f'cp -a {tmpdir}/{dl_name}/{s} {d}' for s, d in [ + ('syncthing', '/usr/bin'), + #('etc/linux-systemd/user/syncthing.service', ''), # unused + ('etc/linux-systemd/system/syncthing@.service', user_svc_template), + ('etc/linux-systemd/system/syncthing-resume.service', '/etc/systemd/system/syncthing-resume.service'), + ] + ]) + files.link(f'/etc/systemd/system/multi-user.target.wants/syncthing@{user}.service', user_svc_template) + systemd.service(service=f'syncthing@{user}', enabled=True, restarted=True, daemon_reload=True) + + +# also see /my/serv/filesync/syncthing/deploy.yaml for the container one +version = 'v1.23.1' # primary instance is in k8s (/my/serv/filesync/syncthing); the rest are run with systemd. # Configs are in ~/.config/syncthing/ on each box - apt.packages(packages=['syncthing']) if host.name in ['dash', 'dot', 'slash', 'plus', 'bang' ,'ditto']: + apt.packages(packages=['syncthing'], present=False) + user = 'ari' if host.name == 'dot' else 'drewp' - # now we have /lib/systemd/system/syncthing@.service - user = 'ari' if host.name == 'dot' else 'drewp' - systemd.service(service=f'syncthing@{user}', running=True, enabled=True) + if not host_running_version(user, version): + install_syncthing(user, version) - # also consider https://github.com/Martchus/syncthingtray tray status viewer on dtops +# something above has broken devnull #bugreport +server.shell(['chmod a+w /dev/null']) # also consider https://github.com/Martchus/syncthingtray tray status viewer on dtops