Add port for postgres commands, let env define "workon home" for fabric
This commit is contained in:
parent
286a9bb4c0
commit
cd7e99b006
|
@ -75,7 +75,7 @@ class NewBatch(commands.Subcommand):
|
||||||
'table_name': table_name,
|
'table_name': table_name,
|
||||||
}
|
}
|
||||||
template_dir = resource_path('rattail:data/new-batch')
|
template_dir = resource_path('rattail:data/new-batch')
|
||||||
for name in ('__init__', 'model', 'handler', 'webview'):
|
for name in ('model', 'handler', 'webview'):
|
||||||
template_path = os.path.join(template_dir, '{}.py'.format(name))
|
template_path = os.path.join(template_dir, '{}.py'.format(name))
|
||||||
template = Template(filename=template_path)
|
template = Template(filename=template_path)
|
||||||
output_path = os.path.join(args.output_dir, '{}.py'.format(name))
|
output_path = os.path.join(args.output_dir, '{}.py'.format(name))
|
||||||
|
|
|
@ -52,11 +52,14 @@ def get_version():
|
||||||
return float(match.group(1))
|
return float(match.group(1))
|
||||||
|
|
||||||
|
|
||||||
def sql(sql, database=''):
|
def sql(sql, database='', port=None):
|
||||||
"""
|
"""
|
||||||
Execute some SQL as the 'postgres' user.
|
Execute some SQL as the 'postgres' user.
|
||||||
"""
|
"""
|
||||||
return sudo('sudo -u postgres psql --tuples-only --no-align --command="{0}" {1}'.format(sql, database), shell=False)
|
cmd = 'sudo -u postgres psql {port} --tuples-only --no-align --command="{sql}" {database}'.format(
|
||||||
|
port='--port={}'.format(port) if port else '',
|
||||||
|
sql=sql, database=database)
|
||||||
|
return sudo(cmd, shell=False)
|
||||||
|
|
||||||
|
|
||||||
def script(path, database='', user=None, password=None):
|
def script(path, database='', user=None, password=None):
|
||||||
|
@ -73,49 +76,53 @@ def script(path, database='', user=None, password=None):
|
||||||
return sudo("sudo -u postgres psql --file='{}' {}".format(path, database), shell=False)
|
return sudo("sudo -u postgres psql --file='{}' {}".format(path, database), shell=False)
|
||||||
|
|
||||||
|
|
||||||
def user_exists(name):
|
def user_exists(name, port=None):
|
||||||
"""
|
"""
|
||||||
Determine if a given PostgreSQL user exists.
|
Determine if a given PostgreSQL user exists.
|
||||||
"""
|
"""
|
||||||
user = sql("SELECT rolname FROM pg_roles WHERE rolname = '{0}'".format(name))
|
user = sql("SELECT rolname FROM pg_roles WHERE rolname = '{0}'".format(name), port=port)
|
||||||
return bool(user)
|
return bool(user)
|
||||||
|
|
||||||
|
|
||||||
def create_user(name, password=None, checkfirst=True, createdb=False):
|
def create_user(name, password=None, port=None, checkfirst=True, createdb=False):
|
||||||
"""
|
"""
|
||||||
Create a PostgreSQL user account.
|
Create a PostgreSQL user account.
|
||||||
"""
|
"""
|
||||||
if not checkfirst or not user_exists(name):
|
if not checkfirst or not user_exists(name, port=port):
|
||||||
createdb = '--{}createdb'.format('' if createdb else 'no-')
|
sudo('sudo -u postgres createuser {port} {createdb} --no-createrole --no-superuser {name}'.format(
|
||||||
sudo('sudo -u postgres createuser {createdb} --no-createrole --no-superuser {name}'.format(
|
port='--port={}'.format(port) if port else '',
|
||||||
createdb=createdb, name=name))
|
createdb='--{}createdb'.format('' if createdb else 'no-'),
|
||||||
|
name=name))
|
||||||
if password:
|
if password:
|
||||||
set_user_password(name, password)
|
set_user_password(name, password, port=port)
|
||||||
|
|
||||||
|
|
||||||
def set_user_password(name, password):
|
def set_user_password(name, password, port=None):
|
||||||
"""
|
"""
|
||||||
Set the password for a PostgreSQL user account
|
Set the password for a PostgreSQL user account
|
||||||
"""
|
"""
|
||||||
with hide('running'):
|
with hide('running'):
|
||||||
sql("ALTER USER \\\"{}\\\" PASSWORD '{}';".format(name, password))
|
sql("ALTER USER \\\"{}\\\" PASSWORD '{}';".format(name, password), port=port)
|
||||||
|
|
||||||
|
|
||||||
def db_exists(name):
|
def db_exists(name, port=None):
|
||||||
"""
|
"""
|
||||||
Determine if a given PostgreSQL database exists.
|
Determine if a given PostgreSQL database exists.
|
||||||
"""
|
"""
|
||||||
db = sql("SELECT datname FROM pg_database WHERE datname = '{0}'".format(name))
|
db = sql("SELECT datname FROM pg_database WHERE datname = '{0}'".format(name), port=port)
|
||||||
return db == name
|
return db == name
|
||||||
|
|
||||||
|
|
||||||
def create_db(name, owner=None, checkfirst=True):
|
def create_db(name, owner=None, port=None, checkfirst=True):
|
||||||
"""
|
"""
|
||||||
Create a PostgreSQL database.
|
Create a PostgreSQL database.
|
||||||
"""
|
"""
|
||||||
if not checkfirst or not db_exists(name):
|
if not checkfirst or not db_exists(name, port=port):
|
||||||
args = '--owner={0}'.format(owner) if owner else ''
|
cmd = 'sudo -u postgres createdb {port} {owner} {name}'.format(
|
||||||
sudo('sudo -u postgres createdb {0} {1}'.format(args, name), shell=False)
|
port='--port={}'.format(port) if port else '',
|
||||||
|
owner='--owner={}'.format(owner) if owner else '',
|
||||||
|
name=name)
|
||||||
|
sudo(cmd, shell=False)
|
||||||
|
|
||||||
|
|
||||||
def drop_db(name, checkfirst=True):
|
def drop_db(name, checkfirst=True):
|
||||||
|
|
|
@ -62,11 +62,12 @@ def pip(*packages):
|
||||||
sudo('pip install --upgrade {0}'.format(' '.join(packages)))
|
sudo('pip install --upgrade {0}'.format(' '.join(packages)))
|
||||||
|
|
||||||
|
|
||||||
def install_virtualenvwrapper(workon_home='/srv/envs', user='root', use_apt=False):
|
def install_virtualenvwrapper(workon_home=None, user='root', use_apt=False):
|
||||||
"""
|
"""
|
||||||
Install the `virtualenvwrapper`_ system, with the given ``workon`` home,
|
Install the `virtualenvwrapper`_ system, with the given ``workon`` home,
|
||||||
owned by the given user.
|
owned by the given user.
|
||||||
"""
|
"""
|
||||||
|
workon_home = workon_home or getattr(env, 'python_workon_home', '/srv/envs')
|
||||||
mkdir(workon_home, owner=user)
|
mkdir(workon_home, owner=user)
|
||||||
if use_apt:
|
if use_apt:
|
||||||
apt.install('virtualenvwrapper')
|
apt.install('virtualenvwrapper')
|
||||||
|
@ -78,10 +79,11 @@ def install_virtualenvwrapper(workon_home='/srv/envs', user='root', use_apt=Fals
|
||||||
configure_virtualenvwrapper(env.user, workon_home)
|
configure_virtualenvwrapper(env.user, workon_home)
|
||||||
|
|
||||||
|
|
||||||
def configure_virtualenvwrapper(user, workon_home='/srv/envs', wrapper='/usr/local/bin/virtualenvwrapper.sh'):
|
def configure_virtualenvwrapper(user, workon_home=None, wrapper='/usr/local/bin/virtualenvwrapper.sh'):
|
||||||
"""
|
"""
|
||||||
Configure virtualenvwrapper for the given user account.
|
Configure virtualenvwrapper for the given user account.
|
||||||
"""
|
"""
|
||||||
|
workon_home = workon_home or getattr(env, 'python_workon_home', '/srv/envs')
|
||||||
home = sudo('echo $HOME', user=user)
|
home = sudo('echo $HOME', user=user)
|
||||||
home = home.rstrip('/')
|
home = home.rstrip('/')
|
||||||
|
|
||||||
|
@ -97,17 +99,18 @@ def configure_virtualenvwrapper(user, workon_home='/srv/envs', wrapper='/usr/loc
|
||||||
update('.bashrc')
|
update('.bashrc')
|
||||||
|
|
||||||
|
|
||||||
def mkvirtualenv(name, python=None, user=None, upgrade_pip=True):
|
def mkvirtualenv(name, python=None, user=None, workon_home=None, upgrade_pip=True):
|
||||||
"""
|
"""
|
||||||
Make a new Python virtual environment.
|
Make a new Python virtual environment.
|
||||||
"""
|
"""
|
||||||
|
workon_home = workon_home or getattr(env, 'python_workon_home', '/srv/envs')
|
||||||
sudo('mkvirtualenv {} {}'.format('--python={}'.format(python) if python else '', name))
|
sudo('mkvirtualenv {} {}'.format('--python={}'.format(python) if python else '', name))
|
||||||
if upgrade_pip:
|
if upgrade_pip:
|
||||||
with workon(name):
|
with workon(name):
|
||||||
pip('six')
|
pip('six')
|
||||||
pip('pip', 'setuptools', 'wheel', 'ndg-httpsclient')
|
pip('pip', 'setuptools', 'wheel', 'ndg-httpsclient')
|
||||||
if user:
|
if user:
|
||||||
with cdvirtualenv(name):
|
with cdvirtualenv(name, workon_home=workon_home):
|
||||||
mkdir('app/log', owner='{0}:{0}'.format(user))
|
mkdir('app/log', owner='{0}:{0}'.format(user))
|
||||||
|
|
||||||
|
|
||||||
|
@ -121,10 +124,11 @@ def workon(name):
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def cdvirtualenv(name, subdirs=[], workon_home='/srv/envs'):
|
def cdvirtualenv(name, subdirs=[], workon_home=None):
|
||||||
"""
|
"""
|
||||||
Context manager to prefix your command(s) with the ``cdvirtualenv`` command.
|
Context manager to prefix your command(s) with the ``cdvirtualenv`` command.
|
||||||
"""
|
"""
|
||||||
|
workon_home = workon_home or getattr(env, 'python_workon_home', '/srv/envs')
|
||||||
if isinstance(subdirs, basestring):
|
if isinstance(subdirs, basestring):
|
||||||
subdirs = [subdirs]
|
subdirs = [subdirs]
|
||||||
path = '{0}/{1}'.format(workon_home, name)
|
path = '{0}/{1}'.format(workon_home, name)
|
||||||
|
|
Loading…
Reference in a new issue