~psivaa/uci-engine/lander-jenkins-with-proxy

« back to all changes in this revision

Viewing changes to ci-utils/ci_utils/create_db.py

  • Committer: Tarmac
  • Author(s): Andy Doan
  • Date: 2013-12-06 23:25:38 UTC
  • mfrom: (6.1.22 ppa-assigner)
  • Revision ID: tarmac-20131206232538-auo3c8yhml7wbnee
[r=Francis Ginther] adds first pass of ppa-assigner

more changes to come following design updates.  from Andy Doan

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Ubuntu CI Services
 
2
# Copyright 2013 Canonical Ltd.
 
3
 
 
4
# This program is free software: you can redistribute it and/or modify it
 
5
# under the terms of the GNU Affero General Public License version 3, as
 
6
# published by the Free Software Foundation.
 
7
 
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
# PURPOSE.  See the GNU Affero General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
import os
 
17
import subprocess
 
18
 
 
19
 
 
20
def assert_root():
 
21
    if os.geteuid() != 0:
 
22
        print('This command option must be run as root.')
 
23
        exit(1)
 
24
 
 
25
 
 
26
def assert_postgres():
 
27
    if not os.path.exists('/usr/bin/createdb'):
 
28
        print('postgres must be installed manually')
 
29
        exit(1)
 
30
 
 
31
 
 
32
def dbuser_exists(dbconfig):
 
33
    args = [
 
34
        'sudo', '-u', 'postgres', 'psql', '-c',
 
35
        'SELECT usename FROM pg_user WHERE usename=\'%s\';' % dbconfig['USER'],
 
36
    ]
 
37
    output = subprocess.check_output(args)
 
38
    return '(1 row)' in output
 
39
 
 
40
 
 
41
def db_exists(dbconfig):
 
42
    args = [
 
43
        'sudo', '-u', 'postgres', 'psql', '-c',
 
44
        'SELECT datname FROM pg_database WHERE datname=\'%s\'' % (
 
45
            dbconfig['NAME'])
 
46
    ]
 
47
    output = subprocess.check_output(args)
 
48
    return '(1 row)' in output
 
49
 
 
50
 
 
51
def create_user(dbconfig):
 
52
    args = [
 
53
        'sudo', '-u', 'postgres', 'createuser',
 
54
        '--no-createdb', '--encrypted', '--login', '--no-superuser',
 
55
        '--no-createrole', '--no-password', dbconfig['USER']
 
56
    ]
 
57
    subprocess.check_call(args)
 
58
 
 
59
    # Set a password for our new user
 
60
    args = [
 
61
        'sudo', '-u', 'postgres', 'psql', '--quiet', '-c',
 
62
        'ALTER USER \"%s\" WITH PASSWORD \'%s\'' % (
 
63
            dbconfig['USER'], dbconfig['PASSWORD'])
 
64
    ]
 
65
    subprocess.check_call(args)
 
66
 
 
67
 
 
68
def _create(dbconfig):
 
69
    args = [
 
70
        'sudo', '-u', 'postgres', 'createdb',
 
71
        '--owner=%s' % dbconfig['USER'],
 
72
        '--template=template0', '--no-password', dbconfig['NAME']
 
73
    ]
 
74
    subprocess.check_call(args)
 
75
 
 
76
 
 
77
def create_db(dbconfig):
 
78
    if dbconfig['ENGINE'] != 'django.db.backends.postgresql_psycopg2':
 
79
        print('Unsupported db engine: %s' % dbconfig['ENGINE'])
 
80
        return 1
 
81
 
 
82
    assert_root()
 
83
    assert_postgres()
 
84
 
 
85
    if dbuser_exists(dbconfig):
 
86
        print('user already exists, skipping creation')
 
87
        print('  user can be removed with: dropuser %s' % dbconfig['USER'])
 
88
    else:
 
89
        create_user(dbconfig)
 
90
 
 
91
    if db_exists(dbconfig):
 
92
        print('db already exists')
 
93
        print ('  db can be removed with: dropdb %s' % dbconfig['NAME'])
 
94
    else:
 
95
        _create(dbconfig)