Skip to content

Instantly share code, notes, and snippets.

@goindwalia
Created August 20, 2015 12:45
Show Gist options
  • Save goindwalia/48a5f9ab6a2f4eedfdba to your computer and use it in GitHub Desktop.
Save goindwalia/48a5f9ab6a2f4eedfdba to your computer and use it in GitHub Desktop.
Deploy OVF
from pyVim import connect
from pyVmomi import vim, vmodl
import threading, os, sys
import atexit
import argparse
import getpass
def get_args():
"""Get command line args from the user.
"""
parser = argparse.ArgumentParser(
description='Standard Arguments for talking to vCenter')
# because -h is reserved for 'help' we use -s for service
parser.add_argument('-s', '--host',
required=True,
action='store',
help='vSphere service to connect to')
# because we want -p for password, we use -o for port
parser.add_argument('-o', '--port',
type=int,
default=443,
action='store',
help='Port to connect on')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='User name to use when connecting to host')
parser.add_argument('-p', '--password',
required=False,
action='store',
help='Password to use when connecting to host')
parser.add_argument('-f', '--ovf',
required=True,
action='store',
help='Path to OVF file.')
args = parser.parse_args()
if not args.password:
args.password = getpass.getpass(
prompt='Enter password for host %s and user %s: ' %
(args.host, args.user))
return args
def keep_lease_alive(lease):
while(True):
time.sleep(5)
try:
# This keeps the lease alive.
lease.HttpNfcLeaseProgress(50)
if (lease.state == vim.HttpNfcLease.State.done):
return
# If the lease is released, we get an exception.
except:
return
def main():
"""
Simple command-line program for listing the virtual machines on a system.
"""
args = get_args()
si = connect.SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
f = open(args.ovf, 'r')
ovfd = f.read()
f.close()
# Create the objects needed for the import spec
datacenter_list = si.content.rootFolder.childEntity
dc_obj = datacenter_list[0]
datastore_list = dc_obj.datastoreFolder.childEntity
ds_obj = datastore_list[0]
network_list = dc_obj.networkFolder.childEntity
net_obj = network_list[0]
resource_pool = dc_obj.hostFolder.childEntity[0].resourcePool
# Now we create the import spec
manager = si.content.ovfManager
isparams = vim.OvfManager.CreateImportSpecParams()
import_spec = manager.CreateImportSpec(ovfd,
resource_pool,
ds_obj,
isparams)
print import_spec.importSpec
lease = resource_pool.ImportVApp(import_spec.importSpec)
print lease.state
while (True):
if (lease.state == vim.HttpNfcLease.State.ready):
print "Ready to rock and roll"
return 0
elif (lease.state == vim.HttpNfcLease.State.error):
# Print some error message out if you feel so inclined.
print "Failed"
print lease.error
sys.exit(1)
return 0
# Start program
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment