2024-07-12 23:46:18 +00:00
|
|
|
"""Shell to Python Continuous Deployment."""
|
2024-06-09 21:15:19 +00:00
|
|
|
|
2024-06-14 12:25:21 +00:00
|
|
|
__version__ = "0.0.1"
|
|
|
|
|
2024-06-21 10:10:09 +00:00
|
|
|
import sys
|
2024-08-07 23:53:43 +00:00
|
|
|
from os import environ, pathsep
|
2024-06-09 16:09:53 +00:00
|
|
|
from pathlib import Path
|
2024-04-26 19:58:35 +00:00
|
|
|
|
2024-08-07 23:53:43 +00:00
|
|
|
import env
|
2024-06-24 13:33:12 +00:00
|
|
|
from rwx import fs
|
2024-06-13 10:34:05 +00:00
|
|
|
from rwx.log import stream as log
|
2024-07-17 09:57:44 +00:00
|
|
|
from rwx.ps import run
|
2024-06-08 22:01:38 +00:00
|
|
|
|
2024-06-27 11:57:15 +00:00
|
|
|
from spcd import cmd
|
2024-07-28 18:54:20 +00:00
|
|
|
from spcd.ci import project, projects
|
|
|
|
from spcd.util import browse, cat, split, step
|
2024-04-26 23:10:30 +00:00
|
|
|
|
2024-06-27 11:57:15 +00:00
|
|
|
COMMANDS_PREFIX = "spcd-"
|
2024-04-26 21:55:20 +00:00
|
|
|
|
|
|
|
|
2024-07-28 18:54:20 +00:00
|
|
|
def clone_project_branch() -> None:
|
2024-07-28 19:48:52 +00:00
|
|
|
if not projects.environment.get("GITLAB_CI"):
|
|
|
|
step("Clone project branch")
|
|
|
|
log.info(projects)
|
|
|
|
split()
|
|
|
|
log.info(project)
|
|
|
|
split()
|
|
|
|
log.info(f"""\
|
2024-07-28 18:54:20 +00:00
|
|
|
{project.url}
|
|
|
|
↓
|
|
|
|
""")
|
2024-07-28 19:48:52 +00:00
|
|
|
run(
|
|
|
|
"git",
|
|
|
|
"clone",
|
|
|
|
"--branch",
|
|
|
|
project.branch,
|
|
|
|
"--",
|
|
|
|
project.url,
|
|
|
|
project.root,
|
|
|
|
)
|
2024-07-28 18:54:20 +00:00
|
|
|
|
|
|
|
|
2024-06-09 18:28:09 +00:00
|
|
|
def install_commands(path: str) -> None:
|
2024-06-07 08:26:18 +00:00
|
|
|
step("Install commands")
|
2024-06-09 16:09:53 +00:00
|
|
|
user = Path("/usr/local/bin")
|
2024-04-26 23:10:30 +00:00
|
|
|
for command in [
|
2024-06-07 08:26:18 +00:00
|
|
|
"browse-workspace",
|
|
|
|
"build-project",
|
2024-08-07 21:32:56 +00:00
|
|
|
"check-project",
|
2024-06-07 08:26:18 +00:00
|
|
|
"synchronize",
|
2024-04-26 23:10:30 +00:00
|
|
|
]:
|
2024-06-13 11:44:59 +00:00
|
|
|
log.info(command)
|
2024-06-09 18:28:09 +00:00
|
|
|
(user / f"{COMMANDS_PREFIX}{command}").symlink_to(path)
|
2024-05-30 22:29:12 +00:00
|
|
|
|
|
|
|
|
2024-07-29 12:20:15 +00:00
|
|
|
def list_environment_variables() -> None:
|
|
|
|
step("List environment variables")
|
|
|
|
for variable, value in sorted(projects.environment.items()):
|
2024-07-29 22:42:50 +00:00
|
|
|
if variable not in ["SPCD", "SPCD_SSH_KEY"]:
|
2024-07-29 12:20:15 +00:00
|
|
|
log.info(f"{variable} = {value}")
|
|
|
|
else:
|
|
|
|
log.info(f"{variable}")
|
|
|
|
|
|
|
|
|
2024-06-21 14:26:33 +00:00
|
|
|
def main(main: str) -> None:
|
2024-08-07 23:53:43 +00: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 10:10:09 +00:00
|
|
|
path, *arguments = sys.argv
|
|
|
|
name = Path(path).name
|
|
|
|
if name == "__main__.py":
|
2024-07-29 12:20:15 +00:00
|
|
|
list_environment_variables()
|
2024-07-28 18:54:20 +00:00
|
|
|
clone_project_branch()
|
2024-07-29 08:26:07 +00:00
|
|
|
set_ssh()
|
2024-07-28 18:54:20 +00:00
|
|
|
install_commands(main)
|
2024-06-21 10:10:09 +00:00
|
|
|
else:
|
2024-06-24 13:34:38 +00:00
|
|
|
function = getattr(cmd, name.replace("-", "_"))
|
2024-06-21 10:10:09 +00:00
|
|
|
function(*arguments)
|
|
|
|
|
|
|
|
|
2024-07-29 08:26:07 +00:00
|
|
|
def set_ssh() -> None:
|
2024-06-07 08:26:18 +00:00
|
|
|
step("Set SSH")
|
2024-07-12 22:46:07 +00:00
|
|
|
# get variables
|
2024-07-29 08:26:07 +00:00
|
|
|
ssh_hosts = projects.environment.get("SPCD_SSH_HOSTS")
|
|
|
|
ssh_key = projects.environment.get("SPCD_SSH_KEY")
|
2024-07-12 22:46:07 +00:00
|
|
|
# set key type
|
2024-06-07 08:26:18 +00:00
|
|
|
ssh_type = "ed25519"
|
2024-07-12 22:46:07 +00:00
|
|
|
# set home directory
|
2024-06-09 16:09:53 +00:00
|
|
|
home = Path("~").expanduser()
|
2024-07-12 22:46:07 +00:00
|
|
|
# make home directory
|
2024-06-09 16:09:53 +00:00
|
|
|
ssh = home / ".ssh"
|
2024-06-09 16:52:29 +00:00
|
|
|
ssh.mkdir(exist_ok=True, parents=True)
|
2024-06-09 16:09:53 +00:00
|
|
|
ssh.chmod(0o700)
|
2024-07-12 22:46:07 +00:00
|
|
|
# write private key
|
2024-06-09 16:09:53 +00:00
|
|
|
key = ssh / f"id_{ssh_type}"
|
2024-05-30 22:29:12 +00:00
|
|
|
if ssh_key:
|
|
|
|
fs.write(key, ssh_key)
|
2024-06-09 16:09:53 +00:00
|
|
|
key.chmod(0o400)
|
2024-07-12 22:46:07 +00:00
|
|
|
# write known hosts
|
2024-06-09 16:09:53 +00:00
|
|
|
known = ssh / "known_hosts"
|
2024-05-30 22:29:12 +00:00
|
|
|
if ssh_hosts:
|
|
|
|
fs.write(known, ssh_hosts)
|
2024-06-09 16:09:53 +00:00
|
|
|
known.chmod(0o400)
|
2024-07-12 22:46:07 +00:00
|
|
|
# display
|
2024-06-06 09:36:06 +00:00
|
|
|
browse(ssh)
|
2024-07-29 09:06:58 +00:00
|
|
|
if known.exists():
|
|
|
|
cat(known)
|