Skip to content
Snippets Groups Projects
transfer_to_builder.py 2.48 KiB
import subprocess
import paramiko
import logging
import fnmatch
import getpass
import sys
import os

from scp import SCPClient

logger = logging.getLogger("buildtools/transfer_to_builder")
logger.setLevel(logging.INFO)
hander = logging.StreamHandler(stream=sys.stdout)

def get_build_pkg_names():
    """
    Search through pkgs directory for built .tar.bz2 packages
    :return: List of package archive file names
    """
    pkg_names = []
    d = "build/pkgs/noarch/"
    try:
        for file in os.listdir(d):
            if fnmatch.fnmatch(file, "*.tar.bz2"):
                pkg_names.append(d + file)
    except FileNotFoundError as e:
        logger.error(e)

    return pkg_names

def create_ssh_client(server):
    """
    Use paramiko to load SSH keys if they exist and set up an SSH connection to a server.
    :param server: The server to connect to
    :return: Initialized SSH client object.
    """
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    while True:
        username = input("Enter NRAO username: ")
        password = getpass.getpass(prompt="Enter NRAO password: ")

        try:
            client.connect(server, username=username, password=password)
        except paramiko.AuthenticationException as e:
            logger.error(e)
            logger.error("Invalid credentials. Try again.")
            continue
        break

    return client

def transfer_packages(pkg_names):
    """
    Use shell commands to transfer build archives to builder and update its conda package index.
    :param pkg_names: Names of the .tar.bz2 files for the built packages.
    """
    logger.addHandler(hander)

    if len(pkg_names):
        builder_addr = "builder.aoc.nrao.edu"
        builder_path = "/home/builder.aoc.nrao.edu/content/conda/noarch"
        with create_ssh_client(builder_addr) as ssh:
            with SCPClient(ssh.get_transport()) as scp:
                [scp.put(pkg, builder_path) for pkg in pkg_names]
        cmd_cd = "cd {}".format(builder_path)
        cmd_index = "conda index .."
        cmd_chmod = "chmod -f 664 *"
        subprocess.run(["ssh", builder_addr,
                        cmd_cd + " && " +
                        cmd_index + " && " +
                        cmd_chmod])
    else:
        logger.error("No packages found in build/pkgs/noarch. "
              "Did conda build successfully build the package(s)?")

if __name__ == "__main__":
    transfer_packages(get_build_pkg_names())