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

[Compute] image builder create: add --image-template; image template: rename template to builder #11865

Merged
merged 26 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c8de859
Add test for latest profile
qwordy Jan 6, 2020
428a4f2
Merge branch 'dev' of https://github.com/Azure/azure-cli into imagebu…
qwordy Jan 7, 2020
6735e4f
Merge branch 'dev' of https://github.com/Azure/azure-cli into imagebu…
qwordy Jan 13, 2020
95c68a3
Merge branch 'dev' of https://github.com/Azure/azure-cli into imagebu…
qwordy Jan 15, 2020
448b7b3
[Compute] image template create: add --customize and --distribute
qwordy Jan 15, 2020
782c901
Merge branch 'dev' of https://github.com/Azure/azure-cli into imagebu…
qwordy Jan 15, 2020
67d1b0c
Add history
qwordy Jan 15, 2020
e52f447
Fix style
qwordy Jan 15, 2020
01efbdb
Update test
qwordy Jan 16, 2020
07c00c0
merge
qwordy Feb 28, 2020
a4aa547
rename template to builder
qwordy Mar 1, 2020
57198e5
update help
qwordy Mar 1, 2020
815a91d
add image_template
qwordy Mar 4, 2020
8f8a648
Merge branch 'dev' of https://github.com/Azure/azure-cli into imagebu…
qwordy Mar 4, 2020
29aba58
--image-template
qwordy Mar 4, 2020
50c79dc
Merge branch 'dev' of https://github.com/Azure/azure-cli into imagebu…
qwordy Mar 5, 2020
35fd323
test
qwordy Mar 5, 2020
ef13ba5
remove --customize and --distribute
qwordy Mar 5, 2020
3bd036e
try-catch json error
qwordy Mar 5, 2020
ba7181c
help
qwordy Mar 5, 2020
0ed0d23
fix style
qwordy Mar 5, 2020
785855a
fix a bug; update help
qwordy Mar 5, 2020
c88fda4
test of local file
qwordy Mar 5, 2020
7f9aaf6
Add example
qwordy Mar 5, 2020
8c1b812
error handle
qwordy Mar 5, 2020
b7ce361
help
qwordy Mar 6, 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
92 changes: 48 additions & 44 deletions src/azure-cli/azure/cli/command_modules/vm/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@
short-summary: List custom VM images.
"""

helps['image template'] = """
helps['image builder'] = """
type: group
short-summary: Manage and build image builder templates.
"""

helps['image template create'] = """
helps['image builder create'] = """
type: command
short-summary: Create an image builder template.
parameters:
Expand All @@ -180,10 +180,14 @@
scripts="https://my-script-url.net/customize_script.sh"
imagesource="Canonical:UbuntuServer:18.04-LTS:18.04.201903060"

az image template create --image-source $imagesource -n mytemplate -g my-group \\
az image builder create --image-source $imagesource -n mytemplate -g my-group \\
--scripts $scripts --managed-image-destinations image_1=westus \\
--shared-image-destinations my_shared_gallery/linux_image_def=westus,brazilsouth

- name: Create an image builder template using an image template file.
text: |
az image builder create -g my-group -n mytemplate --image-template filename

- name: >
[Advanced] Create an image template with multiple customizers and distributors using the CLI's object cache via --defer. Supports features such as: customizer and output names, powershell exit codes, inline scripts, windows restart, file customizers, artifact tags and vhd output distributors.
text: |
Expand All @@ -192,171 +196,171 @@

# create and update template object in local cli cache. Defers put request to ARM
# Cache object ttl set via az configure.
az image template create --image-source $imagesource -n mytemplate \\
az image builder create --image-source $imagesource -n mytemplate \\
-g my-group --scripts $script --defer

