import subprocess from pyinfra import host from pyinfra.operations import files, server, git, systemd from pyinfra.facts.files import FindInFile def get_pass(filename: str) -> str: """Get the data from the password manager.""" try: r = subprocess.run(["pass", "show", filename], capture_output=True) except FileNotFoundError: readme_url = "https://git.0x90.space/deltachat/secrets" print(f"Please install pass and pull the latest version of our pass secrets from {readme_url}") exit() return r.stdout.decode('utf-8').splitlines()[0].strip() server.shell( name=f"enable user's systemd units to auto-start at boot", commands=[f"loginctl enable-linger user"], _sudo=True, ) files.line( name=f"user .profile: ensure XDG_RUNTIME_DIR", path=f"/home/user/.profile", line='export XDG_RUNTIME_DIR="/run/user/$UID"', ) files.line( name=f"user .profile: ensure DBUS_SESSION_BUS_ADDRESS", path=f"/home/user/.profile", line='export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"', ) server.crontab( command="/usr/sbin/reboot", user="root", minute="0", hour="6", _sudo=True, ) server.shell( name="Turn on bluetooth", commands=[ "rfkill unblock bluetooth", "bluetoothctl power on", ], _sudo=True, ) git.config( key="rebase.autoStash", value="true", ) git.repo( src="https://github.com/lynxis/keyblepy", dest="/home/user/keyblepy", rebase=True, ) files.file( name="chmod u+x keyblepy.py", path="/home/user/keyblepy/keyblepy.py", mode="u+x", ) doorbot = files.put( src="bot.py", dest="/home/user/bot.py", mode="775", ) files.template( src="unlock.sh.j2", dest="/home/user/unlock.sh", mode="775", device_address=get_pass("r8/keyblepy/device_address"), qr_data=get_pass("r8/keyblepy/qr_data"), user_key=get_pass("r8/keyblepy/user_key"), user_id=get_pass("r8/keyblepy/user_id"), ) server.script( name="Setup virtual environment", src="setup-venv.sh", ) initialized = host.get_fact(FindInFile, "/home/user/.config/doorbot/accounts/accounts.toml", "selected_account = 1") if not initialized: server.shell( name="Initialize bot account", commands=["PATH=/home/user/.local/lib/eqiva.venv/bin:$PATH python3 bot.py init dcaccount:https://e2ee.im/new"], ) unit_file = files.put( src="doorbot.service", dest="/etc/systemd/system/doorbot.service", _sudo=True, ) systemd.service( service="doorbot.service", daemon_reload=unit_file.changed, enabled=True, restarted=True, running=True, _sudo=True, )