diff options
Diffstat (limited to 'vio')
-rw-r--r-- | vio/requirements.txt | 4 | ||||
-rw-r--r-- | vio/vio/vsphere/__init__.py | 0 | ||||
-rw-r--r-- | vio/vio/vsphere/utils.py | 94 |
3 files changed, 98 insertions, 0 deletions
diff --git a/vio/requirements.txt b/vio/requirements.txt index c972b0c..b76f9c3 100644 --- a/vio/requirements.txt +++ b/vio/requirements.txt @@ -37,3 +37,7 @@ oslo.config>=4.11.0 oslo.service>=1.25.0 eventlet>=0.20.0 PyYAML>=3.1.0 + +pyvmomi +pyvim +fire diff --git a/vio/vio/vsphere/__init__.py b/vio/vio/vsphere/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vio/vio/vsphere/__init__.py diff --git a/vio/vio/vsphere/utils.py b/vio/vio/vsphere/utils.py new file mode 100644 index 0000000..8d2a648 --- /dev/null +++ b/vio/vio/vsphere/utils.py @@ -0,0 +1,94 @@ +from pyVim import connect +from pyVmomi import vim + +import os +# import json +import yaml + +vcontent = None + + +def get_obj(content, vimtype, name): + """ + Return an object by name, if name is None the + first found object is returned + """ + obj = None + container = content.viewManager.CreateContainerView( + content.rootFolder, vimtype, True) + for c in container.view: + if name: + if c.name == name: + obj = c + break + else: + obj = c + break + + return obj + + +def get_objs(content, vimtype): + """ + Get all the vsphere objects associated with a given type + """ + obj = {} + container = content.viewManager.CreateContainerView( + content.rootFolder, vimtype, True) + for c in container.view: + obj.update({c: c.name}) + return obj + + +def wait_for_task(task): + """ wait for a vCenter task to finish """ + task_done = False + while not task_done: + if task.info.state == 'success': + return task.info.result + + if task.info.state == 'error': + print("there was an error") + task_done = True + + +def GetClient(): + global vcontent + if vcontent is not None: + return vcontent + vsphere_conf_path = os.getenv("VSPHERE_CONF", "/opt/etc/vsphere.yml") + vsphere_conf = yaml.load(open(vsphere_conf_path, "r"))['vsphere'] + # vsphere_conf = json.load(open(vsphere_conf_path, "r"))['vsphere'] + host = vsphere_conf['host'] + username = vsphere_conf['username'] + password = vsphere_conf['password'] + insecure = vsphere_conf.get("insecure", True) + if insecure: + service_instance = connect.SmartConnectNoSSL( + host=host, user=username, pwd=password) + else: + service_instance = connect.SmartConnect( + host=host, user=username, pwd=password, port=443) + vcontent = service_instance.RetrieveContent() + return vcontent + + +def CloneVM(src, dst, power_on=False, wait=True): + assert src != dst + vcontent = GetClient() + vm = get_obj(vcontent, [vim.VirtualMachine], src) + print("src vm:", vm) + relospec = vim.vm.RelocateSpec(pool=vm.resourcePool) + clonespec = vim.vm.CloneSpec(location=relospec) + clonespec.powerOn = power_on + task = vm.Clone(name=dst, folder=vm.parent, spec=clonespec) + print("clone task:", task) + if wait: + print("wait for task:", task) + result = wait_for_task(task) + print("task result:", result) + return task + + +def DeployOVA(src, datacenter, resource_pool, datastore): + pass |