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

Image-Copy: add support for sources backup by vhd files and snapshots #201

Merged
merged 15 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion src/image-copy/azext_imagecopy/create_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def create_target_image(location, transient_resource_group_name, source_type, so
json_output = run_cli_command(cli_cmd, return_as_json=True)

target_storage_account_key = json_output[0]['value']
logger.debug(target_storage_account_key)
logger.debug("storage account key: %s", target_storage_account_key)

expiry_format = "%Y-%m-%dT%H:%MZ"
expiry = datetime.datetime.utcnow() + datetime.timedelta(hours=1)
Expand Down
51 changes: 41 additions & 10 deletions src/image-copy/azext_imagecopy/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,47 @@ def imagecopy(source_resource_group_name, source_object_name, target_location,

json_cmd_output = run_cli_command(cli_cmd, return_as_json=True)

if 'id' not in json_cmd_output['storageProfile']['osDisk']['managedDisk']:
logger.error(
"It looks like the source resource isn't backed by a managed OS disk. Quitting...")
raise CLIError('Source with no Managed OS disk')

if json_cmd_output['storageProfile']['dataDisks']:
logger.warn("Data disks in the source detected, but are ignored by this extension!")
logger.warn(
"Data disks in the source detected, but are ignored by this extension!")

source_os_disk_id = None
source_os_disk_type = None

try:
source_os_disk_id = json_cmd_output['storageProfile']['osDisk']['managedDisk']['id']
if source_os_disk_id is None:
raise TypeError
source_os_disk_type = "DISK"
logger.debug("found %s: %s", source_os_disk_type, source_os_disk_id)
except TypeError:
try:
source_os_disk_id = json_cmd_output['storageProfile']['osDisk']['blobUri']
if source_os_disk_id is None:
raise TypeError
source_os_disk_type = "BLOB"
logger.debug("found %s: %s", source_os_disk_type, source_os_disk_id)
except TypeError:
try: # images created by e.g. image-copy extension
source_os_disk_id = json_cmd_output['storageProfile']['osDisk']['snapshot']['id']
if source_os_disk_id is None:
raise TypeError
source_os_disk_type = "SNAPSHOT"
logger.debug("found %s: %s", source_os_disk_type, source_os_disk_id)
except TypeError:
pass

if source_os_disk_type is None or source_os_disk_id is None:
logger.error(
'Unable to locate a supported os disk type in the provided source object')
raise CLIError('Invalid OS Disk Source Type')

source_os_disk_id = json_cmd_output['storageProfile']['osDisk']['managedDisk']['id']
source_os_type = json_cmd_output['storageProfile']['osDisk']['osType']
logger.debug("source_os_disk_id: %s. source_os_type: %s",
source_os_disk_id, source_os_type)
logger.debug("source_os_disk_type: %s. source_os_disk_id: %s. source_os_type: %s",
source_os_disk_type, source_os_disk_id, source_os_type)

# create source snapshots
# TODO: skip creating another snapshot when the source is a snapshot
logger.warn("Creating source snapshot")
source_os_disk_snapshot_name = source_object_name + '_os_disk_snapshot'
cli_cmd = prepare_cli_command(['snapshot', 'create',
Expand All @@ -65,7 +92,10 @@ def imagecopy(source_resource_group_name, source_object_name, target_location,
# Start processing in the target locations

transient_resource_group_name = 'image-copy-rg'
create_resource_group(transient_resource_group_name, 'eastus')
# pick the first location for the temp group
transient_resource_group_location = target_location[0].strip()
create_resource_group(transient_resource_group_name,
transient_resource_group_location)

target_locations_count = len(target_location)
logger.warn("Target location count: %s", target_locations_count)
Expand Down Expand Up @@ -125,6 +155,7 @@ def imagecopy(source_resource_group_name, source_object_name, target_location,
run_cli_command(cli_cmd)

# Delete source snapshot
# TODO: skip this if source is snapshot and not creating a new one
cli_cmd = prepare_cli_command(['snapshot', 'delete',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name])
Expand Down
2 changes: 1 addition & 1 deletion src/image-copy/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from codecs import open
from setuptools import setup, find_packages

VERSION = "0.0.6"
VERSION = "0.0.7"

CLASSIFIERS = [
'Development Status :: 4 - Beta',
Expand Down
44 changes: 44 additions & 0 deletions src/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,50 @@
"summary": "Support for copying managed vm images between regions",
"version": "0.0.6"
}
},
{
"filename": "image_copy_extension-0.0.7-py2.py3-none-any.whl",
"sha256Digest": "01a055c909a68f8990d8bab544b20a783c3fbe4d006947887ea9f253f358519e",
"downloadUrl": "https://files.pythonhosted.org/packages/2f/1c/71f08dfe9f1af698459384aeb141d7debeb09ecb5d6fcf359486196bc86c/image_copy_extension-0.0.7-py2.py3-none-any.whl",
"metadata": {
"azext.minCliCoreVersion": "2.0.24",
"classifiers": [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"License :: OSI Approved :: MIT License"
],
"extensions": {
"python.details": {
"contacts": [
{
"email": "tamir.kamara@microsoft.com",
"name": "Tamir Kamara",
"role": "author"
}
],
"document_names": {
"description": "DESCRIPTION.rst"
},
"project_urls": {
"Home": "https://github.com/Azure/azure-cli-extensions"
}
}
},
"generator": "bdist_wheel (0.30.0)",
"license": "MIT",
"metadata_version": "2.0",
"name": "image-copy-extension",
"summary": "Support for copying managed vm images between regions",
"version": "0.0.7"
}
}
],
"aem": [
Expand Down