/usr/lib/python2.7/dist-packages/maasserver/management/commands/dbshell.py is in python-django-maas 1.5.4+bzr2294-0ubuntu1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | # Copyright 2012-2014 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Django command: start a database shell.
Overrides the default implementation.
"""
from __future__ import (
absolute_import,
print_function,
unicode_literals,
)
str = None
__metaclass__ = type
__all__ = ['Command']
from optparse import make_option
import subprocess
from django.core.management.base import (
BaseCommand,
CommandError,
)
from django.core.management.commands import dbshell
class Command(dbshell.Command):
"""Customized "dbshell" command."""
help = "Interactive psql shell for the MAAS database."
option_list = BaseCommand.option_list + (
make_option(
'--database', default=None, help="Database to connect to."),
make_option(
'--installed', '-i', action='store_true', default=False,
help=(
"Connect to global, system-installed database. "
"Default is to start, and connect to, database in a "
"development branch.")),
)
def handle(self, **options):
if options.get('installed'):
# Access the global system-installed MAAS database.
database = options.get('database')
if database is None:
database = 'maasdb'
try:
subprocess.check_call(
['sudo', '-u', 'postgres', 'psql', database])
except subprocess.CalledProcessError:
# If psql fails to run, it will print a message to stderr.
# Capturing that can get a little involved; psql might think
# it was running noninteractively. So just produce a standard
# error message, and rely on the stderr output coming from
# psql itself.
raise CommandError("psql failed.")
else:
# Don't call up to Django's dbshell, because that ends up exec'ing
# the shell, preventing this from clearing down the fixture.
# Import fixture here, because installed systems won't have it.
try:
from maasserver.testing import database
except ImportError as e:
raise ImportError(
unicode(e) + "\n"
"If this is an installed MAAS, use the --installed "
"option.")
cluster = database.MAASClusterFixture(options.get('database'))
with cluster:
cluster.shell(cluster.dbname)
|