Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACR] Add new commands az acr taskrun show/list/delete for show list and remove #11858

Merged
merged 27 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
089c62e
Add TaskRun CLI for show list and remove
huanwu Jan 15, 2020
5e21545
Add taskrun test. Update the output format and history.
huanwu Jan 17, 2020
ab64ea0
Update name
huanwu Jan 17, 2020
dbb10b5
Add final newline
huanwu Jan 17, 2020
55957bf
Update code style to meet CLI style requirements
huanwu Jan 17, 2020
5aa473f
Update style
huanwu Jan 17, 2020
74f4618
Add help for taskrun
huanwu Jan 17, 2020
961c895
Update arguments
huanwu Jan 17, 2020
72b7d80
Add help for acr taskrun
huanwu Jan 17, 2020
f019cc2
Remove white space
huanwu Jan 17, 2020
723c056
update mode in record file
huanwu Jan 17, 2020
f100e1b
Update taskrun record file
huanwu Jan 18, 2020
3dc0fae
Update taskrun record file
huanwu Jan 18, 2020
ec34523
Add acr taskrun logs command. Fix several things based on PR comments.
huanwu Jan 22, 2020
f5b8bda
Fix some style issue. Disable the step to show log because the step c…
huanwu Jan 22, 2020
99cd4ad
Update taskrun_format_group to pick up the right value.
huanwu Jan 22, 2020
365ee98
Update help
huanwu Jan 22, 2020
091182e
fix style error.
huanwu Jan 22, 2020
bddd06d
add indentation
huanwu Jan 22, 2020
e1f5f2c
fix style
huanwu Jan 22, 2020
f37316a
user taskrun_name when to get logs
huanwu Jan 23, 2020
30a14ea
Update help for taskrun logs command update
huanwu Jan 23, 2020
5629f40
update style
huanwu Jan 23, 2020
d05154b
remove run_id from taskrun parameters
huanwu Jan 23, 2020
a9f1a70
update comments
huanwu Jan 23, 2020
37a6d54
Call client.get to get the taskrun
huanwu Jan 24, 2020
f958caa
Trigger notification
huanwu Jan 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/azure-cli/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Release History
**ACR**

* [BREAKING CHANGE] `az acr delete` will prompt
* [BREAKING CHANGE] 'az acr task delete' will prompt
* Add a new command group 'az acr taskrun show/list/delete' for taskrun management

**AppConfig**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def cf_acr_tasks(cli_ctx, *_):
return get_acr_service_client(cli_ctx, VERSION_2019_06_01_PREVIEW).tasks


def cf_acr_taskruns(cli_ctx, *_):
return get_acr_service_client(cli_ctx, VERSION_2019_06_01_PREVIEW).task_runs


def cf_acr_runs(cli_ctx, *_):
return get_acr_service_client(cli_ctx, VERSION_2019_06_01_PREVIEW).runs

Expand Down
2 changes: 2 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
TASK_VALID_VSTS_URLS = ['visualstudio.com', 'dev.azure.com']
TASK_RESOURCE_ID_TEMPLATE = '/subscriptions/{sub_id}/resourceGroups/{rg}/providers/Microsoft.ContainerRegistry/registries/{reg}/tasks/{name}'

TASKRUN_RESOURCE_TYPE = REGISTRY_RESOURCE_TYPE + '/taskruns'

ACR_TASK_YAML_DEFAULT_NAME = 'acb.yaml'

ACR_CACHED_BUILDER_IMAGES = ('cloudfoundry/cnb:bionic',)
Expand Down
20 changes: 19 additions & 1 deletion src/azure-cli/azure/cli/command_modules/acr/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def task_output_format(result):
return _output_format(result, _task_format_group)


def taskrun_output_format(result):
return _output_format(result, _taskrun_format_group)


def build_output_format(result):
return _output_format(result, _build_format_group)

Expand Down Expand Up @@ -184,6 +188,19 @@ def _task_format_group(item):
])


def _taskrun_format_group(item):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huanwu some other properties to add similar to the standard run output would be 'RUN ID', 'STARTED', AND 'DURATION'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do we know if run has started?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The taskrun deployment won't return if run not finish. But for show/list/delete method, why do we care if run started or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duration might be a good thing to support in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaysterp @shahzzzam Thanks! Updated the output and added a test to cover all the commands.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shahzzzam @huanwu duration is caluculated from the startTime and finishTime of the completed task run, see how we calculate this for our current run output run. These sort of metrics may be useful to customers so that they can track when a task was run and how long that run took to finish. Also has continuity with our current output format for runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaysterp @shahzzzam Yes, the duration was added in the format calculated from startTime and finishTime.

