__init__.py 8.74 KB
Newer Older
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
##############################################################################
#
# Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

28
import md5
29 30 31 32 33 34 35
import os
import subprocess
import textwrap
from zc.buildout import UserError

from slapos.recipe.librecipe import GenericBaseRecipe

36

37

38
class Recipe(GenericBaseRecipe):
39 40 41 42 43 44 45 46 47 48 49 50
    """\
    This recipe creates:

        - a Postgres cluster
        - configuration to allow connections from IPV6 only (or unix socket)
        - a superuser with provided name and generated password
        - a database with provided name
        - a foreground start script in the services directory

    then adds the connection URL to the options.
    The URL can be used as-is (ie. in sqlalchemy) or by the _urlparse.py recipe.
    """
51

Marco Mariani's avatar
Marco Mariani committed
52 53 54 55 56 57 58
    def fetch_host(self, options):
        """
        Returns a string represtation of ipv6_host.
        May receive a regular string, a set or a string serialized by buildout.
        """
        ipv6_host = options['ipv6_host']

59
        if isinstance(ipv6_host, set):
Marco Mariani's avatar
Marco Mariani committed
60 61 62 63 64
            return ipv6_host.pop()
        else:
            return ipv6_host


65 66
    def _options(self, options):
        options['password'] = self.generatePassword()
Marco Mariani's avatar
Marco Mariani committed
67
        options['url'] = 'postgresql://%(user)s:%(password)s@[%(host)s]:%(port)s/%(dbname)s' % dict(options, host=self.fetch_host(options))
68 69 70 71 72


    def install(self):
        pgdata = self.options['pgdata-directory']

73 74 75 76
        if not os.path.exists(pgdata):
            self.createCluster()
            self.createConfig()
            self.createDatabase()
77
            self.createSuperuser()
78 79
            self.createRunScript()

80
        return [
81
                # XXX should we really return something here?
82
                # os.path.join(pgdata, 'postgresql.conf')
83 84 85
                ]


Marco Mariani's avatar
Marco Mariani committed
86 87 88 89 90
    def check_exists(self, path):
        if not os.path.isfile(path):
            raise IOError('File not found: %s' % path)


91 92
    def createCluster(self):
        initdb_binary = os.path.join(self.options['bin'], 'initdb')
Marco Mariani's avatar
Marco Mariani committed
93
        self.check_exists(initdb_binary)
94 95 96

        pgdata = self.options['pgdata-directory']

97 98 99 100 101 102 103 104
        try:
            subprocess.check_call([initdb_binary,
                                   '-D', pgdata,
                                   '-A', 'ident',
                                   '-E', 'UTF8',
                                   ])
        except subprocess.CalledProcessError:
            raise UserError('Could not create cluster directory in %s' % pgdata)
105 106 107


    def createConfig(self):
108 109
        pgdata = self.options['pgdata-directory']

110 111
        with open(os.path.join(pgdata, 'postgresql.conf'), 'wb') as cfg:
            cfg.write(textwrap.dedent("""\
112
                    listen_addresses = '%s'
113 114 115 116 117 118 119 120 121 122
                    logging_collector = on
                    log_rotation_size = 50MB
                    max_connections = 100
                    datestyle = 'iso, mdy'

                    lc_messages = 'en_US.UTF-8'
                    lc_monetary = 'en_US.UTF-8'
                    lc_numeric = 'en_US.UTF-8'
                    lc_time = 'en_US.UTF-8'
                    default_text_search_config = 'pg_catalog.english'
123 124 125 126 127 128 129

                    unix_socket_directory = '%s'
                    unix_socket_permissions = 0700
                    """ % (
                        self.fetch_host(self.options),
                        pgdata,
                        )))
130 131 132 133 134 135 136 137


        with open(os.path.join(pgdata, 'pg_hba.conf'), 'wb') as cfg:
            # see http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html

            cfg.write(textwrap.dedent("""\
                    # TYPE  DATABASE        USER            ADDRESS                 METHOD

138
                    # "local" is for Unix domain socket connections only (check unix_socket_permissions!)
139 140 141
                    local   all             all                                     ident
                    host    all             all             127.0.0.1/32            md5
                    host    all             all             ::1/128                 md5
142
                    host    all             all             %s/128                  md5
Marco Mariani's avatar
-  
Marco Mariani committed
143
                    """ % self.fetch_host(self.options)))
