-
Notifications
You must be signed in to change notification settings - Fork 3
/
cbt_vm_metadata_export.py
58 lines (45 loc) · 1.82 KB
/
cbt_vm_metadata_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
"""
For a given vm this script will export the metadata not including the vm
snapshot data.
example: python cbt_vm_metadata_export.py -ip <host address> -u <host username>
-p <host password> -v <vm uuid> -o <metadata output path>
"""
import XenAPI
import shutil
import urllib3
import requests
import argparse
import re
import sys
def export_vm(host, session_id, vm_uuid, export_path):
# export_snapshots option determines whether vm snapshot data is included
url = ("https://%s/export_metadata?session_id=%s&uuid=%s"
"&export_snapshots=false"
% (host, session_id, vm_uuid))
with requests.Session() as session:
# ToDo: Security - We need to verify the SSL certificate here.
# Depends on CP-23051.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
request = session.get(url, verify=False, stream=True)
with open(export_path, 'wb') as filehandle:
shutil.copyfileobj(request.raw, filehandle)
request.raise_for_status()
print "Metadata saved to: %s" % export_path
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-ip', '--host-ip', dest='host')
parser.add_argument('-u', '--username', dest='username')
parser.add_argument('-p', '--password', dest='password')
parser.add_argument('-v', '--vm-uuid', dest='vm_uuid')
parser.add_argument('-o', '--output-path', dest='output_path')
args = parser.parse_args()
session = XenAPI.Session("https://" + args.host, ignore_ssl=True)
session.login_with_password(args.username, args.password, "0.1",
"CBT example")
try:
export_vm(args.host, session._session, args.vm_uuid, args.output_path)
finally:
session.xenapi.session.logout()
if __name__ == "__main__":
main()