コード例 #1
0
ファイル: launch_e2e.py プロジェクト: gmarcy/kubeinit
def main(cluster_type, job_type):
    """Run the main method."""
    #
    # This method can deploy multiple
    # cluster_type configurations like:
    # multinode or singlenode
    # And multiple job_types like:
    # pr, periodic or submariner
    #

    if cluster_type == 'singlenode':
        c_type = '1'
    else:
        c_type = '[2-9]'

    now = datetime.now()

    #
    # KubeInit's pull request check
    #
    if job_type == 'pr':
        #
        # We will check for labels in opened PRs in the main
        # kubeinit project
        #
        print("'launch_e2e.py' ==> Pull request job")
        gh = Github(os.environ['GH_TOKEN'])
        pipeline_id = os.getenv('CI_PIPELINE_ID', 0)
        repo = gh.get_repo("kubeinit/kubeinit")
        branches = repo.get_branches()
        output = 0
        url = os.getenv('CI_PIPELINE_URL', "")
        print(
            "'launch_e2e.py' ==> The job results will be published in runtime at: "
            + url)
        for branch in branches:
            for pr in repo.get_pulls(state='open',
                                     sort='created',
                                     base=branch.name):
                labels = [item.name for item in pr.labels]

                #
                # Adjust dinamically the verbosity based on a GH label.
                #
                ansible_label = 'verbosity'
                ansible_label_found = next(
                    (s for s in labels if ansible_label in s), None)
                if ansible_label_found:
                    global GH_LABELS
                    GH_LABELS.append('GH_ANSIBLE_VERBOSITY=' +
                                     ansible_label_found.split("=")[1])

                sha = pr.head.sha
                execute = False
                print("'launch_e2e.py' ==> The current labels in PR: " +
                      str(pr.number) + " are:")
                print(labels)
                for label in labels:
                    # DISTRO-DRIVER-CONTROLLERS-COMPUTES-HYPERVISORS-[VIRTUAL_SERVICES|CONTAINERIZED_SERVICES]-[LAUNCH_FROM_CONTAINER|LAUNCH_FROM_HOST]
                    if re.match(
                            r"[a-z|0-9|\.]+-[a-z]+-[1-9]-[0-9]-" + c_type +
                            "-[c|h]", label):
                        print("'launch_e2e.py' ==> Matching a PR label")
                        params = label.split("-")
                        distro = params[0]
                        driver = params[1]
                        masters = params[2]
                        workers = params[3]
                        hypervisors = params[4]
                        launch_from = params[5]
                        execute = True
                        remove_label(label, pr, repo)
                        break
                if execute:
                    repo.get_commit(sha=sha).create_status(
                        state="pending",
                        target_url=url + str(pipeline_id),
                        description="Running...",
                        context="%s-%s-%s-%s-%s-%s" %
                        (distro, driver, masters, workers, hypervisors,
                         launch_from))
                    repository = 'kubeinit/kubeinit'
                    branch_name = branch.name
                    pr_number = pr.number
                    start_time = time.time()
                    timestamp = now.strftime("%Y.%m.%d.%H.%M.%S")
                    job_name = (distro + "-" + driver + "-" + masters + "-" +
                                workers + "-" + hypervisors + "-" +
                                launch_from + "-" + job_type + "-" +
                                pipeline_id + "-" + timestamp)

                    output = run_e2e_job(distro, driver, masters, workers,
                                         hypervisors, job_type, pipeline_id,
                                         repository, branch_name, pr_number,
                                         launch_from, job_name)
                    if output == 0:
                        state = "success"
                    else:
                        state = "failure"

                    dur_mins = str(round((time.time() - start_time) / 60, 2))
                    desc = ("Ended with %s in %s minutes" % (state, dur_mins))

                    dest_url = 'https://storage.googleapis.com/kubeinit-ci/jobs/' + str(
                        job_name) + "-" + str(output) + '/index.html'
                    print("'launch_e2e.py' ==> The destination URL is: " +
                          dest_url)
                    # We update the status with the job result
                    repo.get_commit(sha=sha).create_status(
                        state=state,
                        target_url=dest_url,
                        description=desc,
                        context="%s-%s-%s-%s-%s-%s" %
                        (distro, driver, masters, workers, hypervisors,
                         launch_from))
                    pr.create_issue_comment(
                        body="%s-%s-%s-%s-%s-%s-%s-%s-%s" %
                        (distro, driver, masters, workers, hypervisors,
                         launch_from, state, str(start_time), dur_mins))

                    if output == 0:
                        exit()
                    else:
                        exit(1)
                else:
                    print("'launch_e2e.py' ==> No need to do anything")
                    print("'launch_e2e.py' ==> Trying another PR...")
                    # We can not have exit() here, in that case we will be
                    # checking only the first PR, if the first PR do not have
                    # the labels, then we need to check the next one until we find a
                    # PR with the labels.

    #
    # KubeInit's periodic job check
    #
    if (re.match(r"periodic(=[a-z|0-9|,|\.]+)?", job_type) or re.match(
            r"periodic=([a-z|0-9|\.]+-[a-z]+-[1-9]-[0-9]-[1-9]-[c|h],?)+",
            job_type) or job_type == 'periodic=random'):

        #
        # We will run the periodic jobs depending on the
        # hardware we called this script from [multinode-singlenode]
        #
        gh = Github(os.environ['GH_TOKEN'])
        pipeline_id = os.getenv('CI_PIPELINE_ID', 0)
        repo = gh.get_repo("kubeinit/kubeinit")

        output = "go"
        # Something linke:
        # url = "https://gitlab.com/kubeinit/kubeinit-ci/pipelines/"
        url = os.getenv('CI_PIPELINE_URL', "")
        print(
            "'launch_e2e.py' ==> The job results will be published in runtime at: "
            + url)

        if '=' in job_type:
            labels = get_periodic_jobs_labels(job_type.split("=")[1])
        else:
            labels = get_periodic_jobs_labels('all')
        job_type = job_type.split("=")[0]
        print("'launch_e2e.py' ==> All the labels to check are")
        print(labels)

        for label in labels:
            execute = False
            print("'launch_e2e.py' ==> The label to be processed is: " + label)

            # DISTRO-DRIVER-CONTROLLERS-COMPUTES-HYPERVISORS-[VIRTUAL_SERVICES|CONTAINERIZED_SERVICES]-[LAUNCH_FROM_CONTAINER|LAUNCH_FROM_HOST]
            if re.match(
                    r"[a-z|0-9|\.]+-[a-z]+-[1-9]-[0-9]-" + c_type + "-[c|h]",
                    label):
                print("'launch_e2e.py' ==> Matching a PR label")
                params = label.split("-")
                distro = params[0]
                driver = params[1]
                masters = params[2]
                workers = params[3]
                hypervisors = params[4]
                launch_from = params[5]
                execute = True

            if execute:
                repository = 'kubeinit/kubeinit'
                branch_name = 'main'
                pr_number = 'latest'
                timestamp = 'weekly'
                start_time = time.time()
                job_name = (distro + "-" + driver + "-" + masters + "-" +
                            workers + "-" + hypervisors + "-" + launch_from +
                            "-" + job_type + "-" + pipeline_id + "-" +
                            timestamp)

                # print(distro)
                # raise Exception("'launch_e2e.py' ==> STOP!")

                output = run_e2e_job(distro, driver, masters, workers,
                                     hypervisors, job_type, pipeline_id,
                                     repository, branch_name, pr_number,
                                     launch_from, job_name)

                if output == 0:
                    state = "success"
                else:
                    state = "failure"
                dur_mins = str(round((time.time() - start_time) / 60, 2))
                issue = repo.get_issue(number=595)
                issue.create_comment(
                    body="%s-%s-%s-%s-%s-%s-%s-%s-%s" %
                    (distro, driver, masters, workers, hypervisors,
                     launch_from, state, str(start_time), dur_mins))

    #
    # KubeInit's submariner PR check
    #
    if job_type == 'submariner':
        gh = Github(os.environ['GH_SUBMARINER_TOKEN'])

        pipeline_id = os.getenv('CI_PIPELINE_ID', 0)

        repo = gh.get_repo("submariner-io/submariner-operator")
        branches = repo.get_branches()

        output = 0

        url = os.getenv('CI_PIPELINE_URL', "")
        print(
            "'launch_e2e.py' ==> The job results will be published in runtime at: "
            + url)

        for branch in branches:
            for pr in repo.get_pulls(state='open',
                                     sort='created',
                                     base=branch.name):
                labels = [item.name for item in pr.labels]
                sha = pr.head.sha
                execute = False
                if ("check-okd-rke" in labels):
                    distro = "okd.rke"
                    driver = "libvirt"
                    masters = "1"
                    workers = "2"
                    hypervisors = "1"
                    launch_from = "h"
                    execute = True
                    remove_label("check-okd-rke", pr, repo)

                if execute:
                    repo.get_commit(sha=sha).create_status(
                        state="pending",
                        target_url=url + str(pipeline_id),
                        description="Running...",
                        context="%s-%s-%s-%s-%s-%s" %
                        (distro, driver, masters, workers, hypervisors,
                         launch_from))
                    repository = repo.name
                    print("'launch_e2e.py' ==> The repository name is: " +
                          repository)
                    branch_name = branch.name
                    pr_number = pr.number
                    start_time = time.time()
                    timestamp = now.strftime("%Y.%m.%d.%H.%M.%S")
                    job_name = (distro + "-" + driver + "-" + masters + "-" +
                                workers + "-" + hypervisors + "-" +
                                launch_from + "-" + job_type + "-" +
                                pipeline_id + "-" + timestamp)
                    output = run_e2e_job(distro, driver, masters, workers,
                                         hypervisors, job_type, pipeline_id,
                                         repository, branch_name, pr_number,
                                         launch_from, job_name)
                    if output == 0:
                        print("'launch_e2e.py' ==> The current job succeeded")
                        state = "success"
                    else:
                        print("'launch_e2e.py' ==> The current job failed")
                        state = "failure"

                    dur_mins = str(round((time.time() - start_time) / 60, 2))
                    desc = ("Ended with %s in %s minutes" % (state, dur_mins))

                    dest_url = 'https://storage.googleapis.com/kubeinit-ci/jobs/' + str(
                        job_name) + "-" + str(output) + '/index.html'
                    print("'launch_e2e.py' ==> Desc message: " + desc)

                    print("'launch_e2e.py' ==> The destination URL is: " +
                          dest_url)
                    # We update the status with the job result
                    repo.get_commit(sha=sha).create_status(
                        state=state,
                        target_url=dest_url,
                        description=desc,
                        context="%s-%s-%s-%s-%s-%s" %
                        (distro, driver, masters, workers, hypervisors,
                         launch_from))
                    pr.create_issue_comment(
                        body="%s-%s-%s-%s-%s-%s-%s-%s-%s" %
                        (distro, driver, masters, workers, hypervisors,
                         launch_from, state, str(start_time), dur_mins))
                    if output == 0:
                        exit()
                    else:
                        exit(1)
                else:
                    print("'launch_e2e.py' ==> No need to do anything")
                    exit()
