Ejemplo n.º 1
0
def main():

    common.connect()

    [name, namespace, container] = common.get_core_node_parameter_list()
    common.log_pod_parameters(log, {
        'name': name,
        'namespace': namespace,
        'container_name': container
    })
    common.verify_pod_exists(name, namespace)

    source_file = os.environ.get('RD_FILE_COPY_FILE')
    destination_file = os.environ.get('RD_FILE_COPY_DESTINATION')

    # force print destination to avoid error with node-executor
    print(destination_file)

    log.debug("Copying file from %s to %s", source_file, destination_file)

    destination_path = os.path.dirname(destination_file)
    destination_file_name = os.path.basename(destination_file)

    common.copy_file(name, namespace, container, source_file, destination_path,
                     destination_file_name)
Ejemplo n.º 2
0
def main():

    common.connect()

    [name, namespace, container] = common.get_core_node_parameter_list()

    if not container:
        core_v1 = client.CoreV1Api()
        response = core_v1.read_namespaced_pod_status(
            name=name,
            namespace=namespace,
            pretty="True"
        )
        container = response.spec.containers[0].name

    common.log_pod_parameters(log, {'name': name, 'namespace': namespace, 'container_name': container})
    common.verify_pod_exists(name, namespace)

    shell = os.environ.get('RD_CONFIG_SHELL')

    if 'RD_EXEC_COMMAND' in os.environ:
        command = os.environ['RD_EXEC_COMMAND']
    else:
        command = os.environ['RD_CONFIG_COMMAND']

    log.debug("Command: %s ", command)

    # calling exec and wait for response.
    exec_command = [
        shell,
        '-c',
        command]

    resp, error = common.run_interactive_command(name, namespace, container, exec_command)

    if error:
        log.error("error running script")
        sys.exit(1)
Ejemplo n.º 3
0
def main():
    common.connect()
    api = core_v1_api.CoreV1Api()

    [name, namespace, container] = common.get_core_node_parameter_list()
    common.verify_pod_exists(name, namespace)

    delete_on_fail = False
    if os.environ.get('RD_CONFIG_DELETEONFAIL') == 'true':
        delete_on_fail = True

    resp = None
    try:
        resp = api.read_namespaced_pod(name=name, namespace=namespace)
    except ApiException as e:
        if e.status != 404:
            log.exception("Unknown error:")
            exit(1)

    if not resp:
        log.error("Pod %s does not exits.", name)
        exit(1)

    if not container:
        core_v1 = client.CoreV1Api()
        response = core_v1.read_namespaced_pod_status(name=name,
                                                      namespace=namespace,
                                                      pretty="True")

        if response.spec.containers:
            container = response.spec.containers[0].name
        else:
            log.error("Container not found")
            exit(1)

    common.log_pod_parameters(log, {
        'name': name,
        'namespace': namespace,
        'container_name': container
    })

    script = os.environ.get('RD_CONFIG_SCRIPT')

    # Python 3 expects bytes string to transfer the data.
    if PY == 3:
        script = script.encode('utf-8')

    log.debug("--------------------------")
    log.debug("Pod Name:  %s", name)
    log.debug("Namespace: %s", namespace)
    log.debug("Container: %s", container)
    log.debug("--------------------------")

    invocation = "/bin/bash"
    if 'RD_CONFIG_INVOCATION' in os.environ:
        invocation = os.environ.get('RD_CONFIG_INVOCATION')

    destination_path = "/tmp"

    if 'RD_NODE_FILE_COPY_DESTINATION_DIR' in os.environ:
        destination_path = os.environ.get('RD_NODE_FILE_COPY_DESTINATION_DIR')

    temp = tempfile.NamedTemporaryFile()
    destination_file_name = os.path.basename(temp.name)
    full_path = destination_path + "/" + destination_file_name

    try:
        temp.write(script)
        temp.seek(0)

        log.debug("coping script from %s to %s", temp.name, full_path)

        common.copy_file(name=name,
                         namespace=namespace,
                         container=container,
                         source_file=temp.name,
                         destination_path=destination_path,
                         destination_file_name=destination_file_name)

    finally:
        temp.close()

    permissions_command = ["chmod", "+x", full_path]

    log.debug("setting permissions %s", permissions_command)
    resp = common.run_command(name=name,
                              namespace=namespace,
                              container=container,
                              command=permissions_command)

    if resp.peek_stdout():
        print(resp.read_stdout())

    if resp.peek_stderr():
        print(resp.read_stderr())
        sys.exit(1)

    # calling exec and wait for response.
    exec_command = invocation.split(" ")
    exec_command.append(full_path)

    if 'RD_CONFIG_ARGUMENTS' in os.environ:
        arguments = os.environ.get('RD_CONFIG_ARGUMENTS')
        exec_command.append(arguments)

    log.debug("running script %s", exec_command)

    resp, error = common.run_interactive_command(name=name,
                                                 namespace=namespace,
                                                 container=container,
                                                 command=exec_command)
    if error:
        log.error("error running script")

        if delete_on_fail:
            log.info("removing POD on fail")
            data = {"name": name, "namespace": namespace}
            common.delete_pod(data)
            log.info("POD deleted")
        sys.exit(1)

    rm_command = ["rm", full_path]

    log.debug("removing file %s", rm_command)
    resp = common.run_command(name=name,
                              namespace=namespace,
                              container=container,
                              command=rm_command)

    if resp.peek_stdout():
        log.debug(resp.read_stdout())

    if resp.peek_stderr():
        log.debug(resp.read_stderr())
        sys.exit(1)
