~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to utilities/maasdb

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /bin/bash -e
2
 
#
3
 
# MAAS database control script.  See main() at the bottom for usage.
4
 
#
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
7
 
# reside.
8
 
#
9
 
# Some design choices for this module:
10
 
#
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.
16
 
 
17
 
POSTGRES_VERSION=9.1
18
 
PGCTL="/usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_ctl"
19
 
 
20
 
 
21
 
# Figure out the full absolute data directory path for a given cluster, even
22
 
# if a relative path is given.
23
 
maasdb_locate() {
24
 
    local DATADIR
25
 
    DATADIR="$1"
26
 
 
27
 
    if test -z "$1"
28
 
    then
29
 
        echo "Specify a data directory for the MAAS database cluster." >&2
30
 
        return 1
31
 
    fi
32
 
    if ! echo "$DATADIR" | grep '^/'
33
 
    then
34
 
        echo "`pwd`/$DATADIR"
35
 
    fi
36
 
}
37
 
 
38
 
 
39
 
# Create a database cluster.
40
 
maasdb_create_cluster() {
41
 
    local DATADIR
42
 
    DATADIR="`maasdb_locate "$1"`"
43
 
 
44
 
    if ! test -d "$DATADIR/base"
45
 
    then
46
 
        mkdir -p -- "$DATADIR"
47
 
        $PGCTL init -s -D "$DATADIR" -o '-E utf8 -A trust'
48
 
    fi
49
 
}
50
 
 
51
 
 
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.
58
 
    DISPOSABLE="$2"
59
 
 
60
 
    if test "$DISPOSABLE" = "disposable"
61
 
    then
62
 
        #  -F -- don't bother fsync'ing.
63
 
        EXTRA_POSTGRES_OPTS="-F"
64
 
    else
65
 
        EXTRA_POSTGRES_OPTS=""
66
 
    fi
67
 
 
68
 
    maasdb_create_cluster "$DATADIR"
69
 
 
70
 
    if ! test -f "$DATADIR/postmaster.pid"
71
 
    then
72
 
        # pg_ctl options:
73
 
        #  -D <dir> -- data directory.
74
 
        #  -l <file> -- log file.
75
 
        #  -s -- no informational messages.
76
 
        #  -w -- wait until startup is complete.
77
 
        # postgres options:
78
 
        #  -h <arg> -- host name; empty arg means Unix socket only.
79
 
        #  -k -- socket directory.
80
 
        $PGCTL start \
81
 
            -D "$DATADIR" -l "$DATADIR/backend.log" -s -w \
82
 
            -o "-h '' -k '$DATADIR' $EXTRA_POSTGRES_OPTS"
83
 
    fi
84
 
}
85
 
 
86
 
 
87
 
# Stop a database cluster.
88
 
maasdb_stop_cluster() {
89
 
    local DATADIR
90
 
    DATADIR="`maasdb_locate "$1"`"
91
 
 
92
 
    if test -f "$DATADIR/postmaster.pid"
93
 
    then
94
 
        $PGCTL stop -W -m fast -D "$DATADIR"
95
 
    fi
96
 
}
97
 
 
98
 
 
99
 
# Initialize a MAAS database.
100
 
maasdb_init_db() {
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.
105
 
    DISPOSABLE="$2"
106
 
 
107
 
    maasdb_start_cluster "$DATADIR" "$DISPOSABLE"
108
 
 
109
 
    MARKER="$DATADIR/maas-created"
110
 
    if ! test -f "$MARKER"
111
 
    then
112
 
        createdb -h "$DATADIR" maas && touch "$MARKER"
113
 
    fi
114
 
}
115
 
 
116
 
 
117
 
# Open a psql shell on a MAAS database.
118
 
maasdb_shell() {
119
 
    local DATADIR
120
 
    DATADIR="`maasdb_locate "$1"`"
121
 
 
122
 
    maasdb_init_db "$DATADIR"
123
 
    psql -h "$DATADIR" maas
124
 
}
125
 
 
126
 
 
127
 
# Execute a query on a MAAS database.
128
 
maasdb_query() {
129
 
    local DATADIR QUERY
130
 
    DATADIR="`maasdb_locate "$1"`"
131
 
    QUERY="$2"
132
 
 
133
 
    maasdb_init_db "$DATADIR"
134
 
    psql -h "$DATADIR" maas -c "$QUERY"
135
 
}
136
 
 
137
 
 
138
 
# Delete an entire MAAS database and cluster.  Use only with extreme care!
139
 
maasdb_delete_cluster() {
140
 
    local DATADIR
141
 
    DATADIR="`maasdb_locate "$1"`"
142
 
 
143
 
    # Before deleting anything, does this at least look like a MAAS database
144
 
    # cluster?
145
 
    if test -d "$DATADIR/base"
146
 
    then
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"
152
 
        do
153
 
            # If this fails for a more persistent reason, too bad.  Ctrl-C.
154
 
            echo "Retrying deletion of $DATADIR." >&2
155
 
            sleep 0.01
156
 
        done
157
 
    fi
158
 
}
159
 
 
160
 
 
161
 
usage() {
162
 
    cat <<EOF >&2
163
 
Usage: maasdb <command> <cluster-path>
164
 
 
165
 
Where <command> is one of:
166
 
  start
167
 
  stop
168
 
  query
169
 
  shell
170
 
  delete-cluster
171
 
 
172
 
And <cluster-path> is the path to the MAAS development database cluster,
173
 
typically "./db"
174
 
EOF
175
 
}
176
 
 
177
 
 
178
 
unknown_command() {
179
 
    echo >&2 "** Unknown command: $1 **"
180
 
    echo
181
 
    usage
182
 
    exit 1
183
 
}
184
 
 
185
 
 
186
 
main() {
187
 
    local COMMAND DATADIR
188
 
    COMMAND="$1"
189
 
    DATADIR="$2"
190
 
    if ! shift 2
191
 
    then
192
 
        usage
193
 
        exit 1
194
 
    fi
195
 
 
196
 
    case "$COMMAND" in
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" ;;
203
 
    esac
204
 
}
205
 
 
206
 
 
207
 
main "$@"