# add customizers
az image template customizer add -n mytemplate -g my-group \\
az image builder customizer add -n mytemplate -g my-group \\
--customizer-name my-pwsh-script --exit-codes 0 1 --inline-script \\
"mkdir c:\\buildActions" "echo Azure-Image-Builder-Was-Here \\
> c:\\buildActions\\Output.txt" --type powershell --defer

az image template customizer add -n mytemplate -g my-group \\
az image builder customizer add -n mytemplate -g my-group \\
--customizer-name my-file-customizer --type file \\
--file-source "https://my-file-source.net/file.txt" \\
--dest-path "c:\\buildArtifacts\\file.txt" --defer

# add distributors
az image template output add -n mytemplate -g my-group --is-vhd \\
az image builder output add -n mytemplate -g my-group --is-vhd \\
--output-name my-win-image-vhd --artifact-tags "is_vhd=True" --defer

az image template output add -n mytemplate -g my-group \\
az image builder output add -n mytemplate -g my-group \\
--output-name my-win-image-managed --managed-image winImage \\
--managed-image-location eastus \\
--artifact-tags "is_vhd=False" --defer

# Stop deferring put request to ARM. Create the template from the object cache.
# Cache object will be deleted.
az image template update -n mytemplate -g my-group
az image builder update -n mytemplate -g my-group
"""

helps['image template customizer'] = """
helps['image builder customizer'] = """
type: group
short-summary: Manage image builder template customizers.
"""

helps['image template customizer add'] = """
helps['image builder customizer add'] = """
type: command
short-summary: Add an image builder customizer to an image builder template.
long-summary: Must be used with --defer
examples:
- name: Add an inline shell customizer to an image template in the cli object cache
text: |
az image template customizer add -n mytemplate -g my-group \\
az image builder customizer add -n mytemplate -g my-group \\
--inline-script "sudo mkdir /buildArtifacts" \\
"sudo cp /tmp/index.html /buildArtifacts/index.html" \\
--customizer-name shell-script-inline --type shell --defer

- name: Add a file customizer to an image template in the cli object cache
text: |
az image template customizer add -n mytemplate -g my-group \\
az image builder customizer add -n mytemplate -g my-group \\
--customizer-name my-file --type file \\
--file-source "https://my-remote-file.html" --dest-path "/tmp/index.html" --defer

- name: Add a windows restart customizer to an image template in the cli object cache
text: |
az image template customizer add -n mytemplate -g my-group \\
az image builder customizer add -n mytemplate -g my-group \\
--customizer-name shell-script-url \\
--restart-check-command "echo Azure-Image-Builder-Restarted-the-VM > \\
c:\\buildArtifacts\\restart.txt" \\
--type windows-restart --restart-timeout 10m --defer

"""

helps['image template customizer clear'] = """
helps['image builder customizer clear'] = """
type: command
short-summary: Remove all image builder customizers from an image builder template.
long-summary: Must be used with --defer
"""

helps['image template customizer remove'] = """
helps['image builder customizer remove'] = """
type: command
short-summary: Remove an image builder customizer from an image builder template.
long-summary: Must be used with --defer
"""

helps['image template delete'] = """
helps['image builder delete'] = """
type: command
short-summary: Delete image builder template.
"""

helps['image template list'] = """
helps['image builder list'] = """
type: command
short-summary: List image builder templates.
"""

helps['image template output'] = """
helps['image builder output'] = """
type: group
short-summary: Manage image builder template output distributors.
long-summary: >
A customized image can be distributed as a managed image,
a shared image in a shared image gallery (SIG), or as a VHD blob.
"""

helps['image template output add'] = """
helps['image builder output add'] = """
type: command
short-summary: Add an image builder output distributor to an image builder template.
long-summary: Must be used with --defer. The output distributor can be a managed image, a gallery image, or as a VHD blob.
examples:
- name: Add a managed image distributor to an image template in the cli object cache. Specify a run output name.
text: |
az image template output add -n mytemplate -g my-group \\
az image builder output add -n mytemplate -g my-group \\
--managed-image my_desired_image_name --output-name managed_image_run_01 --defer

