Пример #1
0
    def handle(self, *args, **options):
        aggressive = options['aggressive']

        if options['makeitso']:
            if options['no_input']:
                do_kill = True
            else:
                check_kill = raw_input(
                    """You sure you want to slay these gunicorn processes???

                    Note, you should only do this when you know supervisorctl is in a stuck state.
                    So, in short, I hope you know what you're doing.

                    Proceed with the slaying? (yes/no) """)
                do_kill = check_kill == "yes"

            if do_kill:
                if not aggressive:
                    print "\tShutting down django via supervisor:"
                    self.supervisor("stop")
                print "\tProceeding to kill gunicorn processes."
                self.check_unicorns()
                pk = Popen(['pkill', '-9', '-f', GUNICORN_CMD],
                           stdout=PIPE,
                           stderr=PIPE)
                print pk.stdout.read()

                if not aggressive:
                    print "\tRestarting django via supervisor:"
                    self.supervisor("start")
                AuditEvent.audit_command()
            else:
                print "\tNo slaying...for now"
    def handle(self, *args, **options):
        self.source_uri = getattr(settings, 'PRODUCTION_COUCHDB_URI', None)
        self.target_uri = XFormInstance.get_db().uri
        if self.source_uri is None:
            print "\n\tNo production URI to replicate from, we're done here.\n"
            print "\n\tNo settings.PRODUCTION_COUCHDB_URI has been set\n"
            sys.exit()

        self.domain_list = getattr(settings, 'STAGING_DOMAINS', [])
        if len(self.domain_list) == 0:
            print "\n\tUh, there aren't any domains, so this'll replicate everything, " \
                  "\n\tI don't think you want to do this." \
                  "\n\tPlease set a list of domains in localsettings.STAGING_DOMAINS " \
                  "\n\n\tgoodbye."
            sys.exit()

        if 'cancel' in args:
            self.cancel = True
        else:
            self.cancel = False

        self.repl_domains()
        self.repl_docs()
        self.repl_docs_of_type('CommCareBuild')
        self.repl_docs_of_type('CommCareBuildConfig')
        self.repl_docs_of_type('Organization')
        AuditEvent.audit_command()
Пример #3
0
    def do_process_view(request, view_func, view_args, view_kwargs, extra={}):
        if (getattr(settings, 'AUDIT_MODULES', False) or
                getattr(settings, 'AUDIT_ALL_VIEWS', False) or
                getattr(settings, "AUDIT_VIEWS", False)):

            if hasattr(view_func, 'func_name'): #is this just a plain jane __builtin__.function
                fqview = "%s.%s" % (view_func.__module__, view_func.func_name)
            else:
                #just assess it from the classname for the class based view
                fqview = "%s.%s" % (view_func.__module__, view_func.__class__.__name__)
            if fqview == "django.contrib.staticfiles.views.serve" or fqview == "debug_toolbar.views.debug_media":
                return None

            def check_modules(view, audit_modules):
                return any((view.startswith(m) for m in audit_modules))

            audit_doc = None

            if (fqview.startswith('django.contrib.admin') or fqview.startswith('reversion.admin')) and getattr(settings, "AUDIT_ADMIN_VIEWS", True):
                audit_doc = AuditEvent.audit_view(request, request.user, view_func, view_kwargs)
            elif (check_modules(fqview, settings.AUDIT_MODULES) or
                  fqview in settings.AUDIT_VIEWS or
                  getattr(settings, 'AUDIT_ALL_VIEWS', False)):
                audit_doc = AuditEvent.audit_view(request, request.user, view_func, view_kwargs, extra=extra)
            if audit_doc:
                setattr(request, 'audit_doc', audit_doc)
        return None
Пример #4
0
    def handle(self, *args, **options):
        aggressive = options['aggressive']

        if options['makeitso']:
            if options['no_input']:
                do_kill = True
            else:
                check_kill = raw_input(
                    """You sure you want to slay these gunicorn processes???

                    Note, you should only do this when you know supervisorctl is in a stuck state.
                    So, in short, I hope you know what you're doing.

                    Proceed with the slaying? (yes/no) """)
                do_kill = check_kill == "yes"

            if do_kill:
                if not aggressive:
                    print "\tShutting down django via supervisor:"
                    self.supervisor("stop")
                print "\tProceeding to kill gunicorn processes."
                self.check_unicorns()
                pk = Popen(['pkill', '-9', '-f', GUNICORN_CMD], stdout=PIPE, stderr=PIPE)
                print pk.stdout.read()

                if not aggressive:
                    print "\tRestarting django via supervisor:"
                    self.supervisor("start")
                AuditEvent.audit_command()
            else:
                print "\tNo slaying...for now"
Пример #5
0
def couch_audit_save(instance, *args, **kwargs):
    instance.__orig_save(*args, **kwargs)
    instance_json = instance.to_json()
    from auditcare.models import AuditEvent
    usr = get_current_user()
    if usr != None:
        try:
            User.objects.get(id=usr.id)
        except:
            usr = None
    AuditEvent.audit_couch_save(instance.__class__, instance, instance_json, usr)
