def __init__(self, server=None, username=None, password=None, wsdl_location="local", timeout=30): self._logged_in = False if server is None: server = _config_value("general", "server") if username is None: username = _config_value("general", "username") if password is None: password = _config_value("general", "password") if server is None: raise ConfigError("server must be set in config file or Client()") if username is None: raise ConfigError( "username must be set in config file or Client()") if password is None: raise ConfigError( "password must be set in config file or Client()") self.server = server self.username = username self.password = password url = "https://%s/sdk" % self.server if wsdl_location == "local": current_path = os.path.abspath(os.path.dirname(__file__)) current_path = current_path.replace('\\', '/') if not current_path.startswith('/'): current_path = '/' + current_path if current_path.endswith('/'): current_path = current_path[:-1] wsdl_uri = ("file://%s/wsdl/vimService.wsdl" % current_path) elif wsdl_location == "remote": wsdl_uri = url + "/vimService.wsdl" else: raise ValueError("wsdl_location must be \"local\" or \"remote\"") # Init the base class try: suds.client.Client.__init__(self, wsdl_uri) except URLError: logger.critical("Failed to connect to %s", self.server) raise except IOError: logger.critical("Failed to load the local WSDL from %s", wsdl_uri) raise except TransportError: logger.critical("Failed to load the remote WSDL from %s", wsdl_uri) raise self.options.transport.options.timeout = timeout self.set_options(location=url) mo_ref = soap.ManagedObjectReference("ServiceInstance", "ServiceInstance") self.si = ServiceInstance(mo_ref, self) try: self.sc = self.si.RetrieveServiceContent() except URLError, e: logger.critical("Failed to connect to %s" % self.server) logger.critical("urllib2 said: %s" % e.reason) raise
def __init__(self, args): self.hotplug = args.hotplug self.coldplug = args.coldplug self.memory = args.memory self.cpu = args.cpu self.disks = args.disksize self.nics = args.nic self.isofile = args.iso # self.tmplname = args.template self.server = config._config_value('general', 'server', args.server) self.username = config._config_value('general', 'username', args.username) self.password = config._config_value('general', 'password', args.password) self.crname = args.compute_resource self.hostname = args.host self.dsname = args.datastore self.thick_disks = args.thick_disks self.name = args.name self.guestid = args.guest_id # self.template = None self.resource_pool = None self.diskctlr = None self.datastore = None self.datacenter = None self.target = None self.devices = [] self.vmdisks = [] self.vmnics = [] self.unit = 0 self.key = 0 self.cdrom = None u.Client.__init__(self)
def __init__(self, args): self.dcname = args.datacenter self.dsname = args.datastore self.server = config._config_value('general', 'server', args.server) self.username = config._config_value('general', 'username', args.username) self.password = config._config_value('general', 'password', args.username) self.client = None self.datacenter = None self.datastore = None
def __init__(self, server=None, username=None, password=None, wsdl_location="local", timeout=30, plugins=[]): self._logged_in = False if server is None: server = _config_value("general", "server") if username is None: username = _config_value("general", "username") if password is None: password = _config_value("general", "password") if server is None: raise ConfigError("server must be set in config file or Client()") if username is None: raise ConfigError("username must be set in config file or Client()") if password is None: raise ConfigError("password must be set in config file or Client()") self.server = server self.username = username self.password = password url = "https://%s/sdk" % self.server if wsdl_location == "local": current_path = os.path.abspath(os.path.dirname(__file__)) current_path = current_path.replace('\\', '/') if not current_path.startswith('/') : current_path = '/' + current_path if current_path.endswith('/') : current_path = current_path[:-1] wsdl_uri = ("file://%s/wsdl/smsService.wsdl" % current_path) elif wsdl_location == "remote": # don't know where the remote wsdl is for the sms service, so dont support yet raise ValueError('wsdl_location=remote not yet supported for the SMS service') else: raise ValueError("wsdl_location must be \"local\" or \"remote\"") # Init the base class try: # Add ExtraConfigPlugin to the plugins plugins.append(ExtraConfigPlugin()) suds.client.Client.__init__(self, wsdl_uri, plugins=plugins) except URLError: logger.critical("Failed to connect to %s", self.server) raise except IOError: logger.critical("Failed to load the local WSDL from %s", wsdl_uri) raise except TransportError: logger.critical("Failed to load the remote WSDL from %s", wsdl_uri) raise self.options.transport.options.timeout = timeout self.set_options(location=url) mo_ref = soap.ManagedObjectReference("SmsServiceInstance", "SmsServiceInstance") self.si = SmsServiceInstance(mo_ref, self) try: self.sc = self.si.RetrieveServiceContent() except URLError, e: logger.critical("Failed to connect to %s" % self.server) logger.critical("urllib2 said: %s" % e.reason) raise
def main(name, options): """The main method for this script. :param name: The name of the VM to create. :type name: str :param template_name: The name of the template to use for creating \ the VM. :type template_name: str """ server = config._config_value("general", "server", options.server) if server is None: raise ValueError("server must be supplied on command line" " or in configuration file.") username = config._config_value("general", "username", options.username) if username is None: raise ValueError("username must be supplied on command line" " or in configuration file.") password = config._config_value("general", "password", options.password) if password is None: raise ValueError("password must be supplied on command line" " or in configuration file.") vm_template = None if options.template is not None: try: vm_template = template.load_template(options.template) except TemplateNotFoundError: print("ERROR: Template \"%s\" could not be found." % options.template) sys.exit(1) expected_opts = ["compute_resource", "datastore", "disksize", "nics", "memory", "num_cpus", "guest_id", "host"] vm_opts = {} for opt in expected_opts: vm_opts[opt] = getattr(options, opt) if vm_opts[opt] is None: if vm_template is None: raise ValueError("%s not specified on the command line and" " you have not specified any template to" " inherit the value from." % opt) try: vm_opts[opt] = vm_template[opt] except AttributeError: raise ValueError("%s not specified on the command line and" " no value is provided in the specified" " template." % opt) client = Client(server=server, username=username, password=password) create_vm(client, name, vm_opts["compute_resource"], vm_opts["datastore"], vm_opts["disksize"], vm_opts["nics"], vm_opts["memory"], vm_opts["num_cpus"], vm_opts["guest_id"], host=vm_opts["host"]) client.logout()
def _init_logging(self): """Initialize logging.""" log_destination = _config_value("logging", "destination", "CONSOLE") if log_destination == "CONSOLE": lh = logging.StreamHandler() else: lh = logging.FileHandler(os.path.expanduser(log_destination)) log_level = _config_value("logging", "level", "WARNING") print("Logging to %s at %s level" % (log_destination, log_level)) lh.setLevel(getattr(logging, log_level)) logger.setLevel(getattr(logging, log_level)) logger.addHandler(lh) # Initialise logging for the SOAP module soap._init_logging(log_level, lh) logger.info("Initialised logging")
import glob import os import yaml import logging from psphere import config from psphere.errors import TemplateNotFoundError logger = logging.getLogger("psphere") template_path = os.path.expanduser(config._config_value("general", "template_dir")) def _merge(first, second): """Merge a list of templates. The templates will be merged with values in higher templates taking precedence. :param templates: The templates to merge. :type templates: list """ return dict(first.items() + second.items()) def load_template(name=None): """Loads a template of the specified name. Templates are placed in the <template_dir> directory in YAML format with a .yaml extension.
def __init__(self, args): self.server = config._config_value('general', 'server', args.server) self.username = config._config_value('general', 'username', args.username) self.password = config._config_value('general', 'username', args.password)
def __init__(self, server=None, username=None, password=None, wsdl_location="local", timeout=30, init_clone=False, clone=None): self._logged_in = False if server is None: server = _config_value("general", "server") if username is None: username = _config_value("general", "username") if password is None: password = _config_value("general", "password") if server is None: raise ConfigError("server must be set in config file or Client()") if username is None: raise ConfigError("username must be set in config file or Client()") if password is None: raise ConfigError("password must be set in config file or Client()") self.server = server self.username = username self.password = password url = "https://%s/sdk" % self.server if wsdl_location == "local": current_path = os.path.abspath(os.path.dirname(__file__)) current_path = current_path.replace('\\', '/') if not current_path.startswith('/') : current_path = '/' + current_path if current_path.endswith('/') : current_path = current_path[:-1] wsdl_uri = ("file://%s/wsdl/vimService.wsdl" % current_path) elif wsdl_location == "remote": wsdl_uri = url + "/vimService.wsdl" else: raise ValueError("wsdl_location must be \"local\" or \"remote\"") # Init the base class if clone: self.sd = clone.sd self.options = Options() cp = Unskin(self.options) mp = Unskin(clone.options) cp.update(deepcopy(mp)) self.wsdl = clone.wsdl self.factory = clone.factory self.service = ServiceSelector(self, clone.wsdl.services) self.messages = dict(tx=None, rx=None) else: try: suds.client.Client.__init__(self, wsdl_uri) except URLError: logger.critical("Failed to connect to %s", self.server) raise except IOError: logger.critical("Failed to load the local WSDL from %s", wsdl_uri) raise except TransportError: logger.critical("Failed to load the remote WSDL from %s", wsdl_uri) raise if init_clone: return self.options.transport.options.timeout = timeout self.set_options(location=url) mo_ref = soap.ManagedObjectReference("ServiceInstance", "ServiceInstance") self.si = ServiceInstance(mo_ref, self) try: self.sc = self.si.RetrieveServiceContent() except URLError, e: logger.critical("Failed to connect to %s" % self.server) logger.critical("urllib2 said: %s" % e.reason) raise
import glob import os import yaml import logging from psphere import config from psphere.errors import TemplateNotFoundError logger = logging.getLogger(__name__) template_path = os.path.expanduser( config._config_value("general", "template_dir")) def _merge(first, second): """Merge a list of templates. The templates will be merged with values in higher templates taking precedence. :param templates: The templates to merge. :type templates: list """ return dict(first.items() + second.items()) def load_template(name=None): """Loads a template of the specified name. Templates are placed in the <template_dir> directory in YAML format with