Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1.  
  2. import subprocess
  3. from kokki import *
  4.  
  5. def setup():
  6.     postgresql_locale = env.system.locales[0]
  7.     for l in env.system.locales:
  8.         if 'utf8' in l.lower() or 'utf-8' in l.lower():
  9.             postgresql_locale = l
  10.             break
  11.     env.set_attributes({
  12.         'postgresql84.locale': postgresql_locale,
  13.     })
  14.  
  15. setup()
  16.  
  17. def postgresql_user(username, exists=True, password=None, **privileges):
  18.     '''
  19.    Defines a PostgreSQL user to either exist or not. Takes an optional
  20.    password (ignored if None) and a dictionary of boolean privileges.
  21.    The available privileges are: superuser, createdb, inherit and login.
  22.    '''
  23.     if exists:
  24.         # Create role update SQL
  25.         defaults = {
  26.                 'superuser': False, 'createdb': False,
  27.                 'createrole': False, 'inherit': True,
  28.                 'login': True,
  29.             }
  30.         defaults.update(privileges)
  31.         sql_privileges = ['%s%s' % ('' if defaults[p] else 'NO', p.upper()) for p in defaults.keys()]
  32.         sql = "ALTER ROLE %s %s" % (username, ' '.join(sql_privileges))
  33.         if password is not None:
  34.             sql = "%s WITH PASSWORD '%s'" % (sql)
  35.         # Run update if nescessary
  36.         Execute('alter postgresql84 user %s' % username,
  37.             command='sudo -u postgres psql -c "%s"' % sql)
  38.     else:
  39.         sql = 'DROP ROLE IF EXISTS %s' % username
  40.         Execute('drop postgresql84 user %s' % username,
  41.             command='sudo -u postgres psql -c "%s"' % sql)
  42.  
  43. def postgresql_db(name, owner='postgres', exists=True):
  44.     '''
  45.    Defines a PostgreSQL database to either exist or not. Takes an
  46.    optional database owner.
  47.    '''
  48.     if exists:
  49.         exists_cmd = "sudo -u postgres psql -f /dev/null %s" % name
  50.         print 'exists_cmd', exists_cmd
  51.         retval = subprocess.call(exists_cmd, shell=True)
  52.         print 'retval', retval
  53.         # Only create the database if it does not exist already
  54.         if retval is not 0:
  55.             Execute('creating postgresql84 database %s' % name,
  56.                 command='sudo -u postgres createdb %s' % name)
  57.         # Update database owner
  58.         Execute('updating postgresql84 database owner for %s' % name,
  59.             command="sudo -u postgres psql -c 'ALTER DATABASE %s OWNER TO %s'" % (name, owner))
  60.     else:
  61.         Execute('dropping postgresql84 database "%s" if it exists' % name,
  62.             command="sudo -u postgres psql -c 'DROP DATABASE IF EXISTS %s" % name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement