def handle_status(self, collections=None): if not collections: collections = list(Collection.iter_collections()) for c in collections: dc = Collection(c) dc.load() print "*", c for status, ci in dc.get_status(): print status, ci
def handle_sync(self): DocCategory.fix_all() try: for name in Collection.iter_collections(): lc = Collection(name, local=True) lc.load() dc = Collection(name) dc.load() lc.apply(dc) except ValueError, why: raise CommandError(why)
def handle_check(self): try: for name in Collection.iter_collections(): dc = Collection(name) dc.load() for mi in dc.items.itervalues(): r = dc.check_item(mi) if r: print mi.name for x in r: print " %s" % x except ValueError, why: raise CommandError(why)
def get_count(cn, m): collection = Collection(cn, m) collection.load() total = m.objects.count() builtin = len(collection.items) local = total - builtin return [builtin, local, total]
def handle_upgrade(self, collections): for c in collections: self.log("Upgrading %s" % c) dc = Collection(c) cr = "%s/collections/%s" % (dc.module, dc.name) if os.path.exists(os.path.join(cr, "manifest.csv")): raise CommandError("Collection %s is already upgraded" % c) fl = [] for root, dirs, files in os.walk(cr): for f in files: if f.endswith(".json"): fl += [os.path.join(root, f)] for p in fl: s = os.stat(p) data = json_decode(read_file(p)) if isinstance(data, list): for o in data: if "__aliases" in o: del o["__aliases"] dc.install_item(o) elif isinstance(data, dict): if "__aliases" in data: del data["__aliases"] dc.install_item(data) if os.stat(p)[stat.ST_MTIME] == s[stat.ST_MTIME]: # Remove stale file self.log(" ... removing %s" % p) os.unlink(p) self.log(" ... saving manifest.csv") dc.save() self.log(" ... done")
def handle_install(self, files): def iter_files(names): for f in names: if os.path.isdir(f): for dirpath, dirnames, filenames in os.walk(f): for ff in filenames: if ff.endswith(".json"): yield os.path.join(dirpath, ff) else: yield f dcs = {} # name -> collection files = list(files) if files[0] in Collection.COLLECTIONS: # Get collection name hints current_name = files.pop(0) else: # Inplace update parts = files[0].split(os.path.sep) if (len(parts) > 3 and parts[1] == "collections" and "%s.%s" % (parts[0], parts[2]) in Collection.COLLECTIONS): current_name = "%s.%s" % (parts[0], parts[2]) else: current_name = None self.log("Installing files") for f in iter_files(files): self.log(" ... read %s" % f) fd = read_file(f) if not fd: raise CommandError("Cannot read file: %s" % f) try: data = json_decode(fd) except ValueError: raise CommandError("Unable to parse JSON file: %s" % f) if "$collection" in data: current_name = data["$collection"] if not current_name: raise CommandError("Cannot detect collection for file: %s" % f) if current_name in dcs: dc = dcs[current_name] else: self.log(" ... open collection %s" % current_name) dc = Collection(current_name) dc.load() dcs[current_name] = dc if "uuid" in data: ci = dc.get_item(data["uuid"]) if ci and dc.get_hash(fd) == ci.hash: self.log(" ... not changed. Skipping") continue # Not changed dc.install_item(data, load=True) # Save all modified manifests for n, dc in dcs.iteritems(): self.log(" ... saving manifest.csv for %s" % n) dc.save()
def _api_install_json(self, request, id): """ Expose JSON collection item when available """ from noc.lib.collection import Collection o = self.get_object_or_404(self.model, id=id) data = json_decode(o.to_json()) dc = Collection(self.json_collection) dc.load() dc.install_item(data) dc.save() return True
def fix_uuids(self): """ Fix collection uuids to binary format """ self.info("Checking collections UUID") for n in Collection.iter_collections(): c = Collection.COLLECTIONS[n] if ("uuid" in c._fields and hasattr(c._fields["uuid"], "_binary") and c._fields["uuid"]._binary ): self.fix_collection_uuids(n, c) self.info("... done")
from noc.lib.fields import TextArrayField, CIDRField from noc.lib.middleware import get_user, get_request from noc.lib.timepattern import TimePattern as TP from noc.lib.timepattern import TimePatternList from noc.sa.interfaces.base import interface_registry from noc.lib.periodic import periodic_registry from noc.lib.app.site import site from noc.lib.validators import check_extension, check_mimetype from noc.lib import nosql from noc.lib.validators import is_int ## Register periodics periodic_registry.register_all() from audittrail import AuditTrail from fts_queue import FTSQueue from noc.lib.collection import Collection Collection.install() from noc.pm.models.probeconfig import ProbeConfig AuditTrail.install() FTSQueue.install() ProbeConfig.install() ## ## Initialize download registry ## downloader_registry.register_all() from customfieldenumgroup import CustomFieldEnumGroup from customfieldenumvalue import CustomFieldEnumValue from customfield import CustomField from permission import Permission
def not_found(self, name): msg = ["Collection not found: %s" % name, "Available collections are:"] msg += [" %s" % c for c in sorted(Collection.iter_collections())] return "\n".join(msg)
def import_object(self, obj): collection = obj.get("$collection") if not collection: raise ValueError("No $collection attribute") c = Collection(collection, local=True) # Can raise ValueError c.upload_data(obj)