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): 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): 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): m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue) if not m: raise HostinfoException("Must be specified in key=value format") key = m.group('key').lower() value = m.group('value').lower() if not namespace.origin: namespace.origin = os.path.basename(sys.argv[0]) for host in namespace.host: host = host.lower().strip() try: addKeytoHost(host=host, key=key, value=value, origin=namespace.origin, readonlyFlag=namespace.readonlyupdate, updateFlag=namespace.update, appendFlag=namespace.append) except RestrictedValueException: raise RestrictedValueException( "Cannot add %s=%s to a restricted key" % (key, value), key=key, retval=2) except ReadonlyValueException: raise ReadonlyValueException( "Cannot add %s=%s to a readonly key" % (key, value), retval=3) except HostinfoException as err: raise except TypeError as err: # pragma: nocover raise HostinfoException("Couldn't add value %s to %s - %s" % (value, host, err)) 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): m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue) if not m: raise HostinfoException("Must be specified in key=value format") key = m.group('key').lower() value = m.group('value').lower() rvallist = RestrictedValue.objects.filter(keyid__key=key, value=value) if len(rvallist) != 1: raise HostinfoException( "No key %s=%s in the restrictedvalue list" % (key, value)) rvallist[0].delete() return None, 0
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) for host in namespace.host: host = host.lower() if self.checkHost(host): raise HostinfoException("Host %s already exists" % host) if host[0] in ('-', ): raise HostinfoException( "Host begins with a forbidden character ('%s') - not adding" % host[0]) hobj = Host(hostname=host, origin=origin) hobj.save() return None, 0
def handle(self, namespace): self.namespace = namespace try: xmltree = xml.etree.ElementTree.parse(namespace.xmlfile) except IOError as exc: if exc.errno == 2: raise HostinfoException("File %s doesn't exist" % namespace.xmlfile) else: raise HostinfoException("File %s not readable (errno=%d)" % (namespace.xmlfile, exc.errno)) for key in xmltree.findall('key'): self.handleKey(key) for host in xmltree.findall('host'): self.handleHost(host) return None, 0
def handle(self, namespace): desc = "" if namespace.keytype: keytype = namespace.keytype desc = " ".join(namespace.key[1:]) else: if len(namespace.key) == 1: keytype = 'single' else: keytype = namespace.key[1] desc = " ".join(namespace.key[2:]) key = namespace.key[0].lower() keytype = self.validateKeytype(keytype) try: AllowedKey.objects.get(key=key) except: newak = AllowedKey( key=key, validtype=keytype, desc=desc, restrictedFlag=namespace.restricted, readonlyFlag=namespace.readonly, auditFlag=namespace.audit) newak.save() else: raise HostinfoException("Key already exists with that name: %s" % key) return None, 0
def handle(self, namespace): alias = namespace.alias.lower() aliases = HostAlias.objects.filter(alias=alias) if len(aliases) == 0: raise HostinfoException("No alias called %s" % alias) aliases[0].delete() return None, 0
def handle(self, namespace): m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue) if not m: raise HostinfoException("Must be specified in key=value format") key = m.group('key').lower() value = m.group('value').lower() keyobjlist = AllowedKey.objects.filter(key=key) if len(keyobjlist) != 1: raise HostinfoException("No key %s found" % key) keyobj = keyobjlist[0] if not keyobj.restrictedFlag: raise HostinfoException("Key %s isn't a restrictedvalue key" % key) rvallist = RestrictedValue.objects.filter(keyid=keyobj, value=value) if rvallist: raise HostinfoException("Already a key %s=%s in the restrictedvalue list" % (key, value)) rv = RestrictedValue(keyid=keyobj, value=value) rv.save() return None, 0
def validateKeytype(self, keytype): # Work out which type it should be vt = -1 for knum, desc in AllowedKey.TYPE_CHOICES: if keytype == desc: vt = knum break if vt < 0: raise HostinfoException( "Unknown type %s - should be one of %s" % (keytype, ",".join(self.type_choices))) return vt
def handle(self, namespace): outstr = "" key = namespace.key.lower() keyobjlist = AllowedKey.objects.filter(key=key) if len(keyobjlist) != 1: raise HostinfoException("No key %s found" % key) vals = [] rvallist = RestrictedValue.objects.filter(keyid=keyobjlist[0]) for rv in rvallist: vals.append(rv.value) for rv in sorted(vals): outstr += "%s\n" % rv return outstr, 0
def handle(self, namespace): m = re.match("(?P<key>\w+)=(?P<value>.+)", namespace.keyvalue[0]) if not m: raise HostinfoException("Must be in key=value format, not %s" % namespace.keyvalue[0]) key = m.group('key').lower() value = m.group('value').lower() keyid = getAK(key) if not namespace.hosts and not namespace.all: raise HostinfoException( "Must specify a list of hosts or the --all flag") kvlist = KeyValue.objects.filter(keyid=keyid, value=value) for kv in kvlist: if (namespace.hosts and kv.hostid.hostname in namespace.hosts) or not namespace.hosts: if not namespace.kidding: kv.value = namespace.newvalue[0] kv.save() else: sys.stderr.write( "Would replace %s=%s with %s on %s\n" % (kv.keyid, kv.value, namespace.newvalue[0], kv.hostid)) return None, 0
def validateKeytype(self, keytype): # Work out which type it should be vt = -1 for knum, desc in AllowedKey.TYPE_CHOICES: try: if int(keytype) == knum: vt = knum break except ValueError: pass if keytype == desc: vt = knum break if vt < 0: raise HostinfoException("Unknown type %s - should be one of %s" % (keytype, ",".join([d for k, d in AllowedKey.TYPE_CHOICES]))) return vt
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): outstr = "" allkeys = AllowedKey.objects.all() if namespace.keylist: keys = [k for k in allkeys if k.key in namespace.keylist] else: keys = [k for k in allkeys] if not keys: raise HostinfoException("No keys to show") for key in keys: if namespace.typeflag: outstr += "%s\t%s\n" % (key.key, key.get_validtype_display()) else: notes = "" if key.restrictedFlag: notes = "\t[KEY RESTRICTED]" if key.readonlyFlag: notes += "\t[KEY READ ONLY]" outstr += "%s\t%s\t%s%s\n" % ( key.key, key.get_validtype_display(), key.desc, notes) return outstr, 0
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 handleKey(self, key): """ <key> <name>appsla</name> <type>single</type> <readonlyFlag>1</readonlyFlag> <auditFlag>1</auditFlag> <restricted> <value>Foo</value> </restricted> <docpage>None</docpage> <desc>Application SLA</desc> </key> """ name = '' keytype = '' restrictedKid = None restrictedFlag = False readonlyFlag = False auditFlag = True docpage = '' desc = '' for kid in key.getchildren(): if kid.tag == 'name': name = kid.text if kid.tag == 'type': keytype = self.validateKeytype(kid.text) if kid.tag == 'restricted': restrictedKid = kid restrictedFlag = True if kid.tag == 'readonlyFlag': readonlyFlag = (kid.text == '1') if kid.tag == 'auditFlag': auditFlag = (kid.text == '0') if kid.tag == 'docpage': if kid.text: docpage = kid.text.strip() if docpage == 'None': docpage = None if kid.tag == 'desc': if kid.text: desc = kid.text.strip() if not name: raise HostinfoException("No name specified for key") try: ak = AllowedKey.objects.get(key=name) except ObjectDoesNotExist: ak = AllowedKey(key=name, validtype=keytype, restricted=restrictedFlag, readonlyFlag=readonlyFlag, auditFlag=auditFlag, docpage=docpage, desc=desc) self.verbose("New key %s" % repr(ak)) if not self.namespace.kiddingFlag: ak.save() else: change = False if ak.validtype != keytype: self.verbose("Changing %s: keytype from %s to %s" % (name, ak.get_validtype_display(ak.validtype), ak.get_validtype_display(keytype))) ak.validtype = keytype change = True if ak.restrictedFlag != restrictedFlag: self.verbose("Changing %s: restrictedFlag from %s to %s" % (name, ak.restrictedFlag, restrictedFlag)) ak.restrictedFlag = restrictedFlag change = True if ak.readonlyFlag != readonlyFlag: self.verbose("Changing %s: readonlyFlag from %s to %s" % (name, ak.readonlyFlag, readonlyFlag)) ak.readonlyFlag = readonlyFlag change = True if ak.auditFlag != auditFlag: self.verbose("Changing %s: auditFlag from %s to %s" % (name, ak.auditFlag, auditFlag)) ak.auditFlag = auditFlag change = True if ak.docpage != docpage: self.verbose("Changing %s: docpage from '%s' to '%s'" % (name, ak.docpage, docpage)) ak.docpage = docpage change = True if ak.desc != desc: self.verbose("Changing %s: desc from '%s' to '%s'" % (name, ak.desc, desc)) ak.desc = desc change = True if change and not self.namespace.kiddingFlag: ak.save() if restrictedKid: self.handleRestrictedKey(restrictedKid, ak)