diff options
| -rw-r--r-- | scripts/.gitignore | 1 | ||||
| -rw-r--r-- | scripts/p7s/bitwarden.py | 39 | ||||
| -rw-r--r-- | scripts/p7s/ssh.py | 8 | ||||
| -rw-r--r-- | scripts/p7s/ubpkg.py | 4 | ||||
| -rw-r--r-- | scripts/pyproject.toml | 2 | ||||
| -rw-r--r-- | workstation/README.md | 3 | ||||
| -rwxr-xr-x | workstation/root-setup.py | 12 | ||||
| -rwxr-xr-x | workstation/test.py | 42 | ||||
| -rwxr-xr-x | workstation/user-setup.py | 21 |
9 files changed, 131 insertions, 1 deletions
diff --git a/scripts/.gitignore b/scripts/.gitignore index bee8a64b..76e51dbb 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore @@ -1 +1,2 @@ __pycache__ +p7s.egg-info diff --git a/scripts/p7s/bitwarden.py b/scripts/p7s/bitwarden.py index ab6cc4f2..7818b26a 100644 --- a/scripts/p7s/bitwarden.py +++ b/scripts/p7s/bitwarden.py @@ -1,13 +1,19 @@ +import argparse import contextlib import json import os +import pathlib import subprocess +import tempfile +import urllib.request +import zipfile class Bitwarden(): @contextlib.contextmanager def login(self, server, email): - subprocess.run(["bw", "config", "server", server], check=True) + if subprocess.run(["bw", "config", "server"], check=True, text=True, stdout=subprocess.PIPE).stdout.strip() != server: + subprocess.run(["bw", "config", "server", server], check=True) status = self.status()["status"] if status == "unauthenticated": command = ["login", email] @@ -34,9 +40,40 @@ class Bitwarden(): def get_item(self, uuid): return json.loads(subprocess.run(["bw", "get", "item", uuid], check=True, stdout=subprocess.PIPE).stdout) + def get_attachment(self, attachment_id, item_id, destination): + subprocess.run(["bw", "get", "attachment", attachment_id, "--itemid", item_id, "--output", destination], check=True) def get_item(server, email, uuid): b = Bitwarden() with b.login(server, email): b.sync() return b.get_item(uuid) + + +@contextlib.contextmanager +def default(): + b = Bitwarden() + with b.login("https://vaultwarden.pdp7.net", "alex@corcoles.net"): + b.sync() + yield b + + +def install_cli(version): + with tempfile.TemporaryDirectory() as tempdir: + tempdir = pathlib.Path(tempdir) + zip = tempdir / "zip" + with urllib.request.urlopen(f"https://github.com/bitwarden/clients/releases/download/cli-v{version}/bw-oss-linux-{version}.zip") as urlopen: + zip.write_bytes(urlopen.read()) + with zipfile.ZipFile(zip) as zip: + with zip.open("bw") as bw: + bw_path = pathlib.Path.home() / ".local" / "bin" / "bw" + bw_path.unlink() + bw_path.write_bytes(bw.read()) + bw_path.chmod(0o555) + + +def install_cli_main(): + parser = argparse.ArgumentParser() + parser.add_argument("version") + args = parser.parse_args() + install_cli(args.version) diff --git a/scripts/p7s/ssh.py b/scripts/p7s/ssh.py new file mode 100644 index 00000000..f53cad33 --- /dev/null +++ b/scripts/p7s/ssh.py @@ -0,0 +1,8 @@ +import pathlib + +from p7s import bitwarden + + +def setup_ssh(): + with bitwarden.default() as b: + b.get_attachment("06ba85e9172775f8f320", "e1d1a559-feaa-4834-b192-a688708bdba3", pathlib.Path.home() / ".ssh" / "id_rsa") diff --git a/scripts/p7s/ubpkg.py b/scripts/p7s/ubpkg.py index 3a333254..4249c3e0 100644 --- a/scripts/p7s/ubpkg.py +++ b/scripts/p7s/ubpkg.py @@ -1,6 +1,7 @@ import os import pathlib import platform +import subprocess import urllib.request @@ -9,3 +10,6 @@ def setup_ubpkg(): with urllib.request.urlopen(f"https://github.com/alexpdp7/ubpkg/releases/latest/download/ubpkg-linux-{platform.machine()}") as urlopen: ubpkg.write_bytes(urlopen.read()) os.chmod(ubpkg, 0o700) + + # Currently Vaultwarden in EPEL is not compatible with latest Bitwarden CLI https://github.com/dani-garcia/vaultwarden/issues/7820 + subprocess.run([ubpkg, "deltachat-rpc-server", "hatch", "kubectl", "nix-portable", "ressh", "talosctl", "typst"], check=True) diff --git a/scripts/pyproject.toml b/scripts/pyproject.toml index 36bf52c2..85ced5c4 100644 --- a/scripts/pyproject.toml +++ b/scripts/pyproject.toml @@ -14,10 +14,12 @@ setup-imapfilter = 'p7s.mail:setup_imapfilter' setup-soju = 'p7s.soju:setup_soju' setup-ubpkg = 'p7s.ubpkg:setup_ubpkg' setup-x12 = 'p7s.x12:setup_x12' +setup-ssh = 'p7s.ssh:setup_ssh' senpai = 'p7s.senpai:main' ledger = 'p7s.ledger:main' google-meet-toggle-mute = 'p7s.google_meet:toggle_mute' google-meet-install-stream-deck-wsbroad-server = 'p7s.google_meet:install_stream_deck_google_meet_wsbroad_server' +install-bitwarden-cli = 'p7s.bitwarden:install_cli_main' [tool.uv] package = true diff --git a/workstation/README.md b/workstation/README.md new file mode 100644 index 00000000..6a13bd3a --- /dev/null +++ b/workstation/README.md @@ -0,0 +1,3 @@ +# workstation + +Yet another attempt to automate workstation configuration because I am stubborn and I do not want to lock myself into any Linux distro. diff --git a/workstation/root-setup.py b/workstation/root-setup.py new file mode 100755 index 00000000..4d764a50 --- /dev/null +++ b/workstation/root-setup.py @@ -0,0 +1,12 @@ +#!/usr/bin/python3 +import subprocess + + +def _(*args): + subprocess.run(args, check=True) + + +_("apt", "update") +_("apt", "install", "-y", "git", "tasksel", "pipx") +_("tasksel", "install", "desktop") +_("systemctl", "start", "gdm3") diff --git a/workstation/test.py b/workstation/test.py new file mode 100755 index 00000000..467c0fb5 --- /dev/null +++ b/workstation/test.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 +import os +import pathlib +import subprocess +import time + + +INSTANCE_NAME="workstation-test" +USER="alex" + +THIS_REPO = pathlib.Path(__file__).parent.parent +THIS_UID = os.getuid() + + +def _(*args, check=True, input=None): + return subprocess.run(args, check=check, input=input, text=True) + + +_("incus", "launch", + "images:debian/13", + INSTANCE_NAME, + "--ephemeral", + "--vm", + "-c", "limits.cpu=4", + "-c", "limits.memory=6GiB", + "-d", "root,size=50GiB") + +while _("incus", "exec", INSTANCE_NAME, "uptime", check=False).returncode != 0: + print("waiting...") + time.sleep(1) + +_("incus", "config", "device", "add", INSTANCE_NAME, "here", "disk", f"source={THIS_REPO}", "path=/here") +_("incus", "exec", INSTANCE_NAME, "--", "apt", "full-upgrade", "-U") +_("incus", "exec", INSTANCE_NAME, "--", "apt", "install", "-y", "python3") +_("incus", "exec", INSTANCE_NAME, "--", "/here/workstation/root-setup.py") +_("incus", "exec", INSTANCE_NAME, "--", "adduser", USER, "--disabled-password", "--comment", "") +_("incus", "exec", INSTANCE_NAME, "--", "passwd", USER, "--stdin", input=USER) + +# Comment out to test with real repo +_("incus", "exec", INSTANCE_NAME, "--", "mount", "/here/", f"/home/{USER}/git/alexpdp7/", "-o", f"bind,X-mount.idmap={THIS_UID}:1000:1") + +_("incus", "exec", INSTANCE_NAME, "--", "sudo", "-u", USER, "/here/workstation/user-setup.py") diff --git a/workstation/user-setup.py b/workstation/user-setup.py new file mode 100755 index 00000000..2b57e29a --- /dev/null +++ b/workstation/user-setup.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 +import pathlib +import subprocess + + +def _(*args): + subprocess.run(args, check=True) + +HOME = pathlib.Path.home() +BIN = HOME / ".local" / "bin" +GIT = HOME / "git" +ALEXPDP7_REPO = GIT / "alexpdp7" + +if not ALEXPDP7_REPO.exists(): + _("git", "clone", "https://github.com/alexpdp7/alexpdp7.git", ALEXPDP7_REPO) + +_("pipx", "install", "-e", ALEXPDP7_REPO / "scripts", "--force") +_(BIN / "setup-ubpkg") +# Latest Bitwarden CLI not compatible with EPEL Vaultwarden https://github.com/dani-garcia/vaultwarden/issues/7820 +_(BIN / "install-bitwarden-cli", "2026.8.0") +_(BIN / "setup-ssh") |
