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 9 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
4 changes: 4 additions & 0 deletions src/azure-cli/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Release History
===============

**Compute**

* image template create: Add --customize and --distribute.
yungezz marked this conversation as resolved.
Show resolved Hide resolved

**IoT Central**

* Support app creation/update with the new sku name ST0, ST1, ST2.
Expand Down
63 changes: 41 additions & 22 deletions src/azure-cli/azure/cli/command_modules/vm/_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# i.e something like image_builder/_client_factory image_builder/commands.py image_builder/_params.py

import re
import json
from json import JSONDecodeError
from enum import Enum


Expand Down Expand Up @@ -374,7 +376,7 @@ def create_image_template( # pylint: disable=too-many-locals
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, customize=None, distribute=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,
Expand All @@ -394,29 +396,46 @@ def create_image_template( # pylint: disable=too-many-locals

# create image template customizer settings
# Script structure can be found in _parse_script's function definition
for script in scripts_list:
script.pop("is_url")
script["script_uri"] = script.pop("script")

if script["type"] == ScriptType.SHELL:
template_scripts.append(ImageTemplateShellCustomizer(**script))
elif script["type"] == ScriptType.POWERSHELL:
template_scripts.append(ImageTemplatePowerShellCustomizer(**script))
else: # Should never happen
logger.debug("Script %s has type %s", script["script"], script["type"])
raise CLIError("Script {} has an invalid type.".format(script["script"]))
if customize is not None:
yungezz marked this conversation as resolved.
Show resolved Hide resolved
if scripts_list:
raise CLIError('usage error: Do not use --scripts and --customize together')
try:
template_scripts = json.loads(customize)
yungezz marked this conversation as resolved.
Show resolved Hide resolved
except JSONDecodeError:
raise CLIError('usage error: JSON decode error in --customize')
Copy link
Member

Choose a reason for hiding this comment

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

show error stacktrace to user

Copy link
Member Author

Choose a reason for hiding this comment

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

Good advice

else:
for script in scripts_list:
script.pop("is_url")
script["script_uri"] = script.pop("script")

if script["type"] == ScriptType.SHELL:
template_scripts.append(ImageTemplateShellCustomizer(**script))
elif script["type"] == ScriptType.POWERSHELL:
template_scripts.append(ImageTemplatePowerShellCustomizer(**script))
else: # Should never happen
logger.debug("Script %s has type %s", script["script"], script["type"])
raise CLIError("Script {} has an invalid type.".format(script["script"]))

# create image template distribution / destination settings
for dest_type, rid, loc_info in destinations_lists:
parsed = parse_resource_id(rid)
if dest_type == _DestType.MANAGED_IMAGE:
template_destinations.append(ImageTemplateManagedImageDistributor(
image_id=rid, location=loc_info, run_output_name=parsed['name']))
elif dest_type == _DestType.SHARED_IMAGE_GALLERY:
template_destinations.append(ImageTemplateSharedImageDistributor(
gallery_image_id=rid, replication_regions=loc_info, run_output_name=parsed['child_name_1']))
else:
logger.info("No applicable destination found for destination %s", str(tuple([dest_type, rid, loc_info])))
if distribute is not None:
if destinations_lists:
raise CLIError('usage error: Do not use (--managed-image-destinations | --shared-image-destinations) and --distribute together')
try:
template_destinations = json.loads(distribute)
yungezz marked this conversation as resolved.
Show resolved Hide resolved
except JSONDecodeError:
raise CLIError('usage error: JSON decode error in --distribute')
Copy link
Member

Choose a reason for hiding this comment

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

pls append error stacktrace to usre


else:
for dest_type, rid, loc_info in destinations_lists:
parsed = parse_resource_id(rid)
Copy link
Member

Choose a reason for hiding this comment

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

is rid validated at above?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. The code leaves the work to server side.

if dest_type == _DestType.MANAGED_IMAGE:
template_destinations.append(ImageTemplateManagedImageDistributor(
image_id=rid, location=loc_info, run_output_name=parsed['name']))
elif dest_type == _DestType.SHARED_IMAGE_GALLERY:
template_destinations.append(ImageTemplateSharedImageDistributor(
gallery_image_id=rid, replication_regions=loc_info, run_output_name=parsed['child_name_1']))
else:
logger.info("No applicable destination found for destination %s", str(tuple([dest_type, rid, loc_info])))

image_template = ImageTemplate(source=template_source, customize=template_scripts, distribute=template_destinations,
location=location, build_timeout_in_minutes=build_timeout, tags=(tags or {}))
Expand Down
2 changes: 2 additions & 0 deletions src/azure-cli/azure/cli/command_modules/vm/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ def load_arguments(self, _):
c.argument('', arg_type=ib_source_type)

# Image Customizer Arguments
c.argument('customize', arg_type=ib_customizer_type, help='Specify the properties used to describe the customization steps of the image. JSON format. Do not use it together with --scripts. Examples: https://github.com/danielsollondon/azvmimagebuilder/tree/master/quickquickstarts')
Copy link
Member

Choose a reason for hiding this comment

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

it could be string, local/remote file path..

c.argument('scripts', arg_type=ib_customizer_type)
c.argument('', arg_type=ib_customizer_type)
c.argument('', arg_type=ib_customizer_type)

# Image Output Arguments
c.argument('distribute', arg_type=ib_cutput_type, help='The distribution targets where the image output needs to go to. JSON format. Do not use it together with other arguments in Ouput Arguments.')
Copy link
Member

Choose a reason for hiding this comment

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

same as above

c.argument('managed_image_destinations', arg_type=ib_cutput_type)
c.argument('shared_image_destinations', arg_type=ib_cutput_type)
c.argument('output_name', arg_type=ib_cutput_type)
Expand Down
39 changes: 39 additions & 0 deletions src/azure-cli/azure/cli/command_modules/vm/tests/latest/cus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
{
"type": "Shell",
"name": "RunScriptFromSource",
"scriptUri": "https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/customizeScript.sh"
},

{
"type": "Shell",
"name": "CheckSumCompareShellScript",
"scriptUri": "https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/customizeScript2.sh",
"sha256Checksum": "ade4c5214c3c675e92c66e2d067a870c5b81b9844b3de3cc72c49ff36425fc93"
},

{
"type": "File",
"name": "downloadBuildArtifacts",
"sourceUri": "https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/exampleArtifacts/buildArtifacts/index.html",
"destination":"/tmp/index.html"
},

{
"type": "Shell",
"name": "setupBuildPath",
"inline": [
"sudo mkdir /buildArtifacts",
"sudo cp /tmp/index.html /buildArtifacts/index.html"
]
},

{
"type": "Shell",
"name": "InstallUpgrades",
"inline": [
"sudo apt install unattended-upgrades"
]
}

]
Loading