2024-07-13 01:46:18 +02:00
|
|
|
"""Shell to Python Continuous Deployment."""
|
2024-06-09 23:15:19 +02:00
|
|
|
|
2024-06-14 14:25:21 +02:00
|
|
|
__version__ = "0.0.1"
|
|
|
|
|
2024-06-21 12:10:09 +02:00
|
|
|
import sys
|
2024-08-08 01:53:43 +02:00
|
|
|
from os import environ, pathsep
|
2024-06-09 18:09:53 +02:00
|
|
|
from pathlib import Path
|
2024-04-26 21:58:35 +02:00
|
|
|
|
2024-06-24 15:33:12 +02:00
|
|
|
from rwx import fs
|
2024-06-13 12:34:05 +02:00
|
|
|
from rwx.log import stream as log
|
2024-07-17 11:57:44 +02:00
|
|
|
from rwx.ps import run
|
2024-06-09 00:01:38 +02:00
|
|
|
|
2024-06-27 13:57:15 +02:00
|
|
|
from spcd import cmd
|
2024-07-28 20:54:20 +02:00
|
|
|
from spcd.ci import project, projects
|
2024-09-14 03:17:28 +02:00
|
|
|
from spcd.shell import env
|
2024-07-28 20:54:20 +02:00
|
|
|
from spcd.util import browse, cat, split, step
|
2024-04-27 01:10:30 +02:00
|
|
|
|
2024-06-27 13:57:15 +02:00
|
|
|
COMMANDS_PREFIX = "spcd-"
|
2024-04-26 23:55:20 +02:00
|
|
|
|
|
|
|
|
2024-07-28 20:54:20 +02:00
|
|
|
def clone_project_branch() -> None:
|
2024-08-19 17:50:34 +02:00
|
|
|
"""Clone project on triggering branch into the current workspace."""
|
2024-07-28 21:48:52 +02:00
|
|
|
if not projects.environment.get("GITLAB_CI"):
|
|
|
|
step("Clone project branch")
|
|
|
|
log.info(projects)
|
|
|
|
split()
|
|
|
|
log.info(project)
|
|
|
|
split()
|
2024-09-16 01:13:18 +02:00
|
|
|
log.info(
|
|
|
|
"""\
|
2024-09-15 01:00:17 +02:00
|
|
|
%s
|
2024-07-28 20:54:20 +02:00
|
|
|
↓
|
2024-09-16 01:13:18 +02:00
|
|
|
""",
|
|
|
|
project.url,
|
|
|
|
)
|
2024-07-28 21:48:52 +02:00
|
|
|
run(
|
|
|
|
"git",
|
|
|
|
"clone",
|
|
|
|
"--branch",
|
|
|
|
project.branch,
|
|
|
|
"--",
|
|
|
|
project.url,
|
2024-09-14 02:44:50 +02:00
|
|
|
str(project.root),
|
2024-07-28 21:48:52 +02:00
|
|
|
)
|
2024-07-28 20:54:20 +02:00
|
|
|
|
|
|
|
|
2024-06-09 20:28:09 +02:00
|
|
|
def install_commands(path: str) -> None:
|
2024-08-19 17:50:34 +02:00
|
|
|
"""Make commands callable in the operating system."""
|
2024-06-07 10:26:18 +02:00
|
|
|
step("Install commands")
|
2024-06-09 18:09:53 +02:00
|
|
|
user = Path("/usr/local/bin")
|
2024-04-27 01:10:30 +02:00
|
|
|
for command in [
|
2024-06-07 10:26:18 +02:00
|
|
|
"browse-workspace",
|
|
|
|
"build-project",
|
2024-08-07 23:32:56 +02:00
|
|
|
"check-project",
|
2024-06-07 10:26:18 +02:00
|
|
|
"synchronize",
|
2024-04-27 01:10:30 +02:00
|
|
|
]:
|
2024-06-13 13:44:59 +02:00
|
|
|
log.info(command)
|
2024-06-09 20:28:09 +02:00
|
|
|
(user / f"{COMMANDS_PREFIX}{command}").symlink_to(path)
|
2024-05-31 00:29:12 +02:00
|
|
|
|
|
|
|
|
2024-08-19 10:11:39 +02:00
|
|
|
def install_python_packages() -> None:
|
2024-08-19 17:50:34 +02:00
|
|
|
"""Upgrade pip then install extra Python packages."""
|
2024-08-19 10:11:39 +02:00
|
|
|
step("Install Python packages")
|
|
|
|
log.info("pip")
|
|
|
|
run("pip", "install", "--upgrade", "pip")
|
|
|
|
split()
|
|
|
|
packages = [
|
2024-08-30 10:05:52 +02:00
|
|
|
"gitlint",
|
2024-08-19 17:02:06 +02:00
|
|
|
"hatch",
|
2024-08-19 10:11:39 +02:00
|
|
|
"mypy",
|
|
|
|
"pelican",
|
2024-09-15 15:04:52 +02:00
|
|
|
"pydoclint",
|
2024-09-14 21:48:38 +02:00
|
|
|
"pylint",
|
2024-08-19 10:11:39 +02:00
|
|
|
"pytest",
|
|
|
|
"ruff",
|
|
|
|
"sphinx",
|
|
|
|
"sphinx-rtd-theme",
|
|
|
|
"twine",
|
|
|
|
]
|
|
|
|
for package in packages:
|
|
|
|
log.info(package)
|
|
|
|
run("pip", "install", *packages)
|
|
|
|
|
|
|
|
|
2024-07-29 14:20:15 +02:00
|
|
|
def list_environment_variables() -> None:
|
2024-08-19 17:50:34 +02:00
|
|
|
"""List accessible variables and their public contents."""
|
2024-07-29 14:20:15 +02:00
|
|
|
step("List environment variables")
|
|
|
|
for variable, value in sorted(projects.environment.items()):
|
2024-07-30 00:42:50 +02:00
|
|
|
if variable not in ["SPCD", "SPCD_SSH_KEY"]:
|
2024-09-15 01:00:17 +02:00
|
|
|
log.info("%s = %s", variable, value)
|
2024-07-29 14:20:15 +02:00
|
|
|
else:
|
2024-09-15 01:00:17 +02:00
|
|
|
log.info("%s", variable)
|
2024-07-29 14:20:15 +02:00
|
|
|
|
|
|
|
|
2024-09-15 00:03:48 +02:00
|
|
|
def main(main_file: str) -> None:
|
2024-08-19 17:56:35 +02:00
|
|
|
"""Entry point to initialize environment or run a specific command."""
|
2024-08-08 01:53:43 +02:00
|
|
|
paths = environ["PATH"].split(pathsep)
|
|
|
|
if env.SPCD_PYTHON_VENV_BINARIES not in paths:
|
|
|
|
environ["PATH"] = pathsep.join([env.SPCD_PYTHON_VENV_BINARIES, *paths])
|
2024-06-21 12:10:09 +02:00
|
|
|
path, *arguments = sys.argv
|
|
|
|
name = Path(path).name
|
|
|
|
if name == "__main__.py":
|
2024-07-29 14:20:15 +02:00
|
|
|
list_environment_variables()
|
2024-07-28 20:54:20 +02:00
|
|
|
clone_project_branch()
|
2024-07-29 10:26:07 +02:00
|
|
|
set_ssh()
|
2024-09-15 00:03:48 +02:00
|
|
|
install_commands(main_file)
|
2024-08-19 10:11:39 +02:00
|
|
|
install_python_packages()
|
2024-06-21 12:10:09 +02:00
|
|
|
else:
|
2024-09-14 00:35:47 +02:00
|
|
|
f = getattr(cmd, name.replace("-", "_"))
|
|
|
|
f(*arguments)
|
2024-06-21 12:10:09 +02:00
|
|
|
|
|
|
|
|
2024-07-29 10:26:07 +02:00
|
|
|
def set_ssh() -> None:
|
2024-08-19 17:50:34 +02:00
|
|
|
"""Set things up to enable access to targets through SSH."""
|
2024-06-07 10:26:18 +02:00
|
|
|
step("Set SSH")
|
2024-07-13 00:46:07 +02:00
|
|
|
# get variables
|
2024-07-29 10:26:07 +02:00
|
|
|
ssh_hosts = projects.environment.get("SPCD_SSH_HOSTS")
|
|
|
|
ssh_key = projects.environment.get("SPCD_SSH_KEY")
|
2024-07-13 00:46:07 +02:00
|
|
|
# set key type
|
2024-06-07 10:26:18 +02:00
|
|
|
ssh_type = "ed25519"
|
2024-07-13 00:46:07 +02:00
|
|
|
# set home directory
|
2024-06-09 18:09:53 +02:00
|
|
|
home = Path("~").expanduser()
|
2024-07-13 00:46:07 +02:00
|
|
|
# make home directory
|
2024-06-09 18:09:53 +02:00
|
|
|
ssh = home / ".ssh"
|
2024-06-09 18:52:29 +02:00
|
|
|
ssh.mkdir(exist_ok=True, parents=True)
|
2024-06-09 18:09:53 +02:00
|
|
|
ssh.chmod(0o700)
|
2024-07-13 00:46:07 +02:00
|
|
|
# write private key
|
2024-06-09 18:09:53 +02:00
|
|
|
key = ssh / f"id_{ssh_type}"
|
2024-05-31 00:29:12 +02:00
|
|
|
if ssh_key:
|
|
|
|
fs.write(key, ssh_key)
|
2024-06-09 18:09:53 +02:00
|
|
|
key.chmod(0o400)
|
2024-07-13 00:46:07 +02:00
|
|
|
# write known hosts
|
2024-06-09 18:09:53 +02:00
|
|
|
known = ssh / "known_hosts"
|
2024-05-31 00:29:12 +02:00
|
|
|
if ssh_hosts:
|
|
|
|
fs.write(known, ssh_hosts)
|
2024-06-09 18:09:53 +02:00
|
|
|
known.chmod(0o400)
|
2024-07-13 00:46:07 +02:00
|
|
|
# display
|
2024-06-06 11:36:06 +02:00
|
|
|
browse(ssh)
|
2024-07-29 11:06:58 +02:00
|
|
|
if known.exists():
|
|
|
|
cat(known)
|