Ejemplo n.º 4
0
def main():

    common.connect()

    api = core_v1_api.CoreV1Api()

    data = common.get_code_node_parameter_dictionary()
    common.log_pod_parameters(log, data)

    data["api_version"] = os.environ.get('RD_CONFIG_API_VERSION')
    data["image"] = os.environ.get('RD_CONFIG_IMAGE')
    data["ports"] = os.environ.get('RD_CONFIG_PORTS')
    data["replicas"] = os.environ.get('RD_CONFIG_REPLICAS')
    data["labels"] = os.environ.get('RD_CONFIG_LABELS')
    if os.environ.get('RD_CONFIG_ENVIRONMENTS'):
        data["environments"] = os.environ.get('RD_CONFIG_ENVIRONMENTS')

    if os.environ.get('RD_CONFIG_ENVIRONMENTS_SECRETS'):
        evs = os.environ.get('RD_CONFIG_ENVIRONMENTS_SECRETS')
        data["environments_secrets"] = evs

    if os.environ.get('RD_CONFIG_LIVENESS_PROBE'):
        data["liveness_probe"] = os.environ.get('RD_CONFIG_LIVENESS_PROBE')

    if os.environ.get('RD_CONFIG_READINESS_PROBE'):
        data["readiness_probe"] = os.environ.get('RD_CONFIG_READINESS_PROBE')

    if os.environ.get('RD_CONFIG_VOLUME_MOUNTS'):
        data["volume_mounts"] = os.environ.get('RD_CONFIG_VOLUME_MOUNTS')

    if os.environ.get('RD_CONFIG_VOLUMES'):
        data["volumes"] = os.environ.get('RD_CONFIG_VOLUMES')

    if os.environ.get('RD_CONFIG_CONTAINER_COMMAND'):
        cc = os.environ.get('RD_CONFIG_CONTAINER_COMMAND')
        data["container_command"] = cc

    if os.environ.get('RD_CONFIG_CONTAINER_ARGS'):
        data["container_args"] = os.environ.get('RD_CONFIG_CONTAINER_ARGS')

    if os.environ.get('RD_CONFIG_RESOURCES_REQUESTS'):
        rr = os.environ.get('RD_CONFIG_RESOURCES_REQUESTS')
        data["resources_requests"] = rr

    if os.environ.get('RD_CONFIG_WAITREADY'):
        data["waitready"] = os.environ.get('RD_CONFIG_WAITREADY')

    if os.environ.get('RD_CONFIG_IMAGEPULLSECRETS'):
        data["image_pull_secrets"] = os.environ.get(
            'RD_CONFIG_IMAGEPULLSECRETS')

    pod = create_pod(data)
    resp = None
    try:
        resp = api.create_namespaced_pod(namespace=data['namespace'],
                                         body=pod,
                                         pretty="True")

        print("Pod Created successfully")

    except ApiException:
        log.exception("Exception creating pod:")
        exit(1)

    if not resp:
        print("Pod %s does not exist" % data['name'])
        exit(1)
Ejemplo n.º 5
0
def wait():
    try:
        data = common.get_code_node_parameter_dictionary()
        common.log_pod_parameters(log, data)

        name = data['name']
        namespace = data['namespace']
        retries = int(environ.get("RD_CONFIG_RETRIES"))
        sleep = float(environ.get("RD_CONFIG_SLEEP"))
        show_log = environ.get("RD_CONFIG_SHOW_LOG") == "true"

        core_v1 = client.CoreV1Api()

        print("Checking job status ...")
        api_response = core_v1.read_namespaced_pod_status(name=name,
                                                          namespace=namespace,
                                                          pretty="True")
        log.debug(api_response.status)

        status = False

        if api_response.status.container_statuses:
            status = api_response.status.container_statuses[0].ready

        # Poll for completion if retries
        retries_count = 0
        while not status:
            retries_count = retries_count + 1
            if retries_count > retries:
                log.error("Number of retries exceeded")
                sys.exit(1)

            print("Waiting for pod completion ... ")
            time.sleep(sleep)
            api_response = core_v1.read_namespaced_pod_status(
                name=name, namespace=namespace, pretty="True")

            if api_response.status.container_statuses:
                status = api_response.status.container_statuses[0].ready

            log.debug(api_response.status.container_statuses)

        if show_log:
            for i in range(len(api_response.status.container_statuses)):
                log.info(
                    "Fetching logs from pod: {0}    -- container {1} ".format(
                        name, api_response.status.container_statuses[i].name))
                pod_log = core_v1.read_namespaced_pod_log(
                    name=name,
                    namespace=namespace,
                    container=api_response.status.container_statuses[i].name)

                print(
                    "========================== job log start =========================="
                )
                print(pod_log)
                print(
                    "=========================== job log end ==========================="
                )

        if status:
            print("Pod ready")
            sys.exit(0)

    except ApiException:
        log.exception("Exception waiting for job:")
        sys.exit(1)