Verified Commit 19d9bc74 authored by Aleksey Shirokih's avatar Aleksey Shirokih
Browse files

Merge branch 'master' of code.getnoc.com:noc/noc

parents 2db26a6b a8e5a034
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# Create User and Group models
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python import datetime
import datetime
# Third-party modules
from django.db import models
# NOC modules
from noc.core.migration.base import BaseMigration
class Migration(BaseMigration):
def migrate(self):
# Elder installations has auth_user and auth_group tables
# created by django"s auth application.
# Skip if tables exists
if self.db.has_table("auth_user"):
# Enlarge username, django creates too short
self.db.execute("ALTER TABLE auth_user ALTER username TYPE VARCHAR(75)")
return
# User model
self.db.create_table(
"auth_user", (
("id", models.AutoField(verbose_name="ID", primary_key=True, auto_created=True)),
("username", models.CharField(max_length=75, unique=True)),
("first_name", models.CharField(max_length=75, blank=True)),
("last_name", models.CharField( max_length=75, blank=True)),
("email", models.EmailField(blank=True)),
("password", models.CharField(max_length=128)),
("is_active", models.BooleanField(default=True)),
("is_superuser", models.BooleanField(default=False)),
("date_joined", models.DateTimeField(default=datetime.datetime.now))
))
# Group model
self.db.create_table(
"auth_group", (
("id", models.AutoField(verbose_name="ID", primary_key=True, auto_created=True)),
("name", models.CharField(max_length=80, unique=True))
)
)
#
# Adding ManyToManyField "User.groups"
User = self.db.mock_model(
model_name="User",
db_table="auth_user"
)
Group = self.db.mock_model(
model_name="Group",
db_table="auth_group"
)
self.db.create_table(
"auth_user_groups", (
("id", models.AutoField(verbose_name="ID", primary_key=True, auto_created=True)),
("user", models.ForeignKey(User, null=False)),
("group", models.ForeignKey(Group, null=False))
)
)
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# Create default user
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# NOC modules
from noc.core.migration.base import BaseMigration
class Migration(BaseMigration):
def migrate(self):
# Create default admin user if no user exists
if not self.db.execute("SELECT COUNT(*) FROM auth_user")[0][0] == 0:
return
self.db.execute(
"INSERT INTO auth_user"
"(username, first_name, last_name, email, password, is_active, is_superuser, date_joined) "
"VALUES(%s, %s, %s, %s, %s, %s, %s, 'now')", [
"admin", "NOC", "Admin", "test@example.com",
"sha1$235c1$e8e4d9aaa945e1fae62a965ee87fbf7b4a185e3f",
True, True
]
)
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# permission
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
from django.db import models
# NOC modules
from noc.core.migration.base import BaseMigration
class Migration(BaseMigration):
def migrate(self):
if self.db.has_table("main_permission"):
# Already created for elder installations
return
# Adding model "Permission"
self.db.create_table(
"main_permission", (
("id", models.AutoField(verbose_name="ID", primary_key=True, auto_created=True)),
("name", models.CharField("Name", max_length=128, unique=True)),
)
)
Permission = self.db.mock_model(
model_name="Permission",
db_table="main_permission"
)
# Adding ManyToManyField "Permission.groups"
Group = self.db.mock_model(
model_name="Group",
db_table="auth_group"
)
self.db.create_table(
"main_permission_groups", (
("id", models.AutoField(verbose_name="ID", primary_key=True, auto_created=True)),
("permission", models.ForeignKey(Permission, null=False)),
("group", models.ForeignKey(Group, null=False))
)
)
# Adding ManyToManyField "Permission.users"
User = self.db.mock_model(
model_name="User",
db_table="auth_user"
)
self.db.create_table(
"main_permission_users", (
("id", models.AutoField(verbose_name="ID", primary_key=True, auto_created=True)),
("permission", models.ForeignKey(Permission, null=False)),
("user", models.ForeignKey(User, null=False))
)
)
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# aaa module models
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from __future__ import absolute_import
# NOC modules
from .user import User
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# Group model
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from threading import Lock
import operator
# Third-party modules
import cachetools
import six
from django.db import models
# NOC modules
from noc.core.model.hacks import tuck_up_pants
id_lock = Lock()
@tuck_up_pants
@six.python_2_unicode_compatible
class Group(models.Model):
class Meta(object):
verbose_name = "Group"
verbose_name_plural = "Groups"
app_label = "aaa"
# Point to django's auth_user table
db_table = "auth_group"
ordering = ["name"]
name = models.CharField(max_length=80, unique=True)
_id_cache = cachetools.TTLCache(maxsize=100, ttl=60)
def __str__(self):
return self.name
@classmethod
@cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock)
def get_by_id(cls, id):
return Group.objects.filter(id=id).first()
......@@ -13,15 +13,17 @@ import operator
# Third-party modules
import six
from django.db.models import Model, CharField, ManyToManyField
from django.contrib.auth.models import Group
import cachetools
# NOC modules
from noc.aaa.models.user import User
from noc.aaa.models.group import Group
from noc.core.model.hacks import tuck_up_pants
perm_lock = Lock()
id_lock = Lock()
@tuck_up_pants
@six.python_2_unicode_compatible
class Permission(Model):
"""
......@@ -34,7 +36,7 @@ class Permission(Model):
verbose_name = "Permission"
verbose_name_plural = "Permissions"
db_table = "main_permission"
app_label = "main"
app_label = "aaa"
# module:app:permission
name = CharField("Name", max_length=128, unique=True)
......
......@@ -6,29 +6,100 @@
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from __future__ import absolute_import
import datetime
from threading import Lock
import operator
# Third-party modules
import cachetools
import six
from django.contrib.auth.models import AbstractUser
from django.core.validators import MaxLengthValidator
from django.db import models
from django.core import validators
from django.contrib.auth.hashers import check_password, make_password
# NOC modules
from noc.core.model.hacks import tuck_up_pants
from noc.core.translation import ugettext as _
from .group import Group
id_lock = Lock()
@tuck_up_pants
@six.python_2_unicode_compatible
class User(AbstractUser):
class Meta(AbstractUser.Meta):
class User(models.Model):
class Meta(object):
verbose_name = "User"
verbose_name_plural = "Users"
app_label = "aaa"
# Point to django's auth_user table
# Point to django"s auth_user table
db_table = "auth_user"
abstract = False
ordering = ["username"]
username = models.CharField(
max_length=75,
unique=True,
help_text=_("Required. 30 characters or fewer. Letters, digits and "
"@/./+/-/_ only."),
validators=[
validators.RegexValidator(r"^[\w.@+-]+$", _("Enter a valid username."), "invalid")
])
first_name = models.CharField(max_length=75, blank=True)
last_name = models.CharField(max_length=75, blank=True)
email = models.EmailField(blank=True)
password = models.CharField(max_length=128)
is_active = models.BooleanField(
default=True,
help_text=_("Designates whether this user should be treated as "
"active. Unselect this instead of deleting accounts."))
is_superuser = models.BooleanField(
default=False,
help_text=_(
"Designates that this user has all permissions without "
"explicitly assigning them."))
date_joined = models.DateTimeField(default=datetime.datetime.now)
groups = models.ManyToManyField(
Group,
blank=True,
help_text=_("The groups this user belongs to. A user will "
"get all permissions granted to each of "
"his/her group."),
related_name="user_set", related_query_name="user"
)
_id_cache=cachetools.TTLCache(maxsize=100, ttl=60)
_name_cache = cachetools.TTLCache(maxsize=100, ttl=60)
def __str__(self):
return self.username
@classmethod
@cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock)
def get_by_id(cls, id):
return User.objects.filter(id=id).first()
@classmethod
@cachetools.cachedmethod(operator.attrgetter("_name_cache"), lock=lambda _: id_lock)
def get_by_username(cls, name):
return User.objects.filter(username=name).first()
def is_authenticated(self):
"""
Always return True. This is a way to tell if the user has been
authenticated in templates.
"""
return True
def set_password(self, raw_password):
self.password = make_password(raw_password)
def check_password(self, raw_password):
"""
Returns a boolean of whether the raw_password was correct. Handles
hashing formats behind the scenes.
"""
def setter(raw_password):
self.set_password(raw_password)
self.save(update_fields=["password"])
# Enlarge username field size and replace validators
User._meta.get_field("username").max_length = User._meta.get_field("email").max_length
User._meta.get_field("username").validators = [
v for v in User._meta.get_field("username").validators if not isinstance(v, MaxLengthValidator)
] + [MaxLengthValidator(User._meta.get_field("username").max_length)]
return check_password(raw_password, self.password, setter)
......@@ -13,10 +13,9 @@ import six
from mongoengine.document import Document, EmbeddedDocument
from mongoengine.fields import (StringField, DateTimeField, ListField,
IntField, BinaryField, EmbeddedDocumentField)
# Third-party modules
from django.contrib.auth.models import Group
# NOC modules
from noc.aaa.models.user import User
from noc.aaa.models.group import Group
from noc.lib.nosql import ForeignKeyField
DAL_NONE = -1
......
......@@ -10,6 +10,7 @@
import six
from django.db import models
# NOC modules
from noc.core.model.hacks import tuck_up_pants
from noc.main.models.notificationgroup import NotificationGroup
from noc.sa.models.administrativedomain import AdministrativeDomain
......@@ -17,6 +18,7 @@ OBJECT_TYPES = ["config", "dns", "prefix-list", "rpsl"]
OBJECT_TYPE_CHOICES = [(x, x) for x in OBJECT_TYPES if x != "config"]
@tuck_up_pants
@six.python_2_unicode_compatible
class ObjectNotify(models.Model):
class Meta(object):
......
......@@ -8,7 +8,19 @@
# NOC modules
from noc.core.migration.runner import MigrationRunner
from noc.core.management.base import BaseCommand
class Command(BaseCommand):
"""
Perform database migrations
"""
help = "migrate database"
def handle(self, *args, **options):
runner = MigrationRunner()
runner.migrate()
if __name__ == "__main__":
runner = MigrationRunner()
runner.migrate()
Command().run()
......@@ -2,14 +2,11 @@
# ----------------------------------------------------------------------
# ./noc apply-nri-links
# ----------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
import operator
# Third-party modules
import cachetools
from pymongo.errors import BulkWriteError
from pymongo import UpdateOne
# NOC modules
......@@ -41,10 +38,10 @@ class Command(BaseCommand):
self.stdout.write("Apply NRI links from %s\n" % ExtNRILink._meta["collection"])
for l in ExtNRILink.objects.filter(link__exists=False):
# Get objects
src_mo = self.get_mo(l.src_mo)
src_mo = ManagedObject.get_by_id(l.src_mo)
if not src_mo or src_mo.profile.is_generic:
continue
dst_mo = self.get_mo(l.dst_mo)
dst_mo = ManagedObject.get_by_id(l.dst_mo)
if not dst_mo or dst_mo.profile.is_generic:
continue
#
......@@ -149,13 +146,6 @@ class Command(BaseCommand):
else:
self.stdout.write("Nothing changed\n")
@cachetools.cachedmethod(operator.attrgetter("mo_cache"))
def get_mo(self, mo_id):
try:
return ManagedObject.objects.get(id=int(mo_id))
except ManagedObject.DoesNotExist:
return None
@staticmethod
def get_port_mapper(mo):
if mo.remote_system:
......
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# ./noc rca-debug
# ----------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -17,7 +17,6 @@ from noc.sa.models.managedobject import ManagedObject
from noc.fm.models.archivedalarm import ArchivedAlarm
from noc.sa.models.objectdata import ObjectData
from noc.config import config
from noc.lib.dateutils import total_seconds
Record = namedtuple("Record", [
......@@ -97,7 +96,8 @@ class Command(BaseCommand):
self.print(MASK % x)
if trace:
self.print("Time range: %s -- %s" % (t0, t1))
self.print("Topology RCA Window: %s" % ("%ss" % config.correlator.topology_rca_window if config.correlator.topology_rca_window else "Disabled"))
self.print("Topology RCA Window: %s" % ("%ss" % config.correlator.topology_rca_window if
config.correlator.topology_rca_window else "Disabled"))
amap = dict((a.id, a) for a in alarms.values())
for x in sorted(r, key=operator.attrgetter("timestamp")):
if not x.alarm_id:
......@@ -113,7 +113,7 @@ class Command(BaseCommand):
def can_correlate(a1, a2):
return (
not config.correlator.topology_rca_window or
total_seconds(a1.timestamp - a2.timestamp) <= config.correlator.topology_rca_window
(a1.timestamp - a2.timestamp).total_seconds() <= config.correlator.topology_rca_window
)
ts = ts or alarm.timestamp
......@@ -145,7 +145,8 @@ class Command(BaseCommand):
a = alarms.get(n)
if a and a.timestamp <= ts:
na[n] = a
self.print(" Neighbor alarms: %s" % ", ".join("%s%s (%s)" % ("U:" if x in uplinks else "", na[x], ManagedObject.get_by_id(x).name) for x in na))
self.print(" Neighbor alarms: %s" % ", ".join("%s%s (%s)" % ("U:" if x in uplinks else "", na[x],
ManagedObject.get_by_id(x).name) for x in na))
self.print(" Uplinks: %s" % ", ".join(ManagedObject.get_by_id(u).name for u in uplinks))
if uplinks and len([na[o] for o in uplinks if o in na]) == len(uplinks):
# All uplinks are faulty
......
......@@ -111,7 +111,8 @@ class Command(BaseCommand):
version = {
"vendor": obj.vendor.name if obj.vendor else None,
"platform": obj.platform.name if obj.platform else None,
"version": obj.version.version if obj.version else None
"version": obj.version.version if obj.version else None,
"image": obj.image if obj.image else None
}
else:
version = None
......@@ -386,6 +387,7 @@ class JSONObject(object):
self.vendor = VendorStub(data["vendor"]) if "vendor" in data else None
self.platform = PlatformStub(data["platform"]) if "platform" in data else None
self.version = VersionStub(data["version"]) if "version" in data else None
self.image = data["image"] if "image" in data else None
@property
def credentials(self):
......
......@@ -8,7 +8,7 @@
# NOC modules
from noc.core.management.base import BaseCommand
from noc.main.models.permission import Permission
from noc.aaa.models.permission import Permission
from noc.core.service.loader import get_service
......
......@@ -12,7 +12,7 @@ import random
# NOC modules
from noc.core.management.base import BaseCommand, CommandError
from noc.aaa.models.user import User
from noc.main.models.permission import Permission
from noc.aaa.models.permission import Permission
class Command(BaseCommand):
......
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# Alarms Extractor
# ----------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -16,7 +16,6 @@ from noc.fm.models.alarmclass import AlarmClass
from noc.sa.models.managedobject import ManagedObject
from noc.bi.models.alarms import Alarms
from noc.core.etl.bi.stream import Stream
from noc.lib.dateutils import total_seconds
from noc.config import config
from noc.lib.dateutils import hits_in_range
from noc.sa.models.serviceprofile import ServiceProfile
......@@ -97,7 +96,7 @@ class AlarmsExtractor(ArchivingExtractor):
self.alarm_stream.push(
ts=d["timestamp"],
close_ts=d["clear_timestamp"],
duration=max(0, int(total_seconds(d["clear_timestamp"] - d["timestamp"]))),
duration=max(0, int((d["clear_timestamp"] - d["timestamp"]).total_seconds())),
alarm_id=str(d["_id"]),
root=str(d.get("root") or ""),
alarm_class=AlarmClass.get_by_id(d["alarm_class"]),
......
......@@ -377,7 +377,7 @@ class BaseLoader(object):
nv = sorted(
[
x for x in ov
if not x.startswith(self.system.name + ":")
if not (x.startswith(self.system.name + ":") or x == "remote:deleted")
] + [
"%s:%s" % (self.system.name, x) for x in nv
]
......
......@@ -20,9 +20,9 @@ class LoaderChain(object):
self.loaders = {} # name -> loader
self.lseq = []
self.cache = cachetools.LRUCache(
maxsize=1000,
missing=self.get_cached
maxsize=1000
)
self.cache.__missing__ = self.get_cached
def get_loader(self, name):
loader = self.loaders.get(name)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment