Beispiel #1
0
def handle_plan_report(obj: JenkinsJobManager,
                       use_pager=True,
                       output=None) -> bool:
    """cli helper for plan report"""
    changes = obj.detected_changes()

    if output:
        # machine readable
        for line in obj.plan_report(report_format=output):
            click.echo(line, nl=False)
    elif changes:
        # default "human" readable, show changes
        def output_format(line):
            if line.startswith("+"):
                return click.style(line, fg="green")
            elif line.startswith("-"):
                return click.style(line, fg="red")
            else:
                return line

        gen_lines = map(output_format, obj.plan_report())
        if use_pager is True:
            click.echo_via_pager(gen_lines)
        else:
            for line in gen_lines:
                click.echo(line, nl=False)
    else:
        # default "human" readable, no changes
        click.secho("No changes.", fg="green")

    return changes
Beispiel #2
0
def jjm_plan(obj: JenkinsJobManager, skip_pager: bool,
             target: typing.List[str]):
    """check for changes"""
    check_auth(obj)
    obj.gather(target)
    handle_validation_errors(obj)
    changes = handle_plan_report(obj, use_pager=not skip_pager)
    if changes is True:
        raise click.exceptions.Exit(2)
Beispiel #3
0
def handle_validation_errors(obj: JenkinsJobManager, ignore=False):
    warnings = obj.validate_metadata()
    errors = False
    for job_name, warning in warnings:
        if errors is False:
            click.secho("Validation Errors", fg="red", bold=True)
            errors = True
        click.secho(f"Job {job_name!r}: {warning}", fg="red")

    if errors is True and ignore is False:
        raise click.exceptions.Exit(5)
Beispiel #4
0
def jjm_apply(obj: JenkinsJobManager, target: str, allow_delete: bool):
    """check and apply changes"""
    if allow_delete:
        obj.config.allow_delete = True
    check_auth(obj)
    obj.gather(target)
    handle_validation_errors(obj)
    if obj.detected_changes() is False:
        click.secho("No changes to apply.", fg="green")
        return
    handle_plan_report(obj, use_pager=False)
    click.confirm(click.style("Apply changes?", bold=True), abort=True)
    changecounts, msg = obj.apply_plan()
    click.echo(msg)
    _deletes = changecounts[DELETE]
    if allow_delete is False and _deletes > 0:
        click.secho(
            f"Ignored {_deletes} deletes.\nUse --allow-delete to override.",
            fg="red",
        )
Beispiel #5
0
def handle_plan_report(obj: JenkinsJobManager, use_pager=True) -> bool:
    """cli helper for plan report"""
    def output_format(line):
        if line.startswith("+"):
            return click.style(line, fg="green")
        elif line.startswith("-"):
            return click.style(line, fg="red")
        else:
            return line

    if obj.detected_changes() is True:
        gen_lines = map(output_format, obj.plan_report())
        if use_pager is True:
            click.echo_via_pager(gen_lines)
        else:
            for line in gen_lines:
                click.echo(line, nl=False)
        changes = True
    else:
        click.secho("No changes.", fg="green")
        changes = False
    return changes
Beispiel #6
0
def jjm(ctx, debug, working_dir, url):
    """Jenkins Job Management"""
    if debug:
        log.setLevel(logging.DEBUG)
    if working_dir:
        os.chdir(working_dir)

    config = {}
    if url:
        config["url"] = url
    jjm = JenkinsJobManager(config_overrides=config)
    ctx.obj = jjm
    if not jjm.config.url:
        click.echo("\n"
                   "ERROR: No jenkins url configured.\n"
                   "Create a ./jjm.ini file with contents:\n"
                   "    [jenkins]\n"
                   "    url = https://yourjenkinsurl.com/\n")
        raise click.exceptions.Exit(1)
Beispiel #7
0
def jjm_import(obj: JenkinsJobManager, target: str):
    check_auth(obj)
    obj.gather(target)
    missing = obj.import_missing()
    click.secho(f"Imported {len(missing)} jobs.", fg="green")
Beispiel #8
0
def check_auth(obj: JenkinsJobManager):
    """cli helper for auth check"""
    if not obj.check_authentication():
        click.secho(f"Bad login detected for {obj.config}", fg="red")
        click.echo("Try the login subcommand")
        raise click.exceptions.Exit(1)
Beispiel #9
0
def jjm_check(obj: JenkinsJobManager, load_plugins):
    """check syntax/config"""
    if load_plugins:
        obj.load_plugins_list()
    obj.generate_jjb_xml()
    handle_validation_errors(obj)