~andreserl/maas/packaging_lp1413388

« back to all changes in this revision

Viewing changes to debian/extras/maas-region-admin

  • Committer: MAAS Lander
  • Author(s): Andres Rodriguez, Gavin Panella
  • Date: 2015-06-24 21:36:21 UTC
  • mfrom: (381.2.14 packaging.regiond-conf)
  • Revision ID: maas_lander-20150624213621-odxv7u7bi72a2bw4
[r=andreserl][bug=][author=allenap] Use new local configuration support for the region.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
 
2
# Copyright 2015 Canonical Ltd.  This software is licensed under the
 
3
# GNU Affero General Public License version 3 (see the file LICENSE).
 
4
 
 
5
from __future__ import (
 
6
    absolute_import,
 
7
    print_function,
 
8
    unicode_literals,
 
9
)
 
10
 
 
11
str = None
 
12
 
 
13
__metaclass__ = type
 
14
__all__ = []
 
15
 
 
16
import grp
2
17
import os
3
18
import sys
4
19
 
5
 
user_id = os.getuid()
6
 
if user_id != 0:
7
 
    print("This utility may only be run as root.")
8
 
    sys.exit(1)
9
 
 
10
 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "maas.settings")
11
 
sys.path.append('/usr/share/maas')
12
 
 
13
 
# Use Django 1.6 if the python-django16 package is installed: this is
14
 
# to get MAAS to work on vivid: vivid ships with Django 1.7 by default
15
 
# and MAAS isn't yet compatible with Django 1.7.
16
 
if os.path.exists('/usr/lib/django16'):
17
 
    sys.path.insert(1, '/usr/lib/django16')
18
 
 
19
 
from django.core import management
 
20
 
 
21
def check_user():
 
22
    # At present, only root should execute this.
 
23
    if os.getuid() != 0:
 
24
        raise SystemExit("This utility may only be run as root.")
 
25
 
 
26
 
 
27
def set_group():
 
28
    # Ensure that we're running as the `maas` group.
 
29
    try:
 
30
        gr_maas = grp.getgrnam("maas")
 
31
    except KeyError:
 
32
        raise SystemExit("No such group: maas")
 
33
    else:
 
34
        os.setegid(gr_maas.gr_gid)
 
35
 
 
36
 
 
37
def set_umask():
 
38
    # Prevent creation of world-readable (or writable, executable) files.
 
39
    os.umask(0o007)
 
40
 
 
41
 
 
42
def run():
 
43
    # Force the production MAAS Django configuration.
 
44
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "maas.settings")
 
45
 
 
46
    # The Django configuration lives outside of sys.path.
 
47
    sys.path.append('/usr/share/maas')
 
48
 
 
49
    # Use Django 1.6 if the python-django16 package is installed: this is to
 
50
    # get MAAS to work on vivid: vivid ships with Django 1.7 by default and
 
51
    # MAAS isn't yet compatible with Django 1.7.
 
52
    if os.path.exists('/usr/lib/django16'):
 
53
        sys.path.insert(1, '/usr/lib/django16')
 
54
 
 
55
    # Let Django do the rest.
 
56
    from django.core import management
 
57
    management.execute_from_command_line()
 
58
 
 
59
 
 
60
def main():
 
61
    check_user()
 
62
    set_group()
 
63
    set_umask()
 
64
    run()
 
65
 
20
66
 
21
67
if __name__ == "__main__":
22
 
    management.execute_from_command_line()
 
68
    main()