annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
152
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
1 from pathlib import Path
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
2
326
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
3 from pyinfra.context import host
152
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
4 from pyinfra.facts.server import Command
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
5 from pyinfra.operations import apt, files, server, systemd
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
6
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
7
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
8 def host_running_version(user: str, v: str):
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
9 # if this command fails, we go in to error state and run nothing else #bugreport
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
10 out = host.get_fact(Command, f"runuser -u {user} syncthing serve --version || exit 0")
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
11 if out is None:
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
12 return False
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
13 _, sv, *_ = out.split()
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
14 return sv == v
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
15
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
16
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
17 def install_syncthing(user, version, os='linux', arch='amd64'):
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
18 tmpdir = Path('/tmp/syncthing_install')
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
19 dl_name = f'syncthing-{os}-{arch}-{version}'
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
20 url = f'https://github.com/syncthing/syncthing/releases/download/{version}/{dl_name}.tar.gz'
288
drewp@bigasterisk.com
parents: 283
diff changeset
21 files.directory(path=tmpdir)
drewp@bigasterisk.com
parents: 283
diff changeset
22 files.download(src=url, dest=str(tmpdir / f'{dl_name}.tgz')) # bugreport
drewp@bigasterisk.com
parents: 283
diff changeset
23 server.shell(commands=[f'cd {tmpdir}; aunpack {dl_name}.tgz'])
152
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
24
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
25 systemd.service(service=f'syncthing@{user}', running=False)
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
26
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
27 user_svc_template = '/lib/systemd/system/syncthing@.service'
288
drewp@bigasterisk.com
parents: 283
diff changeset
28 server.shell(commands=[
152
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
29 f'cp -a {tmpdir}/{dl_name}/{s} {d}' for s, d in [
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
30 ('syncthing', '/usr/bin'),
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
31 ('etc/linux-systemd/system/syncthing@.service', user_svc_template),
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
32 ]
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
33 ])
288
drewp@bigasterisk.com
parents: 283
diff changeset
34 files.link(path=f'/etc/systemd/system/multi-user.target.wants/syncthing@{user}.service', target=user_svc_template)
152
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
35 systemd.service(service=f'syncthing@{user}', enabled=True, restarted=True, daemon_reload=True)
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
36
326
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
37 def syncthing():
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
38 # also see /my/serv/filesync/syncthing/deploy.yaml for the container one
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
39 version = 'v1.27.10'
152
4f1f39f81bc1 fetch and install newer syncthing
drewp@bigasterisk.com
parents: 146
diff changeset
40
326
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
41 # primary instance is in k8s (/my/serv/filesync/syncthing); the rest are run with systemd.
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
42 # Configs are in ~/.config/syncthing/ on each box
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
43 if host.data.get('syncthing'):
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
44 apt.packages(packages=['syncthing'], present=False)
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
45 user = 'ari' if host.name == 'dot' else 'drewp'
14
ac4d24d01b68 add syncthing service
drewp@bigasterisk.com
parents:
diff changeset
46
326
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
47 if not host_running_version(user, version):
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
48 install_syncthing(user, version)
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
49
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
50 # something above has broken devnull #bugreport
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
51 server.shell(commands=['chmod a+w /dev/null'])
14
ac4d24d01b68 add syncthing service
drewp@bigasterisk.com
parents:
diff changeset
52
326
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
53 # also consider https://github.com/Martchus/syncthingtray tray status viewer on dtops
14
ac4d24d01b68 add syncthing service
drewp@bigasterisk.com
parents:
diff changeset
54
326
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
55 operations = [
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
56 syncthing,
5b88b38f2471 huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents: 305
diff changeset
57 ]