示例#1
0
    def setNodeForNetworkBoot(self, session: Session, dbNode: Node) -> None:         \
        # pylint: disable=unused-argument

        # Update node status to "Expired" and boot from network
        dbNode.state = state.NODE_STATE_EXPIRED
        dbNode.bootFrom = 0

        self.deletePuppetNodeCert(dbNode.name)
示例#2
0
    def createNewNode(self,
                      session: Session,
                      addNodeRequest: dict,
                      dbHardwareProfile: HardwareProfileModel,
                      dbSoftwareProfile: Optional[SoftwareProfileModel] = None,
                      validateIp: bool = True,
                      bGenerateIp: bool = True,
                      dns_zone: Optional[str] = None) -> NodeModel:
        """
        Convert the addNodeRequest into a Nodes object

        Raises:
            NicNotFound
        """

        self._logger.debug(
            'createNewNode(): session=[%s], addNodeRequest=[%s],'
            ' dbHardwareProfile=[%s], dbSoftwareProfile=[%s],'
            ' validateIp=[%s], bGenerateIp=[%s]' %
            (id(session), addNodeRequest, dbHardwareProfile.name,
             dbSoftwareProfile.name if dbSoftwareProfile else '(none)',
             validateIp, bGenerateIp))

        hostname = addNodeRequest['name'] \
            if 'name' in addNodeRequest else None

        # Ensure no conflicting options (ie. specifying host name for
        # hardware profile in which host names are generated)
        self.__validateHostName(hostname, dbHardwareProfile.nameFormat)

        node = NodeModel(name=hostname)

        if 'rack' in addNodeRequest:
            node.rack = addNodeRequest['rack']

        node.addHostSession = addNodeRequest['addHostSession']

        # Complete initialization of new node record
        nic_defs = addNodeRequest['nics'] \
            if 'nics' in addNodeRequest else []

        AddHostServerLocal().initializeNode(session,
                                            node,
                                            dbHardwareProfile,
                                            dbSoftwareProfile,
                                            nic_defs,
                                            bValidateIp=validateIp,
                                            bGenerateIp=bGenerateIp,
                                            dns_zone=dns_zone)

        # Set hardware profile of new node
        node.hardwareprofile = dbHardwareProfile

        node.softwareprofile = dbSoftwareProfile

        # Return the new node
        return node
示例#3
0
    def initializeNode(self,
                       session: Session,
                       dbNode: Node,
                       dbHardwareProfile: HardwareProfile,
                       dbSoftwareProfile: Optional[SoftwareProfile],
                       nic_defs: List[dict],
                       bValidateIp: bool = True,
                       bGenerateIp: bool = True,
                       dns_zone: Optional[str] = None) -> None:         \
            # pylint: disable=unused-argument
        """
        Assigns hostname and IP address, and inserts new record into
        Node table.

        Raises:
            InvalidArgument
            NodeAlreadyExists
        """

        # Do not attempt to validate IP addresses for hardware profiles
        # representing remote nodes.
        bValidateIp &= dbHardwareProfile.location != 'remote'

        if dbHardwareProfile.location != 'remote' and \
                not dbHardwareProfile.hardwareprofilenetworks:
            raise NetworkNotFound(
                'Hardware profile [{}] does not have a provisioning'
                ' network'.format(dbHardwareProfile.name))

        try:
            if not dbNode.name:
                # Generate unique name for new node
                dbNode.name = self.generate_node_name(
                    session,
                    dbHardwareProfile.nameFormat,
                    rackNumber=dbNode.rack,
                    dns_zone=dns_zone)

            # Create NIC entries
            dbNode.nics = self._initializeNics(session,
                                               dbNode,
                                               dbHardwareProfile,
                                               nic_defs,
                                               bValidateIp=bValidateIp,
                                               bGenerateIp=bGenerateIp)

            self._logger.debug('initializeNode(): initialized new node [%s]' %
                               (dbNode.name))
        except Exception:
            with session_nodes_lock:
                self.clear_session_node(dbNode)

            raise