- name: Add a shared image gallery distributor to an image template in the cli object cache. Specify its replication regions.
text: |
az image template output add -n mytemplate -g my-group --gallery-name my_shared_gallery \\
az image builder output add -n mytemplate -g my-group --gallery-name my_shared_gallery \\
--gallery-replication-regions westus brazilsouth \\
--gallery-image-definition linux_image_def --defer

- name: Add a VHD distributor to an image template in the cli object cache.
text: |
az image template output add -n mytemplate -g my-group \\
az image builder output add -n mytemplate -g my-group \\
--output-name my_vhd_image --is-vhd --defer

"""

helps['image template output clear'] = """
helps['image builder output clear'] = """
type: command
short-summary: Remove all image builder output distributors from an image builder template.
long-summary: Must be used with --defer
"""

helps['image template output remove'] = """
helps['image builder output remove'] = """
type: command
short-summary: Remove an image builder output distributor from an image builder template.
long-summary: Must be used with --defer
"""

helps['image template run'] = """
helps['image builder run'] = """
type: command
short-summary: Build an image builder template.
examples:
- name: Start a template build run and then wait for it to finish.
text: |
az image template run -n mytemplate -g my-group --no-wait
az image builder run -n mytemplate -g my-group --no-wait

az image template wait -n mytemplate -g aibmdi \\
az image builder wait -n mytemplate -g aibmdi \\
--custom "lastRunStatus.runState!='running'"

az image template show -n mytemplate -g my-group
az image builder show -n mytemplate -g my-group
"""

helps['image template show'] = """
helps['image builder show'] = """
type: command
short-summary: Show an image builder template.
examples:
- name: Show an image builder template (autogenerated)
text: |
az image template show --name mytemplate --resource-group my-group
az image builder show --name mytemplate --resource-group my-group
crafted: true
"""

helps['image template show-runs'] = """
helps['image builder show-runs'] = """
type: command
short-summary: Show an image builder template's run outputs.
examples:
- name: Run a template build run and then view its run outputs.
text: |
az image template run -n mytemplate -g my-group --no-wait
az image builder run -n mytemplate -g my-group --no-wait

az image template wait -n mytemplate -g aibmdi \\
az image builder wait -n mytemplate -g aibmdi \\
--custom "lastRunStatus.runState!='running'"

az image template show-runs -n mytemplate -g my-group
az image builder show-runs -n mytemplate -g my-group
"""

helps['image template update'] = """
helps['image builder update'] = """
type: command
short-summary: Update an image builder template.
long-summary: >
Expand All @@ -367,33 +371,33 @@
examples:
- name: |
Create a template resource from a template object in the cli cache.
See "az image template create / output add / customizer add --help" and "az cache -h" for more information
See "az image builder create / output add / customizer add --help" and "az cache -h" for more information
text: |
# create and write template object to local cli cache
az image template create --image-source {image_source} -n mytemplate -g my-group \\
az image builder create --image-source {image_source} -n mytemplate -g my-group \\
--scripts {script} --managed-image-destinations image_1=westus --defer

# add customizers and outputs to local cache template object via az image template output / customizer add
# one can also update cache object properties through generic update options, such as: --set
az image template output add -n mytemplate -g my-group --output-name my-win-image-managed \\
az image builder output add -n mytemplate -g my-group --output-name my-win-image-managed \\
--artifact-tags "is_vhd=False" --managed-image winImage --managed-image-location eastus --defer

# send template create request to azure to create template resource
az image template update -n mytemplate -g my-group
az image builder update -n mytemplate -g my-group
"""

helps['image template wait'] = """
helps['image builder wait'] = """
type: command
short-summary: Place the CLI in a waiting state until a condition of the template is met.
examples:
- name: Start a template build run and then wait for it to finish.
text: |
az image template run -n mytemplate -g my-group --no-wait
az image builder run -n mytemplate -g my-group --no-wait

az image template wait -n mytemplate -g aibmdi \\
az image builder wait -n mytemplate -g aibmdi \\
--custom "lastRunStatus.runState!='running'"

az image template show -n mytemplate -g my-group
az image builder show -n mytemplate -g my-group
"""