return OrderedDict([
('NAME', _get_value(item, 'name')),
('RUN ID', _get_value(item, 'runResult', 'runId')),
('TASK', _get_value(item, 'runResult', 'task')),
('PLATFORM', _get_value(item, 'runResult', 'platform', 'os')),
('STATUS', _get_value(item, 'runResult', 'status')),
('STARTED', _format_datetime(_get_value(item, 'runResult', 'startTime'))),
('DURATION', _get_duration(_get_value(item, 'runResult', 'startTime'),
_get_value(item, 'runResult', 'finishTime')))
])


def _build_format_group(item):
return OrderedDict([
('BUILD ID', _get_value(item, 'buildId')),
Expand All @@ -193,7 +210,8 @@ def _build_format_group(item):
("TRIGGER", _get_build_trigger(_get_value(item, 'imageUpdateTrigger'),
_get_value(item, 'sourceTrigger', 'eventType'))),
('STARTED', _format_datetime(_get_value(item, 'startTime'))),
('DURATION', _get_duration(_get_value(item, 'startTime'), _get_value(item, 'finishTime')))
('DURATION', _get_duration(_get_value(item, 'startTime'),
_get_value(item, 'finishTime')))
])


Expand Down
42 changes: 42 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,48 @@
az acr task update-run -r MyRegistry --run-id runId --no-archive false
"""

helps['acr taskrun'] = """
type: group
short-summary: Manage taskruns using Azure Container Registries.
"""


helps['acr taskrun delete'] = """
type: command
short-summary: Delete a taskrun from an Azure Container Registry.
examples:
- name: Delete a taskrun from an Azure Container Registry.
text: >
az acr taskrun delete -r MyRegistry -n MyTaskRun -g MyResourceGroup
"""

helps['acr taskrun list'] = """
type: command
short-summary: List the taskruns for an Azure Container Registry.
examples:
- name: List taskruns and show the results in a table.
text: >
az acr taskrun list -r MyRegistry -g MyResourceGroup -o table
"""

helps['acr taskrun show'] = """
type: command
short-summary: Get the properties of a named taskrun for an Azure Container Registry.
examples:
- name: Get the properties of a taskrun, displaying the results in a table.
text: >
az acr taskrun show -r MyRegistry -n MyTaskRun -o table
"""

helps['acr taskrun logs'] = """
type: command
short-summary: Show logs for a particular run.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taskrun

examples:
- name: Show logs for a particular run.
text: >
az acr taskrun logs -r MyRegistry --run-id runId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it accept --name of the taskrun?