示例#4
0
    def test_simple_validate_delete_nodes_request(
            self, get_os_object_factory_mock): \
            # pylint: disable=unused-argument
        """
        Simple delete of multiple nodes in same software profile
        """

        swprofile = SoftwareProfile(name='swprofile1', lockedState='Unlocked')

        nodes = [
            Node(name='compute-01', softwareprofile=swprofile),
            Node(name='compute-02', softwareprofile=swprofile),
        ]

        NodeManager()._NodeManager__validate_delete_nodes_request(nodes, False)
示例#5
0
def test_addnode(dbm):
    node = Node(name='mike')

    with dbm.session() as session:
        session.add(node)

        session.commit()
    def _simulate_state_changes(self, node: Node, final_state: str):
        """
        Simulate the node transitioning through multiple states by firing
        state change events.

        :param Node node:       the node to transition through the state
                                changes
        :param str final_state: the final state the node must reach in the
                                simulation

        """
        initial_state_found = False

        for state in self.STATE_TRANSITIONS:
            #
            # Find the current node state in the list of transitions
            #
            if not initial_state_found:
                if state == node.state:
                    initial_state_found = True
                continue

            #
            # Fire a state change event to get to the next state
            #
            previous_state = node.state
            node.state = state
            self.fire_state_change_event(node, previous_state)

            #
            # If this is the final state, exit the loop
            #
            if state == final_state:
                break
    def test_validateNode(self):
        osFamilyInfo = OsFamilyInfo('rhel', '7', 'x86_64')

        osSupport = OSSupport(osFamilyInfo)

        node = Node()

        with pytest.raises(NodeNotFound):
            osSupport._OSSupport__validate_node(node)
示例#8
0
    def test_validate_delete_nodes_request_alt(
            self, get_os_object_factory_mock): \
            # pylint: disable=unused-argument
        """
        Simple delete of multiple nodes with one profile locked and one not
        """

        swprofile1 = SoftwareProfile(name='swprofile1', lockedState='Unlocked')
        swprofile2 = SoftwareProfile(name='swprofile1', lockedState='SoftLocked')

        nodes = [
            Node(name='compute-01', softwareprofile=swprofile1),
            Node(name='compute-02', softwareprofile=swprofile2),
        ]

        with pytest.raises(OperationFailed):
            NodeManager()._NodeManager__validate_delete_nodes_request(
                nodes, False)
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, [])
示例#10
0
def primeDb(session: Session, settings: Dict[str, Any]):
    """
    Prime database with initial data
    """

    # Create node entry for installer
    node = Node(name=settings['fqdn'])
    node.state = state.NODE_STATE_INSTALLED
    node.lockedState = 'HardLocked'
    node.lastUpdate = time.strftime('%Y-%m-%d %H:%M:%S',
                                    time.localtime(time.time()))
    node.bootFrom = 1
    node.isIdle = False

    # Create Installer Software Profile
    node.softwareprofile = SoftwareProfile(
        name=settings['installer_software_profile'],
        description='Installer software profile',
        type='installer',
        minNodes=1,
        maxNodes=1,
        lockedState='HardLocked',
    )
    node.softwareprofile.os = OperatingSystem(
        name=settings['osInfo'].getName(),
        version=settings['osInfo'].getVersion(),
        arch=settings['osInfo'].getArch())

    node.softwareprofile.os.family = OperatingSystemFamily(
        name=settings['osInfo'].getOsFamilyInfo().getName(),
        version=settings['osInfo'].getOsFamilyInfo().getVersion(),
        arch=settings['osInfo'].getOsFamilyInfo().getArch())

    # Create Installer Hardware Profile
    node.hardwareprofile = HardwareProfile(
        name=settings['installer_hardware_profile'])
    node.hardwareprofile.description = 'Installer hardware profile'
    node.hardwareprofile.nameFormat = 'installer'
    node.hardwareprofile.installType = 'package'
    node.hardwareprofile.setLocation = 'local'
    node.hardwareprofile.mappedsoftwareprofiles.append(node.softwareprofile)

    session.add(node)
