3
# MAAS database control script. See main() at the bottom for usage.
5
# Most functions take as their first argument a database cluster's data
6
# directory. This is where the database's socket, pidfile, log, and data will
9
# Some design choices for this module:
11
# * Everything is PostgreSQL on Ubuntu.
12
# * Each branch gets its own cluster. Kill & delete when done.
13
# * Databases run under the system user that creates them. No root required.
14
# * No global configuration apart from a basic PostgreSQL install.
15
# * Connections use Unix sockets. No TCP port hogging.
18
PGCTL="/usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_ctl"
21
# Figure out the full absolute data directory path for a given cluster, even
22
# if a relative path is given.
29
echo "Specify a data directory for the MAAS database cluster." >&2
32
if ! echo "$DATADIR" | grep '^/'
39
# Create a database cluster.
40
maasdb_create_cluster() {
42
DATADIR="`maasdb_locate "$1"`"
44
if ! test -d "$DATADIR/base"
46
mkdir -p -- "$DATADIR"
47
$PGCTL init -s -D "$DATADIR" -o '-E utf8 -A trust'
52
# Start a database cluster.
53
maasdb_start_cluster() {
54
local DATADIR DISPOSABLE EXTRA_POSTGRES_OPTS
55
DATADIR="`maasdb_locate "$1"`"
56
# Pass "disposable" as the second argument if the data in this database
57
# is not important at all and you're willing to cut corners for speed.
60
if test "$DISPOSABLE" = "disposable"
62
# -F -- don't bother fsync'ing.
63
EXTRA_POSTGRES_OPTS="-F"
65
EXTRA_POSTGRES_OPTS=""
68
maasdb_create_cluster "$DATADIR"
70
if ! test -f "$DATADIR/postmaster.pid"
73
# -D <dir> -- data directory.
74
# -l <file> -- log file.
75
# -s -- no informational messages.
76
# -w -- wait until startup is complete.
78
# -h <arg> -- host name; empty arg means Unix socket only.
79
# -k -- socket directory.
81
-D "$DATADIR" -l "$DATADIR/backend.log" -s -w \
82
-o "-h '' -k '$DATADIR' $EXTRA_POSTGRES_OPTS"
87
# Stop a database cluster.
88
maasdb_stop_cluster() {
90
DATADIR="`maasdb_locate "$1"`"
92
if test -f "$DATADIR/postmaster.pid"
94
$PGCTL stop -W -m fast -D "$DATADIR"
99
# Initialize a MAAS database.
101
local DATADIR DISPOSABLE MARKER
102
DATADIR="`maasdb_locate "$1"`"
103
# Pass "disposable" as the second argument if the data in this database
104
# is not important at all and you're willing to cut corners for speed.
107
maasdb_start_cluster "$DATADIR" "$DISPOSABLE"
109
MARKER="$DATADIR/maas-created"
110
if ! test -f "$MARKER"
112
createdb -h "$DATADIR" maas && touch "$MARKER"
117
# Open a psql shell on a MAAS database.
120
DATADIR="`maasdb_locate "$1"`"
122
maasdb_init_db "$DATADIR"
123
psql -h "$DATADIR" maas
127
# Execute a query on a MAAS database.
130
DATADIR="`maasdb_locate "$1"`"
133
maasdb_init_db "$DATADIR"
134
psql -h "$DATADIR" maas -c "$QUERY"
138
# Delete an entire MAAS database and cluster. Use only with extreme care!
139
maasdb_delete_cluster() {
141
DATADIR="`maasdb_locate "$1"`"
143
# Before deleting anything, does this at least look like a MAAS database
145
if test -d "$DATADIR/base"
147
maasdb_stop_cluster "$DATADIR"
148
# Removing the data directory may fail because of a race condition
149
# where db/global is nonempty because of a statistics write. Rather
150
# than block on shutdown, be optimistic and spin on retries.
151
while ! rm -rf -- "$DATADIR"
153
# If this fails for a more persistent reason, too bad. Ctrl-C.
154
echo "Retrying deletion of $DATADIR." >&2
163
Usage: maasdb <command> <cluster-path>
165
Where <command> is one of:
172
And <cluster-path> is the path to the MAAS development database cluster,
179
echo >&2 "** Unknown command: $1 **"
187
local COMMAND DATADIR
197
start) maasdb_init_db "$DATADIR" "$@" ;;
198
stop) maasdb_stop_cluster "$DATADIR" "$@" ;;
199
query) maasdb_query "$DATADIR" "$@" ;;
200
shell) maasdb_shell "$DATADIR" "$@" ;;
201
delete-cluster) maasdb_delete_cluster "$DATADIR" "$@" ;;
202
*) unknown_command "$COMMAND" ;;