helps['image update'] = """
Expand Down
43 changes: 40 additions & 3 deletions src/azure-cli/azure/cli/command_modules/vm/_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

# TODO refactor out _image_builder commands.
# i.e something like image_builder/_client_factory image_builder/commands.py image_builder/_params.py

import os
import re
import json
import traceback
from enum import Enum

import requests

try:
from urllib.parse import urlparse
Expand Down Expand Up @@ -152,6 +155,9 @@ def _validate_location(location, location_names, location_display_names):


def process_image_template_create_namespace(cmd, namespace): # pylint: disable=too-many-locals, too-many-branches, too-many-statements
if namespace.image_template is not None:
return

from azure.cli.core.commands.parameters import get_subscription_locations

source = None
Expand Down Expand Up @@ -370,16 +376,47 @@ def process_img_tmpl_output_add_namespace(cmd, namespace):

# region Custom Commands

def create_image_template( # pylint: disable=too-many-locals
def create_image_template( # pylint: disable=too-many-locals, too-many-branches, too-many-statements
cmd, client, resource_group_name, image_template_name, location=None,
source_dict=None, scripts_list=None, destinations_lists=None, build_timeout=None, tags=None,
source=None, scripts=None, checksum=None, managed_image_destinations=None, # pylint: disable=unused-argument
shared_image_destinations=None, no_wait=False): # pylint: disable=unused-argument, too-many-locals
shared_image_destinations=None, no_wait=False, image_template=None): # pylint: disable=unused-argument, too-many-locals
from azure.mgmt.imagebuilder.models import (ImageTemplate, ImageTemplateSharedImageVersionSource,
ImageTemplatePlatformImageSource, ImageTemplateIsoSource, ImageTemplateManagedImageSource, # pylint: disable=line-too-long
ImageTemplateShellCustomizer, ImageTemplatePowerShellCustomizer,
ImageTemplateManagedImageDistributor, ImageTemplateSharedImageDistributor) # pylint: disable=line-too-long

if image_template is not None:
if os.path.exists(image_template):
Copy link
Member

Choose a reason for hiding this comment

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

This only supports absolute path? Can you add an example in help?

Copy link
Member Author

Choose a reason for hiding this comment

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

It supports both absolute path and relative path.
Added.

# Local file
with open(image_template) as f:
content = f.read()
Copy link
Member

Choose a reason for hiding this comment

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

why local file content has no json schema validation?

Copy link
Contributor

Choose a reason for hiding this comment

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

compare to URL logic, seems it should be content = json.loads(f.read()) here ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I should reduce indent. Fixed.

else:
# It should be an URL
msg = '\nusage error: --image-template is not a correct local path or URL'
try:
r = requests.get(image_template)
except Exception:
raise CLIError(traceback.format_exc() + msg)
if r.status_code != 200:
raise CLIError(traceback.format_exc() + msg)
content = r.content

try:
obj = json.loads(content)
except json.JSONDecodeError:
raise CLIError(traceback.format_exc() +
'\nusage error: Content of --image-template is not a valid JSON string')
content = {}
if 'properties' in obj:
content = obj['properties']
if 'location' in obj:
content['location'] = obj['location']
if 'tags' in obj:
content['tags'] = obj['tags']
return client.virtual_machine_image_templates.create_or_update(
yungezz marked this conversation as resolved.
Show resolved Hide resolved
parameters=content, resource_group_name=resource_group_name, image_template_name=image_template_name)

template_source, template_scripts, template_destinations = None, [], []

# create image template source settings
Expand Down
Loading