Пример #6
0
def couch_audit_save(instance, *args, **kwargs):
    instance.__orig_save(*args, **kwargs)
    instance_json = instance.to_json()
    from auditcare.models import AuditEvent
    usr = get_current_user()
    if usr != None:
        try:
            User.objects.get(id=usr.id)
        except:
            usr = None
    AuditEvent.audit_couch_save(instance.__class__, instance, instance_json, usr)
Пример #7
0
 def testModifyUser(self):
     model_count = ModelActionAudit.view("auditcare/model_actions").count()
     total_count = AuditEvent.view("auditcare/all_events").count()
     
     usr = User.objects.get(username='******')
     usr.first_name='aklsjfl'
     usr.save()
     
     model_count2 = ModelActionAudit.view("auditcare/model_actions").count()
     total_count2 = AuditEvent.view("auditcare/all_events").count()
             
     self.assertEqual(model_count+1, model_count2)    
     self.assertEqual(total_count+1, total_count2)
Пример #8
0
    def testModifyUser(self):
        model_count = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count = AuditEvent.view("auditcare/all_events").count()
        
        usr = User.objects.get(username='******')
        usr.first_name='aklsjfl'
        time.sleep(1)
        usr.save()
        time.sleep(1)

        model_count2 = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count2 = AuditEvent.view("auditcare/all_events").count()
                
        self.assertEqual(model_count+1, model_count2)    
        self.assertEqual(total_count+1, total_count2)
Пример #9
0
    def testModifyUser(self):
        model_count = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count = AuditEvent.view("auditcare/all_events").count()
        
        usr = User.objects.get(username='******')
        usr.first_name='aklsjfl'
        time.sleep(1)
        usr.save()
        time.sleep(1)

        model_count2 = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count2 = AuditEvent.view("auditcare/all_events").count()
                
        self.assertEqual(model_count+1, model_count2)    
        self.assertEqual(total_count+1, total_count2)
Пример #10
0
    def _createUser(self):
        model_count = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count = AuditEvent.view("auditcare/all_events").count()
        
        usr = User()
        usr.username = '******'
        usr.set_password('mockmock')
        usr.first_name='mocky'
        usr.last_name = 'mock'
        usr.save()

        model_count2 = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count2 = AuditEvent.view("auditcare/all_events").count()
        
        self.assertEqual(model_count+1, model_count2)    
        self.assertEqual(total_count+1, total_count2)
Пример #11
0
    def _createUser(self):
        model_count = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count = AuditEvent.view("auditcare/all_events").count()
        
        usr = User()
        usr.username = '******'
        usr.set_password('mockmock')
        usr.first_name='mocky'
        usr.last_name = 'mock'
        usr.save()

        model_count2 = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count2 = AuditEvent.view("auditcare/all_events").count()
        
        self.assertEqual(model_count+1, model_count2)    
        self.assertEqual(total_count+1, total_count2)
Пример #12
0
def django_audit_save(sender, instance, created, **kwargs):
    """
    Audit Save is a signal to attach post_save to any arbitrary django model
    """
    usr = get_current_user()

    # a silly wrap the instance in an array to serialize it, then pull it out of the array to get the json data standalone
    instance_json = json.loads(json_serializer.serialize([instance], ensure_ascii=False))[0]['fields']

    #really stupid sanity check for unit tests when threadlocals doesn't update and user model data is updated.
    if usr != None:
        try:
            User.objects.get(id=usr.id)
        except:
            usr = None
    from auditcare.models import AuditEvent
    AuditEvent.audit_django_save(sender, instance, instance_json, usr)
Пример #13
0
def django_audit_save(sender, instance, created, raw=False, **kwargs):
    """
    Audit Save is a signal to attach post_save to any arbitrary django model
    """
    if raw:
        return

    usr = get_current_user()

    instance_json = model_to_json(instance)

    #really stupid sanity check for unit tests when threadlocals doesn't update and user model data is updated.
    if usr != None:
        try:
            User.objects.get(id=usr.id)
        except:
            usr = None
    from auditcare.models import AuditEvent
    AuditEvent.audit_django_save(sender, instance, instance_json, usr)
Пример #14
0
def django_audit_save(sender, instance, created, raw=False, **kwargs):
    """
    Audit Save is a signal to attach post_save to any arbitrary django model
    """
    if raw:
        return

    usr = get_current_user()

    instance_json = model_to_json(instance)

    #really stupid sanity check for unit tests when threadlocals doesn't update and user model data is updated.
    if usr != None:
        try:
            User.objects.get(id=usr.id)
        except:
            usr = None
    from auditcare.models import AuditEvent
    AuditEvent.audit_django_save(sender, instance, instance_json, usr)
Пример #15
0
    def _createUser(self):
        model_count = ModelActionAudit.view("auditcare/model_actions").count()
        total_count = AuditEvent.view("auditcare/all_events").count()

        usr = User()
        usr.username = '******'
        usr.set_password('mockmock')
        usr.first_name='mocky'
        usr.last_name = 'mock'
        usr.email = '*****@*****.**'
        usr.save()

        self.user = usr

        model_count2 = ModelActionAudit.view("auditcare/model_actions").count()
        total_count2 = AuditEvent.view("auditcare/all_events").count()

        self.assertEqual(model_count+1, model_count2)
        self.assertEqual(total_count+1, total_count2)
        return usr
