From 6de0fb7775eb749523ecd31a25ea1dec4ebc1604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 26 Jul 2023 11:31:33 +0200 Subject: [PATCH 1/7] clan-template: drop unused dependencies --- templates/new-clan/default.nix | 16 ---------------- templates/new-clan/flake.nix | 8 ++------ templates/new-clan/shell.nix | 2 -- 3 files changed, 2 insertions(+), 24 deletions(-) delete mode 100644 templates/new-clan/default.nix delete mode 100644 templates/new-clan/shell.nix diff --git a/templates/new-clan/default.nix b/templates/new-clan/default.nix deleted file mode 100644 index 4691d64..0000000 --- a/templates/new-clan/default.nix +++ /dev/null @@ -1,16 +0,0 @@ -# This file provides backward compatibility to nix < 2.4 clients -let - flake = - import - ( - let - lock = builtins.fromJSON (builtins.readFile ./flake.lock); - in - fetchTarball { - url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } - ) - { src = ./.; }; -in -flake.defaultNix diff --git a/templates/new-clan/flake.nix b/templates/new-clan/flake.nix index c710aae..fed790b 100644 --- a/templates/new-clan/flake.nix +++ b/templates/new-clan/flake.nix @@ -1,13 +1,9 @@ { - description = ""; + description = ""; inputs = { + clan-core.url = "git+https://git.clan.lol/clan/clan-core"; flake-parts.url = "github:hercules-ci/flake-parts"; - flake-compat = { - url = "github:edolstra/flake-compat"; - flake = false; - }; - nixpkgs.url = "nixpkgs/nixos-unstable"; }; outputs = inputs @ { flake-parts, ... }: diff --git a/templates/new-clan/shell.nix b/templates/new-clan/shell.nix deleted file mode 100644 index 804c2a4..0000000 --- a/templates/new-clan/shell.nix +++ /dev/null @@ -1,2 +0,0 @@ -(import ./default.nix).devShells.${builtins.currentSystem}.default - or (throw "dev-shell not defined. Cannot find flake attribute devShell.${builtins.currentSystem}.default") From fde5155195b665f168957a3f0741bd60ff7ca2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 26 Jul 2023 11:32:27 +0200 Subject: [PATCH 2/7] clan-template: introduce clan-flake-module --- templates/new-clan/clan-flake-module.nix | 9 +++++++++ templates/new-clan/flake.nix | 12 +++--------- 2 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 templates/new-clan/clan-flake-module.nix diff --git a/templates/new-clan/clan-flake-module.nix b/templates/new-clan/clan-flake-module.nix new file mode 100644 index 0000000..cffd20a --- /dev/null +++ b/templates/new-clan/clan-flake-module.nix @@ -0,0 +1,9 @@ +# AUTOMATICALLY GENERATED by clan +{ ... }: { + imports = + let + relPaths = builtins.fromJSON (builtins.readFile ./imports.json); + paths = map (path: ./. + path) relPaths; + in + paths; +} diff --git a/templates/new-clan/flake.nix b/templates/new-clan/flake.nix index fed790b..ca463ed 100644 --- a/templates/new-clan/flake.nix +++ b/templates/new-clan/flake.nix @@ -8,14 +8,8 @@ outputs = inputs @ { flake-parts, ... }: flake-parts.lib.mkFlake { inherit inputs; } { - - systems = builtins.fromJSON (builtins.readFile ./systems.json); - - imports = - let - relPaths = builtins.fromJSON (builtins.readFile ./imports.json); - paths = map (path: ./. + path) relPaths; - in - paths; + imports = [ + ./clan-flake-module.nix + ]; }; } From a426a6a3317d6b0ef6b7e2968678f610c180adb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 26 Jul 2023 11:59:51 +0200 Subject: [PATCH 3/7] clan-template: add marker file to discover a clan flake --- templates/new-clan/.clan-flake | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 templates/new-clan/.clan-flake diff --git a/templates/new-clan/.clan-flake b/templates/new-clan/.clan-flake new file mode 100644 index 0000000..406fcfe --- /dev/null +++ b/templates/new-clan/.clan-flake @@ -0,0 +1,2 @@ +# DO NOT DELETE +# This file is used by the clan cli to discover a clan flake From 20dc480123cc555cc06f9d1d131c828174aaa8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 26 Jul 2023 14:33:15 +0200 Subject: [PATCH 4/7] add tty module to color text --- pkgs/clan-cli/clan_cli/tty.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 pkgs/clan-cli/clan_cli/tty.py diff --git a/pkgs/clan-cli/clan_cli/tty.py b/pkgs/clan-cli/clan_cli/tty.py new file mode 100644 index 0000000..5d930ec --- /dev/null +++ b/pkgs/clan-cli/clan_cli/tty.py @@ -0,0 +1,25 @@ +import sys +from typing import IO, Any, Callable + + +def is_interactive() -> bool: + """Returns true if the current process is interactive""" + return sys.stdin.isatty() and sys.stdout.isatty() + + +def color_text(code: int, file: IO[Any] = sys.stdout) -> Callable[[str], None]: + """ + Print with color if stderr is a tty + """ + + def wrapper(text: str) -> None: + if file.isatty(): + print(f"\x1b[{code}m{text}\x1b[0m", file=file) + else: + print(text, file=file) + + return wrapper + + +warn = color_text(91, file=sys.stderr) +info = color_text(92, file=sys.stderr) From a61d0c5a424923d759cf5ead837c68bd38d38c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 26 Jul 2023 14:34:39 +0200 Subject: [PATCH 5/7] add dirs module to get toplevel flake and configuration dir --- pkgs/clan-cli/clan_cli/dirs.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 pkgs/clan-cli/clan_cli/dirs.py diff --git a/pkgs/clan-cli/clan_cli/dirs.py b/pkgs/clan-cli/clan_cli/dirs.py new file mode 100644 index 0000000..e895df0 --- /dev/null +++ b/pkgs/clan-cli/clan_cli/dirs.py @@ -0,0 +1,25 @@ +import os +import sys +from pathlib import Path + + +def get_clan_flake_toplevel() -> Path: + """Returns the path to the toplevel of the clan flake""" + initial_path = Path(os.getcwd()) + path = Path(initial_path) + while path.parent == path: + project_files = [".clan-flake"] + for project_file in project_files: + if (path / project_file).exists(): + return path + path = path.parent + return initial_path + + +def user_data_dir() -> Path: + if sys.platform == "win32": + raise NotImplementedError("Windows is not supported") + elif sys.platform == "darwin": + return Path(os.path.expanduser("~/Library/Application Support/")) + else: + return Path(os.getenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))) From b4ba9c70cd43266df889634ed5d6fcbf84809689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 26 Jul 2023 15:14:29 +0200 Subject: [PATCH 6/7] move moc_env to module --- pkgs/clan-cli/tests/__init__.py | 0 pkgs/clan-cli/tests/environment.py | 14 ++++++++++++++ pkgs/clan-cli/tests/test_clan_ssh.py | 17 +++-------------- 3 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 pkgs/clan-cli/tests/__init__.py create mode 100644 pkgs/clan-cli/tests/environment.py diff --git a/pkgs/clan-cli/tests/__init__.py b/pkgs/clan-cli/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pkgs/clan-cli/tests/environment.py b/pkgs/clan-cli/tests/environment.py new file mode 100644 index 0000000..24226db --- /dev/null +++ b/pkgs/clan-cli/tests/environment.py @@ -0,0 +1,14 @@ +import os +from contextlib import contextmanager +from typing import Iterator + + +@contextmanager +def mock_env(**environ: str) -> Iterator[None]: + original_environ = dict(os.environ) + os.environ.update(environ) + try: + yield + finally: + os.environ.clear() + os.environ.update(original_environ) diff --git a/pkgs/clan-cli/tests/test_clan_ssh.py b/pkgs/clan-cli/tests/test_clan_ssh.py index e864a18..628bc0e 100644 --- a/pkgs/clan-cli/tests/test_clan_ssh.py +++ b/pkgs/clan-cli/tests/test_clan_ssh.py @@ -1,7 +1,5 @@ -import os import sys -from contextlib import contextmanager -from typing import Iterator, Union +from typing import Union import pytest import pytest_subprocess.fake_process @@ -9,6 +7,8 @@ from pytest_subprocess import utils import clan_cli.ssh +from .environment import mock_env + def test_no_args( capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch @@ -20,17 +20,6 @@ def test_no_args( assert captured.err.startswith("usage:") -@contextmanager -def mock_env(**environ: str) -> Iterator[None]: - original_environ = dict(os.environ) - os.environ.update(environ) - try: - yield - finally: - os.environ.clear() - os.environ.update(original_environ) - - # using fp fixture from pytest-subprocess def test_ssh_no_pass(fp: pytest_subprocess.fake_process.FakeProcess) -> None: with mock_env(CLAN_FLAKE="/mocked-flake"): From 4101b9adb41aa7dcba0719df907c228709b4e827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 26 Jul 2023 15:50:27 +0200 Subject: [PATCH 7/7] introduce ClanError type --- pkgs/clan-cli/clan_cli/errors.py | 4 ++++ pkgs/clan-cli/clan_cli/zerotier/__init__.py | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 pkgs/clan-cli/clan_cli/errors.py diff --git a/pkgs/clan-cli/clan_cli/errors.py b/pkgs/clan-cli/clan_cli/errors.py new file mode 100644 index 0000000..32a26df --- /dev/null +++ b/pkgs/clan-cli/clan_cli/errors.py @@ -0,0 +1,4 @@ +class ClanError(Exception): + """Base class for exceptions in this module.""" + + pass diff --git a/pkgs/clan-cli/clan_cli/zerotier/__init__.py b/pkgs/clan-cli/clan_cli/zerotier/__init__.py index 48a2488..be00bc6 100644 --- a/pkgs/clan-cli/clan_cli/zerotier/__init__.py +++ b/pkgs/clan-cli/clan_cli/zerotier/__init__.py @@ -8,6 +8,7 @@ from pathlib import Path from tempfile import TemporaryDirectory from typing import Any, Iterator, Optional +from ..errors import ClanError from ..nix import nix_shell @@ -30,11 +31,11 @@ def try_connect_port(port: int) -> bool: return result == 0 -def find_free_port(port_range: range) -> int: +def find_free_port(port_range: range) -> Optional[int]: for port in port_range: if try_bind_port(port): return port - raise Exception("cannot find a free port") + return None class ZerotierController: @@ -86,6 +87,8 @@ class ZerotierController: def zerotier_controller() -> Iterator[ZerotierController]: # This check could be racy but it's unlikely in practice controller_port = find_free_port(range(10000, 65535)) + if controller_port is None: + raise ClanError("cannot find a free port for zerotier controller") cmd = nix_shell(["bash", "zerotierone"], ["bash", "-c", "command -v zerotier-one"]) res = subprocess.run( cmd, @@ -95,10 +98,10 @@ def zerotier_controller() -> Iterator[ZerotierController]: ) zerotier_exe = res.stdout.strip() if zerotier_exe is None: - raise Exception("cannot find zerotier-one executable") + raise ClanError("cannot find zerotier-one executable") if not zerotier_exe.startswith("/nix/store"): - raise Exception( + raise ClanError( f"zerotier-one executable needs to come from /nix/store: {zerotier_exe}" ) @@ -136,7 +139,7 @@ def zerotier_controller() -> Iterator[ZerotierController]: while not try_connect_port(controller_port): status = p.poll() if status is not None: - raise Exception( + raise ClanError( f"zerotier-one has been terminated unexpected with {status}" ) time.sleep(0.1)