Skip to content

Instantly share code, notes, and snippets.

@gowatana
Last active March 17, 2019 06:32
Show Gist options
  • Save gowatana/02d1a44057d5cce156f41681f1079e4d to your computer and use it in GitHub Desktop.
Save gowatana/02d1a44057d5cce156f41681f1079e4d to your computer and use it in GitHub Desktop.
Nutanix AHV の VM から、イメージサービスに Disk イメージを登録。
import sys
import json
import requests
import datetime
import time
image_create_wait_sec = 2
args = sys.argv
conf_file = args[1]
vm_uuid = args[2]
def gen_image_config(
img_name, img_annotation, dev_bus, dev_index, vmdisk_uuid):
image_config = {
'annotation': img_annotation,
'image_type': 'DISK_IMAGE',
'name': img_name,
'vm_disk_clone_spec': {
'disk_address': {
'device_bus': dev_bus,
'device_index': dev_index,
'vmdisk_uuid': vmdisk_uuid
}
}
}
return image_config
def print_task_to_image(
session, base_url, task_uuid, image_create_wait_sec):
s = session
print('---')
url = base_url + 'tasks/' + task_uuid
task_data = s.get(url, verify=False).json()
image_uuid = task_data['entity_list'][0]['entity_id']
time.sleep(image_create_wait_sec)
url = base_url + 'images/' + image_uuid
image_data = s.get(url, verify=False).json()
#print(json.dumps(image_data, indent=2))
print('image name: ' + image_data['name'])
print('annotation: ' + image_data['annotation'])
print('uuid : ' + image_data['uuid'])
def main():
with open(conf_file, "r") as file:
conf = file.read()
conf = json.loads(conf)
prism_addr = conf["prism_address"]
prism_user = conf["user_name"]
prism_pass = conf["password"]
base_url = 'https://' + prism_addr + ':9440/api/nutanix/v2.0/'
img_timestamp = datetime.datetime.now()
img_timestamp_str = img_timestamp.strftime('%Y%m%d%H%M%S')
img_timestamp_annotation = img_timestamp.strftime('%Y/%m/%d %H:%M:%S')
insecure_warn = requests.packages.urllib3.exceptions.InsecureRequestWarning
requests.packages.urllib3.disable_warnings(insecure_warn)
s = requests.Session()
s.auth = (prism_user, prism_pass)
s.headers.update({'Content-Type': 'application/json; charset=utf-8'})
url = base_url + 'vms/?include_vm_disk_config=true'
data = s.get(url, verify=False).json()
for vm in data['entities']:
if (vm['uuid'] == vm_uuid):
src_vm = vm
img_vm_name = src_vm['name']
img_vm_uuid = src_vm['uuid']
for vdisk in src_vm['vm_disk_info']:
if (vdisk['disk_address']['device_bus'] == 'scsi'):
img_disk_label = vdisk['disk_address']['disk_label']
img_disk_bus = vdisk['disk_address']['device_bus']
img_disk_index = vdisk['disk_address']['device_index']
img_vmdisk_uuid = vdisk['disk_address']['vmdisk_uuid']
img_name = '_'.join([img_vm_name, img_disk_label, img_timestamp_str])
image_config = gen_image_config(
img_name, img_timestamp_annotation, img_disk_bus, img_disk_index, img_vmdisk_uuid)
# Create Disk Image.
url = base_url + 'images/'
task_info = s.post(url, json=image_config, verify=False).json()
task_uuid = task_info['task_uuid']
print_task_to_image(s, base_url, task_uuid, image_create_wait_sec)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment