def handle(self, namespace): self.kidding = namespace.kidding self.force = namespace.force srchostobj = getHost(namespace.srchost[0]) if not srchostobj: raise HostinfoException("Source host %s doesn't exist" % namespace.srchost[0]) dsthostobj = getHost(namespace.dsthost[0]) if not dsthostobj: raise HostinfoException("Destination host %s doesn't exist" % namespace.dsthost[0]) ok = True # Get all the key/values from the source host and see if # they exist on the dest host srckeylist = KeyValue.objects.filter( hostid__hostname=namespace.srchost[0]) for srckey in srckeylist: ok = self.transferKey(srckey, srchostobj, dsthostobj) if not ok: break if ok and not self.kidding: srchostobj.delete() return None, 0 return "Failed to merge", 1
def handle(self, namespace): hostobj = getHost(namespace.srchost[0]) if not hostobj: raise HostinfoException("There is no host called %s" % namespace.srchost[0]) dsthostobj = getHost(namespace.dsthost[0]) if dsthostobj: raise HostinfoException("A host already exists with the name %s" % namespace.dsthost[0]) hostobj.hostname = namespace.dsthost[0] hostobj.save() return None, 0
def handle(self, namespace): origin = getOrigin(namespace.origin) host = namespace.host.lower() alias = namespace.alias.lower() targhost = getHost(host) if not targhost: raise HostinfoException("Host %s doesn't exist" % host) if getHost(alias): raise HostinfoException("Host %s already exists" % alias) haobj = HostAlias(hostid=targhost, alias=alias, origin=origin) haobj.save() return None, 0
def handle(self, namespace): origin = getOrigin(namespace.origin) host = namespace.host.lower() alias = namespace.alias.lower() if not getHost(host): raise HostinfoException("Host %s doesn't exist" % host) if getHost(alias): raise HostinfoException("Host %s already exists" % alias) haobj = HostAlias( hostid=Host.objects.get(hostname=host), alias=alias, origin=origin) haobj.save() return None, 0
def handle(self, namespace): outstr = "" host = getHost(namespace.host) if not host: return outstr, 1 hostchanges = Host.history.filter(id=host.id).order_by('history_date') for hc in hostchanges: if hc.history_type == '+': msg = "Host:%s added on %s" % (host.hostname, hc.history_date) # simple_history currently can't handle deleted hosts # elif hc.history_type == '-': # msg = "Host:%s deleted on %s" % (host.hostname, hc.history_date) if namespace.originFlag: msg = "%s %s" % (msg, hc.origin) outstr += "%s\n" % msg kvchanges = KeyValue.history.filter( hostid_id=host.id).order_by('history_date') for kv in kvchanges: key = self.getKeyName(kv.keyid_id) if kv.history_type == '+': msg = "added %s:%s=%s on %s" % (host.hostname, key, kv.value, kv.history_date) elif kv.history_type == '-': msg = "deleted %s:%s=%s on %s" % (host.hostname, key, kv.value, kv.history_date) elif kv.history_type == '~': msg = "changed %s:%s=%s on %s" % (host.hostname, key, kv.value, kv.history_date) if namespace.originFlag: msg = "%s %s" % (msg, kv.origin) outstr += "%s\n" % msg return outstr, 0
def handle(self, namespace): m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue) if m: key = m.group('key').lower() value = m.group('value').lower() else: key = namespace.keyvalue.lower() value = '' keyid = getAK(key) for host in namespace.host: hostid = getHost(host) if not hostid: raise HostinfoException("Unknown host: %s" % host) if value: kvlist = KeyValue.objects.filter(hostid=hostid, keyid=keyid, value=value) else: kvlist = KeyValue.objects.filter(hostid=hostid, keyid=keyid) if not kvlist: raise HostinfoException("Host %s doesn't have key %s" % (host, key)) else: for kv in kvlist: try: kv.delete(readonlychange=namespace.readonlyupdate) except ReadonlyValueException: raise HostinfoException("Cannot delete a readonly value") return None, 0
def handle(self, namespace): outstr = "" host = getHost(namespace.host) if not host: return outstr, 1 hostchanges = Host.history.filter(id=host.id).order_by('history_date') for hc in hostchanges: if hc.history_type == '+': msg = "Host:%s added on %s" % (host.hostname, hc.history_date) # simple_history currently can't handle deleted hosts # elif hc.history_type == '-': # msg = "Host:%s deleted on %s" % (host.hostname, hc.history_date) if namespace.originFlag: msg = "%s %s" % (msg, hc.origin) outstr += "%s\n" % msg kvchanges = KeyValue.history.filter(hostid_id=host.id).order_by('history_date') for kv in kvchanges: key = self.getKeyName(kv.keyid_id) if kv.history_type == '+': msg = "added %s:%s=%s on %s" % (host.hostname, key, kv.value, kv.history_date) elif kv.history_type == '-': msg = "deleted %s:%s=%s on %s" % (host.hostname, key, kv.value, kv.history_date) elif kv.history_type == '~': msg = "changed %s:%s=%s on %s" % (host.hostname, key, kv.value, kv.history_date) if namespace.originFlag: msg = "%s %s" % (msg, kv.origin) outstr += "%s\n" % msg return outstr, 0
def handle(self, namespace): host = namespace.host.lower() h = getHost(host) if not h: raise HostinfoException("Host %s doesn't exist" % host) if not namespace.lethal: raise HostinfoException("Didn't do delete as no --lethal specified") # Delete aliases aliases = HostAlias.objects.filter(hostid=h.id) for alias in aliases: if namespace.lethal: alias.delete() # Delete key/values kvs = KeyValue.objects.filter(hostid=h.id) for kv in kvs: if namespace.lethal: kv.delete(readonlychange=True) # Delete the host if namespace.lethal: h.delete() return None, 0
def handle(self, namespace): host = namespace.host.lower() h = getHost(host) if not h: raise HostinfoException("Host %s doesn't exist" % host) if not namespace.lethal: raise HostinfoException( "Didn't do delete as no --lethal specified") # Delete aliases aliases = HostAlias.objects.filter(hostid=h.id) for alias in aliases: if namespace.lethal: alias.delete() # Delete key/values kvs = KeyValue.objects.filter(hostid=h.id) for kv in kvs: if namespace.lethal: kv.delete(readonlychange=True) # Delete the host if namespace.lethal: h.delete() return None, 0
def handle(self, namespace): m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue) if m: key = m.group('key').lower() value = m.group('value').lower() else: key = namespace.keyvalue.lower() value = '' keyid = getAK(key) for host in namespace.host: hostid = getHost(host) if not hostid: raise HostinfoException("Unknown host: %s" % host) if value: kvlist = KeyValue.objects.filter(hostid=hostid, keyid=keyid, value=value) else: kvlist = KeyValue.objects.filter(hostid=hostid, keyid=keyid) if not kvlist: raise HostinfoException("Host %s doesn't have key %s" % (host, key)) else: for kv in kvlist: try: kv.delete(readonlychange=namespace.readonlyupdate) except ReadonlyValueException: raise HostinfoException( "Cannot delete a readonly value") return None, 0
def handle(self, namespace): host = namespace.host.lower() targhost = getHost(host) if not targhost: raise HostinfoException("Host %s doesn't exist" % host) links = Links.objects.filter(hostid=targhost) if namespace.everytag: pass if namespace.tag: links = links.filter(tag=namespace.tag[0].lower()) for link in links: link.delete() return None, 0
def handle(self, namespace): self.kidding = namespace.kidding self.force = namespace.force srchostobj = getHost(namespace.srchost[0]) if not srchostobj: raise HostinfoException("Source host %s doesn't exist" % namespace.srchost[0]) dsthostobj = getHost(namespace.dsthost[0]) if not dsthostobj: raise HostinfoException("Destination host %s doesn't exist" % namespace.dsthost[0]) ok = True # Get all the key/values from the source host and see if # they exist on the dest host srckeylist = KeyValue.objects.filter(hostid__hostname=namespace.srchost[0]) for srckey in srckeylist: ok = self.transferKey(srckey, srchostobj, dsthostobj) if not ok: break if ok and not self.kidding: srchostobj.delete() return None, 0 return "Failed to merge", 1
def handle(self, namespace): outstr = "" if namespace.all or not namespace.host: aliases = HostAlias.objects.all() for alias in aliases: outstr += "%s %s\n" % (alias.alias, alias.hostid.hostname) return outstr, 0 hid = getHost(namespace.host.lower()) if not hid: raise HostinfoException("Host %s doesn't exist" % namespace.host) outstr += "%s\n" % hid.hostname aliases = HostAlias.objects.filter(hostid=hid) if not aliases: return outstr, 1 for alias in aliases: outstr += "%s\n" % alias.alias return outstr, 0
def handle(self, namespace): host = namespace.host.lower() tag = namespace.tag.lower() url = namespace.url.lower() targhost = getHost(host) # Add url validation if not targhost: raise HostinfoException("Host %s doesn't exist" % host) link = Links.objects.filter(hostid=targhost, tag=tag) if link: if namespace.update: link[0].url = url link[0].save() return None, 0 else: return "Host %s already has a link with tag %s" % (host, tag), 1 link = Links(hostid=targhost, tag=tag, url=url) link.save() return None, 0
def handle(self, namespace): global _hostcache self.namespace = namespace self.printout = namespace.printout if namespace.host: host = getHost(namespace.host[0]) if host: matches = [host.id] else: matches = [] else: try: qualifiers = parseQualifiers(namespace.criteria) except TypeError as err: # pragma: no cover raise HostinfoException(err) matches = getMatches(qualifiers) _hostcache = self.getHostCache(matches) output = self.Display(matches) if matches: retval = 0 else: retval = 1 return output, retval
def handle(self, namespace): global _akcache, _hostcache self.namespace = namespace self.printout = namespace.printout _hostcache = self.getHostCache() if namespace.host: host = getHost(namespace.host[0]) if host: matches = [host.id] else: matches = [] else: try: qualifiers = parseQualifiers(namespace.criteria) except TypeError as err: # pragma: no cover raise HostinfoException(err) matches = getMatches(qualifiers) output = self.Display(matches) if matches: retval = 0 else: retval = 1 return output, retval
def checkHost(self, host): h = getHost(host) if h: return True return False