"""

helps['acr token'] = """
type: group
short-summary: Manage tokens for an Azure Container Registry.
Expand Down
8 changes: 7 additions & 1 deletion src/azure-cli/azure/cli/command_modules/acr/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
REGISTRY_RESOURCE_TYPE,
WEBHOOK_RESOURCE_TYPE,
REPLICATION_RESOURCE_TYPE,
TASK_RESOURCE_TYPE
TASK_RESOURCE_TYPE,
TASKRUN_RESOURCE_TYPE
)
from ._validators import (
validate_headers,
Expand Down Expand Up @@ -236,6 +237,11 @@ def load_arguments(self, _): # pylint: disable=too-many-statements
c.argument('timer_schedule', options_list=['--schedule'], help="The schedule of the timer trigger represented as a cron expression.")
c.argument('enabled', help="Indicates whether the timer trigger is enabled.", arg_type=get_three_state_flag())

with self.argument_context('acr taskrun') as c:
c.argument('registry_name', options_list=['--registry', '-r'])
c.argument('run_id', help='The unique run identifier.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If taskrun logs take taskrun name, I assume you don't need run_id argument.

c.argument('taskrun_name', options_list=['--name', '-n'], help='The name of the taskrun.', completer=get_resource_name_completion_list(TASKRUN_RESOURCE_TYPE))

with self.argument_context('acr helm') as c:
c.argument('resource_group_name', deprecate_info=c.deprecate(hide=True))
c.argument('repository', help=argparse.SUPPRESS)
Expand Down
15 changes: 15 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
replication_output_format,
build_output_format,
task_output_format,
taskrun_output_format,
run_output_format,
helm_list_output_format,
helm_show_output_format,
Expand All @@ -29,6 +30,7 @@
cf_acr_replications,
cf_acr_webhooks,
cf_acr_tasks,
cf_acr_taskruns,
cf_acr_runs,
cf_acr_scope_maps,
cf_acr_tokens,
Expand Down Expand Up @@ -105,6 +107,12 @@ def load_command_table(self, _): # pylint: disable=too-many-statements
client_factory=cf_acr_tasks
)

acr_taskrun_util = CliCommandType(
operations_tmpl='azure.cli.command_modules.acr.taskrun#{}',
table_transformer=taskrun_output_format,
client_factory=cf_acr_taskruns
)

acr_helm_util = CliCommandType(
operations_tmpl='azure.cli.command_modules.acr.helm#{}'
)
Expand Down Expand Up @@ -237,6 +245,13 @@ def load_command_table(self, _): # pylint: disable=too-many-statements
g.command('logs', 'acr_task_logs', client_factory=cf_acr_runs,
table_transformer=None)

with self.command_group('acr taskrun', acr_taskrun_util, is_preview=True) as g:
g.command('list', 'acr_taskrun_list')
g.command('delete', 'acr_taskrun_delete')
g.command('show', 'acr_taskrun_show')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just occurs to me if we can introduce az taskrun logs, it will be very handy. So users don't have to pipe the runid from az taskrun show to az task logs. You may consider in future pr.

g.command('logs', 'acr_taskrun_logs', client_factory=cf_acr_runs,
table_transformer=None)

with self.command_group('acr config content-trust', acr_policy_util) as g:
g.command('show', 'acr_config_content_trust_show')
g.command('update', 'acr_config_content_trust_update')
Expand Down
9 changes: 7 additions & 2 deletions src/azure-cli/azure/cli/command_modules/acr/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# pylint: disable=C0302
import re
from msrest.exceptions import ValidationError
from knack.log import get_logger
Expand All @@ -17,7 +18,8 @@
build_timers_info,
remove_timer_trigger,
get_task_id_from_task_name,
prepare_source_location
prepare_source_location,
user_confirmation
)
from ._stream_utils import stream_logs
from ._constants import (
Expand Down Expand Up @@ -283,9 +285,12 @@ def acr_task_delete(cmd,
client,
task_name,
registry_name,
resource_group_name=None):
resource_group_name=None,
yes=False):
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASK_NOT_SUPPORTED)

user_confirmation("Are you sure you want to delete the task '{}' ".format(task_name), yes)
return client.delete(resource_group_name, registry_name, task_name)


Expand Down
56 changes: 56 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acr/taskrun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from ._stream_utils import stream_logs
from ._utils import (
user_confirmation,
validate_managed_registry
)


TASKRUN_NOT_SUPPORTED = 'TaskRun is only supported for managed registries.'


def acr_taskrun_show(cmd,
client,
taskrun_name,
registry_name,
resource_group_name=None):
_, resource_group_name = validate_managed_registry(
Copy link
Contributor

@Juliehzl Juliehzl Jan 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use validator for you command or argument https://github.com/Azure/azure-cli/blob/dev/doc/authoring_command_modules/authoring_commands.md#validators?
and you can use the function directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Juliehzl current validate_managed_registry also returns the resource group to be used in the subsequent codes. It's also used in other acr commands. So I think it's out of the scope of current pull request. It's better we evaluate the validator option later for acr commands together. /cc: @djyou @yugangw-msft

cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)
return client.get(resource_group_name, registry_name, taskrun_name)


def acr_taskrun_list(cmd,
client,
registry_name,
resource_group_name=None):

_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)
return client.list(resource_group_name, registry_name)


def acr_taskrun_delete(cmd,
client,
taskrun_name,
registry_name,
resource_group_name=None,
yes=False):
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)

user_confirmation("Are you sure you want to delete the taskrun '{}' ".format(taskrun_name), yes)
return client.delete(resource_group_name, registry_name, taskrun_name)


def acr_taskrun_logs(cmd,
client, # cf_acr_runs
registry_name,
run_id=None,
resource_group_name=None):
_, resource_group_name = validate_managed_registry(
cmd, registry_name, resource_group_name, TASKRUN_NOT_SUPPORTED)
return stream_logs(client, run_id, registry_name, resource_group_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function needs to take the taskrun name, get the runid using the name and stream the log using the run id.

Loading