Beispiel #1
0
def list(format: str):
    arch = get_current_arch()
    repositories = []
    for dir in os.listdir(snap_common() / "addons"):
        try:
            repo_dir = snap_common() / "addons" / dir
            addons_yaml = repo_dir / "addons.yaml"
            with open(addons_yaml, "r") as fin:
                addons = yaml.safe_load(fin)

            count = 0
            for addon in addons["microk8s-addons"]["addons"]:
                if arch in addon["supported_architectures"]:
                    count += 1

            source = "(built-in)"
            try:
                remote_url = subprocess.check_output(
                    [GIT, "remote", "get-url", "origin"],
                    cwd=repo_dir,
                    stderr=subprocess.DEVNULL).decode()
                revision = subprocess.check_output(
                    [GIT, "rev-parse", "HEAD"],
                    cwd=repo_dir,
                    stderr=subprocess.DEVNULL).decode()[:6]
                source = "{}@{}".format(remote_url.strip(), revision.strip())
            except (subprocess.CalledProcessError, TypeError, ValueError):
                pass

            repositories.append({
                "name":
                dir,
                "addons":
                count,
                "source":
                source,
                "description":
                addons["microk8s-addons"]["description"],
            })

        except Exception as e:
            click.echo("could not load addons from {}: {}".format(
                addons_yaml, e),
                       err=True)

    if format == "json":
        click.echo(json.dumps(repositories))
    elif format == "yaml":
        click.echo(yaml.safe_dump(repositories))
    elif format == "table":
        click.echo(("{:10} {:>6} {}").format("REPO", "ADDONS", "SOURCE"))
        for repo in repositories:
            click.echo("{:10} {:>6} {}".format(repo["name"], repo["addons"],
                                               repo["source"]))
Beispiel #2
0
def update(name: str):
    repo_dir = snap_common() / "addons" / name
    if not repo_dir.exists():
        click.echo("Error: repository '{}' does not exist".format(name),
                   err=True)
        sys.exit(1)

    if not (repo_dir / ".git").exists():
        click.echo(
            "Error: built-in repository '{}' cannot be updated".format(name),
            err=True)
        sys.exit(1)

    commit_before_pull = git_current_commit(repo_dir)

    click.echo("Updating repository {}".format(name))
    subprocess.check_call([GIT, "pull"], cwd=repo_dir)

    try:
        validate_addons_repo(repo_dir)
    except RepoValidationError as err:
        click.echo(err.message, err=True)
        click.echo(f"Rolling back repository {name}")
        git_rollback(commit_before_pull, repo_dir)
        sys.exit(1)
Beispiel #3
0
def add(name: str, repository: str, reference: str, force: bool):
    repo_dir = snap_common() / "addons" / name
    if repo_dir.exists():
        if not force:
            click.echo("Error: repository '{}' already exists!".format(name),
                       err=True)
            click.echo("Use the --force flag to overwrite it", err=True)
            sys.exit(1)

        click.echo("Removing {}".format(repo_dir))
        shutil.rmtree(repo_dir)

    cmd = [GIT, "clone", repository, repo_dir]
    if reference is not None:
        cmd += ["-b", reference]
    subprocess.check_call(cmd)
    subprocess.check_call(["chgrp", get_group(), "-R", repo_dir])

    try:
        validate_addons_repo(repo_dir)
    except RepoValidationError as err:
        click.echo(err.message, err=True)
        click.echo(f"Removing {repo_dir}")
        shutil.rmtree(repo_dir)
        sys.exit(1)
Beispiel #4
0
def remove(name: str):
    repo_dir = snap_common() / "addons" / name
    if not repo_dir.exists():
        click.echo("Error: repository '{}' does not exist".format(name), err=True)
        sys.exit(1)

    click.echo("Removing {}".format(repo_dir))
    shutil.rmtree(repo_dir)
Beispiel #5
0
def disable_addon(repo, addon, args=None):
    """
    Try to disable an addon. Ignore any errors and/or silence any output.
    """
    if args is None:
        args = []
    wait_for_ready(timeout=30)
    script = snap_common() / "addons" / repo / "addons" / addon / "disable"
    if not script.exists():
        return

    try:
        subprocess.run([script, *args], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    except (FileNotFoundError, PermissionError, subprocess.CalledProcessError) as e:
        print(f"Ignoring error: {e}", file=sys.stderr)

    wait_for_ready(timeout=30)