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

Feature/blob disk support #122

Closed
wants to merge 1 commit into from
Closed
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
90 changes: 57 additions & 33 deletions src/image-copy/azext_imagecopy/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import datetime

from multiprocessing import Pool

from azext_imagecopy.cli_utils import run_cli_command, prepare_cli_command
Expand All @@ -23,34 +25,55 @@ def imagecopy(source_resource_group_name, source_object_name, target_location,
'--resource-group', source_resource_group_name])

json_cmd_output = run_cli_command(cli_cmd, return_as_json=True)

source_os_disk_id = json_cmd_output['storageProfile']['osDisk']['managedDisk']['id']
source_os_disk_snapshot_url = ''
source_os_disk_snapshot_name = source_object_name + '_os_disk_snapshot'
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)

# create source snapshots
logger.warn("Creating source snapshot")
source_os_disk_snapshot_name = source_object_name + '_os_disk_snapshot'
cli_cmd = prepare_cli_command(['snapshot', 'create',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name,
'--source', source_os_disk_id])
is_blob_disk = json_cmd_output['storageProfile']['osDisk']['managedDisk'] is None

run_cli_command(cli_cmd)
if not is_blob_disk:
source_os_disk_id = json_cmd_output['storageProfile']['osDisk']['managedDisk']['id']
logger.debug("source_os_disk_id: %s. source_os_type: %s", source_os_disk_id, source_os_type)

# create source snapshots
logger.warn("Creating source snapshot")
cli_cmd = prepare_cli_command(['snapshot', 'create',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name,
'--source', source_os_disk_id])

run_cli_command(cli_cmd)

# Get SAS URL for the snapshotName
logger.warn("Getting sas url for the source snapshot")
cli_cmd = prepare_cli_command(['snapshot', 'grant-access',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name,
'--duration-in-seconds', '3600'])
# Get SAS URL for the snapshotName
logger.warn("Getting sas url for the source snapshot")
cli_cmd = prepare_cli_command(['snapshot', 'grant-access',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name,
'--duration-in-seconds', '3600'])

json_output = run_cli_command(cli_cmd, return_as_json=True)
json_output = run_cli_command(cli_cmd, return_as_json=True)

source_os_disk_snapshot_url = json_output['accessSas']
logger.debug("source os disk snapshot url: %s",
source_os_disk_snapshot_url)
source_os_disk_snapshot_url = json_output['accessSas']
logger.debug("source os disk snapshot url: %s", source_os_disk_snapshot_url)
else:
logger.warn("Getting sas url for the source blob")
blob_uri = json_cmd_output['storageProfile']['osDisk']['blobUri'].replace('https://', '')
logger.debug("source blob uri: %s" % blob_uri)
blob_account = blob_uri.split('.', 1)[0]
blob_uri_components = blob_uri.split('/',)
blob_container = blob_uri_components[1]
blob_name = blob_uri_components[2]
expiry_date = ((datetime.datetime.utcnow() + datetime.timedelta(days=1))
.isoformat().split('.', 1)[0]+'Z')
cli_cmd = prepare_cli_command(['storage', 'blob', 'generate-sas',
'--https-only',
'--expiry', expiry_date,
'--permissions=r',
'--account-name', blob_account,
'--container-name', blob_container,
'--name', blob_name])
sas = run_cli_command(cli_cmd, return_as_json=False).strip().replace('"', '')
source_os_disk_snapshot_url = 'https://%s?%s' % (blob_uri, sas)

# Start processing in the target locations

Expand Down Expand Up @@ -107,17 +130,18 @@ def imagecopy(source_resource_group_name, source_object_name, target_location,
'--name', transient_resource_group_name])
run_cli_command(cli_cmd)

# Revoke sas for source snapshot
cli_cmd = prepare_cli_command(['snapshot', 'revoke-access',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name])
run_cli_command(cli_cmd)

# Delete source snapshot
cli_cmd = prepare_cli_command(['snapshot', 'delete',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name])
run_cli_command(cli_cmd)
if not is_blob_disk:
# Revoke sas for source snapshot
cli_cmd = prepare_cli_command(['snapshot', 'revoke-access',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name])
run_cli_command(cli_cmd)

# Delete source snapshot
cli_cmd = prepare_cli_command(['snapshot', 'delete',
'--name', source_os_disk_snapshot_name,
'--resource-group', source_resource_group_name])
run_cli_command(cli_cmd)


def create_resource_group(resource_group_name, location):
Expand Down