コード例 #2
0
def main():
    """Run the main method."""
    gh = Github(os.environ['GH_SUBMARINER_TOKEN'])
    gh_token = os.environ['GH_SUBMARINER_TOKEN']

    vars_file_path = os.getenv('VARS_FILE', "")
    pipeline_id = os.getenv('CI_PIPELINE_ID', 0)

    repo = gh.get_repo("submariner-io/submariner-operator")
    branches = repo.get_branches()

    output = 0
    # Something linke:
    # url = "https://gitlab.com/kubeinit/kubeinit-ci/pipelines/"
    url = os.getenv('CI_PIPELINE_URL', "")
    print("The job results will be published in runtime at: " + url)

    for branch in branches:
        for pr in repo.get_pulls(state='open', sort='created', base=branch.name):
            labels = [item.name for item in pr.labels]

            sha = pr.head.sha
            committer_email = repo.get_commit(sha=sha).commit.committer.email
            print(committer_email)

            execute = False
            scenario = "default"

            if ("check-okd-rke" in labels):
                distro = "okd.rke"
                driver = "libvirt"
                master = "1"
                worker = "2"
                execute = True
                scenario = "submariner"
                remove_label("check-okd-rke", pr, repo)

            if execute:
                now = datetime.now()
                now.strftime("%m.%d.%Y.%H.%M.%S")
                job_name = pipeline_id + "-" + distro + "-" + driver + "-" + master + "-" + worker + "-" + scenario + "-" + now.strftime("%Y.%m.%d.%H.%M.%S")
                print("Let's run the e2e job, distro %s driver %s " % (distro, driver))
                print("-------------")
                print("-------------")
                print("Running the e2e job for: " + str(pr.number) + " " + pr.title)
                print("-------------")
                print("-------------")
                print("-------------")

                # We update the status to show that we are executing the e2e test
                print("Current status")
                print(repo.get_commit(sha=sha).get_statuses())
                repo.get_commit(sha=sha).create_status(state="pending",
                                                       target_url=url + str(pipeline_id),
                                                       description="Running...",
                                                       context="%s-%s-%s-master-%s-worker-%s" % (distro,
                                                                                                 driver,
                                                                                                 master,
                                                                                                 worker,
                                                                                                 scenario))
                print("The pipeline ID is: " + str(pipeline_id))
                print("The clouds.yml path is: " + str(vars_file_path))
                # We trigger the e2e job
                start_time = time.time()
                try:
                    print("We call the downstream job configuring its parameters")
                    subprocess.check_call("./ci/run_submariner.sh %s %s %s %s %s %s %s %s" % (str(branch.name),
                                                                                              str(pr.number),
                                                                                              str(vars_file_path),
                                                                                              str(distro),
                                                                                              str(driver),
                                                                                              str(master),
                                                                                              str(worker),
                                                                                              str(scenario)),
                                          shell=True)
                except Exception as e:
                    print('An exception hapened executing Ansible')
                    print(e)
                    output = 1

                try:
                    print("Render ara data")
                    subprocess.check_call("./ci/ara.sh %s" % (str(job_name) + "-" + str(output)), shell=True)
                except Exception as e:
                    print('An exception hapened rendering ara data')
                    print(e)
                    output = 1

                print("starting the uploader job")
                upload_error = upload_logs(str(job_name) + "-" + str(output), gh_token)
                print("finishing the uploader job")

                if output == 0:
                    state = "success"
                else:
                    state = "failure"

                desc = ("Ended with %s in %s minutes" % (state, round((time.time() - start_time) / 60, 2)))

                print(desc)
                print(state)

                if upload_error == 1:
                    dest_url = url + str(pipeline_id)
                else:
                    dest_url = 'https://kubeinit-bot.github.io/kubeinit-ci-results/' + str(job_name) + "-" + str(output) + '/index.html'

                print("The destination URL is: " + dest_url)
                # We update the status with the job result
                repo.get_commit(sha=sha).create_status(state=state,
                                                       target_url=dest_url,
                                                       description=desc,
                                                       context="%s-%s-%s-master-%s-worker-%s" % (distro,
                                                                                                 driver,
                                                                                                 master,
                                                                                                 worker,
                                                                                                 scenario))
            else:
                print("No need to do anything")
            if execute:
                exit()