def makeRequest(self, *args, **kwargs): expectedStatusCodes = kwargs.pop('expectedStatusCodes', None) # Sanitize error codes a bit if expectedStatusCodes is not None: if not isinstance(expectedStatusCodes, set): expectedStatusCodes = set(expectedStatusCodes) try: resp = self.request(*args, **kwargs) except restclient.ResponseError, e: if expectedStatusCodes and e.status in expectedStatusCodes: return e # XXX Munge exception here if self.getContentTypeFromHeaders(e.headers) == self.TYPES.error: try: error = Models.handler.parseString(e.contents) message = "vCloud error: %s" % error.message except: error = message = e.contents raise errors.CatalogError(status=e.status, message=message, error=error) raise errors.CatalogError("Unknown error: %s" % e)
def _xePutFile(self, urlSelector, fileObj, taskRef, loopCount=1000, loopTimeout=0.5, **kwargs): urlTemplate = 'http://%s:%s@%s/%s?%s' cloudConfig = self.getTargetConfiguration() cloudName = cloudConfig['name'] creds = self.credentials username, password = creds['username'], creds['password'] kwargs['task_id'] = taskRef query = '&'.join("%s=%s" % (k, kwargs[k]) for k in sorted(kwargs)) #print ("curl -T /tmp/foo.iso '%s'" % (urlTemplate % (username, password, cloudName, urlSelector, query))) client = UploadClient( urlTemplate % (username, password, cloudName, urlSelector, query)) resp = client.request(fileObj) # The server does not send any useful information back. Close the # request and fetch the status from the task if resp: resp.close() rec = self._pollTask(taskRef, loopCount, timeout=loopTimeout) if not rec: # timeout raise errors.CatalogError("Failure uploading file") status = rec.get('status') if status == 'success': return rec if status == 'failure': errorInfo = rec['error_info'] raise errors.CatalogError("Unable to upload file: %s" % (errorInfo, )) # Canceled raise errors.CatalogError("Task has finished unexpectedly: %s" % status)
def extractImage(self, path): if path.endswith('.zip'): workdir = path[:-4] util.mkdirChain(workdir) cmd = 'unzip -d %s %s' % (workdir, path) elif path.endswith('.tgz'): workdir = path[:-4] util.mkdirChain(workdir) cmd = 'tar zxSf %s -C %s' % (path, workdir) else: raise errors.CatalogError('unsupported rBuilder image archive format') p = subprocess.Popen(cmd, shell = True, stderr = file(os.devnull, 'w')) p.wait() return workdir
def drvPopulateLaunchDescriptor(self, descr, extraArgs=None): descr.setDisplayName("Xen Enterprise Launch Parameters") descr.addDescription("Xen Enterprise Launch Parameters") self.drvLaunchDescriptorCommonFields(descr) storageRepos = self._getStorageRepos() if not storageRepos: # No storage repositories defined; fail raise errors.CatalogError("No Storage Repositories defined") descr.addDataField( "storageRepository", descriptions="Storage Repository", required=True, help=[("launch/storageRepository.html", None)], type=descr.EnumeratedType( descr.ValueWithDescription(x[0], descriptions=x[1][0]) for x in storageRepos), default=storageRepos[0][0], ) return descr
def _putVmImage(self, vmFile, srUuid, taskRef): client = self.client srRef = self._cachedGet(srUuid, client.xenapi.SR.get_by_uuid) task = self._xePutFile('import', file(vmFile), taskRef, loopCount=10000, sr_id=srRef) # Wrap the pseudo-XMLRPC response params = XenAPI.xmlrpclib.loads(self._XmlRpcWrapper % task['result'])[0] reflist = params[0] if len(reflist) < 1: raise errors.CatalogError( "Unable to publish image, no results found") vmRef = reflist[0] # Make it a template client.xenapi.VM.set_is_a_template(vmRef, True) vmUuid = client.xenapi.VM.get_uuid(vmRef) return vmRef, vmUuid
# Connection reset by peer. xen has the bad habit of # closing the request before one had the chance to read # the response print "Failure", i time.sleep(1) continue raise else: # for raise if task.get('status') != 'success': errorInfo = task.get('error_info', '') # Get rid of the vdi, it's not useful anymore client.xenapi.VDI.destroy(vdiRef) raise errors.CatalogError( "Unable to upload initial credentials for %s: %s" % vmUuid, errorInfo) return vdiRef def _createVdi(self, srRef, vmUuid, fileSize): client = self.client vdiRec = { 'SR': srRef, 'type': 'system', 'virtual_size': str(fileSize), 'name_label': 'Credentials for %s' % vmUuid, 'name_description': 'Credentials for %s' % vmUuid, 'sharable': False, 'read_only': True, 'other_config': {}, }