示例#11
0
    def test_simple_validate_delete_nodes_request_alt_with_force(
            self, get_os_object_factory_mock): \
            # pylint: disable=unused-argument
        """
        Delete from soft locked software profile with force
        """

        nodes = [
            Node(name='compute-01',
                softwareprofile=SoftwareProfile(name='swprofile1',
                                                lockedState='SoftLocked')),
        ]

        NodeManager()._NodeManager__validate_delete_nodes_request(nodes, True)
    def __initialize_node(self, name, db_hardware_profile,
                          db_software_profile):
        node = Node(name=name)
        node.softwareprofile = db_software_profile
        node.hardwareprofile = db_hardware_profile
        node.isIdle = False
        node.addHostSession = self.addHostSession

        return node
示例#13
0
    def test_simple_validate_delete_nodes_request_alt2(
            self, get_os_object_factory_mock): \
            # pylint: disable=unused-argument
        """
        Delete from hard locked software profile
        """

        nodes = [
            Node(name='compute-01',
                softwareprofile=SoftwareProfile(name='swprofile1',
                                                lockedState='HardLocked')),
        ]

        with pytest.raises(OperationFailed):
            NodeManager()._NodeManager__validate_delete_nodes_request(
                nodes, False)
示例#14
0
    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_generate_node_name(dbm):
    name_format = 'compute-#NN'

    with dbm.session() as session:
        name1 = api.generate_node_name(session, name_format)

        # fixture sets up compute-01..compute-10; next generated name
        # must be 'compute-11'
        assert name1 == 'compute-11'

        name2 = api.generate_node_name(session, name_format)

        assert name1 != name2, 'failed to generate unique node name'

        api.clear_session_node(Node(name=name1))

        name3 = api.generate_node_name(session, name_format)

        assert name1 == name3
def test_initializeNode(dbm):
    with dbm.session() as session:
        hardware_profile = HardwareProfilesDbHandler().getHardwareProfile(
            session, 'localiron')

        software_profile = SoftwareProfilesDbHandler().getSoftwareProfile(
            session, 'compute')

        node = Node()

        nic_defs = [{
            'mac': '00:00:00:00:00:01',
        }]

        api.initializeNode(session, node, hardware_profile, software_profile,
                           nic_defs)

        assert node.name
        assert node.nics and len(node.nics) == 1 and \
            node.nics[0].mac == '00:00:00:00:00:01' and \
            node.nics[0].boot
    def start(self, addNodesRequest: dict, dbSession: Session,
              dbHardwareProfile: HardwareProfile,
              dbSoftwareProfile: Optional[SoftwareProfile] = None):
        """
        Create nodes

        """
        #
        # Load resource adapter settings
        #
        config = self.getResourceAdapterConfig(
            sectionName=addNodesRequest.get(
                'resource_adapter_configuration', 'default')
        )

        nodes = []

        for _ in range(addNodesRequest['count']):
            random_host_name_suffix = get_random_host_name_suffix()

            node = Node(name='compute-{}'.format(random_host_name_suffix))
            node.softwareprofile = dbSoftwareProfile
            node.hardwareprofile = dbHardwareProfile
            node.isIdle = False
            node.state = self.STATE_TRANSITIONS[0]

            # create dummy nic
            nic = Nic(boot=True, ip=generate_fake_ip())

            node.nics.append(nic)

            self._simulate_state_changes(
                node,
                config.get('state', self.settings['state'].default)
            )

            nodes.append(node)

        return nodes
示例#18
0
def get_node():
    node = Node(name='compute-01.private')
    node.nics = []

    return node
