Beispiel #1
0
def acr_build(cmd,
              client,
              registry_name,
              source_location,
              image_names=None,
              resource_group_name=None,
              timeout=None,
              arg=None,
              secret_arg=None,
              docker_file_path='',
              no_format=False,
              no_push=False,
              no_logs=False,
              os_type=OS.linux.value):
    _, resource_group_name = validate_managed_registry(cmd.cli_ctx,
                                                       registry_name,
                                                       resource_group_name,
                                                       BUILD_NOT_SUPPORTED)

    from ._client_factory import cf_acr_registries
    client_registries = cf_acr_registries(cmd.cli_ctx)

    if os.path.exists(source_location):
        if not os.path.isdir(source_location):
            raise CLIError(
                "Source location should be a local directory path or remote URL."
            )

        # NOTE: If docker_file_path is not specified, the default is Dockerfile in source_location.
        # Otherwise, it's based on current working directory.
        if not docker_file_path:
            docker_file_path = os.path.join(source_location, "Dockerfile")
            logger.info("'--file or -f' is not provided. '%s' is used.",
                        docker_file_path)

        _check_local_docker_file(docker_file_path)

        tar_file_path = os.path.join(
            tempfile.gettempdir(),
            'build_archive_{}.tar.gz'.format(uuid.uuid4().hex))

        try:
            # NOTE: os.path.basename is unable to parse "\" in the file path
            original_docker_file_name = os.path.basename(
                docker_file_path.replace("\\", "/"))
            docker_file_in_tar = '{}_{}'.format(uuid.uuid4().hex,
                                                original_docker_file_name)

            source_location = upload_source_code(
                client_registries, registry_name, resource_group_name,
                source_location, tar_file_path, docker_file_path,
                docker_file_in_tar)
            # For local source, the docker file is added separately into tar as the new file name (docker_file_in_tar)
            # So we need to update the docker_file_path
            docker_file_path = docker_file_in_tar
        except Exception as err:
            raise CLIError(err)
        finally:
            try:
                logger.debug("Deleting the archived source code from '%s'...",
                             tar_file_path)
                os.remove(tar_file_path)
            except OSError:
                pass
    else:
        # NOTE: If docker_file_path is not specified, the default is Dockerfile. It's the same as docker build command.
        if not docker_file_path:
            docker_file_path = "Dockerfile"
            logger.info("'--file or -f' is not provided. '%s' is used.",
                        docker_file_path)

        source_location = check_remote_source_code(source_location)
        logger.warning("Sending context to registry: %s...", registry_name)

    if no_push:
        is_push_enabled = False
    else:
        if image_names:
            is_push_enabled = True
            _warn_unsupported_image_name(image_names)
        else:
            is_push_enabled = False
            logger.warning(
                "'--image or -t' is not provided. Skipping image push after build."
            )

    docker_build_request = DockerBuildRequest(
        image_names=image_names,
        is_push_enabled=is_push_enabled,
        source_location=source_location,
        platform=PlatformProperties(os=os_type,
                                    architecture=Architecture.amd64.value),
        docker_file_path=docker_file_path,
        timeout=timeout,
        arguments=(arg if arg else []) + (secret_arg if secret_arg else []))

    queued_build = LongRunningOperation(cmd.cli_ctx)(
        client_registries.schedule_run(resource_group_name=resource_group_name,
                                       registry_name=registry_name,
                                       run_request=docker_build_request))

    run_id = queued_build.run_id
    logger.warning("Queued a build with ID: %s", run_id)
    logger.warning("Waiting for agent...")

    if no_logs:
        return get_run_with_polling(client, run_id, registry_name,
                                    resource_group_name)

    return stream_logs(client, run_id, registry_name, resource_group_name,
                       no_format, True)
