def testBindingIPtoOSPort(self): s1 = clusto.get_by_name('bs1') ipm = IPManager('ipman', netmask='255.255.0.0', baseip='10.0.0.1', gateway='10.0.0.1') self.assertRaises(Exception, s1.bind_ip_to_osport, '10.0.0.100', 'eth0', porttype='nic-eth') self.assertRaises(Exception, s1.bind_ip_to_osport, '10.0.0.100', 'eth0', portnum=0) s1.bind_ip_to_osport('10.0.0.100', 'eth0')#, porttype='nic-eth', portnum=1) self.assertEqual(IPManager.get_devices('10.0.0.100'), [s1])
def data(self): ip1 = IPManager('a1', gateway='192.168.1.1', netmask='255.255.255.0', baseip='192.168.1.0') ip2 = IPManager('b1', gateway='10.0.128.1', netmask='255.255.252.0', baseip='10.0.128.0') s = BasicServer('s1')
def update_clusto(trap): ts = strftime('[%Y-%m-%d %H:%M:%S]') if trap['operation'] != 1: return if not trap['mac'].startswith('00'): return switch = IPManager.get_devices(trap['switch']) if not switch: log.warning('Unknown trap source: %s' % trap['switch']) return else: switch = switch[0] if not switch.attrs( key='snmp', subkey='discovery', value=1, merge_container_attrs=True): log.debug( 'key=snmp, subkey=discovery for %s not set to 1, ignoring trap' % switch.name) return rack = switch.parents(clusto_types=['rack'])[0] try: factory = rackfactory.get_factory(rack.name) if not factory: log.warning('Unable to locate rack factory for %s' % rack.name) return except: log.error(format_exc()) return server = switch.get_connected('nic-eth', trap['port']) if not server: servernames = clusto.get_by_name('servernames') clusto.SESSION.clusto_description = 'SNMP allocate new server' driver = factory.get_driver(trap['port']) server = servernames.allocate(driver) log.info('Created new %s on %s port %s: %s' % (driver.__name__, trap['switch'], trap['port'], server.name)) try: clusto.begin_transaction() if not trap['mac'] in server.attr_values( key='bootstrap', subkey='mac', value=trap['mac']): log.debug('Adding bootstrap mac to', server.name) server.add_attr(key='bootstrap', subkey='mac', value=trap['mac']) factory.add_server(server, trap['port']) switch.set_port_attr('nic-eth', trap['port'], 'vlan', trap['vlan']) clusto.SESSION.clusto_description = 'SNMP update MAC and connections on %s' % server.name clusto.commit() except: log.error(format_exc()) clusto.rollback_transaction() log.debug(repr(trap))
class ClusterUsageTest(testbase.ClustoTestBase): """Test managing a cluster create pools, find services, query machine properties, etc. """ def data(self): def createRack(datacenter, rackprefix): try: clusto.begin_transaction() r = APCRack(rackprefix) pwr = PowerTowerXM(rackprefix + '-pwr1', withslave=True) sw = Cisco4948(rackprefix + '-sw1') sw.connect_ports('nic-eth', 48, pwr, 1) pwr.connect_ports('pwr-nema-5', 'aa8', sw, 1) r.insert(pwr, [1, 2, 3, 4]) r.insert(sw, [5]) for i in range(20): s = BasicServer(rackprefix + '-s' + '%02d' % i) r.insert(s, [6 + i]) s.connect_ports('nic-eth', 1, sw, i + 1) s.connect_ports( 'pwr-nema-5', 1, pwr, 'ab'[i / 10 % 2] + 'ab'[i / 5 % 2] + str(i % 5 + 1)) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x return r ds = map(BasicDatacenter, ['dc1', 'dc2', 'dc3']) for num, d in enumerate(ds): for i in range(1): rackname = 'rack-' + str(num) + '%03d' % i r = createRack(d, rackname) d.insert(r) ipmans = [ IPManager('block-' + x, netmask='255.255.0.0', baseip=x) for x in ('10.1.0.0', '10.2.0.0', '10.3.0.0') ] state_pools = map(Pool, ('production', 'development')) type_pools = map(Pool, ('webserver', 'database')) db_group_pools = map(Pool, ('users', 'objects', 'logs')) web_group_pools = map(Pool, ('frontend', 'api', 'admin')) for num, d in enumerate(ds): ipman = ipmans[num] for s in d.contents(clusto_types=[BasicServer], search_children=True): ipman.allocate(s) ipman.allocate(s)
def testAddingIPfromIPManagerWithGateway(self): s1 = clusto.get_by_name('bs1') ipm = IPManager('ipman', netmask='255.255.0.0', baseip='10.0.0.1', gateway='10.0.0.1') s1.add_ip(ipman=ipm) self.assertTrue(s1.has_ip('10.0.0.2'))
def testBindingIPtoOSPort(self): s1 = clusto.get_by_name('bs1') s2 = clusto.get_by_name('bs2') ipm = IPManager('ipman', netmask='255.255.255.0', baseip='192.168.1.0') s1.bind_ip_to_osport('192.168.1.20', 'eth0', porttype='nic-eth', portnum=1)
def testGetIP(self): ip1, ip2, s1 = map(clusto.get_by_name, ['a1', 'b1', 's1']) ip1.allocate(s1) ip2.allocate(s1) self.assertEqual(sorted(IPManager.get_ips(s1)), sorted(['192.168.1.2', '10.0.128.2']))
def get_ip_manager(self, request): if not 'ip' in request.params: return Response(status=400, body='400 Bad Request\nYou must specify an IP\n') try: ipman = IPManager.get_ip_manager(request.params['ip']) except: return Response(status=404, body='404 Not Found\n') return dumps(request, unclusto(ipman))
def get_by_ip(ip): try: return IPManager.get_devices(ip) except: return get_entities(attrs=[{ 'key': 'ip', 'subkey': 'ipstring', 'value': ip }])
def update_clusto(trap): ts = strftime("[%Y-%m-%d %H:%M:%S]") if trap["operation"] != 1: return if not trap["mac"].startswith("00"): return switch = IPManager.get_devices(trap["switch"]) if not switch: log.warning("Unknown trap source: %s" % trap["switch"]) return else: switch = switch[0] if not switch.attrs(key="snmp", subkey="discovery", value=1, merge_container_attrs=True): log.debug("key=snmp, subkey=discovery for %s not set to 1, ignoring trap" % switch.name) return rack = switch.parents(clusto_types=["rack"])[0] try: factory = rackfactory.get_factory(rack.name) if not factory: log.warning("Unable to locate rack factory for %s" % rack.name) return except: log.error(format_exc()) return server = switch.get_connected("nic-eth", trap["port"]) if not server: servernames = clusto.get_by_name("servernames") clusto.SESSION.clusto_description = "SNMP allocate new server" driver = factory.get_driver(trap["port"]) server = servernames.allocate(driver) log.info("Created new %s on %s port %s: %s" % (driver.__name__, trap["switch"], trap["port"], server.name)) try: clusto.begin_transaction() if not trap["mac"] in server.attr_values(key="bootstrap", subkey="mac", value=trap["mac"]): log.debug("Adding bootstrap mac to", server.name) server.add_attr(key="bootstrap", subkey="mac", value=trap["mac"]) factory.add_server(server, trap["port"]) switch.set_port_attr("nic-eth", trap["port"], "vlan", trap["vlan"]) clusto.SESSION.clusto_description = "SNMP update MAC and connections on %s" % server.name clusto.commit() except: log.error(format_exc()) clusto.rollback_transaction() log.debug(repr(trap))
def testBindingIPtoOSPort(self): s1 = clusto.get_by_name('bs1') ipm1 = IPManager('ipman1', netmask='255.255.255.0', baseip='10.0.0.1', gateway='10.0.0.1') ipm2 = IPManager('ipman2', netmask='255.255.255.0', baseip='10.0.1.1', gateway='10.0.1.1') self.assertRaises(Exception, s1.bind_ip_to_osport, '10.0.0.100', 'eth0', porttype='nic-eth') self.assertRaises(Exception, s1.bind_ip_to_osport, '10.0.0.100', 'eth0', portnum=0) s1.bind_ip_to_osport('10.0.0.100', 'eth0')#, porttype='nic-eth', portnum=1) self.assertEqual(IPManager.get_devices('10.0.0.100'), [s1]) self.assertRaises(Exception, s1.bind_ip_to_osport, '10.0.1.100', 'eth1', porttype='nic-eth') self.assertRaises(Exception, s1.bind_ip_to_osport, '10.0.1.100', 'eth1', portnum=1) s1.bind_ip_to_osport('10.0.1.100', 'eth1')#, porttype='nic-eth', portnum=2) self.assertEqual(IPManager.get_devices('10.0.1.100'), [s1])
def testAddingIP(self): s1 = clusto.get_by_name('bs1') self.assertRaises(ResourceException, s1.add_ip, '10.0.0.100') ipm = IPManager('ipman', netmask='255.255.0.0', baseip='10.0.0.1') s1.add_ip('10.0.0.100') self.assertTrue(s1.has_ip('10.0.0.100')) s1.add_ip(ipman=ipm) self.assertTrue(s1.has_ip('10.0.0.1'))
def testGet(self): s1 = BasicServer('s1') s2 = BasicServer('s2') s3 = BasicServer('s3') ipm = IPManager('testnet', baseip='10.0.0.0', netmask='255.255.255.0') s1.set_attr(key='system', subkey='serial', value='P0000000000') s2.set_port_attr('nic-eth', 1, 'mac', '00:11:22:33:44:55') s3.bind_ip_to_osport('10.0.0.1', 'eth0') self.assertEqual(clusto.get('s1')[0], s1) self.assertEqual(clusto.get('00:11:22:33:44:55')[0], s2) self.assertEqual(clusto.get('10.0.0.1')[0], s3) self.assertEqual(clusto.get('P0000000000')[0], s1) self.assertEqual(clusto.get('foo'), None) self.assertRaises(ValueError, clusto.get, None)
def testGettingByIP(self): s1 = clusto.get_by_name('bs1') ipm = IPManager('ipman', netmask='255.255.0.0', baseip='10.0.0.1') s1.add_ip(ipman=ipm) self.assertTrue(clusto.get_by_ip('10.0.0.1'), [s1])
def testGetIPManager(self): ip1, ip2 = map(clusto.get_by_name, ['a1', 'b1']) self.assertEqual(ip1, IPManager.get_ip_manager('192.168.1.23')) self.assertEqual(ip2, IPManager.get_ip_manager('10.0.129.22'))
def testGetIPManagers(self): ip3, ip4 = map(clusto.get_by_name, ['c1', 'c2']) self.assertEqual([ip3, ip4], IPManager.get_ip_managers('172.16.40.2')) self.assertEqual([ip4], IPManager.get_ip_managers('172.16.0.2')) self.assertEqual([], IPManager.get_ip_managers('192.168.40.1'))
def testSettingUpServer(self): from clusto.drivers import SimpleEntityNameManager servernames = SimpleEntityNameManager('servernames', basename='server', digits=4) newserver = servernames.allocate(BasicServer) sw = clusto.get_by_name('sw1') p1 = clusto.get_by_name('p1') r = clusto.get_by_name('r1') self.assertEqual('server0001', newserver.name) self.assertRaises(TypeError, r.insert, newserver, 1) r.insert(newserver, 2) p1.connect_ports('pwr-nema-5', 1, newserver, 1) sw.connect_ports('nic-eth', 1, newserver, 1) sw.connect_ports('nic-eth', 3, p1, 1) self.assertEqual(BasicRack.get_rack_and_u(newserver)['rack'], r) ipman = IPManager('subnet-10.0.0.1', netmask='255.255.255.0', baseip='10.0.0.1') newserver.bind_ip_to_osport('10.0.0.2', 'eth0', porttype='nic-eth', portnum=1) ipvals = newserver.attrs(value='10.0.0.2') self.assertEqual(len(ipvals), 1) self.assertEqual(ipvals[0].value, '10.0.0.2') self.assertEqual(clusto.get_by_attr('ip', '10.0.0.2'), [newserver]) aserver = servernames.allocate(BasicServer) ipattr = ipman.allocate(aserver) aserver.bind_ip_to_osport(ipattr.value, 'eth0', porttype='nic-eth', portnum=1) ip = aserver.attr_values(ipattr.key, number=ipattr.number, subkey=ipattr.subkey) self.assertEqual(aserver.get_port_attr('nic-eth', 1, 'osportname'), 'eth0') self.assertEqual(len(aserver.attrs(subkey='osportname', value='eth0')), 2) self.assertEqual( aserver.attrs( IPManager._attr_name, subkey='ipstring', number=aserver.attrs(IPManager._attr_name, subkey='osportname', value='eth0')[0].number, )[0].value, '10.0.0.1') ipattr2 = ipman.allocate(aserver) self.assertEqual(sorted(aserver.get_ips()), sorted(['10.0.0.1', '10.0.0.3']))
def testSettingUpServer(self): from clusto.drivers import SimpleEntityNameManager servernames = SimpleEntityNameManager('servernames', basename='server', digits=4 ) newserver = servernames.allocate(BasicServer) sw = clusto.get_by_name('sw1') p1 = clusto.get_by_name('p1') r = clusto.get_by_name('r1') self.assertEqual('server0001', newserver.name) self.assertRaises(TypeError, r.insert, newserver, 1) r.insert(newserver,2) p1.connect_ports('pwr-nema-5', 1, newserver, 1) sw.connect_ports('nic-eth', 1, newserver, 1) sw.connect_ports('nic-eth', 3, p1, 1) self.assertEqual(BasicRack.get_rack_and_u(newserver)['rack'], r) ipman = IPManager('subnet-10.0.0.1', netmask='255.255.255.0', baseip='10.0.0.1') newserver.bind_ip_to_osport('10.0.0.2', 'eth0', porttype='nic-eth', portnum=1) ipvals = newserver.attrs(value='10.0.0.2') self.assertEqual(len(ipvals), 1) self.assertEqual(ipvals[0].value, '10.0.0.2') self.assertEqual(clusto.get_by_attr('ip', '10.0.0.2'), [newserver]) aserver = servernames.allocate(BasicServer) ipattr = ipman.allocate(aserver) aserver.bind_ip_to_osport(ipattr.value, 'eth0', porttype='nic-eth', portnum=1) ip = aserver.attr_values(ipattr.key, number=ipattr.number, subkey=ipattr.subkey) self.assertEqual(aserver.get_port_attr('nic-eth', 1, 'osportname'), 'eth0') self.assertEqual(len(aserver.attrs(subkey='osportname', value='eth0')), 2) self.assertEqual(aserver.attrs(IPManager._attr_name, subkey='ipstring', number=aserver.attrs(IPManager._attr_name, subkey='osportname', value='eth0')[0].number, )[0].value, '10.0.0.1') ipattr2 = ipman.allocate(aserver) self.assertEqual(sorted(aserver.get_ips()), sorted(['10.0.0.1', '10.0.0.3']))