def initDatabase(self) -> Tuple[Any, Session]: msg = _('Initializing database') self._logger.info(msg) print_('\n' + msg + '... ', end='') # This cannot be a global import since the database configuration # may be set in this script. from tortuga.db.dbManager import DbManager dbm = DbManager() # create database dbm.init_database() session = dbm.openSession() # Prime the database previously created as part of the bootstrap try: dbUtility.primeDb(session, self._settings) dbUtility.init_global_parameters(session, self._settings) print_(_('done')) session.commit() except Exception as exc: # pylint: disable=broad-except session.rollback() print_(_('failed.')) print_(_('Exception raised initializing database:') + ' {0}'.format(exc), file=sys.stderr) self._logger.debug('Done initializing database') return dbm, session
def initDatabase(self): msg = _('Initializing database') self._logger.info(msg) print_('\n' + msg + '... ', end='') # This cannot be a global import since the database configuration # may be set in this script. from tortuga.db.dbManager import DbManager dbm = DbManager() dbm.init_database() # Prime the database previously created as part of the bootstrap with dbm.session() as session: try: dbUtility.primeDb(session, self._settings['fqdn'], self._osInfo, self._settings) dbUtility.init_global_parameters(session, self._settings) print_(_('done')) session.commit() except Exception as exc: session.rollback() print_(_('failed.')) print_(_('Exception raised initializing database:') + ' {0}'.format(exc), file=sys.stderr) self._logger.debug('Done initializing database')
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
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
def dbm(): dbm = DbManagerBase(create_engine('sqlite:///:memory:', echo=False)) dbm.init_database() os_info = osInfo.OsInfo('centos', '7.4', 'x86_64') os_info.setOsFamilyInfo(osFamilyInfo.OsFamilyInfo('rhel', '7', 'x86_64')) settings = { 'language': 'en', 'keyboard': 'en_US', 'timezone': 'UTC', 'utc': 'true', 'intWebPort': '8008', 'intWebServicePort': '8444', 'adminPort': '8443', 'eulaAccepted': 'true', 'depotpath': '/depot', } installer_fqdn = socket.getfqdn() with dbm.session() as session: primeDb(session, installer_fqdn, os_info, settings) init_global_parameters(session, settings) installer_node = session.query(Nodes).filter( Nodes.name == installer_fqdn).one() os_ = session.query(OperatingSystems).filter( OperatingSystems.name == 'centos').one() os_family = session.query(OperatingSystemsFamilies).filter( OperatingSystemsFamilies.name == 'rhel').one() admin = Admins('admin', 'password', 'realname', 'description') session.add(admin) eth1_network_device = NetworkDevices(name='eth1') # Add dummy provisioning network network = Networks() network.address = '10.2.0.0' network.netmask = '255.255.255.0' network.name = 'Provisioning network on eth1' network.type = 'provision' session.add(network) installer_nic = Nics() installer_nic.network = network installer_nic.networkdevice = eth1_network_device installer_node.nics = [installer_nic] kit = Kits() kit.name = 'base' kit.version = '6.3.0' kit.iteration = '0' kit.description = 'Sample base kit' installer_component = Components() installer_component.name = 'installer' installer_component.version = '6.3' installer_component.family = [os_family] installer_component.kit = kit core_component = Components(name='core', version='6.3', description='Compute component') core_component.family = [os_family] core_component.kit = kit compute_swprofile = SoftwareProfiles(name='compute') compute_swprofile.os = os_ compute_swprofile.components = [core_component] compute_swprofile.type = 'compute' localiron_hwprofile = HardwareProfiles(name='localiron') session.add(kit) for n in range(1, 11): compute_node = Nodes('compute-{0:02d}.private'.format(n)) compute_node.softwareprofile = compute_swprofile compute_node.hardwareprofile = localiron_hwprofile session.add(compute_node) session.commit() return dbm