-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilderctl.py
More file actions
executable file
·397 lines (307 loc) · 13.8 KB
/
builderctl.py
File metadata and controls
executable file
·397 lines (307 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
# mypy: disallow_untyped_defs, disallow_incomplete_defs, disallow_untyped_calls, disallow_untyped_decorators, warn_return_any, warn_unreachable, warn_unused_ignores, no_implicit_optional
"""
builderctl - Interact with autoPush and autoUpdate services.
It provides an easy way to interact with a server that has autoPush
and autoUpdate installed.
"""
import argparse
import shlex
import subprocess
import sys
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Callable, Optional, Protocol
# =============================================================================
# HARDCODED SERVER SETTINGS
# =============================================================================
SERVER_HOST = "fde3:b424:b5ce:1:be24:11ff:feb5:580c" # nix-builder.boreth.pve.6nw.de
SERVER_USER = "root"
REPO_REMOTE = "git@github.com:Zocker1999NET/server"
REPO_LOCAL = Path.cwd()
STAGING_BRANCH = "staging"
MAIN_BRANCH = "main"
AUTO_UPDATED_REMOTE = "nix-builder"
AUTO_UPDATED_BRANCH = "autoUpdated"
# =============================================================================
# =============================================================================
# REMOTE COMMAND EXECUTION HELPERS
# =============================================================================
def run_remote_command(
*cmd: str,
ssh_args: tuple[str, ...] = (),
capture_output: bool = False,
check: bool = True,
) -> subprocess.CompletedProcess[str]:
"""Execute a command on the remote server via SSH."""
cmd_str = shlex.join(cmd)
ssh_cmd = ["ssh", *ssh_args, f"{SERVER_USER}@{SERVER_HOST}", cmd_str]
return run_command(*ssh_cmd, check=check, capture_output=capture_output)
def run_remote_command_follow(*cmd: str) -> subprocess.CompletedProcess[str]:
"""Execute a command on the remote server via SSH with pseudo-terminal for interactive output."""
return run_remote_command(*cmd, ssh_args=("-t",), check=True)
def run_remote_command_follow_safe(*cmd: str) -> subprocess.CompletedProcess[str]:
"""Execute a command on the remote server via SSH with pseudo-terminal, handling KeyboardInterrupt gracefully."""
try:
return run_remote_command_follow(*cmd)
except KeyboardInterrupt:
# Return a dummy result for KeyboardInterrupt
# Create a minimal CompletedProcess to satisfy return type
return subprocess.CompletedProcess(args=[], returncode=0, stdout="", stderr="")
def get_service_status(*services: str) -> None:
"""Get and print the status of one or more systemd services on the remote server."""
# Note: systemctl returns exit code 3 if any service is inactive, so we don't check exit code
try:
cmd_args = ["systemctl", "status", "--no-pager"] + list(services)
result = run_remote_command(*cmd_args, capture_output=True, check=False)
print(result.stdout)
if result.stderr:
print(result.stderr, file=sys.stderr)
except subprocess.SubprocessError as e:
print(f"Error getting status for services: {e}", file=sys.stderr)
def start_service_follow(service_name: str) -> int:
"""Start a systemd service on the remote server and follow its journal logs."""
# Run commands sequentially, not chained with &&
# Use check=True for the first command (like && would)
run_remote_command("systemctl", "start", service_name, check=True)
result = run_remote_command_follow("journalctl", "--follow", f"--unit={service_name}")
return result.returncode
def get_service_last_start_time(service_name: str) -> str:
"""Get the last start time of a systemd service on the remote server."""
result = run_remote_command(
"systemctl", "show", service_name,
"--property=ActiveEnterTimestamp", "--value",
capture_output=True, check=True
)
return result.stdout.strip()
def journal_service(service_name: str, follow: bool = False) -> int:
"""
Show journal logs for a systemd service starting from its last start time.
Args:
service_name: The full systemd service name (e.g., "srv-autoPush.service")
follow: Whether to follow the journal (like tail -f)
"""
last_start = get_service_last_start_time(service_name)
if not last_start:
print(f"Error: Could not determine last start time for {service_name}", file=sys.stderr)
return 1
cmd_args = [
"journalctl",
f"--unit={service_name}",
f"--since={last_start}",
]
if follow:
cmd_args.append("--follow")
print(f"Showing logs since: {last_start}")
print(f"Command: {shlex.join(cmd_args)}")
print("---")
result = run_remote_command_follow(*cmd_args)
return result.returncode
def run_command(
*cmd: str, check: bool = True, capture_output: bool = False
) -> subprocess.CompletedProcess[str]:
"""Run a shell command locally and return the result."""
return subprocess.run(cmd, check=check, capture_output=capture_output, text=True)
def run_commands(
*cmds: tuple[str, ...], check: bool = True, capture_output: bool = False
) -> list[subprocess.CompletedProcess[str]]:
"""Run multiple shell commands sequentially and return all results."""
results: list[subprocess.CompletedProcess[str]] = []
for cmd in cmds:
result = run_command(*cmd, check=check, capture_output=capture_output)
results.append(result)
return results
# =============================================================================
# GIT HELPER FUNCTIONS
# =============================================================================
def check_git_clean() -> bool:
"""Check if the git working directory is clean (no uncommitted changes)."""
result = run_command(
"git", "status", "--untracked-files=no", "--porcelain",
check=True,
capture_output=True,
)
return result.stdout.strip() == ""
# =============================================================================
# GLOBAL COMMANDS
# =============================================================================
def show_config() -> int:
"""Display the current configuration."""
print(f"Server Host: {SERVER_HOST}")
print(f"Server User: {SERVER_USER}")
print(f"Repo Remote: {REPO_REMOTE}")
print(f"Staging Branch: {STAGING_BRANCH}")
print(f"Main Branch: {MAIN_BRANCH}")
print(f"autoUpdate Remote: {AUTO_UPDATED_REMOTE}")
print(f"autoUpdate Branch: {AUTO_UPDATED_BRANCH}")
return 0
# =============================================================================
# SERVICE DEFINITIONS
# =============================================================================
class CommandHandler(Protocol):
"""Protocol defining the interface for command handlers."""
def __call__(self, args: argparse.Namespace) -> int:
"""Execute the command and return exit code."""
...
class SubparsersAction(Protocol):
"""Protocol defining the interface for subparser actions."""
def add_parser(self, name: str, **kwargs: Any) -> argparse.ArgumentParser:
...
class ServiceDefinition(ABC):
"""Abstract base class for all services."""
name: str = ""
systemd_name: str = ""
help: str = ""
def _add_command(
self,
subparsers: SubparsersAction,
cmd_name: str,
cmd_help: str,
handler: CommandHandler,
) -> argparse.ArgumentParser:
"""Add a command subparser and attach its handler to the namespace."""
cmd_parser = subparsers.add_parser(cmd_name, help=cmd_help)
cmd_parser.set_defaults(**{f"{self.name}_handler": handler})
return cmd_parser
def _add_commands(self, subparsers: SubparsersAction) -> None:
"""
Add the base commands (trigger, status, journal) to the subparsers.
Override in subclasses to add more commands.
"""
self._add_command(subparsers, "trigger", "Trigger the service", self.trigger)
self._add_command(subparsers, "status", "Show service status", self.status)
journal_parser = self._add_command(subparsers, "journal", "Show service journal", self.journal)
journal_parser.add_argument(
"-f", "--follow", action="store_true",
help="Follow the journal (like tail -f)"
)
def create_subparser(self, subparsers: SubparsersAction) -> None:
"""Create the subparser for this service."""
parser = subparsers.add_parser(self.name, help=self.help)
sub = parser.add_subparsers(dest=f"{self.name}_subcommand")
self._add_commands(sub)
@property
def service_name(self) -> str:
"""Return the systemd service name with proper suffix."""
return f"{self.systemd_name}.service"
def trigger(self, args: argparse.Namespace) -> int:
"""Trigger the service to run."""
return start_service_follow(self.service_name)
def status(self, args: argparse.Namespace) -> int:
"""Show the status of the service."""
get_service_status(self.service_name)
return 0
def journal(self, args: argparse.Namespace) -> int:
"""Show the journal of the service."""
follow = getattr(args, "follow", False)
return journal_service(self.service_name, follow=follow)
class AutoPushService(ServiceDefinition):
"""Service for pushing NixOS configurations and triggering builds."""
name = "autoPush"
systemd_name = "srv-autoPush"
help = "autoPush service commands"
# Uses base class create_subparser and _add_commands (trigger, status, journal)
def trigger(self, args: argparse.Namespace) -> int:
# auto-push local changes for convenience
result = run_command(
"git", "branch", "--show-current", check=True, capture_output=True
)
branch = result.stdout.strip()
if branch == STAGING_BRANCH:
run_command("git", "push")
return super().trigger(args)
class AutoUpdateService(ServiceDefinition):
"""Service for pulling updated configurations after builds."""
name = "autoUpdate"
systemd_name = "srv-autoUpdate"
help = "autoUpdate service commands"
def _add_commands(self, subparsers: SubparsersAction) -> None:
"""Add commands: pull + base commands (trigger, status, journal)."""
self._add_command(subparsers, "pull", "Pull autoUpdate from server", self.pull)
super()._add_commands(subparsers)
def pull(self, args: argparse.Namespace) -> int:
"""
Pull autoUpdate from the server.
This replicates the functionality of the former pull_autoUpdate.sh.
"""
if not check_git_clean():
print(
"ERROR: Working directory must be clean for pulling autoUpdate!",
file=sys.stderr,
)
return 1
run_commands(
("git", "switch", STAGING_BRANCH),
("git", "pull"),
("git", "fetch", AUTO_UPDATED_REMOTE),
("git", "merge", "--ff-only", f"{AUTO_UPDATED_REMOTE}/{AUTO_UPDATED_BRANCH}"),
("nix", "flake", "archive"),
(
"git",
"rebase",
"--exec",
"git commit --amend --no-edit",
f"origin/{STAGING_BRANCH}",
),
("git", "push"),
)
return 0
# =============================================================================
# SERVICE REGISTRY
# =============================================================================
class ServiceRegistry:
"""Registry for managing services and dispatching commands."""
def __init__(self) -> None:
self._services: dict[str, ServiceDefinition] = {}
def register(self, service: ServiceDefinition) -> None:
self._services[service.name] = service
def get(self, name: str) -> Optional[ServiceDefinition]:
return self._services.get(name)
def get_all_service_names(self) -> list[str]:
"""Return all systemd service names from registered services."""
return [svc.service_name for svc in self._services.values()]
def status_all(self) -> int:
"""Check the status of all registered services on the server."""
service_names = self.get_all_service_names()
if service_names:
get_service_status(*service_names)
return 0
def create_parser(self) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="builderctl",
description="Control builder services on the remote NixOS builder",
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# Register service subparsers
for svc in self._services.values():
svc.create_subparser(subparsers)
# Add global commands
subparsers.add_parser("status", help="Check status of all services")
subparsers.add_parser("config", help="Show current configuration")
return parser
def dispatch(self, args: argparse.Namespace) -> int:
# Global commands
if args.command == "status":
return self.status_all()
elif args.command == "config":
return show_config()
# Service-specific - handler already attached to namespace via set_defaults
handler: Optional[CommandHandler] = getattr(args, f"{args.command}_handler", None)
if handler is not None:
return handler(args)
return 1
# =============================================================================
# MAIN
# =============================================================================
def main() -> int:
registry = ServiceRegistry()
registry.register(AutoPushService())
registry.register(AutoUpdateService())
parser = registry.create_parser()
args = parser.parse_args()
if not hasattr(args, 'command') or args.command is None:
parser.print_help()
return 1
return registry.dispatch(args)
if __name__ == "__main__":
sys.exit(main())