diff --git a/mmc/admin.py b/mmc/admin.py index 0ebb42a..9abc10a 100644 --- a/mmc/admin.py +++ b/mmc/admin.py @@ -61,17 +61,19 @@ def has_delete_permission(self, request, obj=None): class MMCScriptAdmin(MMCHostAdmin): list_display = ( 'name', 'calls', 'ignore', 'one_copy', 'save_on_error', - 'real_time', 'enable_queries', 'created', 'id') + 'real_time', 'enable_queries', 'temporarily_disabled', 'created', 'id') list_filter = ( - 'ignore', 'one_copy', 'save_on_error', 'real_time', 'enable_queries') + 'temporarily_disabled', 'ignore', 'one_copy', + 'save_on_error', 'real_time', 'enable_queries',) + # list_editable = list_filter fieldsets = [ ('Basic', {'fields': [ - 'ignore', 'one_copy', 'save_on_error', - 'real_time', 'enable_queries', 'interval_restriction' + 'temporarily_disabled', 'ignore', 'one_copy', 'save_on_error', + 'real_time', 'enable_queries', 'interval_restriction', ]}), ('Triggers', {'fields': [ 'enable_triggers', 'trigger_time', 'trigger_memory', - 'trigger_cpu', 'trigger_queries' + 'trigger_cpu', 'trigger_queries', ]}), ] diff --git a/mmc/migrations/0014_mmcscript_temporarily_disabled.py b/mmc/migrations/0014_mmcscript_temporarily_disabled.py new file mode 100644 index 0000000..6cb99b2 --- /dev/null +++ b/mmc/migrations/0014_mmcscript_temporarily_disabled.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('mmc', '0013_mmclog_is_fixed'), + ] + + operations = [ + migrations.AddField( + model_name='mmcscript', + name='temporarily_disabled', + field=models.BooleanField(default=False, help_text=b'Temporarily disable script execution'), + ), + ] diff --git a/mmc/mixins.py b/mmc/mixins.py index ed00d26..ec0b173 100644 --- a/mmc/mixins.py +++ b/mmc/mixins.py @@ -128,6 +128,19 @@ def __mmc_run_is_allowed(self): except Exception as err: stderr("[MMC] {0}".format(err)) + def __mmc_is_enabled(self): + try: + from mmc.models import MMCScript + + try: + if not MMCScript.run_is_enabled(self._mmc_script): + sys.stdout.write("Temporarily disabled\n") + sys.exit(-1) + except MMCScript.DoesNotExist: + pass + except Exception as err: + stderr("[MMC] {0}".format(err)) + def __mmc_one_copy(self): try: from mmc.models import MMCScript @@ -164,6 +177,7 @@ def __mmc_stop_monitor(self): def __mmc_init(self, **options): if not mmc_is_test(): self.__mmc_run_is_allowed() + self.__mmc_is_enabled() self.__mmc_one_copy() atexit.register(self._mmc_at_exit_callback) self._mmc_show_traceback = options.get('traceback', False) diff --git a/mmc/models.py b/mmc/models.py index dd13ee7..66db1ea 100644 --- a/mmc/models.py +++ b/mmc/models.py @@ -62,6 +62,8 @@ class MMCScript(models.Model): default=False, help_text='Enable triggers for receive email ' 'notification, if threshold of counters ' 'will be exceeded') + temporarily_disabled = models.BooleanField( + default=False, help_text='Temporarily disable script execution') def update_calls(self): MMCScript.objects.filter(pk=self.pk).update( @@ -85,6 +87,12 @@ def run_is_allowed(cls, name): return False return True + @classmethod + def run_is_enabled(cls, name): + script = cls.objects.get(name=name) + + return not script.temporarily_disabled + class Meta: verbose_name = 'Script' verbose_name_plural = 'Scripts' diff --git a/mmc/south_migrations/0021_auto__add_field_mmcscript_temporarily_disabled.py b/mmc/south_migrations/0021_auto__add_field_mmcscript_temporarily_disabled.py new file mode 100644 index 0000000..75e5dcc --- /dev/null +++ b/mmc/south_migrations/0021_auto__add_field_mmcscript_temporarily_disabled.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding field 'MMCScript.temporarily_disabled' + db.add_column('mmc_mmcscript', 'temporarily_disabled', + self.gf('django.db.models.fields.BooleanField')(default=False), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'MMCScript.temporarily_disabled' + db.delete_column('mmc_mmcscript', 'temporarily_disabled') + + + models = { + 'mmc.mmcemail': { + 'Meta': {'object_name': 'MMCEmail'}, + 'created': ('django.db.models.fields.DateField', [], {'auto_now': 'True', 'blank': 'True'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ignore': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['mmc.MMCScript']", 'null': 'True', 'blank': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}) + }, + 'mmc.mmchost': { + 'Meta': {'object_name': 'MMCHost'}, + 'created': ('django.db.models.fields.DateField', [], {'auto_now': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ignore': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}) + }, + 'mmc.mmclog': { + 'Meta': {'object_name': 'MMCLog'}, + 'cpu_time': ('django.db.models.fields.FloatField', [], {'default': '0.0'}), + 'created': ('django.db.models.fields.DateField', [], {'auto_now': 'True', 'blank': 'True'}), + 'elapsed': ('django.db.models.fields.FloatField', [], {}), + 'end': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'error_message': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'hostname': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['mmc.MMCHost']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_fixed': ('django.db.models.fields.NullBooleanField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}), + 'memory': ('django.db.models.fields.FloatField', [], {'default': '0.0'}), + 'pid': ('django.db.models.fields.IntegerField', [], {'default': '1'}), + 'queries': ('django.db.models.fields.BigIntegerField', [], {'default': '0'}), + 'script': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['mmc.MMCScript']"}), + 'start': ('django.db.models.fields.DateTimeField', [], {}), + 'stdout_messages': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'success': ('django.db.models.fields.NullBooleanField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}), + 'sys_argv': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}), + 'traceback': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'was_notified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}) + }, + 'mmc.mmcscript': { + 'Meta': {'object_name': 'MMCScript'}, + 'calls': ('django.db.models.fields.BigIntegerField', [], {'default': '0'}), + 'created': ('django.db.models.fields.DateField', [], {'auto_now': 'True', 'blank': 'True'}), + 'enable_queries': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'enable_triggers': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ignore': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'interval_restriction': ('django.db.models.fields.PositiveIntegerField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}), + 'one_copy': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'real_time': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'save_on_error': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'temporarily_disabled': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'trigger_cpu': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), + 'trigger_memory': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), + 'trigger_queries': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), + 'trigger_time': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}) + } + } + + complete_apps = ['mmc'] \ No newline at end of file