Пример #16
0
    def _createUser(self):
        model_count = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count = AuditEvent.view("auditcare/all_events").count()

        usr = User()
        usr.username = "******"
        usr.set_password("mockmock")
        usr.first_name = "mocky"
        usr.last_name = "mock"
        usr.email = "*****@*****.**"
        usr.save()

        self.user = usr

        model_count2 = ModelActionAudit.view("auditcare/model_actions_by_id", include_docs=True, reduce=False).count()
        total_count2 = AuditEvent.view("auditcare/all_events").count()

        self.assertEqual(model_count + 1, model_count2)
        self.assertEqual(total_count + 1, total_count2)
        return usr
    def handle(self, *args, **options):
        source_uri = getattr(settings, 'PRODUCTION_COUCHDB_URI', None)
        target_uri = XFormInstance.get_db().uri
        if source_uri is None:
            print "\n\tNo production URI to replicate from, we're done here.\n"
            print "\n\tNo settings.PRODUCTION_COUCHDB_URI has been set\n"
            sys.exit()

        input_query = options['query_string']

        if not input_query:
            print "\tRunning default query for user.is_superuser"
            query_string = BASE_QUERY

        else:
            query_string = input_query

        print "\n\tRunning user query: %s" % query_string

        user_pillow = UserPillow()
        user_es = user_pillow.get_es()

        doc_ids = [res['_id'] for res in get_query_results(user_es, query_string)]

        do_replicate = options['makeitso']
        repl_params = {
            'doc_ids': doc_ids
        }

        if 'cancel' in args:
            repl_params['cancel'] = True
            print "\n\tSending a replication cancel notification to server"
        else:
            print "\n\tStarting staging replication from prod"

        if do_replicate:
            server = CommCareUser.get_db().server
            server.replicate(source_uri, target_uri, **repl_params)
            AuditEvent.audit_command()
        else:
            print "\n\tReplication dry run with params: %s" % repl_params
Пример #18
0
    def handle(self, *args, **options):
        source_uri = getattr(settings, 'PRODUCTION_COUCHDB_URI', None)
        target_uri = XFormInstance.get_db().uri
        if source_uri is None:
            print "\n\tNo production URI to replicate from, we're done here.\n"
            print "\n\tNo settings.PRODUCTION_COUCHDB_URI has been set\n"
            sys.exit()

        input_query = options['query_string']

        if not input_query:
            print "\tRunning default query for user.is_superuser"
            query_string = BASE_QUERY

        else:
            query_string = input_query

        print "\n\tRunning user query: %s" % query_string

        user_pillow = UserPillow()
        user_es = user_pillow.get_es()

        doc_ids = [
            res['_id'] for res in get_query_results(user_es, query_string)
        ]

        do_replicate = options['makeitso']
        repl_params = {'doc_ids': doc_ids}

        if 'cancel' in args:
            repl_params['cancel'] = True
            print "\n\tSending a replication cancel notification to server"
        else:
            print "\n\tStarting staging replication from prod"

        if do_replicate:
            server = CommCareUser.get_db().server
            server.replicate(source_uri, target_uri, **repl_params)
            AuditEvent.audit_command()
        else:
            print "\n\tReplication dry run with params: %s" % repl_params
Пример #19
0
    def do_process_view(request, view_func, view_args, view_kwargs, extra={}):
        if (getattr(settings, 'AUDIT_MODULES', False)
                or getattr(settings, 'AUDIT_ALL_VIEWS', False)
                or getattr(settings, "AUDIT_VIEWS", False)):

            if hasattr(view_func, 'func_name'
                       ):  #is this just a plain jane __builtin__.function
                fqview = "%s.%s" % (view_func.__module__, view_func.__name__)
            else:
                #just assess it from the classname for the class based view
                fqview = "%s.%s" % (view_func.__module__,
                                    view_func.__class__.__name__)
            if fqview == "django.contrib.staticfiles.views.serve" or fqview == "debug_toolbar.views.debug_media":
                return None

            def check_modules(view, audit_modules):
                return any((view.startswith(m) for m in audit_modules))

            audit_doc = None

            if (fqview.startswith('django.contrib.admin')
                    or fqview.startswith('reversion.admin')) and getattr(
                        settings, "AUDIT_ADMIN_VIEWS", True):
                audit_doc = AuditEvent.audit_view(request, request.user,
                                                  view_func, view_kwargs)
            elif (check_modules(fqview, settings.AUDIT_MODULES)
                  or fqview in settings.AUDIT_VIEWS
                  or getattr(settings, 'AUDIT_ALL_VIEWS', False)):
                audit_doc = AuditEvent.audit_view(request,
                                                  request.user,
                                                  view_func,
                                                  view_kwargs,
                                                  extra=extra)
            if audit_doc:
                setattr(request, 'audit_doc', audit_doc)
        return None