示例#19
0
def dbm():
    dbmgr = DbManager(create_engine('sqlite:///:memory:', echo=False))

    dbmgr.init_database()

    rhel7_os_family_info = osFamilyInfo.OsFamilyInfo('rhel', '7', 'x86_64')

    os_info = osInfo.OsInfo('centos', '7.4', 'x86_64')
    os_info.setOsFamilyInfo(rhel7_os_family_info)

    settings = {
        'language': 'en',
        'keyboard': 'en_US',
        'timezone': 'UTC',
        'utc': 'true',
        'intWebPort': '8008',
        'intWebServicePort': '8444',
        'adminPort': '8443',
        'eulaAccepted': 'true',
        'depotpath': '/opt/tortuga/depot',
        'osInfo': os_info,
        'fqdn': getfqdn(),
        'installer_software_profile': 'Installer',
        'installer_hardware_profile': 'Installer',
    }

    with dbmgr.session() as session:
        primeDb(session, settings)

        init_global_parameters(session, settings)

        # create sample tags
        all_tags = []
        for idx in range(1, 5 + 1):
            tag = dict(
                name='tag{:d}'.format(idx),
                value='value{:d}'.format(idx),
            )
            all_tags.append(tag)

        installer_node = session.query(Node).filter(
            Node.name == settings['fqdn']).one()

        os_ = session.query(OperatingSystem).filter(
            OperatingSystem.name == 'centos').one()

        rhel7_os_family = session.query(OperatingSystemFamily).filter(
            OperatingSystemFamily.name == 'rhel').one()

        # add add'l operating system/family
        rhel75_os = OperatingSystem(name='rhel', version='7.5', arch='x86_64')
        rhel75_os.family = rhel7_os_family

        session.add(rhel75_os)

        admin = Admin(username='******',
                      password=pbkdf2_sha256.hash('password'),
                      realname='realname',
                      description='description')

        session.add(admin)

        eth0_network_device = NetworkDevice(name='eth0')

        eth1_network_device = NetworkDevice(name='eth1')

        # Add dummy provisioning network
        network = Network(address='10.2.0.0',
                          netmask='255.255.255.0',
                          name='Provisioning network on eth1',
                          type='provision')

        installer_node.hardwareprofile.hardwareprofilenetworks.append(
            HardwareProfileNetwork(
                network=network,
                networkdevice=eth1_network_device,
            ))

        # create nic on installer
        installer_nic = Nic(
            ip='10.2.0.1',
            network=network,
            networkdevice=eth1_network_device,
        )

        installer_node.nics = [installer_nic]

        # create 'base' kit
        kit = Kit()
        kit.name = 'base'
        kit.version = '7.1.0'
        kit.iteration = '0'
        kit.description = 'Sample base kit'

        installer_component = Component(name='installer', version='7.0')
        installer_component.family = [rhel7_os_family]
        installer_component.kit = kit

        core_component = Component(name='core',
                                   version='7.0',
                                   description='Compute component')
        core_component.family = [rhel7_os_family]
        core_component.kit = kit

        # add component not enabled by default
        pdsh_component = Component(name='pdsh',
                                   version='7.0',
                                   description='pdsh component')
        pdsh_component.family = [rhel7_os_family]
        pdsh_component.kit = kit

        # add fake dhcp component
        dhcpd_component = Component(name='dhcpd',
                                    version='7.0',
                                    description='Mock dhcpd component')
        dhcpd_component.family = [rhel7_os_family]
        dhcpd_component.kit = kit

        session.add(kit)

        # create OS kit
        os_kit = Kit(name='centos', version='7.4', iteration='0')
        os_kit.isOs = True
        os_component = Component(name='centos-7.4-x86_64', version='7.4')
        os_component.os = [os_]
        os_component.kit = os_kit
        os_kit.components.append(os_component)

        session.add(os_kit)

        # create resource adapter kit
        ra_kit = Kit(name='awsadapter', version='0.0.1', iteration='0')
        ra_component = Component(name='management', version='0.0.1')
        ra_component.family.append(rhel7_os_family)
        ra_kit.components.append(ra_component)

        installer_node.softwareprofile.components.append(ra_component)
        installer_node.softwareprofile.components.append(installer_component)
        installer_node.softwareprofile.components.append(dhcpd_component)

        # create 'default' resource adapter
        default_adapter = ResourceAdapter(name='default', kit=kit)

        # create resource adapter
        aws_adapter = ResourceAdapter(name='aws', kit=ra_kit)

        aws_adapter_cfg = ResourceAdapterConfig(
            name='default',
            description='Example default resource adapter configuration')

        aws_adapter_cfg.configuration.append(
            ResourceAdapterSetting(key='ami', value='ami-XXXXXX'))

        aws_adapter_cfg.configuration.append(
            ResourceAdapterSetting(key='use_instance_hostname', value='true'))

        aws_adapter.resource_adapter_config.append(aws_adapter_cfg)

        # add second resource adapter configuration
        aws_adapter_cfg2 = ResourceAdapterConfig(name='nondefault',
                                                 admin=admin)
        aws_adapter_cfg2.configuration.append(
            ResourceAdapterSetting(key='another_key', value='another_value'))

        aws_adapter.resource_adapter_config.append(aws_adapter_cfg2)

        session.add(aws_adapter)

        # create 'aws' hardware profile
        # does *not* have a default resource adapter config
        aws_hwprofile = HardwareProfile(name='aws')
        aws_hwprofile.location = 'remote'
        aws_hwprofile.resourceadapter = aws_adapter
        aws_hwprofile.nameFormat = '*'

        session.add(aws_hwprofile)

        # add hardware profile 'aws2' with non-default configuration profile
        aws_hwprofile2 = HardwareProfile(
            name='aws2',
            location='remote',
            resourceadapter=aws_adapter,
            default_resource_adapter_config=aws_adapter_cfg2,
            nameFormat='*',
        )

        session.add(aws_hwprofile2)

        # create 'compute' software profile
        compute_swprofile = SoftwareProfile(name='compute')
        compute_swprofile.os = os_
        compute_swprofile.components = [core_component]
        compute_swprofile.type = 'compute'

        # create 'compute2' software profile
        compute2_swprofile = SoftwareProfile(name='compute2',
                                             os=os_,
                                             components=[core_component],
                                             type='compute')

        # map 'aws' and 'aws2' to 'compute'
        compute_swprofile.hardwareprofiles.extend(
            (aws_hwprofile, aws_hwprofile2))

        # create 'localiron' hardware profile
        localiron_hwprofile = HardwareProfile(
            name='localiron',
            nameFormat='compute-#NN',
            location='local',
        )
        localiron_hwprofile.resourceadapter = default_adapter
        localiron_hwprofile.mappedsoftwareprofiles = [
            compute_swprofile, compute2_swprofile
        ]

        localiron_hwprofile.hardwareprofilenetworks.append(
            HardwareProfileNetwork(
                network=network,
                networkdevice=eth0_network_device,
            ))

        session.add(localiron_hwprofile)

        # create "localironalt" hardware profile with nameFormat set to '*'
        localironalt_hwprofile = HardwareProfile(
            name='localironalt',
            nameFormat='*',
            location='local',
        )
        localironalt_hwprofile.resourceadapter = default_adapter
        localironalt_hwprofile.mappedsoftwareprofiles = [
            compute_swprofile, compute2_swprofile
        ]

        localironalt_hwprofile.hardwareprofilenetworks.append(
            HardwareProfileNetwork(
                network=network,
                networkdevice=eth0_network_device,
            ))

        # create 'nonetwork' hardware profile
        nonetwork_hwprofile = HardwareProfile(name='nonetwork')
        nonetwork_hwprofile.resourceadapter = default_adapter
        nonetwork_hwprofile.mappedsoftwareprofiles.append(compute_swprofile)

        # create compute (compute-01, compute-02, ...) nodes
        for n in range(1, 11):
            compute_node = Node(name='compute-{0:02d}.private'.format(n),
                                state='Installed')
            compute_node.addHostSession = '1234'
            compute_node.softwareprofile = compute_swprofile
            compute_node.hardwareprofile = localiron_hwprofile

            compute_node.nics.append(
                Nic(ip='10.2.0.{}'.format(100 + n),
                    mac='FF:00:00:00:00:00:{:02x}'.format(100 + n),
                    boot=True,
                    network=network,
                    networkdevice=eth0_network_device))

            if n in (1, 2):
                # compute-01 and compute-02 have all tags
                for tag in all_tags:
                    compute_node.tags.append(
                        NodeTag(name=tag['name'], value=tag['value']))
            elif n in (3, 4):
                # compute-03 and compute-04 have 'tag1' and 'tag2'
                compute_node.tags.append(
                    NodeTag(name=all_tags[0]['name'],
                            value=all_tags[0]['value']))
                compute_node.tags.append(
                    NodeTag(name=all_tags[1]['name'],
                            value=all_tags[1]['value']))
            elif n in (5, 6):
                # compute-05 and compute-06 have 'tag2' and 'tag3'
                compute_node.tags.append(
                    NodeTag(name=all_tags[1]['name'],
                            value=all_tags[1]['value']))
                compute_node.tags.append(
                    NodeTag(name=all_tags[2]['name'],
                            value=all_tags[2]['value']))
            elif n == 7:
                # compute-07 has 'tag4'
                compute_node.tags.append(
                    NodeTag(name=all_tags[3]['name'],
                            value=all_tags[3]['value']))
            elif n == 8:
                # compute-08 has 'tag5'
                compute_node.tags.append(
                    NodeTag(name=all_tags[4]['name'],
                            value=all_tags[4]['value']))

            session.add(compute_node)

        # create arbitrary hardware profiles
        hwprofile1 = HardwareProfile(name='profile1',
                                     nameFormat='*',
                                     tags=[
                                         HardwareProfileTag(
                                             name=all_tags[0]['name'],
                                             value=all_tags[0]['value'])
                                     ])
        hwprofile2 = HardwareProfile(name='profile2',
                                     nameFormat='*',
                                     tags=[
                                         HardwareProfileTag(
                                             name=all_tags[1]['name'],
                                             value=all_tags[1]['value'])
                                     ])
        hwprofile_notags = HardwareProfile(name='notags', nameFormat='*')

        session.add(hwprofile1)
        session.add(hwprofile2)
        session.add(hwprofile_notags)

        # create arbitrary software profiles
        swprofile1 = SoftwareProfile(name='swprofile1',
                                     os=os_,
                                     type='compute',
                                     tags=[
                                         SoftwareProfileTag(
                                             name=all_tags[0]['name'],
                                             value=all_tags[0]['value'])
                                     ])
        swprofile2 = SoftwareProfile(name='swprofile2',
                                     os=os_,
                                     type='compute',
                                     tags=[
                                         SoftwareProfileTag(
                                             name=all_tags[1]['name'],
                                             value=all_tags[1]['value'])
                                     ])
        swprofile_notags = SoftwareProfile(name='notags',
                                           os=os_,
                                           type='compute')

        session.add(swprofile1)
        session.add(swprofile2)
        session.add(swprofile_notags)

        session.commit()

    return dbmgr
