-
-
Save jmrodri/546b26f7fd9fb302355f to your computer and use it in GitHub Desktop.
sample script to get template status from RHEV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import logging | |
import os | |
import sys | |
import time | |
from optparse import OptionParser | |
try: | |
from ovirtsdk.api import API | |
from ovirtsdk.xml import params | |
from ovirtsdk.infrastructure.errors import RequestError | |
except: | |
print "Please re-run after you have installed 'ovirt-engine-sdk-python'" | |
print "Example: easy_install ovirt-engine-sdk-python" | |
sys.exit() | |
DEFAULT_API_USER="admin@internal" | |
def parse_args(): | |
parser = OptionParser(description="Import CFME Template") | |
parser.add_option("--debug", action="store_true", | |
default=False, help="debug mode") | |
parser.add_option('--api_user', default=DEFAULT_API_USER, | |
help='oVirt API Username. Default: [%s]' % (DEFAULT_API_USER)) | |
parser.add_option("--api_host", | |
default=None, help="oVirt API IP Address/Hostname") | |
parser.add_option('--api_pass', | |
default=None, help='oVirt API Password') | |
parser.add_option('--vm_template_name', | |
default=None, help='VM template name to import') | |
(opts, args) = parser.parse_args() | |
for optname in ["api_host", "api_pass", "vm_template_name"]: | |
optvalue = getattr(opts, optname) | |
if not optvalue: | |
parser.print_help() | |
parser.print_usage() | |
print "Please re-run with an option specified for: '%s'" % (optname) | |
sys.exit(1) | |
return opts | |
def setup_logging(debug=False): | |
if debug: | |
loglevel = logging.DEBUG | |
else: | |
loglevel = logging.INFO | |
logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | |
def import_template(export_domain, vm_template_name, import_template_params, attempts=20): | |
try: | |
vm_templ = export_domain.templates.get(vm_template_name) | |
vm_templ.import_template(import_template_params) | |
except RequestError, e: | |
if "Missing OVF file from VM" in e.detail and attempts > 0: | |
print "Waiting to retry importing template...sleeping 30 seconds" | |
time.sleep(30) | |
return import_template(export_domain, vm_template_name, import_template_params, attempts=attempts-1) | |
print e | |
return False | |
except Exception, e: | |
print e | |
return False | |
return True | |
if __name__ == "__main__": | |
opts = parse_args() | |
debug = opts.debug | |
setup_logging(debug) | |
api_host = opts.api_host | |
api_user = opts.api_user | |
api_pass = opts.api_pass | |
vm_template_name = opts.vm_template_name | |
print "Template name is [%s]" % vm_template_name | |
url = "https://%s" % (api_host) | |
logging.debug("Connecting to oVirt API at: '%s' with user '%s'" % (api_host, api_user)) | |
api = API(url=url, username=api_user, password=api_pass, insecure=True) | |
if not api: | |
print "Failed to connect to '%s'" % (url) | |
sys.exit(1) | |
print 'Waiting for Template to reach "ok" status' | |
print type(api.templates.get(name=vm_template_name)) | |
print api.templates.get(name=vm_template_name) | |
while api.templates.get(name=vm_template_name).status.state != 'ok': | |
time.sleep(1) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment