def get_kit_url(self, name, version, iteration): \ # pylint: disable=no-self-use kit = Kit(name, version, iteration) native_repo = repoManager.getRepo() return os.path.join(native_repo.getRemoteUrl(), kit.getTarBz2FileName())
def _create_kit_db_entry(self, session: Session, kit) -> Kit: """ Creates a database entry for a kit. :param kit: :return: a Kit instance (TortugaObject) """ try: return self._kit_db_api.getKit(session, kit['name'], kit['ver']) except KitNotFound: pass # Add the database entries for the kit kitObj = Kit(name=kit['name'], version=kit['ver'], iteration='0') kitObj.setDescription(kit['sum']) kitObj.setIsOs(True) kitObj.setIsRemovable(True) kit_descr = format_kit_descriptor(kit['name'], kit['ver'], kit['arch']) newComp = Component(name=kit_descr, version=kit['ver']) newComp.setDescription('%s mock component' % (kit_descr)) newComp.addOsInfo( osHelper.getOsInfo(kit['name'], kit['ver'], kit['arch'])) kitObj.addComponent(newComp) # Kit does not previously exist, perform 'normal' add kit operation self._kit_db_api.addKit(session, kitObj) return kitObj
def _cleanup_kit(self, session: Session, kit: Kit, force: bool, skip_db: bool = False): """ Uninstalls the kit and it's file repos. :param session: a database session :param kit: the Kit instance :param force: whether or not to force the deletion """ repo_dir = kit.getKitRepoDir() # # Remove the kit from the DB # if not skip_db: self._kit_db_api.deleteKit(session, kit.getName(), kit.getVersion(), kit.getIteration(), force=force) # # Remove the files and repo # for repo in repoManager.getRepoList(): # # Delete the repo # repo.delete(repo_dir) # # Remove repo files # full_repo_dir = os.path.join(repo.getLocalPath(), repo_dir) self._logger.debug( 'Removing repo dir: {}'.format(full_repo_dir)) # # When LINKOSKITMEDIA is used, the kit directory is a symlink # to the real media, delete the link instead of attempting # to delete the directory. # if os.path.islink(full_repo_dir): os.unlink(full_repo_dir) else: osUtility.removeDir(full_repo_dir) # # Check and clean up proxy # self.remove_proxy(repo_dir) # # Remove the kit installation dir # kit_dir = os.path.join(self._kits_root, kit.getDirName()) if os.path.exists(kit_dir): self._logger.debug( 'Removing kit installation directory: {}'.format(kit_dir)) osUtility.removeDir(kit_dir)
def installKit(self, db_manager, name, version, iteration): """ Install kit using kit name/version/iteration. The kit package must be located in the remote repository for the native OS. Kit will be installed for all operating systems that: 1) have repo configured on the local machine 2) are specified in the kit.xml file Raises: KitAlreadyExists """ kit = Kit(name, version, iteration) # nativeRepo = repoManager.getRepo() kitPkgUrl = self.get_kit_url(name, version, iteration) # Check for kit existence. with db_manager.session() as session: self._check_if_kit_exists(session, kit) self._logger.debug( '[{0}] Installing kit [{1}]'.format( self.__class__.__name__, kit)) return self.installKitPackage(db_manager, kitPkgUrl)
def getKit(self, session: Session, name, version, iteration: Optional[str] = None): """ Get kit from the db. Returns: kit Throws: KitNotFound DbError """ try: dbKit = self._kitsDbHandler.getKit(session, name, version, iteration) kit = Kit.getFromDbDict(dbKit.__dict__) return self.__retrieveAllKitData(dbKit, kit) except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise
def getKit(self, name, version, iteration=None): """ Get kit info. Returns: kit Throws: KitNotFound TortugaException """ dbVersion = '%s-%s' % (version, iteration) if not iteration: dbVersion = '%s' % (version) url = 'v1/kits/%s/%s' % (name, dbVersion) try: _, responseDict = self.sendSessionRequest(url) return Kit.getFromDict(responseDict.get('kit')) except TortugaException: raise except Exception as ex: raise TortugaException(exception=ex)
def _get_kit_and_component_version(self, session: Session, kit_name, kit_version, kit_iteration, comp_name, comp_version=None): """ Gets a Kit instance and component version. :param kit_name: the name of the kit :param kit_version: the version of the kit :param kit_iteration: the iteration of the kit :param comp_name: the component name :param comp_version: the component version (optional) :return: a tuple, consisting of (Kit, component_version) """ kit = None if kit_name is None: kit = self._get_kit_by_component(session, comp_name, comp_version=comp_version) # # Get component version if required # if comp_version is None: for component in kit.getComponentList(): if component.getName() == comp_name: comp_version = component.getVersion() break elif kit_version is None or kit_iteration is None: kits_found = 0 for k in self._kit_db_api.getKitList(session): if k.getName() == kit_name and \ (kit_version is None or k.getVersion() == kit_version) and \ (kit_iteration is None or k.getIteration() == kit_iteration): kit = k kits_found += 1 if kits_found > 1: if kit_version is not None: raise KitNotFound('Multiple kits found: {}-{}'.format( kit_name, kit_version)) else: raise KitNotFound( 'Multiple kits found {}'.format(kit_name)) else: kit = self._kit_db_api.getKit(session, kit_name, kit_version, kit_iteration) if kit is None: raise KitNotFound('Kit [%s] not found' % (Kit(kit_name, kit_version, kit_iteration))) return kit, comp_version
def get_kit_installer(kit_spec: Tuple[str, str, str]): """ Gets a kit installer from the registry. :param kit_spec: a kit spec tuple ('name', 'version', 'iteration') :return: a kit installer instance :raises KitNotfound: """ kit = KIT_INSTALLER_REGISTRY.get(kit_spec) if kit is None: raise KitNotFound('Kit [%s] not found' % (Kit(*kit_spec))) return kit
def test_addKit(dbm): with dbm.session() as session: name = 'testkit' version = '0.0.1' iteration = '0' kitObj = KitTortugaObject(name=name, version=version, iteration=iteration) # create dummy component dummy_component1 = ComponentTortugaObject(name='dummy', version='0.0.1') kitObj.addComponent(dummy_component1) dummy_component2 = ComponentTortugaObject(name='dummy2', version='0.0.1') kitObj.addComponent(dummy_component2) KitsDbHandler().addKit(session, kitObj) new_kit = KitsDbHandler().getKit(session, 'testkit') assert new_kit.name == 'testkit' assert new_kit.components with pytest.raises(KitAlreadyExists): KitsDbHandler().addKit(session, kitObj) KitsDbHandler().deleteKit(session, name, version, iteration) with pytest.raises(KitNotFound): KitsDbHandler().getKit(session, name)
def get_kit(self): """ Gets the Kit instance for this kit. :return: a Kit instance """ kit = Kit(name=self.name, version=self.version, iteration=self.iteration) kit.setDescription(self.meta.get('description', None)) for component_installer in self.get_all_component_installers(): kit.addComponent(component_installer.get_component()) return kit
def getKit(self, name: str, version: Optional[str] = None, iteration: Optional[str] = None) -> Kit: """ Get kit info. Raises: KitNotFound """ url = 'kits/?name={}'.format(urllib.parse.quote_plus(name)) if version is not None: url += '&version={}'.format(urllib.parse.quote_plus(version)) if iteration is not None: url += '&iteration={}'.format(urllib.parse.quote_plus(iteration)) try: responseDict = self.get(url) # response is a list, so reference first item in list kits = responseDict.get('kits') if not kits: kit_spec_str = name if version: kit_spec_str += '-{}'.format(version) if iteration: kit_spec_str += '-{}'.format(iteration) raise KitNotFound( 'Kit matching specification [{}] not found'.format( kit_spec_str)) return Kit.getFromDict(kits[0]) except TortugaException: raise except Exception as ex: raise TortugaException(exception=ex)
def getKitById(self, session: Session, id_): """ Get kit from the db Returns: kit Throws: KitNotFound DbError """ try: dbKit = self._kitsDbHandler.getKitById(session, id_) kit = Kit.getFromDbDict(dbKit.__dict__) return self.__retrieveAllKitData(dbKit, kit) except TortugaException: raise except Exception as ex: self._logger.exception(str(ex)) raise
def getKitById(self, id_: int) -> Kit: """ Get kit info by kitId. Raises: KitNotFound TortugaException """ url = 'kits/%s' % (id_) try: responseDict = self.get(url) return Kit.getFromDict(responseDict.get('kit')) except TortugaException: raise except Exception as ex: raise TortugaException(exception=ex)
def getKitList(self): """ Get kit list. Returns: [kits] Throws: TortugaException """ url = 'v1/kits' try: _, responseDict = self.sendSessionRequest(url) return Kit.getListFromDict(responseDict) except TortugaException: raise except Exception as ex: raise TortugaException(exception=ex)
def getKitById(self, id_): """ Get kit info by kitId. Returns: kit Throws: KitNotFound TortugaException """ url = 'v1/kits/%s' % (id_) try: _, responseDict = self.sendSessionRequest(url) return Kit.getFromDict(responseDict.get('kit')) except TortugaException: raise except Exception as ex: raise TortugaException(exception=ex)
def getKitList(self, session: Session, os_kits_only: Optional[bool] = False): """ Get list of all available or os kits only from the db """ try: kits = [] for kit in self._kitsDbHandler.getKitList( session, os_kits_only=os_kits_only): self.loadRelations(kit, {'components': True}) kits.append(Kit.getFromDbDict(kit.__dict__)) return TortugaObjectList(kits) except TortugaException: raise except Exception as ex: self._logger.exception(str(ex)) raise
def getKitList(self): """ Get kit list. Returns: [kits] Throws: TortugaException """ url = 'kits/' try: responseDict = self.get(url) return Kit.getListFromDict(responseDict) except TortugaException: raise except Exception as ex: raise TortugaException(exception=ex)
def getKitList(self): """ Get list of all available kits from the db. """ session = DbManager().openSession() try: kits = [] for kit in self._kitsDbHandler.getKitList(session): self.loadRelations(kit, {'components': True}) kits.append(Kit.getFromDbDict(kit.__dict__)) return TortugaObjectList(kits) except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % (ex)) raise finally: DbManager().closeSession()
def getKitById(self, id_): """ Get kit from the db Returns: kit Throws: KitNotFound DbError """ session = DbManager().openSession() try: dbKit = self._kitsDbHandler.getKitById(session, id_) kit = Kit.getFromDbDict(dbKit.__dict__) return self.__retrieveAllKitData(dbKit, kit) except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession()
def parse(kitXmlFile): """Parse kit information for a given os and return kit object.""" logger = logging.getLogger('tortuga.kit.parse') logger.addHandler(logging.NullHandler()) try: logger.debug('parse(): parsing [%s]' % (kitXmlFile)) xmlDoc = minidom.parse(kitXmlFile) rootNode = xmlDoc.getElementsByTagName('kit')[0] kit = Kit(name=rootNode.getAttribute('name'), version=rootNode.getAttribute('version'), iteration=rootNode.getAttribute('iteration')) documentationNode = rootNode.getElementsByTagName('documentation')[0] kit.setDocumentation(documentationNode.getAttribute('path')) eulaNodes = rootNode.getElementsByTagName('eula') if len(eulaNodes): try: with open(os.path.join(os.path.dirname(kitXmlFile), eulaNodes[0].getAttribute('path')), 'r') as f: textContents = f.read() kit.setEula( Eula(eulaNodes[0].getAttribute('key'), textContents)) except Exception: logger.exception('Exception raised parsing EULA from kit') logger.debug('Unable to load EULA file contents') kit.setIntegrationModulePath( rootNode.getElementsByTagName('integration-module')[0]. getAttribute('path')) kit.setDescription( rootNode.getElementsByTagName('description')[0].firstChild. nodeValue) for cNode in xmlDoc.getElementsByTagName('component'): component = Component(name=cNode.getAttribute('name'), version=cNode.getAttribute('version')) logger.debug( 'parse(): Found component [%s]' % (component.getName())) # Kit description nodeList = cNode.getElementsByTagName('description') if nodeList: component.setDescription(nodeList[0].firstChild.nodeValue) # process os elements for osNode in cNode.getElementsByTagName('os'): result = _process_component_os(osNode, component) logger.debug( 'parse(): Adding component [%s] for [%s]' % ( component, result)) kit.addComponent(component) return kit except TortugaException: raise except Exception as ex: logger.exception('Exception raised while parsing [%s]' % (kitXmlFile)) raise TortugaException(exception=ex)
def _delete_kit(self, session: Session, kit: Kit, force: bool): """ Deletes a regular kit. :param session: a database instance :param kit: the Kit instance :param force: whether or not to force the deletion """ kit_spec = (kit.getName(), kit.getVersion(), kit.getIteration()) # # If the kit does not exist in the DB, then we want to skip # the step of removing it from the DB # skip_db = False try: self.getKit(session, *kit_spec) except KitNotFound: skip_db = True kit_install_path = os.path.join(self._kits_root, kit.getDirName()) if os.path.exists(kit_install_path): # # Attempt to get the kit installer # installer = None try: installer = get_kit_installer(kit_spec)() installer.session = session except KitNotFound: pass # # Attempt to run pre-uninstall action # if installer: try: installer.run_action('pre_uninstall') except Exception as ex: self._logger.warning( 'Error running pre_uninstall: {}'.format( str(ex) ) ) # # Remove db record and files # self._cleanup_kit(session, kit, force, skip_db) # # Attempt to uninstall puppet modules, and perform post-install # if installer: try: installer.run_action('uninstall_puppet_modules') except Exception as ex: self._logger.warning( 'Error uninstalling puppet modules: {}'.format( str(ex) ) ) try: installer.run_action('post_uninstall') except Exception as ex: self._logger.warning( 'Error running post-install: {}'.format( str(ex) ) )