示例#20
0
def dbm():
    dbmgr = DbManager(create_engine('sqlite:///:memory:', echo=False))

    dbmgr.init_database()

    rhel7_os_family_info = osFamilyInfo.OsFamilyInfo('rhel', '7', 'x86_64')

    os_info = osInfo.OsInfo('centos', '7.4', 'x86_64')
    os_info.setOsFamilyInfo(rhel7_os_family_info)

    installer_fqdn = getfqdn()

    settings = {
        'language': 'en',
        'keyboard': 'en_US',
        'timezone': 'UTC',
        'utc': 'true',
        'intWebPort': '8008',
        'intWebServicePort': '8444',
        'adminPort': '8443',
        'eulaAccepted': 'true',
        'depotpath': '/opt/tortuga/depot',
        'osInfo': os_info,
        'fqdn': installer_fqdn,
        'installer_software_profile': 'Installer',
        'installer_hardware_profile': 'Installer',
    }

    with dbmgr.session() as session:
        primeDb(session, settings)

        init_global_parameters(session, settings)

        # create sample tags
        all_tags = [{
            'name': 'tag{:d}'.format(idx),
            'value': 'value{:d}'.format(idx)
        } for idx in range(1, 5 + 1)]

        installer_node = session.query(Node).filter(
            Node.name == installer_fqdn).one()

        os_ = session.query(OperatingSystem).filter(
            OperatingSystem.name == 'centos').one()

        rhel7_os_family = session.query(OperatingSystemFamily).filter(
            OperatingSystemFamily.name == 'rhel').one()

        # add add'l operating system/family
        rhel75_os = OperatingSystem(name='rhel', version='7.5', arch='x86_64')
        rhel75_os.family = rhel7_os_family

        session.add(rhel75_os)

        admin = Admin(username='******',
                      password=pbkdf2_sha256.hash('password'),
                      realname='realname',
                      description='description')

        session.add(admin)

        eth1_network_device = NetworkDevice(name='eth1')

        # Add dummy provisioning network
        network = Network()
        network.address = '10.2.0.0'
        network.netmask = '255.255.255.0'
        network.name = 'Provisioning network on eth1'
        network.type = 'provision'

        session.add(network)

        # create 'hardwareprofilenetwork' entry
        hwpn1 = HardwareProfileNetwork(
            hardwareprofile=installer_node.hardwareprofile,
            network=network,
            networkdevice=eth1_network_device)

        # create nic on installer
        installer_nic = Nic()
        installer_nic.ip = '10.2.0.1'
        installer_nic.network = network
        installer_nic.networkdevice = eth1_network_device

        installer_node.nics = [installer_nic]

        # create 'base' kit
        kit = Kit()
        kit.name = 'base'
        kit.version = '7.1.1'
        kit.iteration = '0'
        kit.description = 'Sample base kit'

        installer_component = Component(name='installer', version='7.0')
        installer_component.family = [rhel7_os_family]
        installer_component.kit = kit

        core_component = Component(name='core',
                                   version='7.0',
                                   description='Compute component')
        core_component.family = [rhel7_os_family]
        core_component.kit = kit

        session.add(kit)

        # create OS kit
        os_kit = Kit(name='centos', version='7.4', iteration='0')
        os_kit.isOs = True
        os_component = Component(name='centos-7.4-x86_64', version='7.4')
        os_component.os = [os_]
        os_component.kit = os_kit
        os_kit.components.append(os_component)

        session.add(os_kit)

        # create resource adapter kit
        ra_kit = Kit(name='awsadapter', version='0.0.1', iteration='0')
        ra_component = Component(name='management', version='0.0.1')
        ra_component.family.append(rhel7_os_family)
        ra_kit.components.append(ra_component)

        installer_node.softwareprofile.components.append(ra_component)
        installer_node.softwareprofile.components.append(installer_component)
        session.commit()

        # create 'default' resource adapter
        default_adapter = ResourceAdapter(
            name=DEFAULT_CONFIGURATION_PROFILE_NAME,
            kit=kit,
        )

        # create resource adapter
        aws_adapter = ResourceAdapter(name='AWS')
        aws_adapter.kit = ra_kit

        aws_adapter_cfg = ResourceAdapterConfig(
            name=DEFAULT_CONFIGURATION_PROFILE_NAME,
            description='Example default resource adapter configuration')

        with mock_ec2_deprecated():
            ec2_conn = boto.ec2.connect_to_region('us-east-1')

            amis = ec2_conn.get_all_images()

            aws_adapter_cfg.configuration.append(
                ResourceAdapterSetting(key='ami', value=amis[0].id))

        aws_adapter.resource_adapter_config.append(aws_adapter_cfg)

        # add second resource adapter configuration
        aws_adapter_cfg2 = ResourceAdapterConfig(name='nondefault',
                                                 admin=admin)
        aws_adapter_cfg2.configuration.append(
            ResourceAdapterSetting(key='another_key', value='another_value'))

        session.add(aws_adapter)

        # create 'AWS' hardware profile
        aws_hwprofile = HardwareProfile(name='AWS')
        aws_hwprofile.location = 'remote'
        aws_hwprofile.resourceadapter = aws_adapter

        session.add(aws_hwprofile)

        aws_hwprofile2 = HardwareProfile(name='aws2',
                                         location='remote',
                                         resourceadapter=aws_adapter,
                                         nameFormat='*')

        session.add(aws_hwprofile2)

        # create 'compute' software profile
        compute_swprofile = SoftwareProfile(name='compute')
        compute_swprofile.os = os_
        compute_swprofile.components = [core_component]
        compute_swprofile.type = 'compute'

        # create 'compute2' software profile
        compute2_swprofile = SoftwareProfile(name='compute2',
                                             os=os_,
                                             components=[core_component],
                                             type='compute')

        # map 'AWS' to 'compute'
        aws_hwprofile.mappedsoftwareprofiles.append(compute_swprofile)
        aws_hwprofile2.mappedsoftwareprofiles.append(compute_swprofile)

        # create 'localiron' hardware profile
        localiron_hwprofile = HardwareProfile(name='localiron',
                                              nameFormat='compute-#NN')
        localiron_hwprofile.resourceadapter = default_adapter
        localiron_hwprofile.mappedsoftwareprofiles.append(compute_swprofile)
        localiron_hwprofile.mappedsoftwareprofiles.append(compute2_swprofile)

        localiron_hwprofile.hardwareprofilenetworks.append(hwpn1)

        # create 'nonetwork' hardware profile
        nonetwork_hwprofile = HardwareProfile(name='nonetwork')
        nonetwork_hwprofile.resourceadapter = default_adapter
        nonetwork_hwprofile.mappedsoftwareprofiles.append(compute_swprofile)

        eth0_networkdevice = NetworkDevice(name='eth0')

        # create compute (compute-01, compute-02, ...) nodes
        for n in range(1, 11):
            compute_node = Node(name='compute-{0:02d}.private'.format(n),
                                state='Installed')
            compute_node.addHostSession = '1234'
            compute_node.softwareprofile = compute_swprofile
            compute_node.hardwareprofile = localiron_hwprofile

            compute_node.nics.append(
                Nic(ip='10.2.0.{}'.format(100 + n),
                    mac='FF:00:00:00:00:00:{:02x}'.format(100 + n),
                    boot=True,
                    network=network,
                    networkdevice=eth0_networkdevice))

            if n in (1, 2):
                # compute-01 and compute-02 have all tags
                compute_node.tags = [
                    NodeTag(
                        name=tag['name'],
                        value=tag['value'],
                    ) for tag in all_tags
                ]
            elif n in (3, 4):
                # compute-03 and compute-04 have 'tag1' and 'tag2'
                compute_node.tags = [
                    NodeTag(
                        name=all_tags[0]['name'],
                        value=all_tags[0]['value'],
                    ),
                    NodeTag(
                        name=all_tags[1]['name'],
                        value=all_tags[1]['value'],
                    ),
                ]
            elif n in (5, 6):
                # compute-05 and compute-06 have 'tag2' and 'tag3'
                compute_node.tags = [
                    NodeTag(
                        name=all_tags[1]['name'],
                        value=all_tags[1]['value'],
                    ),
                    NodeTag(
                        name=all_tags[2]['name'],
                        value=all_tags[2]['value'],
                    ),
                ]
            elif n == 7:
                # compute-07 has 'tag4'
                compute_node.tags = [
                    NodeTag(
                        name=all_tags[3]['name'],
                        value=all_tags[3]['value'],
                    ),
                ]
            elif n == 8:
                # compute-08 has 'tag5'
                compute_node.tags = [
                    NodeTag(
                        name=all_tags[4]['name'],
                        value=all_tags[4]['value'],
                    ),
                ]

            session.add(compute_node)

        # create arbitrary aws nodes
        for idx in range(1, 10):
            new_node = Node(
                name='ip-10-10-10-{:0d}.ec2.internal'.format(idx),
                hardwareprofile=aws_hwprofile,
                softwareprofile=compute_swprofile,
            )

            new_node.instance = InstanceMapping(
                instance='i-{:08x}'.format(idx))

            new_node.instance.resource_adapter_configuration = aws_adapter_cfg

            session.add(new_node)

        # create arbitrary hardware profiles
        hwprofile1 = HardwareProfile(
            name='profile1',
            tags=[
                HardwareProfileTag(
                    name=all_tags[0]['name'],
                    value=all_tags[0]['value'],
                ),
            ],
        )
        hwprofile2 = HardwareProfile(
            name='profile2',
            tags=[
                HardwareProfileTag(
                    name=all_tags[1]['name'],
                    value=all_tags[1]['value'],
                ),
            ],
        )

        session.add(hwprofile1)
        session.add(hwprofile2)

        # create arbitrary software profiles
        SoftwareProfile(
            name='swprofile1',
            os=os_,
            type='compute',
            tags=[
                SoftwareProfileTag(name=all_tags[0]['name'],
                                   value=all_tags[0]['value']),
            ],
        )

        SoftwareProfile(
            name='swprofile2',
            os=os_,
            type='compute',
            tags=[
                SoftwareProfileTag(
                    name=all_tags[1]['name'],
                    value=all_tags[1]['value'],
                ),
            ],
        )

        session.commit()

    return dbmgr
示例#21
0
    def setNodeForNetworkBoot(self, session: Session, dbNode: Node):         \
            # pylint: disable=unused-argument

        dbNode.state = state.NODE_STATE_EXPIRED