def __init__(self): super().__init__() self._softwareProfilesDbHandler = SoftwareProfilesDbHandler() self._globalParametersDbHandler = GlobalParametersDbHandler() self._adminsDbHandler = AdminsDbHandler() self._osDbHandler = OperatingSystemsDbHandler()
def __init__(self): TortugaDbApi.__init__(self) self._softwareProfilesDbHandler = SoftwareProfilesDbHandler() self._nodesDbHandler = NodesDbHandler() self._globalParametersDbHandler = GlobalParametersDbHandler() self._adminsDbHandler = AdminsDbHandler() self._osDbHandler = OperatingSystemsDbHandler()
def transferNode(self, nodespec, dstSoftwareProfileName, bForce=False): """ Transfer nodes defined by 'nodespec' to 'dstSoftwareProfile' Raises: NodeNotFound SoftwareProfileNotFound NodeTransferNotValid """ session = DbManager().openSession() try: nodes = self.__expand_nodespec(session, nodespec) if not nodes: raise NodeNotFound('No nodes matching nodespec [%s]' % (nodespec)) dbDstSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile(session, dstSoftwareProfileName) results = NodesDbHandler().transferNode(session, nodes, dbDstSoftwareProfile, bForce=bForce) return self.__transferNodeCommon(session, dbDstSoftwareProfile, results) finally: DbManager().closeSession()
def setIdleSoftwareProfile(self, hardwareProfileName, softwareProfileName=None): """ Sets the idle software profile Returns: -none- Raises: SoftwareProfileNotFound SoftwareProfileNotIdle """ session = DbManager().openSession() try: dbSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile(session, softwareProfileName) \ if softwareProfileName else None dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) self._hardwareProfilesDbHandler.setIdleSoftwareProfile( dbHardwareProfile, dbSoftwareProfile) session.commit() except TortugaException as ex: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession()
def get_installer_node(self, session: Session) -> Node: """ Return installer node derived from searching for all software profiles with type 'installer'. Raises: NodeNotFound """ installer_swprofiles = \ SoftwareProfilesDbHandler().getSoftwareProfileList( session, profile_type='installer' ) if not installer_swprofiles: raise NodeNotFound('No installer software profiles found') installer_nodes = installer_swprofiles[0].nodes if not installer_nodes: raise NodeNotFound( 'No installer node found in software profile {}'.format( installer_swprofiles[0].name)) return installer_nodes[0]
def setIdleSoftwareProfile( self, session: Session, hardwareProfileName: str, softwareProfileName: Optional[str] = None) -> None: """ Sets the idle software profile Returns: -none- Raises: SoftwareProfileNotFound SoftwareProfileNotIdle """ try: dbSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile(session, softwareProfileName) \ if softwareProfileName else None dbHardwareProfile = self._hardwareProfilesDbHandler.\ getHardwareProfile(session, hardwareProfileName) self._hardwareProfilesDbHandler.setIdleSoftwareProfile( dbHardwareProfile, dbSoftwareProfile) session.commit() except TortugaException: raise except Exception as ex: self.getLogger().exception('%s' % ex) raise
def __init__(self): TortugaDbObjectHandler.__init__(self) self._nicsDbHandler = NicsDbHandler() self._hardwareProfilesDbHandler = HardwareProfilesDbHandler() self._softwareProfilesDbHandler = SoftwareProfilesDbHandler() self._softwareUsesHardwareDbHandler = SoftwareUsesHardwareDbHandler() self._globalParametersDbHandler = GlobalParametersDbHandler()
def _validate_add_count(self, sess: Session, swp_name: str, count: int): """ See validate_add_count. """ swp_api = SoftwareProfilesDbHandler() swp = swp_api.getSoftwareProfile(sess, swp_name) if swp.maxNodes <= 0: return current_count = len(swp.nodes) request_count = self._count_current_node_requests(sess, swp) if current_count + request_count + count > swp.maxNodes: raise OperationFailed( 'Request to add {} node(s) exceeds software profile' ' limit of {} nodes'.format(count, swp.maxNodes))
def test_getSoftwareProfileList_tags(self): # 'tag1' returns software profile 'profile1' only result = SoftwareProfilesDbHandler().getSoftwareProfileList( self.session, tags={'tag1': None}) assert result and \ len(result) == 1 and \ result[0].name == 'swprofile1'
def test_getSoftwareProfileList_tag1_and_tag2(self): # 'tag1' and 'tag2' returns both profiles result = SoftwareProfilesDbHandler().getSoftwareProfileList( self.session, tags={'tag1': None, 'tag2': None}) assert result and \ len(result) == 2 and \ not set(['swprofile1', 'swprofile2']) - set([swp.name for swp in result])
def test_get_software_profiles_with_component_failed2(self): with pytest.raises(ResourceNotFound): SoftwareProfilesDbHandler().get_software_profiles_with_component( self.session, 'base', 'installerEXAMPLE', kit_version='1.2.3', )
def test_getSoftwareProfileList_tag2(self): # 'tag2' returns software profile 'profile2' only result = SoftwareProfilesDbHandler().getSoftwareProfileList( self.session, tags={'tag2': None}) assert result and \ len(result) == 1 and \ 'tag2' in [tag.name for tag in result[0].tags]
def configure(self): # # Write config file # fp = open(CONFIG_FILE, 'w') try: print("# File generated by genconfig", file=fp) installer = self.kit_installer.config_manager.getInstaller() all_node_list = getNodeApi().getNodeList() node_list = [ node for node in all_node_list if node.getName() != installer and node.getState() != 'Deleted' and not node.getIsIdle() ] for node in node_list: print('{}'.format(node), file=fp) finally: fp.close() # # Write /etc/netgroup # fp = open('/etc/netgroup', 'w') dbm = DbManager() session = dbm.openSession() try: software_profiles = \ SoftwareProfilesDbHandler().getSoftwareProfileList(session) for software_profile in software_profiles: if not software_profile.nodes: continue software_profile_node_list = [ node.name for node in software_profile.nodes if node.state != 'Deleted' ] if not software_profile_node_list: continue fp.write( '{} {}\n\n'.format( software_profile.name, ' '.join(['({},,)'.format(node) for node in software_profile_node_list]) ) ) finally: fp.close() dbm.closeSession() fp.close()
def test_getSoftwareProfileList_tag1_and_tag2(self): # 'tag1' and 'tag2' returns neither profiles (and operator) result = SoftwareProfilesDbHandler().getSoftwareProfileList( self.session, tags={ 'tag1': None, 'tag2': None }) assert len(result) == 0
def test_getSoftwareProfileList_tag2(self): # 'tag2' returns software profile 'profile2' only result = SoftwareProfilesDbHandler().getSoftwareProfileList( self.session, tags=[(self.tags[1].name, )]) assert result assert len(result) == 1 assert self.tags[1] in result[0].tags
def test_getSoftwareProfileList(self): result = SoftwareProfilesDbHandler().\ getSoftwareProfileList(self.session) assert result assert len(result) == 2 assert self.softwareprofiles[0] in result assert self.softwareprofiles[1] in result
def test_get_software_profiles_with_component(self): result = SoftwareProfilesDbHandler( ).get_software_profiles_with_component(self.session, 'base', 'installer') assert result and isinstance(result[0], SoftwareProfile) # 'installer' component must be enabled on 'Installer' software # profile as per the database fixture assert result[0].name == 'Installer'
def test_getSoftwareProfileList_by_type2(self): """ Get software profile list by type 'compute' """ result = SoftwareProfilesDbHandler().getSoftwareProfileList( self.session, profile_type='compute') assert isinstance(result, list) and result and \ isinstance(result[0], SoftwareProfile) and \ result[0].type == 'compute'
def test_getSoftwareProfileList_tag1_and_tag2(self): # 'tag1' and 'tag2' returns both profiles result = SoftwareProfilesDbHandler().getSoftwareProfileList( self.session, tags=[(self.tags[0].name, ), (self.tags[1].name, )]) assert result assert len(result) == 2 assert self.softwareprofiles[0] in result assert self.softwareprofiles[1] in result
def test_start(load_config_dict_mock, pre_add_host_mock, fire_provisioned_even_mock, get_instance_size_mapping_mock, dbm, valid_ami): """ Test ResourceAdapter.start() workflow """ get_instance_size_mapping_mock.return_value = 8 load_config_dict_mock.return_value = { 'awsaccesskey': 'the_key', 'awssecretkey': 'the_secret', 'keypair': 'the_keypair', 'ami': valid_ami, 'use_instance_hostname': 'true', 'instancetype': 'the_instancetype' } with dbm.session() as session: adapter = Aws(addHostSession='123EXAMPLE') # override default sleep time adapter.LAUNCH_INITIAL_SLEEP_TIME = 0.0 hardwareprofile = HardwareProfilesDbHandler().getHardwareProfile( session, 'aws2' ) softwareprofile = SoftwareProfilesDbHandler().getSoftwareProfile( session, 'compute' ) addNodesRequest = { 'count': 2, 'hardwareProfile': hardwareprofile.name, 'softwareProfile': softwareprofile.name, } nodes = adapter.start( addNodesRequest, session, hardwareprofile, dbSoftwareProfile=softwareprofile ) assert nodes and isinstance(nodes, list) and \ isinstance(nodes[0], Node) assert nodes[0].instance.instance if len(nodes) > 1: assert nodes[1].instance.instance pre_add_host_mock.assert_called() fire_provisioned_even_mock.assert_called()
def transferNodes(self, srcSoftwareProfileName, dstSoftwareProfileName, count, bForce=False): """ Transfer 'count' nodes from 'srcSoftwareProfile' to 'dstSoftwareProfile' Raises: SoftwareProfileNotFound """ session = DbManager().openSession() try: # It is not necessary to specify a source software profile. If # not specified, pick any eligible nodes in the hardware profile # mapped to the destination software profile. Don't ask me who # uses this capability, but it's here if you need it... dbSrcSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile( session, srcSoftwareProfileName) \ if srcSoftwareProfileName else None dbDstSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile(session, dstSoftwareProfileName) results = NodesDbHandler().transferNodes(session, dbSrcSoftwareProfile, dbDstSoftwareProfile, int(float(count)), bForce=bForce) return self.__transferNodeCommon(session, dbDstSoftwareProfile, results) finally: DbManager().closeSession()
def test_failed_initializeNode(dbm): with dbm.session() as session: hardware_profile = HardwareProfilesDbHandler().getHardwareProfile( session, 'nonetwork') software_profile = SoftwareProfilesDbHandler().getSoftwareProfile( session, 'compute') node = Node() with pytest.raises(NetworkNotFound): api.initializeNode(session, node, hardware_profile, software_profile, [])
def test_get_software_profiles_with_component_variation(self): """ Test with and without 'kit_version' argument expecting the same results. """ result1 = SoftwareProfilesDbHandler().get_software_profiles_with_component( self.session, 'base', 'core' ) assert result1 result2 = SoftwareProfilesDbHandler().get_software_profiles_with_component( self.session, 'base', 'core', kit_version='7.0.1' ) assert result2 swprofile_names1 = [swprofile1.name for swprofile1 in result1] swprofile_names2 = [swprofile2.name for swprofile2 in result2] assert swprofile_names1 == swprofile_names2
def _is_component_enabled(self, software_profile_name: str) -> bool: """ Returns True if this component is enabled on the specified software profile. """ try: swprofiles = \ SoftwareProfilesDbHandler().\ get_software_profiles_with_component( self.session, 'urb_uge', self.name) return software_profile_name in \ [swprofile.name for swprofile in swprofiles] except ResourceNotFound: return False
def _configure(self, software_profile_name, fd, *args, **kwargs): \ # pylint: disable=unused-argument # # Write config file # with open(CONFIG_FILE, 'w') as fp: print("# File generated by genconfig", file=fp) installer = self.kit_installer.config_manager.getInstaller() for node in NodesDbHandler().getNodeList(self.session): if node.name == installer: continue if node.isIdle: continue if node.state == 'Deleted': continue print(node.name, file=fp) # # Write /etc/netgroup # with open('/etc/netgroup', 'w') as fp: software_profiles = \ SoftwareProfilesDbHandler().getSoftwareProfileList( self.session) for software_profile in software_profiles: if not software_profile.nodes: continue software_profile_node_list = [ node.name for node in software_profile.nodes if node.state != 'Deleted' ] if not software_profile_node_list: continue fp.write('{} {}\n\n'.format( software_profile.name, ' '.join([ '({},,)'.format(node) for node in software_profile_node_list ])))
def activateIdleNode(self, node: Node, softwareProfileName: str, softwareProfileChanged: bool): # pylint: disable=no-self-use if softwareProfileChanged: softwareprofile = \ SoftwareProfilesDbHandler().getSoftwareProfile( self.session, softwareProfileName) # Mark node for network boot if software profile changed node.bootFrom = 0 else: softwareprofile = None self._bhm.writePXEFile( self.session, node, localboot=not softwareProfileChanged, softwareprofile=softwareprofile )
def test_validate_start_arguments(os_obj_factory_mock, dbm): with dbm.session() as session: swprofile = SoftwareProfilesDbHandler().getSoftwareProfile( session, 'compute') hwprofile = HardwareProfilesDbHandler().getHardwareProfile( session, 'localiron') adapter = Default() adapter.session = session addNodesRequest = {} # all 'default' nodes must have a 'nodeDetails' attribute in # addNodesRequest with pytest.raises(CommandFailed): adapter.validate_start_arguments(addNodesRequest, hwprofile, swprofile)
def test_invalid_host_name_request(os_obj_factory_mock, dbm): """ CommandFailed exception should be raised if attempting to add node to hardware profile with name format set to "*" and without specifying a host name. """ with dbm.session() as session: swprofile = SoftwareProfilesDbHandler().getSoftwareProfile( session, 'compute') hwprofile = HardwareProfilesDbHandler().getHardwareProfile( session, 'localironalt') adapter = Default() adapter.session = session with pytest.raises(CommandFailed): adapter.validate_start_arguments({}, hwprofile, swprofile)
def test_duplicate_host_name(os_obj_factory_mock, dbm): with dbm.session() as session: swprofile = SoftwareProfilesDbHandler().getSoftwareProfile( session, 'compute') hwprofile = HardwareProfilesDbHandler().getHardwareProfile( session, 'localiron') adapter = Default() adapter.session = session addNodesRequest = { 'nodeDetails': [{ 'name': 'compute-01.private', }], } with pytest.raises(NodeAlreadyExists): adapter.validate_start_arguments(addNodesRequest, hwprofile, swprofile)
def activateNode(self, nodespec, softwareProfileName): """ Raises: SoftwareProfileNotFound NodeNotFound TortugaException """ session = DbManager().openSession() try: dbSoftwareProfile = SoftwareProfilesDbHandler().\ getSoftwareProfile(session, softwareProfileName) \ if softwareProfileName else None dbNodes = self.__expand_nodespec(session, nodespec) if not dbNodes: raise NodeNotFound('No nodes matching nodespec [%s]' % (nodespec)) tmp_results = NodesDbHandler().activateNode( session, dbNodes, dbSoftwareProfile) results = self.__process_activateNode_results( tmp_results, softwareProfileName) session.commit() # Schedule a cluster update self.__scheduleUpdate() return results except TortugaException as ex: session.rollback() raise except Exception as ex: session.rollback() self.getLogger().exception('%s' % ex) raise finally: DbManager().closeSession()