101 lines
2.4 KiB
Python
101 lines
2.4 KiB
Python
"""Shell to Python Continuous Deployment."""
|
|
|
|
__version__ = "0.0.1"
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from rwx import fs
|
|
from rwx.log import stream as log
|
|
from rwx.ps import run
|
|
|
|
from spcd import cmd
|
|
from spcd.ci import project, projects
|
|
from spcd.util import browse, cat, split, step
|
|
|
|
COMMANDS_PREFIX = "spcd-"
|
|
|
|
|
|
def clone_project_branch() -> None:
|
|
if not projects.environment.get("GITLAB_CI"):
|
|
step("Clone project branch")
|
|
log.info(projects)
|
|
split()
|
|
log.info(project)
|
|
split()
|
|
log.info(f"""\
|
|
{project.url}
|
|
↓
|
|
""")
|
|
run(
|
|
"git",
|
|
"clone",
|
|
"--branch",
|
|
project.branch,
|
|
"--",
|
|
project.url,
|
|
project.root,
|
|
)
|
|
|
|
|
|
def install_commands(path: str) -> None:
|
|
step("Install commands")
|
|
user = Path("/usr/local/bin")
|
|
for command in [
|
|
"browse-workspace",
|
|
"build-project",
|
|
"synchronize",
|
|
]:
|
|
log.info(command)
|
|
(user / f"{COMMANDS_PREFIX}{command}").symlink_to(path)
|
|
|
|
|
|
def list_environment_variables() -> None:
|
|
step("List environment variables")
|
|
for variable, value in sorted(projects.environment.items()):
|
|
if variable not in ["SPCD", "SPCD_SSH_KEY"]:
|
|
log.info(f"{variable} = {value}")
|
|
else:
|
|
log.info(f"{variable}")
|
|
|
|
|
|
def main(main: str) -> None:
|
|
path, *arguments = sys.argv
|
|
name = Path(path).name
|
|
if name == "__main__.py":
|
|
list_environment_variables()
|
|
clone_project_branch()
|
|
set_ssh()
|
|
install_commands(main)
|
|
else:
|
|
function = getattr(cmd, name.replace("-", "_"))
|
|
function(*arguments)
|
|
|
|
|
|
def set_ssh() -> None:
|
|
step("Set SSH")
|
|
# get variables
|
|
ssh_hosts = projects.environment.get("SPCD_SSH_HOSTS")
|
|
ssh_key = projects.environment.get("SPCD_SSH_KEY")
|
|
# set key type
|
|
ssh_type = "ed25519"
|
|
# set home directory
|
|
home = Path("~").expanduser()
|
|
# make home directory
|
|
ssh = home / ".ssh"
|
|
ssh.mkdir(exist_ok=True, parents=True)
|
|
ssh.chmod(0o700)
|
|
# write private key
|
|
key = ssh / f"id_{ssh_type}"
|
|
if ssh_key:
|
|
fs.write(key, ssh_key)
|
|
key.chmod(0o400)
|
|
# write known hosts
|
|
known = ssh / "known_hosts"
|
|
if ssh_hosts:
|
|
fs.write(known, ssh_hosts)
|
|
known.chmod(0o400)
|
|
# display
|
|
browse(ssh)
|
|
if known.exists():
|
|
cat(known)
|