def checkout_branch(self): if path.exists(path.join(self.clone_location,".git")): get_back = getcwd() chdir(self.clone_location) cmd1 = "{base_command} fetch --all".format( base_command=self.base_command ) _, e = run_command( cmd=cmd1, shell=True ) no_err = str.format( "Cloning into '{}'...\n", self.clone_location ) if e and e != no_err: raise CommandOutputError(e) #need to fix this properly git checkout sends the output to #stderr, but it can result in missing on actual error cmd2 = "{base_command} checkout -b {branch_name}" \ " origin/{branch_name}".format( base_command=self.base_command, branch_name=self.git_branch ) o, e = run_command( cmd=cmd2, shell=True ) chdir(get_back)
def delete_buildconfigs(self, bcs, wait_between_delete=5): """ Deletes the given list of bcs """ command = ("oc delete -n {} bc {} --ignore-not-found=true " "--now=true --include-uninitialized=true") for bc in bcs: _print("Deleting buildConfig {}".format(bc)) run_command(command.format(self.namespace, bc), shell=True) time.sleep(wait_between_delete)
def start_build(self, pipeline_name): """ Given a pipeline name, start the build for same """ command = "oc start-build {} -n {}".format(pipeline_name, self.namespace) out, _ = run_command(command, shell=True) _print(out)
def start_build(self, pipeline_name): """ Given a pipeline name, start the build for same """ command = "oc start-build {} -n {}".format( pipeline_name, self.namespace) out, _ = run_command(command, shell=True) _print(out)
def list_all_builds(self): """ List all the builds """ command = """\ oc get builds -o name -o template \ --template='{{range .items }}{{.metadata.name}}:{{.status.phase}} {{end}}'""" output, _ = run_command(command, shell=True) return output.strip().split()
def checkout_branch(self): if path.exists(path.join(self.clone_location, ".git")): get_back = getcwd() chdir(self.clone_location) cmd1 = "{base_command} fetch --all".format( base_command=self.base_command) _, e = run_command(cmd=cmd1, shell=True) no_err = str.format("Cloning into '{}'...\n", self.clone_location) if e and e != no_err: raise CommandOutputError(e) #need to fix this properly git checkout sends the output to #stderr, but it can result in missing on actual error cmd2 = "{base_command} checkout -b {branch_name}" \ " origin/{branch_name}".format( base_command=self.base_command, branch_name=self.git_branch ) o, e = run_command(cmd=cmd2, shell=True) chdir(get_back)
def list_all_buildConfigs(self): """ List all available buildConfigs returns list of buildConfigs available """ command = "oc get bc -o name -n {}".format(self.namespace) bcs, _ = run_command(command, shell=True) if not bcs.strip(): return [] else: return bcs.strip().split("\n")
def pull_remote(self): if path.exists(path.join(self.clone_location, ".git")): self.checkout_branch() get_back = getcwd() chdir(self.clone_location) cmd1 = "{base_command} pull origin/{branch_name}".format( base_command=self.base_command, branch_name=self.git_branch) o, e = run_command(cmd=cmd1, shell=True) chdir(get_back) else: self.clone() self.checkout_branch()
def pull_remote(self): if path.exists(path.join(self.clone_location,".git")): self.checkout_branch() get_back = getcwd() chdir(self.clone_location) cmd1 = "{base_command} pull origin/{branch_name}".format( base_command=self.base_command, branch_name=self.git_branch ) o, e = run_command(cmd = cmd1, shell=True) chdir(get_back) else: self.clone() self.checkout_branch()
def apply_build_job(self, project, template_location="seed-job/template.yaml" ): """ Applies the build job template that creates pipeline to build image, and trigger first time build as well. :param project: The name of project, where the template is to be applied :param template_location: The location of the template file. """ oc_process = "oc process -f {0} {1}".format( template_location, self.template_params ) oc_apply = "oc apply -n {} -f -".format(self.namespace) # oc process and oc apply command combined with a shell pipe command = oc_process + " | " + oc_apply # format the command with project params command = command.format( git_url=project.git_url, git_path=project.git_path, git_branch=project.git_branch, target_file=project.target_file, build_context=project.build_context, desired_tag=project.desired_tag, depends_on=project.depends_on, notify_email=project.notify_email, pipeline_name=project.pipeline_name, app_id=project.app_id, job_id=project.job_id, pre_build_context=project.pre_build_context, pre_build_script=project.pre_build_script, namespace=self.namespace, registry_url=self.registry_url, from_address=self.from_address, smtp_server=self.smtp_server, ccp_openshift_slave_image=self.ccp_openshift_slave_image, notify_cc_emails=self.notify_cc_emails, registry_alias=self.registry_alias, master_job_cpu=self.master_job_cpu, master_job_memory=self.master_job_memory ) # process and apply buildconfig output, _ = run_command(command, shell=True) _print(output)
def apply_build_job(self, project, template_location="seed-job/template.yaml"): """ Applies the build job template that creates pipeline to build image, and trigger first time build as well. :param project: The name of project, where the template is to be applied :param template_location: The location of the template file. """ oc_process = "oc process -f {0} {1}".format(template_location, self.template_params) oc_apply = "oc apply -n {} -f -".format(self.namespace) # oc process and oc apply command combined with a shell pipe command = oc_process + " | " + oc_apply # format the command with project params command = command.format( git_url=project.git_url, git_path=project.git_path, git_branch=project.git_branch, target_file=project.target_file, build_context=project.build_context, desired_tag=project.desired_tag, depends_on=project.depends_on, notify_email=project.notify_email, pipeline_name=project.pipeline_name, app_id=project.app_id, job_id=project.job_id, pre_build_context=project.pre_build_context, pre_build_script=project.pre_build_script, namespace=self.namespace, registry_url=self.registry_url, from_address=self.from_address, smtp_server=self.smtp_server, ccp_openshift_slave_image=self.ccp_openshift_slave_image, notify_cc_emails=self.notify_cc_emails, registry_alias=self.registry_alias, master_job_cpu=self.master_job_cpu, master_job_memory=self.master_job_memory) # process and apply buildconfig output, _ = run_command(command, shell=True) _print(output)
def email(self, smtp_server, sub, body, from_add, to_adds, cc_adds=[]): """ Send email using given details smtp_server: URL of smtp server sub: Subject of email body: Body of email from_add: From address to_adds: A list of to addresses cc_adds: (optional) A list addresses to mark in Cc returns status, msg status=True/False msg=stdout/stderr """ command = """\ echo -e '{body}' | /usr/bin/mailx -r {from_address} {cc_opts} -S \ {smtp_server} -s "{subject}" {to_addresses}""" # it would return '' for when cc_add==[] # eg output: "-c [email protected] -c [email protected]" cc_opts = " ".join(["-c " + i for i in cc_adds]) # to addresses to_addresses = " ".join(to_adds) # escape the \n and \t characters body = self.escape_text(body) command = command.format( body=body, from_address=from_add, cc_opts=cc_opts, smtp_server=smtp_server, subject=sub, to_addresses=to_addresses) # send email stdout, stderr = run_command(command, shell=True) if stderr: return False, stderr else: return True, "Email sent to {}".format(to_adds)
def list_builds_except(self, status=["Complete", "Failed"], filter_builds=["seed-job"]): """ List the builds except the phase(s) provided default status=["Complete", "Failed"] <-- This will return all the builds except the status.phase in ["Complete", "Failed"]. If provided a list of $filter_builds, it will filter mentioned builds from outstanding builds. The builds name has build number string appended, for eg seed-job-1, seed-job-2, thus filtering checks if outstanding build name starts with given $filter_builds. If status=[], return all the builds :arg status: Status of outstanding builds to filter :type status: List :arg filter_builds: Builds to filter from outstanding builds :type filter_builds: List :return: List of outstanding builds :rtype: List """ if not status: return self.list_all_builds() conditional = '(ne .status.phase "{}") ' condition = '' for phase in status: condition = condition + conditional.format(phase) command = """\ oc get builds -o name -o template --template='{{range .items }} \ {{if and %s }} {{.metadata.name}}:{{.status.phase}} \ {{end}}{{end}}'""" % condition output, _ = run_command(command, shell=True) output = output.strip().split(' ') output = [ each for each in output if not each.startswith(tuple(filter_builds)) and each ] return output
def list_builds_except( self, status=["Complete", "Failed"], filter_builds=["seed-job"]): """ List the builds except the phase(s) provided default status=["Complete", "Failed"] <-- This will return all the builds except the status.phase in ["Complete", "Failed"]. If provided a list of $filter_builds, it will filter mentioned builds from outstanding builds. The builds name has build number string appended, for eg seed-job-1, seed-job-2, thus filtering checks if outstanding build name starts with given $filter_builds. If status=[], return all the builds :arg status: Status of outstanding builds to filter :type status: List :arg filter_builds: Builds to filter from outstanding builds :type filter_builds: List :return: List of outstanding builds :rtype: List """ if not status: return self.list_all_builds() conditional = '(ne .status.phase "{}") ' condition = '' for phase in status: condition = condition + conditional.format(phase) command = """\ oc get builds -o name -o template --template='{{range .items }} \ {{if and %s }} {{.metadata.name}}:{{.status.phase}} \ {{end}}{{end}}'""" % condition output, _ = run_command(command, shell=True) output = output.strip().split(' ') output = [each for each in output if not each.startswith(tuple(filter_builds)) and each] return output
def apply_weekly_scan(self, project, template_location="weekly-scan/template.yaml" ): """ Applies the weekly scan template, creating a pipeline for same :param project: The name of the project where the template is to be applied. :param template_location: The location of template file. """ oc_process = "oc process -f {0} {1}".format( template_location, self.weekly_scan_template_params ) oc_apply = "oc apply -n {} -f -".format(self.namespace) # oc process and oc apply command combined with a shell pipe command = oc_process + " | " + oc_apply # format the command with project params command = command.format( namespace=self.namespace, git_url=project.git_url, git_branch=project.git_branch, desired_tag=project.desired_tag, notify_email=project.notify_email, pipeline_name=project.pipeline_name, app_id=project.app_id, job_id=project.job_id, registry_url=self.registry_url, from_address=self.from_address, smtp_server=self.smtp_server, ccp_openshift_slave_image=self.ccp_openshift_slave_image, notify_cc_emails=self.notify_cc_emails, registry_alias=self.registry_alias, ) # process and apply buildconfig output, _ = run_command(command, shell=True) _print(output)
def email(self, smtp_server, sub, body, from_add, to_adds, cc_adds=[]): """ Send email using given details smtp_server: URL of smtp server sub: Subject of email body: Body of email from_add: From address to_adds: A list of to addresses cc_adds: (optional) A list addresses to mark in Cc returns status, msg status=True/False msg=stdout/stderr """ command = """\ echo -e '{body}' | /usr/bin/mailx -r {from_address} {cc_opts} -S \ {smtp_server} -s "{subject}" {to_addresses}""" # it would return '' for when cc_add==[] # eg output: "-c [email protected] -c [email protected]" cc_opts = " ".join(["-c " + i for i in cc_adds]) # to addresses to_addresses = " ".join(to_adds) # escape the \n and \t characters body = self.escape_text(body) command = command.format(body=body, from_address=from_add, cc_opts=cc_opts, smtp_server=smtp_server, subject=sub, to_addresses=to_addresses) # send email stdout, stderr = run_command(command, shell=True) if stderr: return False, stderr else: return True, "Email sent to {}".format(to_adds)
def apply_weekly_scan(self, project, template_location="weekly-scan/template.yaml"): """ Applies the weekly scan template, creating a pipeline for same :param project: The name of the project where the template is to be applied. :param template_location: The location of template file. """ oc_process = "oc process -f {0} {1}".format( template_location, self.weekly_scan_template_params) oc_apply = "oc apply -n {} -f -".format(self.namespace) # oc process and oc apply command combined with a shell pipe command = oc_process + " | " + oc_apply # format the command with project params command = command.format( namespace=self.namespace, git_url=project.git_url, git_branch=project.git_branch, desired_tag=project.desired_tag, notify_email=project.notify_email, pipeline_name=project.pipeline_name, app_id=project.app_id, job_id=project.job_id, registry_url=self.registry_url, from_address=self.from_address, smtp_server=self.smtp_server, ccp_openshift_slave_image=self.ccp_openshift_slave_image, notify_cc_emails=self.notify_cc_emails, registry_alias=self.registry_alias, ) # process and apply buildconfig output, _ = run_command(command, shell=True) _print(output)