def __init__(self, hostname=None, **kwargs): """Create a new server. hostname: The hostname for this server. """ try: clusto.begin_transaction() if hostname: servers = llclusto.get_by_hostname(hostname) else: servers = [] if len(servers) > 0: raise ValueError( "One or more servers with hostname %s already exist: %s" % (hostname, servers)) super(LindenServer, self).__init__(**kwargs) self.hostname = hostname self.server_class = ServerClass.get_server_class( self._server_class_name) clusto.commit() except: clusto.rollback_transaction() raise
def testTransactionRollback4(self): d1 = Driver('d1') try: clusto.begin_transaction() d2 = Driver('d2') try: clusto.begin_transaction() d2.add_attr('foo', 'bar') clusto.commit() except: clusto.rollback_transaction() d1.add_attr('foo2', 'bar2') raise Exception() clusto.commit() except: clusto.rollback_transaction() self.assertEqual(d1.attrs(), []) self.assertRaises(LookupError, clusto.get_by_name, 'd2')
def delete(self): "Delete self and all references to self." clusto.begin_transaction() try: for i in self.references: i.delete() for i in self.attrs: i.delete() for i in self.counters: i.delete() if SESSION.clusto_versioning_enabled: self.deleted_at_version = working_version() else: SESSION.delete(self) SESSION.flush() clusto.commit() audit_log.info('delete entity %s', self.name) except Exception, x: clusto.rollback_transaction() raise x
def kvm_create(self, options): status, response = self._request('POST', '/api/1/%s' % self.name, { 'memory': options.memory, 'disk': options.disk, }) if status != 200: raise DriverException(response) response = json.loads(response) config = response['config'] try: clusto.begin_transaction() self.set_attr(key='system', subkey='memory', value=config['memory']) self.set_attr(key='system', subkey='disk', value=config['disk']) self.set_attr(key='system', subkey='cpucount', value=1) self.set_attr(key='kvm', subkey='console-port', value=config['console']) self.set_attr(key='kvm', subkey='vnc-port', value=5900 + config['vnc']) self.set_port_attr('nic-eth', 1, 'mac', config['mac']) self.set_port_attr('nic-eth', 1, 'model', config['nic']) clusto.SESSION.clusto_description = 'Populate KVM information for %s' % self.name clusto.commit() except: sys.stderr.write(format_exc() + '\n') clusto.rollback_transaction()
def deallocate(self, thing, resource=(), number=True): """deallocates a resource from the given thing.""" clusto.begin_transaction() try: if resource is (): for res in self.resources(thing): res_mgr = self.get_resource_manager(res) if res_mgr != self: continue thing.del_attrs(self._attr_name, number=res.number) elif resource and not self.available(resource, number): resource, number = self.ensure_type(resource, number) try: number = self.get_resource_number(thing, resource) except ResourceException, e: clusto.rollback_transaction() raise res = thing.attrs(self._attr_name, self, subkey='manager', number=number) for a in res: thing.del_attrs(self._attr_name, number=a.number) clusto.commit()
def testSetAttrIncrementsVersion(self): curver = clusto.get_latest_version_number() d = clusto.drivers.Driver('d1') self.assertEqual(curver + 1, clusto.get_latest_version_number()) Attribute(d.entity, 'cat', 'baz') clusto.begin_transaction() SESSION.clusto_description = "TEST" #Attribute(d.entity, 'foo', 'cat') #SESSION.add(Attribute(d.entity, 'coo', 'daa')) Attribute.query().all() a = Attribute(d.entity, 'foo', 'bar') clusto.commit() #clusto.rollback_transaction() #self.assertEqual(['bar', 'bar'], d.attr_values('foo')) self.assert_('bar' in d.attr_values('foo')) self.assertEqual(curver + 3, clusto.get_latest_version_number())
def testEmptyCommits(self): server = clusto.drivers.BasicServer('s1') curver = clusto.get_latest_version_number() server.attrs() self.assertEqual(curver, clusto.get_latest_version_number()) try: clusto.begin_transaction() SESSION.clusto_description = "TEST" Entity.query().all() clusto.commit() except: clusto.rollback_transaction() self.assertEqual(curver, clusto.get_latest_version_number()) self.assertEqual([], server.attr_values('foo'))
def allocate(self, clustotype, resource=None, number=True): """allocates a resource element to the given thing. resource - is passed as an argument it will be checked before assignment. refattr - the attribute name on the entity that will refer back this resource manager. returns the resource that was either passed in and processed or generated. """ if not isinstance(clustotype, type): raise TypeError("thing is not a Driver class") try: clusto.begin_transaction() if not resource: name, num = self.allocator() newobj = clustotype(name) else: name = resource newobj = clustotype(resource) super(SimpleEntityNameManager, self).allocate(newobj, name) clusto.commit() except Exception, x: clusto.rollback_transaction() raise
def delete_dhcp_association(request, hostname, mac_address): """ Function that deletes a hostname from a specified mac_address. Arguments: hostname -- The hostname of an entity. mac_address -- The mac_address of an entity. Exceptions Raised: JinxInvalidStateError -- More than one host had the specified hostname or mac address, or could not be found. """ hostname = hostname.lower() mac_address = mac_address.lower() try: server = clusto.get_by_mac(mac_address)[0] except IndexError: return HttpResponseInvalidState('Could not find any entities with MAC address: "%s".' % mac_address) try: clusto.begin_transaction() for (port_type, port_num, ignore, ignore) in server.port_info_tuples: if server.get_hostname(port_type, port_num) == hostname: server.del_hostname(port_type, port_num) clusto.commit() except: clusto.rollback_transaction() raise
def connect_ports(self, porttype, srcportnum, dstdev, dstportnum): """connect a local port to a port on another device """ for dev, num in [(self, srcportnum), (dstdev, dstportnum)]: if not hasattr(dev, 'port_exists'): msg = "%s has no ports." raise ConnectionException(msg % (dev.name)) num = dev._ensure_portnum(porttype, num) if not dev.port_exists(porttype, num): msg = "port %s:%d doesn't exist on %s" raise ConnectionException(msg % (porttype, num, dev.name)) if not dev.port_free(porttype, num): msg = "port %s%d on %s is already in use" raise ConnectionException(msg % (porttype, num, dev.name)) try: clusto.begin_transaction() self.set_port_attr(porttype, srcportnum, 'connection', dstdev) self.set_port_attr(porttype, srcportnum, 'otherportnum', dstportnum) dstdev.set_port_attr(porttype, dstportnum, 'connection', self) dstdev.set_port_attr(porttype, dstportnum, 'otherportnum', srcportnum) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
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
def update_reserved_ips(self): conn = self._ec2_connection() ips = set() for region in conn.get_all_regions(): conn = self._ec2_connection(region.name) for address in conn.get_all_addresses(): ips.add((region.name, self._ipy_to_int(IPy.IP(address.public_ip)))) try: clusto.begin_transaction() reserved_ips = set((a.subkey, a.value) for a in self.attrs(key='reserved_ip')) for region, ip in reserved_ips.difference(ips): self.del_attrs(key='reserved_ip', subkey=region, value=ip) for region, ip in ips.difference(reserved_ips): self.reserve_ip(region, self._int_to_ipy(ip)) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def allocate(self, clustotype, resource=None, number=True): """allocates a resource element to the given thing. resource - is passed as an argument it will be checked before assignment. refattr - the attribute name on the entity that will refer back this resource manager. returns the resource that was either passed in and processed or generated. """ if not isinstance(clustotype, type): raise TypeError("thing is not a Driver class") try: clusto.begin_transaction() if not resource: name, num = self.allocator() newobj = clustotype(name) else: name = resource newobj = clustotype(resource) super(SimpleEntityNameManager, self).allocate(newobj, name) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def connect_ports(self, porttype, srcportnum, dstdev, dstportnum): """connect a local port to a port on another device """ for dev, num in [(self, srcportnum), (dstdev, dstportnum)]: if not hasattr(dev, 'port_exists'): msg = "%s has no ports." raise ConnectionException(msg % (dev.name)) num = dev._ensure_portnum(porttype, num) if not dev.port_exists(porttype, num): msg = "port %s:%d doesn't exist on %s" raise ConnectionException(msg % (porttype, num, dev.name)) if not dev.port_free(porttype, num): msg = "port %s:%d on %s is already in use" raise ConnectionException(msg % (porttype, num, dev.name)) try: clusto.begin_transaction() self.set_port_attr(porttype, srcportnum, 'connection', dstdev) self.set_port_attr(porttype, srcportnum, 'otherportnum', dstportnum) dstdev.set_port_attr(porttype, dstportnum, 'connection', self) dstdev.set_port_attr(porttype, dstportnum, 'otherportnum', srcportnum) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
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 kvm_create(self, options): status, response = self._request( "POST", "/api/1/%s" % self.name, {"memory": options.memory, "disk": options.disk} ) if status != 200: raise DriverException(response) response = json.loads(response) config = response["config"] try: clusto.begin_transaction() self.set_attr(key="system", subkey="memory", value=config["memory"]) self.set_attr(key="system", subkey="disk", value=config["disk"]) self.set_attr(key="system", subkey="cpucount", value=1) self.set_attr(key="kvm", subkey="console-port", value=config["console"]) self.set_attr(key="kvm", subkey="vnc-port", value=5900 + config["vnc"]) self.set_port_attr("nic-eth", 1, "mac", config["mac"]) self.set_port_attr("nic-eth", 1, "model", config["nic"]) clusto.SESSION.clusto_description = "Populate KVM information for %s" % self.name clusto.commit() except: sys.stderr.write(format_exc() + "\n") clusto.rollback_transaction()
def __init__(self, hostname=None, **kwargs): """Create a new server. hostname: The hostname for this server. """ try: clusto.begin_transaction() if hostname: servers = llclusto.get_by_hostname(hostname) else: servers = [] if len(servers) > 0: raise ValueError("One or more servers with hostname %s already exist: %s" % (hostname, servers)) super(LindenServer, self).__init__(**kwargs) self.hostname = hostname self.server_class = ServerClass.get_server_class(self._server_class_name) clusto.commit() except: clusto.rollback_transaction() raise
def __init__(self, source_entity, user, event_type, timestamp, description=None, **kwargs): """Create a new LogEvent instance Keyword arguments: source_entity -- the source entity that is associated with the log event user -- the user that triggered the log event event_type -- the type of event that occured time_stamp -- the datetime when the log event occured description -- the string describing the event (default None) """ try: name_manager = clusto.get_by_name("LogEvent_name_manager") except LookupError: name_manager = SimpleNameManager("LogEvent_name_manager", basename="Log", digits=10) try: clusto.begin_transaction() name, num = name_manager.allocator() super(LogEvent, self).__init__(name, event_type = event_type, source_entity =source_entity, timestamp = timestamp, user = user, description = description) name_manager.allocate(self, name) for key in kwargs: self.add_attr(key=key, value=kwargs[key]) clusto.commit() except: clusto.rollback_transaction() raise
def allocate(self, thing, resource=(), number=True, force=False): """allocates a resource element to the given thing. resource - is passed as an argument it will be checked before assignment. refattr - the attribute name on the entity that will refer back this resource manager. returns the resource that was either passed in and processed or generated. """ try: clusto.begin_transaction() if not isinstance(thing, Driver): raise TypeError("thing is not of type Driver") if resource is (): # allocate a new resource resource, number = self.allocator(thing) auto_allocated = True else: auto_allocated = False resource, number = self.ensure_type(resource, number, thing) if not force and not self.available(resource, number, thing): raise ResourceException( "Requested resource is not available.") if self._record_allocations: if number == True: c = Counter.get(ClustoMeta().entity, self._attr_name) attr = thing.add_attr(self._attr_name, resource, number=c.value) c.next() else: attr = thing.add_attr(self._attr_name, resource, number=number) clusto.flush() a = thing.add_attr( self._attr_name, self.entity, number=attr.number, subkey='manager', ) clusto.flush() self.additional_attrs(thing, resource, attr.number) else: attr = None clusto.commit() except Exception, x: clusto.rollback_transaction() raise
def allocate(self, thing, resource=(), number=True, force=False): """allocates a resource element to the given thing. resource - is passed as an argument it will be checked before assignment. refattr - the attribute name on the entity that will refer back this resource manager. returns the resource that was either passed in and processed or generated. """ try: clusto.begin_transaction() if not isinstance(thing, Driver): raise TypeError("thing is not of type Driver") if resource is (): # allocate a new resource resource, number = self.allocator(thing) auto_allocated = True else: auto_allocated = False resource, number = self.ensure_type(resource, number, thing) if not force and not self.available(resource, number, thing): raise ResourceException("Requested resource is not available.") if self._record_allocations: if number == True: c = Counter.get(ClustoMeta().entity, self._attr_name) attr = thing.add_attr(self._attr_name, resource, number=c.value ) c.next() else: attr = thing.add_attr(self._attr_name, resource, number=number) clusto.flush() a=thing.add_attr(self._attr_name, self.entity, number=attr.number, subkey='manager', ) clusto.flush() self.additional_attrs(thing, resource, attr.number) else: attr = None clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def testTransactionRollback3(self): d1 = Entity('d1') clusto.begin_transaction() d2 = Entity('d2') clusto.rollback_transaction() clusto.get_by_name('d1') self.assertRaises(LookupError, clusto.get_by_name, 'd2')
def main(): parser = OptionParser(usage='usage: %prog [options] <object>') options, args = parser.parse_args() if not args: parser.print_help() return -1 try: obj = clusto.get_by_name(args[0]) except LookupError: sys.stderr.write('Object does not exist: %s\n' % args[0]) return -1 if obj.type != 'server': obj = obj.contents() else: obj = [obj] for server in obj: if server.type != 'server': sys.stdout.write('Not a server\n') continue #if server.attr_values(key='disk', subkey='serial'): # continue sys.stdout.write(server.name + ' ') sys.stdout.flush() ip = server.get_ips() if not ip: sys.stdout.write('No IP assigned\n') continue ip = ip[0] try: sys.stdout.write('discover_hardware ') sys.stdout.flush() info = discover_hardware(ip) except: sys.stdout.write('Unable to discover. %s\n' % sys.exc_info()[1]) continue try: sys.stdout.write('update_server ') clusto.begin_transaction() update_server(server, info) clusto.commit() sys.stdout.write('.\n') except: sys.stdout.write('Error updating clusto:\n%s\n' % format_exc()) clusto.rollback_transaction() sys.stdout.flush()
def del_ipmi_hostname(self): """ Delete the ipmi hostname attributes. """ try: clusto.begin_transaction() self.del_port_attr(self._ipmi_port_type, self._ipmi_port_num, "ipmi_hostname") clusto.commit() except: clusto.rollback_transaction() raise
def del_ipmi_mac(self): """ Delete the ipmi mac address attributes. """ try: clusto.begin_transaction() self.del_port_attr(self._ipmi_port_type, self._ipmi_port_num, "ipmi_mac") clusto.commit() except: clusto.rollback_transaction() raise
def testTransactionCommit(self): try: clusto.begin_transaction() c1 = Entity('c1') clusto.commit() except Exception: clusto.rollback_transaction() clusto.get_by_name('c1')
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 del_attrs(self, *args, **kwargs): "delete attribute with the given key and value optionally value also" clusto.flush() try: clusto.begin_transaction() for i in self.attr_query(*args, **kwargs): i.delete() clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def testBadRollback(self): clusto.begin_transaction() d1 = Entity('d1') clusto.get_by_name('d1') d2 = Entity('d2') clusto.commit() self.assertRaises(TransactionException, clusto.rollback_transaction)
def del_attrs(self, *args, **kwargs): "delete attribute with the given key and value optionally value also" clusto.flush() try: clusto.begin_transaction() for i in self.attr_query(*args, **kwargs): i.delete() clusto.commit() self.expire(*args, **kwargs) except Exception, x: clusto.rollback_transaction() raise x
def del_ipmi_info(self): """ Delete the ipmi hostname and mac address attributes. """ try: clusto.begin_transaction() self.del_port_attr(self._ipmi_port_type, self._ipmi_port_num, "ipmi_hostname") self.del_port_attr(self._ipmi_port_type, self._ipmi_port_num, "ipmi_mac") clusto.commit() except: clusto.rollback_transaction() raise
def testTransactionRollback2(self): try: clusto.begin_transaction() c1 = Entity('c1') raise Exception() except Exception: clusto.rollback_transaction() c2 = Entity('c2') self.assertRaises(LookupError, clusto.get_by_name, 'c1') clusto.get_by_name('c2')
def set_dhcp_association(request, hostname, mac_address): """ Function that sets a hostname to a specified mac_address. Arguments: hostname -- The hostname of an entity. mac_address -- The mac_address of an entity. Exceptions Raised: JinxInvalidStateError -- More than one host had the specified hostname or mac address, or could not be found. """ hostname = hostname.lower() mac_address = mac_address.lower() servers = clusto.get_by_mac(mac_address) ipmi_hosts = clusto.get_entities(attrs=[{'subkey': 'ipmi_mac', 'value': mac_address}]) hosts = llclusto.get_by_hostname(hostname) if not servers and not ipmi_hosts: return HttpResponseInvalidState('Could not find any entities with MAC address: "%s".' % mac_address) try: clusto.begin_transaction() for host in hosts: for (port_type, port_num, ignore, ignore) in host.port_info_tuples: if host.get_hostname(port_type, port_num) == hostname: host.del_hostname(port_type, port_num) for server in servers: for (port_type, port_num, ignore, ignore) in server.port_info_tuples: if server.get_port_attr(port_type, port_num, "mac") == mac_address: server.set_hostname(hostname, port_type, port_num) for host in ipmi_hosts: ipmi = host.ipmi if ipmi[1] == mac_address: host.set_ipmi_info(hostname, ipmi[1]) clusto.commit() except: clusto.rollback_transaction() raise
def run(self): clusto.connect(self.conf,echo=self.echo) clusto.init_clusto() try: clusto.begin_transaction() e = clusto.Entity('foo'+self.getName()) self.barrier() clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def _set_pgi_image(self, image): """ Setter method for the pgi_image property Automatically keeps track of the previous associated PGI image in self.previous_pgi_image. """ try: clusto.begin_transaction() Driver.__setattr__(self, "previous_pgi_image", self.pgi_image) Driver.__setattr__(self, "pgi_image", image) clusto.commit() except: clusto.rollback_transaction() raise
def run(self): clusto.connect(self.conf, echo=self.echo) clusto.init_clusto() try: clusto.begin_transaction() e = clusto.Entity('foo' + self.getName()) self.barrier() clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def delete(self): "Delete self and all references to self." clusto.begin_transaction() try: self.deleted_at_version = working_version() for i in self.references: i.delete() for i in self.attrs: i.delete() clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def delete_dhcp_association(request, hostname, mac_address): """ Function that deletes a hostname from a specified mac_address. Arguments: hostname -- The hostname of an entity. mac_address -- The mac_address of an entity. Exceptions Raised: JinxInvalidStateError -- More than one host had the specified hostname or mac address, or could not be found. """ hostname = hostname.lower() mac_address = mac_address.lower() server = clusto.get_by_mac(mac_address) server_ipmi = clusto.get_entities(attrs=[{'subkey': 'ipmi_mac', 'value': mac_address}]) if len(server) < 1: if len(server_ipmi) < 1: return HttpResponseInvalidState('Could not find any entities with MAC address: "%s".' % mac_address) elif len(server_ipmi) > 1: return HttpResponseInvalidState('Found multiple IPMI entities with MAC address: "%s: %s".' % mac_address, server_ipmi) elif len(server) > 1: return HttpResponseInvalidState('Found multiple entities with MAC address: "%s: %s".' % mac_address, server) try: clusto.begin_transaction() if server: server = server[0] for (port_type, port_num, ignore, ignore) in server.port_info_tuples: if server.get_hostname(port_type, port_num) == hostname: server.del_hostname(port_type, port_num) if server_ipmi: server = server_ipmi[0] if server.ipmi[0] == hostname: server.del_ipmi_hostname() clusto.commit() except: clusto.rollback_transaction() raise
def set_state(self, state_name): """Changes this host's state. state_name must be the name of an existing HostState entity.""" try: host_state = clusto.get_by_name(state_name) except LookupError: raise ValueError("%s is not a valid host state." % state_name) try: clusto.begin_transaction() self.del_state() host_state.insert(self) clusto.commit() except Exception, e: clusto.rollback_transaction() raise
def __init__(self, *args, **kwargs): try: name_manager = clusto.get_by_name("LindenEquipment_name_manager") except LookupError: name_manager = SimpleNameManager("LindenEquipment_name_manager", basename="LL", digits=10) try: clusto.begin_transaction() name, num = name_manager.allocator() super(LindenEquipment, self).__init__(name, *args, **kwargs) name_manager.allocate(self, name) clusto.commit() except: clusto.rollback_transaction() raise
def set_ipmi_info(self, ipmi_hostname, ipmi_mac): """ Set the ipmi hostname associated with the ipmi interface""" try: clusto.begin_transaction() if not isinstance(ipmi_hostname, basestring): raise TypeError("The IPMI hostname must be a string") elif not ipmi_hostname.startswith("mgmt"): raise IPMIHostnameError("IPMI hostname must start with 'mgmt'") if not isinstance(ipmi_mac, basestring): raise TypeError("The IPMI mac address must be a string") self.set_port_attr(self._ipmi_port_type, self._ipmi_port_num, "ipmi_hostname", ipmi_hostname) self.set_port_attr(self._ipmi_port_type, self._ipmi_port_num, "ipmi_mac", ipmi_mac) clusto.commit() except: clusto.rollback_transaction() raise
def _set_driver_and_type(self, driver, clusto_type): """sets the driver and type for the entity this shouldn't be too dangerous, but be careful params: driver: the driver name clusto_type: the type name """ try: clusto.begin_transaction() self.type = clusto_type self.driver = driver clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def bind_ip_to_osport(self, ip, osportname, ipman=None, porttype=None, portnum=None): """bind an IP to an os port and optionally also asign the os port name to a physical port If the given ip is already allocated to this device then use it. If it isn't, try to allocate it from a matching IPManager. """ if (porttype != None) ^ (portnum != None): raise Exception("both portype and portnum need to be specified or set to None") try: clusto.begin_transaction() if not self.has_ip(ip): if not ipman: ipman = IPManager.get_ip_manager(ip) ipman.allocate(self, ip) clusto.flush() else: ipman = IPManager.get_ip_manager(ip) number = ipman.get_resource_number(self, ip) ipattrs = ipman.get_resource_attrs(self, ip, number=number) if porttype is not None and portnum is not None: self.set_port_attr(porttype, portnum, 'osportname', osportname) self.set_attr(ipattrs[0].key, number=ipattrs[0].number, subkey='osportname', value=osportname) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def disconnect_port(self, porttype, portnum): """disconnect both sides of a port""" portnum = self._ensure_portnum(porttype, portnum) if not self.port_free(porttype, portnum): dev = self.get_connected(porttype, portnum) otherportnum = self.get_port_attr(porttype, portnum, 'otherportnum') clusto.begin_transaction() try: dev.del_port_attr(porttype, otherportnum, 'connection') dev.del_port_attr(porttype, otherportnum, 'otherportnum') self.del_port_attr(porttype, portnum, 'connection') self.del_port_attr(porttype, portnum, 'otherportnum') clusto.commit() except Exception, x: clusto.rollback_transaction() raise x
def _set_pgi_image(self, image): """ Setter method for the pgi_image property Automatically keeps track of the previous associated PGI image in self.previous_pgi_image. """ if not isinstance(image, PGIImage): raise TypeError( "Only PGIImage entities may be assigned to the pgi_image attribute." ) try: clusto.begin_transaction() Driver.__setattr__(self, "previous_pgi_image", self.pgi_image) Driver.__setattr__(self, "pgi_image", image) clusto.commit() except: clusto.rollback_transaction() raise
def createRack(datacenter, rackprefix): try: clusto.begin_transaction() r = BasicRack(rackprefix) pwr = BasicPowerStrip(rackprefix + '-pwr1', withslave=True) sw = BasicNetworkSwitch(rackprefix + '-sw1') sw.connect_ports('nic-eth', 48, pwr, 1) pwr.connect_ports('pwr-nema-5', 1, 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, i + 2) clusto.commit() except Exception, x: clusto.rollback_transaction() raise x