Beispiel #2
0
def acr_task_create(cmd,  # pylint: disable=too-many-locals
                    client,
                    task_name,
                    registry_name,
                    context_path,
                    file,
                    git_access_token=None,
                    image_names=None,
                    status='Enabled',
                    os_type=OS.linux.value,
                    cpu=DEFAULT_CPU,
                    timeout=DEFAULT_TIMEOUT_IN_SEC,
                    values=None,
                    source_trigger_name='defaultSourceTriggerName',
                    commit_trigger_enabled=True,
                    pull_request_trigger_enabled=True,
                    branch='master',
                    no_push=False,
                    no_cache=False,
                    arg=None,
                    secret_arg=None,
                    set_value=None,
                    set_secret=None,
                    base_image_trigger_name='defaultBaseimageTriggerName',
                    base_image_trigger_enabled=True,
                    base_image_trigger_type='Runtime',
                    resource_group_name=None):
    if (commit_trigger_enabled or pull_request_trigger_enabled) and not git_access_token:
        raise CLIError("If source control trigger is enabled [--commit-trigger-enabled] or "
                       "[--pull-request-trigger-enabled] --git-access-token must be provided.")

    if file.endswith(ALLOWED_TASK_FILE_TYPES):
        step = FileTaskStep(
            task_file_path=file,
            values_file_path=values,
            context_path=context_path,
            context_access_token=git_access_token,
            values=(set_value if set_value else []) + (set_secret if set_secret else [])
        )
    else:
        step = DockerBuildStep(
            image_names=image_names,
            is_push_enabled=not no_push,
            no_cache=no_cache,
            docker_file_path=file,
            arguments=(arg if arg else []) + (secret_arg if secret_arg else []),
            context_path=context_path,
            context_access_token=git_access_token
        )

    registry, resource_group_name = validate_managed_registry(
        cmd.cli_ctx, registry_name, resource_group_name, TASK_NOT_SUPPORTED)

    source_control_type = SourceControlType.visual_studio_team_service.value
    if context_path is not None and 'GITHUB.COM' in context_path.upper():
        source_control_type = SourceControlType.github.value

    source_triggers = None
    source_trigger_events = []
    if commit_trigger_enabled:
        source_trigger_events.append(SourceTriggerEvent.commit.value)
    if pull_request_trigger_enabled:
        source_trigger_events.append(SourceTriggerEvent.pullrequest.value)
    # if source_trigger_events contains any event types we assume they are enabled
    if source_trigger_events:
        source_triggers = [
            SourceTrigger(
                source_repository=SourceProperties(
                    source_control_type=source_control_type,
                    repository_url=context_path,
                    branch=branch,
                    source_control_auth_properties=AuthInfo(
                        token=git_access_token,
                        token_type=DEFAULT_TOKEN_TYPE,
                        scope='repo'
                    )
                ),
                source_trigger_events=source_trigger_events,
                status=TriggerStatus.enabled.value,
                name=source_trigger_name
            )
        ]

    base_image_trigger = None
    if base_image_trigger_enabled:
        base_image_trigger = BaseImageTrigger(
            base_image_trigger_type=base_image_trigger_type,
            status=TriggerStatus.enabled.value if base_image_trigger_enabled else TriggerStatus.disabled.value,
            name=base_image_trigger_name
        )

    task_create_parameters = Task(
        location=registry.location,
        step=step,
        platform=PlatformProperties(
            os=os_type,
            architecture=Architecture.amd64.value
        ),
        status=status,
        timeout=timeout,
        agent_configuration=AgentProperties(
            cpu=cpu
        ),
        trigger=TriggerProperties(
            source_triggers=source_triggers,
            base_image_trigger=base_image_trigger
        )
    )

    try:
        return client.create(resource_group_name=resource_group_name,
                             registry_name=registry_name,
                             task_name=task_name,
                             task_create_parameters=task_create_parameters)
    except ValidationError as e:
        raise CLIError(e)
Beispiel #3
0
def acr_run(cmd,
            client,
            registry_name,
            source_location,
            file='acb.yaml',
            values=None,
            set_value=None,
            no_format=False,
            no_logs=False,
            no_wait=False,
            timeout=None,
            resource_group_name=None,
            os_type=OS.linux.value):

    _, resource_group_name = validate_managed_registry(cmd.cli_ctx,
                                                       registry_name,
                                                       resource_group_name,
                                                       RUN_NOT_SUPPORTED)

    client_registries = cf_acr_registries(cmd.cli_ctx)

    if os.path.exists(source_location):
        if not os.path.isdir(source_location):
            raise CLIError(
                "Source location should be a local directory path or remote URL."
            )

        tar_file_path = os.path.join(
            tempfile.gettempdir(),
            'run_archive_{}.tar.gz'.format(uuid.uuid4().hex))

        try:
            source_location = upload_source_code(client_registries,
                                                 registry_name,
                                                 resource_group_name,
                                                 source_location,
                                                 tar_file_path, "", "")
        except Exception as err:
            raise CLIError(err)
        finally:
            try:
                logger.debug("Deleting the archived source code from '%s'...",
                             tar_file_path)
                os.remove(tar_file_path)
            except OSError:
                pass
    else:
        source_location = check_remote_source_code(source_location)
        logger.warning("Sending context to registry: %s...", registry_name)

    request = FileTaskRunRequest(task_file_path=file,
                                 values_file_path=values,
                                 values=(set_value if set_value else []),
                                 source_location=source_location,
                                 timeout=timeout,
                                 platform=PlatformProperties(os=os_type))

    queued = LongRunningOperation(cmd.cli_ctx)(client_registries.schedule_run(
        resource_group_name=resource_group_name,
        registry_name=registry_name,
        run_request=request))

    run_id = queued.run_id
    logger.warning("Queued a run with ID: %s", run_id)

    if no_wait:
        return queued

    logger.warning("Waiting for an agent...")

    if no_logs:
        return get_run_with_polling(client, run_id, registry_name,
                                    resource_group_name)

    return stream_logs(client, run_id, registry_name, resource_group_name,
                       no_format, True)