source: trunk/sara_cmt/sara_cmt/settings.py @ 14161

Last change on this file since 14161 was 14161, checked in by ramonb, 12 years ago

sara_cmt/settings.py:

  • some semi-intelligent config file checks, warnings and hint
  • check if config file may be outdated
  • print some suggestions to resolve them
  • see #9
File size: 8.4 KB
Line 
1
2import os, os.path, sys, ConfigParser, site, string, time
3
4from socket import gethostbyname_ex
5
6# Path's customizable through virtualenv
7sample_configfile = '%s/etc/cmt/cmt.conf.sample' % site.sys.prefix
8configfile = '%s/etc/cmt/cmt.conf' % site.sys.prefix
9
10def count_configlines( filename ):
11
12        line_count      = 0
13        cfg_fp          = open( filename )
14
15        for line in cfg_fp.readlines():
16
17                line = line.strip()
18
19                if len( line ) == 0:
20                        continue
21
22                # RB: ConfigParser considers lines starting with # or ; as comments
23                if line[0] == '#' or line[0] == ';':
24                        continue
25
26                line_count += 1
27
28        cfg_fp.close()
29
30        return line_count
31
32if not os.path.exists( configfile ):
33
34        print 'Unable to find confilefile: %s' %configfile
35
36        if os.path.exists( sample_configfile ):
37
38                print 'Please modify the sample config file: %s to reflect your settings' %sample_configfile
39                print 'and then rename it to: %s' %configfile
40
41        else:
42
43                print 'Also no sample config file was found: %s' %sample_configfile
44                print 'Something is terribly wrong here ;)'
45
46        print ''
47        print 'Fatal: Giving up and exiting now..'
48
49        sys.exit(1)
50
51# We are still here: both configfile AND sample_configfile found
52if exists( sample_configfile ):
53
54        # Is the sample configfile newer?
55        if os.path.getmtime( sample_configfile ) > os.path.getmtime( configfile ):
56
57                # Well this is weird, but not fatal
58                print 'Warning: sample config file(%s) is newer than original config(%s)' %(configfile, sample_configfile)
59
60                # Does the sample config file contain more options?
61                if count_configlines( sample_configfile ) > count_configlines( configfile ):
62
63                        print 'Warning: sample config file contains MORE OPTIONS than original config!'
64                        print ''
65                        print 'This happens for example if you upgraded CMT and the new release incorporates new configuration options!'
66                        print ''
67                        print 'Please update your original config(%s) to incorporate the new config options from sample config(%s)' %( configfile, sample_configfile )
68                        print ''
69
70                else:
71                        # Config is good but mtime is older, weird.. Just print hint
72                        print 'Hint: Remove sample config file(%s) to get rid of this warning..' %sample_configfile
73
74                # Give them some time to think about warnings and generally annoy them just enough to fix it
75                time.sleep(2)
76
77                # Moving right along; print empty line for cosmetic reasons
78                print ''
79
80config = ConfigParser.RawConfigParser()
81config.read( configfile )
82
83try:
84        DATABASE_USER           = config.get('database', 'USER')
85        DATABASE_PASSWORD       = config.get('database', 'PASSWORD')
86        DATABASE_HOST           = config.get('database', 'HOST')
87        DATABASE_ENGINE         = config.get('database', 'ENGINE')
88        DATABASE_NAME           = config.get('database', 'NAME')
89
90except (ConfigParser.NoOptionError, ConfigParser.NoSectionError), details:
91
92        print 'Config file error: %s' %str(details)
93        print ''
94        print 'Giving up and exiting now..'
95        sys.exit(1)
96
97try: # Optional
98        DATABASE_PORT = config.get('database', 'PORT')
99
100except ConfigParser.NoOptionError, details:
101
102        pass
103
104try: # Optional
105        TEST_DATABASE_NAME = config.get('database', 'TEST_NAME')
106
107except ConfigParser.NoOptionError, details:
108
109        pass
110
111try:
112        gethostbyname_ex( DATABASE_HOST )
113
114except socket.gaierror, details:
115
116        print 'Unable to resolve database host: %s' %DATABASE_HOST
117        print ''
118        print 'Giving up and exiting now..'
119        sys.exit(1)
120
121# Documentation of settings can be found on:
122#
123#   http://docs.djangoproject.com/en/dev/ref/settings/
124
125# Only set CLIENT_ONLY to False on the central CMT-server
126CLIENT_ONLY = True
127
128DEBUG = True
129
130ADMINS = (
131    ('Sil Westerveld', 'sil.westerveld@sara.nl'),
132    #('Your Name', 'your_email@domain.com'),
133)
134
135MANAGERS = ADMINS
136
137#####
138#
139# <AUTH AGAINST LDAP> (based on http://packages.python.org/django-auth-ldap/)
140#
141if not CLIENT_ONLY:
142    import ldap
143    from django_auth_ldap.config import LDAPSearch, PosixGroupType
144
145
146    # Baseline configuration.
147    AUTH_LDAP_SERVER_URI = "ldaps://ldap.cua.sara.nl"
148
149    # Set AUTH_LDAP_USER_DN_TEMPLATE to a template that will produce the
150    # authenticating user's DN directly. This template should have one
151    # placeholder, %(user)s.
152    AUTH_LDAP_USER_DN_TEMPLATE = 'uid=%(user)s,ou=Users,dc=hpcv,dc=sara,dc=nl'
153
154    # Set up the basic group parameters.
155    AUTH_LDAP_GROUP_SEARCH = LDAPSearch('ou=Groups,dc=hpcv,dc=sara,dc=nl',
156        ldap.SCOPE_SUBTREE, '(objectClass=posixGroup)',
157    )
158    AUTH_LDAP_GROUP_TYPE = PosixGroupType()
159
160    ## Only users in this group can log in.
161    AUTH_LDAP_REQUIRE_GROUP = 'cn=cmt,ou=Groups,dc=hpcv,dc=sara,dc=nl'
162
163    # Populate the Django user from the LDAP directory.
164    AUTH_LDAP_USER_ATTR_MAP = {
165        'first_name': 'givenName',
166        'last_name': 'sn',
167        'email': 'mail',
168    }
169
170    AUTH_LDAP_USER_FLAGS_BY_GROUP = {
171        'is_active': 'cn=cmt,ou=Groups,dc=hpcv,dc=sara,dc=nl',
172        'is_staff': 'cn=cmt,ou=Groups,dc=hpcv,dc=sara,dc=nl',
173        'is_superuser': 'cn=cmt,ou=Groups,dc=hpcv,dc=sara,dc=nl',
174    }
175
176    # This is the default, but I like to be explicit.
177    AUTH_LDAP_ALWAYS_UPDATE_USER = True
178
179    # Cache group memberships for an hour to minimize LDAP traffic
180    AUTH_LDAP_CACHE_GROUPS = True
181    AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
182
183    # Keep ModelBackend around for per-user permissions and maybe a local
184    # superuser.
185    AUTHENTICATION_BACKENDS = (
186        'django_auth_ldap.backend.LDAPBackend',
187        'django.contrib.auth.backends.ModelBackend',
188    )
189#
190# </AUTH AGAINST LDAP>
191#
192#####
193
194
195
196
197
198# Local time zone for this installation. Choices can be found here:
199# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
200# although not all choices may be available on all operating systems.
201# If running in a Windows environment this must be set to the same as your
202# system time zone.
203TIME_ZONE = 'Europe/Amsterdam'
204
205# Language code for this installation. All choices can be found here:
206# http://www.i18nguy.com/unicode/language-identifiers.html
207LANGUAGE_CODE = 'en-us'
208
209SITE_ID = 1
210
211# If you set this to False, Django will make some optimizations so as not
212# to load the internationalization machinery.
213USE_I18N = True
214
215# Absolute path to the directory that holds media.
216# Example: "/home/media/media.lawrence.com/"
217MEDIA_ROOT = ''
218
219# URL that handles the media served from MEDIA_ROOT. Make sure to use a
220# trailing slash if there is a path component (optional in other cases).
221# Examples: "http://media.lawrence.com", "http://example.com/media/"
222MEDIA_URL = ''
223
224# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
225# trailing slash.
226# Examples: "http://foo.com/media/", "/media/".
227ADMIN_MEDIA_PREFIX = '/admin_media/'
228
229# Make this unique, and don't share it with anybody.
230SECRET_KEY = 'uygv6wrel4o2%x8s4dk2%i6=dp!2bt32$0ne-%_7&j=ez*u$1b'
231
232# List of callables that know how to import templates from various sources.
233TEMPLATE_LOADERS = (
234    'django.template.loaders.filesystem.load_template_source',
235    'django.template.loaders.app_directories.load_template_source',
236    #'django.template.loaders.eggs.load_template_source',
237)
238
239MIDDLEWARE_CLASSES = (
240    'django.middleware.common.CommonMiddleware',
241    'django.contrib.sessions.middleware.SessionMiddleware',
242    'django.contrib.auth.middleware.AuthenticationMiddleware',
243    'debug_toolbar.middleware.DebugToolbarMiddleware',
244)
245
246ROOT_URLCONF = 'sara_cmt.urls'
247
248# Templates for the CMT command line interface.
249# (thus, the templates for our configfiles, etc)
250#TODO: think about a (better) way to make this dynamic:
251#TODO: get this out of the settings.py, since it should be in the client config
252CMT_TEMPLATES_DIR = '%s/etc/cmt/templates' % site.sys.prefix
253
254# Templates for the CMT web-frontend.
255TEMPLATE_DIRS = (
256    # Put strings here, like "/home/html/django_templates"
257    # or "C:/www/django/templates".
258    # Always use forward slashes, even on Windows.
259    # Don't forget to use absolute paths, not relative paths.
260    os.path.normpath(os.path.join(os.path.dirname(__file__), 'cluster/templates')),
261    CMT_TEMPLATES_DIR,
262)
263
264FIXTURE_DIRS = (
265    # A fixture is a collection of files that contain serialized contents of
266    # the database. (can be used for testing)
267    os.path.normpath(os.path.join(os.path.dirname(__file__), 'fixtures')),
268)
269
270
271INSTALLED_APPS = (
272    'django.contrib.auth',
273    'django.contrib.contenttypes',
274    'django.contrib.sessions',
275    'django.contrib.admin',
276    'django.contrib.databrowse',
277    'django.contrib.webdesign',
278    'sara_cmt.cluster',
279    'django_extensions',
280    'tagging',
281
282    # Only serverside:
283    #'debug_toolbar',
284    #'south',
285)
286
287# Append your IP to use the debug_toolbar
288INTERNAL_IPS = (
289    #'145.100.6.163', # saralt0115
290    '127.0.0.1',
291)
Note: See TracBrowser for help on using the repository browser.