Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres: Add 9.5-9.6 config, update pgtune #79

Merged
merged 6 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 45 additions & 23 deletions blues/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@
from . import debian

__all__ = ['start', 'stop', 'restart', 'reload', 'setup', 'configure',
'setup_schemas', 'generate_pgtune_conf', 'dump']
'setup_schemas', 'generate_pgtune_conf', 'dump', 'install',
'download_pgtune', 'stop_all']


blueprint = blueprints.get(__name__)

start = debian.service_task('postgresql', 'start')
stop = debian.service_task('postgresql', 'stop')
restart = debian.service_task('postgresql', 'restart')
reload = debian.service_task('postgresql', 'reload')

version = lambda: blueprint.get('version', '9.1')

start = debian.service_task('postgresql', 'start %s' % version())
stop = debian.service_task('postgresql', 'stop %s' % version())
stop_all = debian.service_task('postgresql', 'stop')
restart = debian.service_task('postgresql', 'restart %s' % version())
reload = debian.service_task('postgresql', 'reload %s' % version())

postgres_root = lambda *a: os.path.join('/etc/postgresql/{}/main/'.format(version()), *a)
pgtune_root = '/usr/local/src/pgtune'


@task
def install(add_repo=None):
with sudo():
v = version()
Expand All @@ -59,11 +64,25 @@ def install(add_repo=None):
add_repository()

debian.apt_get('install',
'postgresql',
'postgresql-{}'.format(v),
'postgresql-server-dev-{}'.format(v),
'libpq-dev',
'postgresql-contrib-{}'.format(v),
'pgtune')
)
download_pgtune()


@task
def download_pgtune():
run('rm -r {} || true'.format(pgtune_root))
run('mkdir -p {}'.format(pgtune_root))

run('curl https://codeload.github.com/andreif/pgtune/legacy.tar.gz/'
'allthethings | tar xzv -C {} --strip=1'.format(pgtune_root))

run('python {}/pgtune --doctest --help'.format(pgtune_root))

info('Downloaded pgtune to {}/'.format(pgtune_root))


def add_repository():
Expand All @@ -83,6 +102,7 @@ def install_postgis(v=None):
debian.apt_get('install', 'postgis',
'postgresql-{}-postgis-scripts'.format(v))


@task
def setup():
"""
Expand Down Expand Up @@ -205,35 +225,37 @@ def generate_pgtune_conf(role='db', **options):

:param role: Which fabric role to place local pgtune.conf template under
"""
info('Generating pgtune conf')

options.setdefault('type', 'Web')
options.setdefault('version', version())
options.setdefault('input-config', postgres_root('postgresql.conf'))
options.setdefault('settings', pgtune_root)

options = ' '.join(
'--{}="{}"'.format(key, value)
for key, value in options.items()
)

conf_path = postgres_root('postgresql.conf')
with sudo(), silent():
output = run('pgtune {} -i {}'.format(options, conf_path)).strip()
info('options: ' + options)
output = run('{}/pgtune {}'.format(pgtune_root, options)).strip()
output = output.rpartition('\n# pgtune')[2].partition('\n')[2]

def parse(c):
lines = [l for l in c.splitlines() if '# pgtune' in l]
for line in lines:
try:
comment = line.index('#')
line = line[:comment]
except ValueError:
pass
clean = lambda s: s.strip('\n\r\t\'" ')
key, _, value = line.partition('=')
key, value = map(clean, (key, value))
if key:
yield key, value or None
for line in c.splitlines():
line = line.split('#')[0].strip()
if line:
clean = lambda s: s.strip('\n\r\t\'" ')
key, _, value = line.partition('=')
key, value = map(clean, (key, value))
if key:
yield key, value or None

tune_conf = dict(parse(output))
tune_conf.update(blueprint.get('pgtune', {}))
tune_conf = '\n'.join(' = '.join(item)
for item in tune_conf.iteritems())
for item in sorted(tune_conf.iteritems()))
conf_dir = os.path.join(
os.path.dirname(env['real_fabfile']),
'templates',
Expand Down
Loading