144 145 146


    def createDatabase(self):
147 148 149 150 151 152 153
        self.runPostgresCommand(cmd='CREATE DATABASE "%s"' % self.options['dbname'])


    def createSuperuser(self):
        """
        Creates a Postgres superuser - other than "slapuser#" for use by the application.
        """
154 155 156

        # http://postgresql.1045698.n5.nabble.com/Algorithm-for-generating-md5-encrypted-password-not-found-in-documentation-td4919082.html

157 158 159 160 161 162 163
        user = self.options['user']
        password = self.options['password']

        # encrypt the password to avoid storing in the logs
        enc_password = 'md5' + md5.md5(password+user).hexdigest()

        self.runPostgresCommand(cmd="""CREATE USER "%s" ENCRYPTED PASSWORD '%s' SUPERUSER""" % (user, enc_password))
164 165 166 167 168 169 170 171 172 173 174


    def runPostgresCommand(self, cmd):
        """
        Executes a command in single-user mode, with no daemon running.

        Multiple commands can be executed by providing newlines,
        preceeded by backslash, between them.
        See http://www.postgresql.org/docs/9.1/static/app-postgres.html
        """

175 176 177 178 179 180 181 182 183
        pgdata = self.options['pgdata-directory']
        postgres_binary = os.path.join(self.options['bin'], 'postgres')

        try:
            p = subprocess.Popen([postgres_binary,
                                  '--single',
                                  '-D', pgdata,
                                  'postgres',
                                  ], stdin=subprocess.PIPE)
184

185
            p.communicate(cmd+'\n')
186 187 188 189 190
        except subprocess.CalledProcessError:
            raise UserError('Could not create database %s' % pgdata)


    def createRunScript(self):
191 192 193 194
        """
        Creates a script that runs postgres in the foreground.
        'exec' is used to allow easy control by supervisor.
        """
195 196
        content = textwrap.dedent("""\
                #!/bin/sh
197
                exec %(bin)s/postgres \\
198 199 200 201 202 203
                    -D %(pgdata-directory)s
                """ % self.options)
        name = os.path.join(self.options['services'], 'postgres-start')
        self.createExecutable(name, content=content)


204 205 206 207 208 209 210 211

class ExportRecipe(GenericBaseRecipe):

    def install(self):
        pgdata = self.options['pgdata-directory']

        ret = []

Marco Mariani's avatar
Marco Mariani committed
212 213 214
        wrapper = self.options['wrapper']
        self.createBackupScript(wrapper)
        ret.append(wrapper)
215 216 217 218 219 220 221 222 223 224 225

        return ret


    def createBackupScript(self, wrapper):
        """
        Create a script to backup the database in plain SQL format.
        """
        content = textwrap.dedent("""\
                #!/bin/sh
                umask 077
Marco Mariani's avatar
Marco Mariani committed
226 227 228 229
                %(bin)s/pg_dump \\
                        -h %(pgdata-directory)s \\
                        -f %(backup-directory)s/backup.sql \\
                        %(dbname)s
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                """ % self.options)
        self.createExecutable(wrapper, content=content)



class ImportRecipe(GenericBaseRecipe):

    def install(self):
        pgdata = self.options['pgdata-directory']

        ret = []
        if not os.path.exists(pgdata):
            wrapper = self.options['wrapper']
            self.createRestoreScript(wrapper)
            ret.append(wrapper)

        return ret


    def createRestoreScript(self, wrapper):
        """
        Create a script to backup the database in plain SQL format.
        """
        content = textwrap.dedent("""\
                #!/bin/sh
                %(bin)s/pg_restore -h %(pgdata-directory)s -d %(dbname)s %(backup-directory)s/backup.sql
                """ % self.options)
        self.createExecutable(wrapper, content=content)