Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nutanix AHV の VM から、イメージサービスに Disk イメージを登録。
import sys
import json
import requests
import datetime
args = sys.argv
conf_file = args[1]
vm_uuid = args[2]
def gen_image_config(img_name, dev_bus, dev_index, vmdisk_uuid):
image_config = {
'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 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/'
ts = datetime.datetime.now().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 = img_vm_name + '_' + img_disk_label + '_' + ts
image_config = gen_image_config(
img_name, 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()
print('---')
print('Create Image: ' + img_name)
print('Task UUID : ' + task_info['task_uuid'])
print(json.dumps(image_config, indent=2))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment