コード例 #1
0
ファイル: agent.py プロジェクト: Dainerx/datadog-agent
def image_build(ctx, base_dir="omnibus", python_version="2", skip_tests=False):
    """
    Build the docker image
    """
    BOTH_VERSIONS = ["both", "2+3"]
    VALID_VERSIONS = ["2", "3"] + BOTH_VERSIONS
    if python_version not in VALID_VERSIONS:
        raise ParseError("provided python_version is invalid")

    base_dir = base_dir or os.environ.get("OMNIBUS_BASE_DIR")
    pkg_dir = os.path.join(base_dir, 'pkg')
    list_of_files = glob.glob(os.path.join(pkg_dir, 'datadog-agent*_amd64.deb'))
    # get the last debian package built
    if not list_of_files:
        print("No debian package build found in {}".format(pkg_dir))
        print("See agent.omnibus-build")
        raise Exit(code=1)
    latest_file = max(list_of_files, key=os.path.getctime)
    shutil.copy2(latest_file, "Dockerfiles/agent/")

    # Pull base image with content trust enabled
    pull_base_images(ctx, "Dockerfiles/agent/Dockerfile", signed_pull=True)
    common_build_opts = "-t {}".format(AGENT_TAG)
    if python_version not in BOTH_VERSIONS:
        common_build_opts = "{} --build-arg PYTHON_VERSION={}".format(common_build_opts, python_version)

    # Build with the testing target
    if not skip_tests:
        ctx.run("docker build {} --target testing Dockerfiles/agent".format(common_build_opts))

    # Build with the release target
    ctx.run("docker build {} --target release Dockerfiles/agent".format(common_build_opts))
    ctx.run("rm Dockerfiles/agent/datadog-agent*_amd64.deb")
コード例 #2
0
def image_build(ctx, arch='amd64', base_dir="omnibus", python_version="2", skip_tests=False, signed_pull=True):
    """
    Build the docker image
    """
    BOTH_VERSIONS = ["both", "2+3"]
    VALID_VERSIONS = ["2", "3"] + BOTH_VERSIONS
    if python_version not in VALID_VERSIONS:
        raise ParseError("provided python_version is invalid")

    build_context = "Dockerfiles/agent"
    base_dir = base_dir or os.environ.get("OMNIBUS_BASE_DIR")
    pkg_dir = os.path.join(base_dir, 'pkg')
    deb_glob = f'datadog-agent*_{arch}.deb'
    dockerfile_path = f"{build_context}/{arch}/Dockerfile"
    list_of_files = glob.glob(os.path.join(pkg_dir, deb_glob))
    # get the last debian package built
    if not list_of_files:
        print(f"No debian package build found in {pkg_dir}")
        print("See agent.omnibus-build")
        raise Exit(code=1)
    latest_file = max(list_of_files, key=os.path.getctime)
    shutil.copy2(latest_file, build_context)
    # Pull base image with content trust enabled
    pull_base_images(ctx, dockerfile_path, signed_pull)
    common_build_opts = f"-t {AGENT_TAG} -f {dockerfile_path}"
    if python_version not in BOTH_VERSIONS:
        common_build_opts = f"{common_build_opts} --build-arg PYTHON_VERSION={python_version}"

    # Build with the testing target
    if not skip_tests:
        ctx.run(f"docker build {common_build_opts} --target testing {build_context}")

    # Build with the release target
    ctx.run(f"docker build {common_build_opts} --target release {build_context}")
    ctx.run(f"rm {build_context}/{deb_glob}")
コード例 #3
0
def confirm(prompt='Continue?\n', failure_prompt='User cancelled task'):
    '''
    Prompt the user to continue. Repeat on unknown response. Raise
    ParseError on negative response
    '''
    response = input(prompt)

    try:
        response_bool = strtobool(response)
    except ValueError:
        print('Unkown Response. Confirm with y, yes, t, true, on or 1; cancel with n, no, f, false, off or 0.')
        confirm(prompt, failure_prompt)

    if not response_bool:
        raise ParseError(failure_prompt)
コード例 #4
0
ファイル: utils.py プロジェクト: rehive/tesserarius
def get_gcloud_wide_flags(config_dict, allow_type=True):
    """
    Fetches the project information
    """
    cluster_type = ""
    if allow_type:
        try:
            cluster_type = '--zone {}'.format(config_dict['gcloud']['zone'])
        except KeyError:
            try:
                cluster_type = '--region {}'.format(
                    config_dict['gcloud']['region'])
            except KeyError:
                raise ParseError(
                    "Couldn't load zonal or regional cluster info")

    return " --project {project} {cluster_type}".format(
        project=config_dict['gcloud']['project'], cluster_type=cluster_type)
コード例 #5
0
def bind(ctx):
    """Add IAM policy binding in rehive-services"""
    settings_dict = get_settings()

    bindings_list = settings_dict["extensions"]["bindings"]

    try:
        for bindings in bindings_list:
            role = "--role=" + bindings["role"]
            members = " ".join(["--member=" + m for m in bindings["members"]])

            command = "gcloud projects add-iam-policy-binding rehive-services "\
                " {members_args} {role_arg}"

            ctx.run(command.format(members_args=members, role_arg=role),
                    echo=True)
    except KeyError:
        raise ParseError("Invalid fields")
コード例 #6
0
def lint(ctx, threshold="error"):
    """
    Run PyLint to check the package and test code.

    By default runs at an "error" threshold, but can be more stringent. The
    checks on the test code are fixed at the "error" threshold. PyLint is not
    perfect, occasionally you'll need to add a # pylint: disable=some-flag to
    lines that misbehave (see __main__.py)
    """
    try:
        exit_mask = LINT_MASKS[threshold]
    except KeyError:
        raise ParseError(
            "Unkown threshold flag {!r}. Must be one of {}".format(
                threshold, list(LINT_MASKS)))

    result = ctx.run(f"pylint {PACKAGE}", warn=True, pty=True)
    if result.return_code & exit_mask:
        raise Exit(f"PyLint returned with {threshold} bits or higher set")

    ctx.run(f"pylint -E {TEST_PACKAGE}")
コード例 #7
0
def bump(c, part, commit=True, tag=True, deploy=True):
    """Bump the project version and optionally deploy the package."""
    current_version = dysco.__version__
    major, minor, patch = map(int, current_version.split('.'))
    if part == 'major':
        major += 1
        minor = 0
        patch = 0
    elif part == 'minor':
        minor += 1
        patch = 0
    elif part == 'patch':
        patch += 1
    else:
        raise ParseError(
            f'The "part" argument must be one of major, minor, or patch.')
    new_version = f'{major}.{minor}.{patch}'

    # Check that the project is clean.
    result = c.run('git diff --name-only')
    changed_file_count = len(result.stdout.split('\n')) - 1
    if changed_file_count > 0:
        raise Exit(
            f'All files tracked in git must be clean, {changed_file_count} were dirty.'
        )

    # Update the version in `__init__.py`.
    path = os.path.join(root_directory, 'dysco', '__init__.py')
    with open(path, 'r') as f:
        lines = f.readlines()
    lines = [
        re.sub(f'__version__ = \'{current_version}\'',
               f'__version__ = \'{new_version}\'', line) for line in lines
    ]
    with open(path, 'w') as f:
        f.write(''.join(lines))

    # Update the version in `pyproject.toml`.
    path = os.path.join(root_directory, 'pyproject.toml')
    with open(path, 'r') as f:
        lines = f.readlines()
    lines = [
        re.sub(f'version = "{current_version}"', f'version = "{new_version}"',
               line) for line in lines
    ]
    with open(path, 'w') as f:
        f.write(''.join(lines))

    # Commit the changes.
    if commit or tag or deploy:
        c.run('git add pyproject.toml', echo=True)
        c.run('git add dysco/__init__.py', echo=True)
        c.run(f'git commit -m "Bump the project version to v{new_version}."',
              echo=True)
        c.run('git push', echo=True)

    # Tag the version.
    if tag or deploy:
        c.run(f'git tag -a v{new_version}', echo=True, pty=True)
        c.run('git push --tags')

    if deploy:
        c.run('poetry publish --build', echo=True, pty=True)