def main(): common.connect() api = core_v1_api.CoreV1Api() container = None name = None namespace = None name = os.environ.get('RD_CONFIG_NAME', os.environ.get('RD_NODE_DEFAULT_NAME')) namespace = os.environ.get( 'RD_CONFIG_NAMESPACE', os.environ.get('RD_NODE_DEFAULT_NAMESPACE', 'default')) if 'RD_NODE_DEFAULT_CONTAINER_NAME' in os.environ: container = os.environ.get('RD_NODE_DEFAULT_CONTAINER_NAME') else: core_v1 = client.CoreV1Api() response = core_v1.read_namespaced_pod_status(name=name, namespace=namespace, pretty="True") container = response.spec.containers[0].name log.debug("--------------------------") log.debug("Pod Name: %s", name) log.debug("Namespace: %s", namespace) log.debug("Container: %s", container) log.debug("--------------------------") 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 exist", name) exit(1) 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)
def post_review(post_review_arguments, verbose=False): # The -u flag is for unbuffered output. This is needed for displaying # available data from stdout of the process up until an input action. post_review_command = ["python2", "-u", POST_REVIEW, "post"] # Provide some defaults that suit us if '--disable-proxy' not in post_review_arguments: post_review_arguments.append('--disable-proxy') if verbose: post_review_arguments.append('--debug') post_review_command.extend(post_review_arguments) environment = os.environ environment['LC_ALL'] = 'POSIX' # Setup PYTHONPATH before calling RBTools. If RBTools was properly # installed, these directories would be the standard 'site-packages' # directory. new_python_path = [RBTOOLS_COMPONENT, SIX_COMPONENT] if 'PYTHONPATH' in environment: new_python_path.append(environment['PYTHONPATH']) environment['PYTHONPATH'] = ":".join(new_python_path) print print "**" print "Posting review:" if verbose: print " Command: %s" % " ".join(post_review_command) print "=================" exit_status, output = \ run_interactive_command(post_review_command, environment) print "=================" if exit_status == 0: return _parse_review_id(output) elif "marked as submitted" in output: raise SubmittedReviewError() else: error("Failed to post review - did you enter the correct username and" " password?")
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)
def main(): common.connect() api = core_v1_api.CoreV1Api() namespace = os.environ.get('RD_CONFIG_NAMESPACE') name = os.environ.get('RD_CONFIG_NAME') log.debug("--------------------------") log.debug("Pod Name: %s" % name) log.debug("Namespace: %s " % namespace) log.debug("--------------------------") 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.error("Unknown error: %s" % e) exit(1) if not resp: log.error("Pod %s does not exits." % name) exit(1) 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) script = os.environ.get('RD_CONFIG_SCRIPT') 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, 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 = {} data["name"] = name data["namespace"] = namespace common.delete_pod(api, 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)