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 get_by_ip(ip): try: return IPManager.get_devices(ip) except: return get_entities(attrs=[{ 'key': 'ip', 'subkey': 'ipstring', 'value': ip }])
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 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 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))