~maas-committers/maas/trunk

3865.2.2 by Gavin Panella
Update copyright notices everywhere.
1
# Copyright 2012-2015 Canonical Ltd.  This software is licensed under the
510.4.23 by Gavin Panella
WIP, overriding dbshell command.
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Django command: start a database shell.
5
6
Overrides the default implementation.
7
"""
8
9
__all__ = ['Command']
10
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
11
from optparse import make_option
12
import subprocess
13
14
from django.core.management.base import (
15
    BaseCommand,
16
    CommandError,
3629.7.3 by ubuntudotcom1
Make format.
17
)
510.4.23 by Gavin Panella
WIP, overriding dbshell command.
18
from django.core.management.commands import dbshell
19
20
21
class Command(dbshell.Command):
22
    """Customized "dbshell" command."""
23
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
24
    help = "Interactive psql shell for the MAAS database."
25
26
    option_list = BaseCommand.option_list + (
27
        make_option(
28
            '--database', default=None, help="Database to connect to."),
29
        make_option(
3979.1.1 by Mike Pontillo
Move detection of development database to beginning of dbshell.
30
            '--dev', action='store_true', default=False,
31
            help=(
32
                "Connect to a development database database. "
33
                "Default is to start, and connect to, a system-installed "
34
                "database.")),
35
        make_option(
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
36
            '--installed', '-i', action='store_true', default=False,
37
            help=(
3979.1.1 by Mike Pontillo
Move detection of development database to beginning of dbshell.
38
                "Connect to global, system-installed database. "
39
                "This is the default, unless a development environment is "
40
                "detected.")),
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
41
        )
42
3979.1.1 by Mike Pontillo
Move detection of development database to beginning of dbshell.
43
    def get_development_database(self):
44
        database = None
45
        try:
46
            # Installed systems won't have this fixture.
47
            from maasserver.testing import database
48
        except ImportError:
49
            pass
50
51
        return database
52
510.4.23 by Gavin Panella
WIP, overriding dbshell command.
53
    def handle(self, **options):
3979.1.2 by Mike Pontillo
Address review comments; add test
54
        database_fixture = self.get_development_database()
3979.1.1 by Mike Pontillo
Move detection of development database to beginning of dbshell.
55
        if options.get('dev'):
3979.1.2 by Mike Pontillo
Address review comments; add test
56
            if database_fixture is None:
3979.1.1 by Mike Pontillo
Move detection of development database to beginning of dbshell.
57
                raise CommandError("No development database found.")
58
        elif options.get('installed'):
3979.1.2 by Mike Pontillo
Address review comments; add test
59
            # If we have a development database but the user passed in
60
            # --installed, we need to use the system database instead.
61
            # So clear it out if we found one.
62
            database_fixture = None
3979.1.1 by Mike Pontillo
Move detection of development database to beginning of dbshell.
63
3979.1.2 by Mike Pontillo
Address review comments; add test
64
        if database_fixture is None:
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
65
            # Access the global system-installed MAAS database.
3979.1.2 by Mike Pontillo
Address review comments; add test
66
            database_name = options.get('database')
67
            if database_name is None:
68
                database_name = 'maasdb'
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
69
            try:
70
                subprocess.check_call(
3979.1.2 by Mike Pontillo
Address review comments; add test
71
                    ['sudo', '-u', 'postgres', 'psql', database_name])
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
72
            except subprocess.CalledProcessError:
73
                # If psql fails to run, it will print a message to stderr.
74
                # Capturing that can get a little involved; psql might think
75
                # it was running noninteractively.  So just produce a standard
76
                # error message, and rely on the stderr output coming from
77
                # psql itself.
78
                raise CommandError("psql failed.")
79
        else:
3979.1.1 by Mike Pontillo
Move detection of development database to beginning of dbshell.
80
            print("Using development database.")
81
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
82
            # Don't call up to Django's dbshell, because that ends up exec'ing
83
            # the shell, preventing this from clearing down the fixture.
3979.1.2 by Mike Pontillo
Address review comments; add test
84
            cluster = database_fixture.MAASClusterFixture(
85
                options.get('database'))
1970.1.1 by jtv at canonical
Make ‘dbshell’ management command work for installed MAAS systems as well.
86
            with cluster:
87
                cluster.shell(cluster.dbname)