From 2156c9bb3e410ef5b84fd08127f1c0508736e057 Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Sat, 19 Sep 2009 23:00:57 +0200 Subject: [PATCH] add a "checkenv" command to manage.py that runs a few tests on the setup of mumble-django, to prevent configuration problems like bug #30. --- pyweb/mumble/management/__init__.py | 8 ++ pyweb/mumble/management/commands/__init__.py | 0 pyweb/mumble/management/commands/checkenv.py | 100 ++++++++++++++++++ .../server_detect.py} | 12 +-- 4 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 pyweb/mumble/management/__init__.py create mode 100644 pyweb/mumble/management/commands/__init__.py create mode 100644 pyweb/mumble/management/commands/checkenv.py rename pyweb/mumble/{management.py => management/server_detect.py} (97%) diff --git a/pyweb/mumble/management/__init__.py b/pyweb/mumble/management/__init__.py new file mode 100644 index 0000000..adb1c17 --- /dev/null +++ b/pyweb/mumble/management/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + +from server_detect import find_existing_instances +from django.db.models import signals +from mumble import models + +signals.post_syncdb.connect( find_existing_instances, sender=models ); + diff --git a/pyweb/mumble/management/commands/__init__.py b/pyweb/mumble/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyweb/mumble/management/commands/checkenv.py b/pyweb/mumble/management/commands/checkenv.py new file mode 100644 index 0000000..c630c58 --- /dev/null +++ b/pyweb/mumble/management/commands/checkenv.py @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- + +import os + +from django.core.management.base import BaseCommand +from django.contrib.auth.models import User +from django.contrib.sites.models import Site +from django.conf import settings + +from mumble.models import Mumble + +class TestFailed( Exception ): + pass; + +class Command( BaseCommand ): + def handle(self, **options): + self.check_dbase(); + self.check_sites(); + self.check_mumbles(); + self.check_admins(); + + + def check_dbase( self ): + print "Checking database access...", + if settings.DATABASE_ENGINE == "sqlite3": + if not os.path.exists( settings.DATABASE_NAME ): + raise TestFailed( "database does not exist. Have you run syncdb yet?" ); + + else: + statinfo = os.stat( settings.DATABASE_NAME ); + + if statinfo.st_uid == 0: + raise TestFailed( "" + "the database file belongs to root. This is most certainly not what " + "you want because it will prevent your web server from being able " + "to write to it. Please check." ); + + elif not os.access( settings.DATABASE_NAME, os.W_OK ): + raise TestFailed( "database file is not writable." ); + + else: + print "[ OK ]"; + + else: + print "not using sqlite, so I can't check."; + + + def check_sites( self ): + print "Checking URL configuration...", + + site = Site.objects.get_current(); + if site.domain == 'example.com': + print( "The domain is configured as example.com, which is the default but does not make sense." + "Please enter the domain where Mumble-Django is reachable." ); + + site.domain = raw_input( "> " ).strip(); + site.save(); + + else: + print site.domain, "[ OK ]"; + + + def check_admins( self ): + print "Checking if an Admin user exists...", + + for user in User.objects.all(): + if user.is_superuser: + print "[ OK ]"; + return; + + raise TestFailed( "" + "No admin user exists, so you won't be able to log in to the admin system. You " + "should run `./manage.py createsuperuser` to create one." ); + + + def check_mumbles( self ): + print "Checking Murmur instances...", + + mm = Mumble.objects.all(); + + if mm.count() == 0: + raise TestFailed( "" + "no Mumble servers are configured, you might want to run " + "`./manage.py syncdb` to run an auto detection." ); + + else: + for mumble in mm: + try: + ctl = mumble.ctl; + except Exception, err: + raise TestFailed( + "Connecting to Murmur `%s` (%s) failed: %s" % ( mumble.name, mumble.dbus, err ) + ); + print "[ OK ]"; + + + + + + diff --git a/pyweb/mumble/management.py b/pyweb/mumble/management/server_detect.py similarity index 97% rename from pyweb/mumble/management.py rename to pyweb/mumble/management/server_detect.py index dd444be..f81fd3e 100644 --- a/pyweb/mumble/management.py +++ b/pyweb/mumble/management/server_detect.py @@ -16,10 +16,8 @@ import os -import models -from django.db.models import signals - -from mctl import * +from mumble import models +from mumble.mctl import * def find_in_dicts( keys, conf, default, valueIfNotFound=None ): if not isinstance( keys, tuple ): @@ -170,9 +168,3 @@ def find_existing_instances( **kwargs ): return True; -signals.post_syncdb.connect( find_existing_instances, sender=models ); - - - - -