Commit 8f9f4c06 authored by Tulio Ruiz's avatar Tulio Ruiz Committed by GitHub
Browse files

Merge pull request #4 from vauxoo-dev/master-configure_postgres-angelfentanez

[REF] Dockerfile: postgres config
parents a68510e9 a57c6338
......@@ -28,6 +28,11 @@ COPY files/entry_point.py /entry_point.py
COPY files/config_db.py /config_db.py
RUN chmod +x /entry_point.py && chmod +x /config_db.py
# Create the directory for the postgres logs and configure postgres
COPY files/common-vauxoo.conf /etc/postgresql-common/common-vauxoo.conf
RUN mkdir -p /var/log/pg_log
RUN chmod 0757 -R /var/log/pg_log
EXPOSE 5432
VOLUME ["/etc/postgresql", "/var/log/supervisor", "/var/lib/postgresql"]
......
listen_addresses = '*'
temp_buffers = 16MB
work_mem = 16MB
max_stack_depth = 7680kB
bgwriter_delay = 500ms
fsync=off
full_page_writes=off
checkpoint_timeout=45min
synchronous_commit=off
autovacuum = off
logging_collector=on
log_destination='stderr'
log_directory='/var/log/pg_log'
log_filename='postgresql.log'
log_rotation_age=0
log_checkpoints=on
log_hostname=on
log_line_prefix='%t [%p]: [%l-1] db=%d,user=%u'
......@@ -46,6 +46,8 @@ def main():
line = re.sub(r'#?(work_mem) .*$', r"\1 = 16MB", line.strip())
line = re.sub(r'#?(max_stack_depth) .*$', r"\1 = 7680kB", line.strip())
print(line)
with open(path.join(POSTGRES_CONFIG, '9.5', 'main', 'postgresql.conf'), 'a') as config:
config.write("include = '/etc/postgresql-common/common-vauxoo.conf'")
call(["/etc/init.d/postgresql", "start"])
call(["su", "postgres", "-c /config_db.py"])
call(["/etc/init.d/postgresql", "stop"])
......
FROM ubuntu:16.04
MAINTAINER Tulio Ruiz <tulio@vauxoo.com>
# Configure locale
RUN apt-get update && apt-get install wget -yq
RUN apt-get install -y locales
RUN locale-gen en_US.UTF-8 && update-locale
RUN echo 'LANG="en_US.UTF-8"' > /etc/default/locale
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
apt-key add -
RUN echo 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main' > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get upgrade -y \
&& apt-get install -y supervisor python-software-properties \
software-properties-common postgresql-common python-psycopg2
# Do not create main cluster
RUN sed -ri "s/#(create_main_cluster) .*$/\1 = false/" /etc/postgresql-common/createcluster.conf
# Install postgres and clean
RUN apt-get install -y postgresql-9.6 \
postgresql-client-9.6 postgresql-contrib-9.6 \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN mkdir -p /var/log/supervisor
COPY files/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY files/entry_point.py /entry_point.py
COPY files/config_db.py /config_db.py
RUN chmod +x /entry_point.py && chmod +x /config_db.py
# Create the directory for the postgres logs and configure postgres
COPY files/common-vauxoo.conf /etc/postgresql-common/common-vauxoo.conf
RUN mkdir -p /var/log/pg_log
RUN chmod 0757 -R /var/log/pg_log
EXPOSE 5432
VOLUME ["/etc/postgresql", "/var/log/supervisor", "/var/lib/postgresql"]
ENTRYPOINT ["/entry_point.py"]
listen_addresses = '*'
temp_buffers = 16MB
work_mem = 16MB
max_stack_depth = 7680kB
bgwriter_delay = 500ms
fsync=off
full_page_writes=off
checkpoint_timeout=45min
synchronous_commit=off
autovacuum = off
logging_collector=on
log_destination='stderr'
log_directory='/var/log/pg_log'
log_filename='postgresql.log'
log_rotation_age=0
log_checkpoints=on
log_hostname=on
log_line_prefix='%t [%p]: [%l-1] db=%d,user=%u'
#!/usr/bin/python
import psycopg2
try:
conn = psycopg2.connect("dbname='template1'")
conn.set_isolation_level(0)
except:
print "Could not connect to PostgreSQL server"
raise
cursor = conn.cursor()
try:
print "Activate unnacent etension for all databases by defult"
cursor.execute("CREATE EXTENSION \"unaccent\";")
except:
print "Could not create extension"
raise
try:
print "Creating users"
cursor.execute("CREATE USER docker WITH SUPERUSER PASSWORD 'docker';")
cursor.execute("CREATE USER odoo WITH CREATEDB PASSWORD 'odoo';")
except:
print "Could not create users"
raise
try:
print "Creating docker database"
cursor.execute("CREATE DATABASE docker ENCODING 'utf8' OWNER docker TEMPLATE template1 ")
except:
print "Could not create docker database"
raise
cursor.close()
conn.close()
#!/usr/bin/python
# -*- coding: utf-8 -*-
from os import listdir, stat, chown, path
from subprocess import call
import fileinput
import pwd
import re
POSTGRES_PATH = '/var/lib/postgresql'
POSTGRES_CONFIG = '/etc/postgresql'
def get_owner(file_name):
'''
This function gets owner name from system for a directory or file
:param str file_name: File or directory name
:returns: Owner name
'''
file_stat = stat(file_name)
try:
owner = pwd.getpwuid(file_stat.st_uid).pw_name
except KeyError:
owner = "None"
return owner
def main():
'''
Main entry point function
'''
if "postgres" != get_owner(POSTGRES_CONFIG):
call(["chown", "-R", "postgres:postgres", POSTGRES_CONFIG])
call(["chmod", "-R", "0700", POSTGRES_CONFIG])
if "postgres" != get_owner(POSTGRES_PATH):
call(["chown", "-R", "postgres:postgres", POSTGRES_PATH])
call(["chmod", "-R", "0700", POSTGRES_PATH])
if listdir(POSTGRES_PATH) == []:
call(['pg_createcluster', '9.6', 'main', '-e=utf8'])
with open(path.join(POSTGRES_CONFIG, "9.6", "main", "pg_hba.conf"), "a") as pg_hba:
pg_hba.write("host all all 0.0.0.0/0 md5")
for line in fileinput.input(path.join(POSTGRES_CONFIG, "9.6", "main", "postgresql.conf"), inplace=True):
line = re.sub(r'#?(listen_addresses) .*$', r"\1 = '*'", line.strip())
line = re.sub(r'#?(temp_buffers) .*$', r"\1 = 16MB", line.strip())
line = re.sub(r'#?(work_mem) .*$', r"\1 = 16MB", line.strip())
line = re.sub(r'#?(max_stack_depth) .*$', r"\1 = 7680kB", line.strip())
print(line)
with open(path.join(POSTGRES_CONFIG, '9.6', 'main', 'postgresql.conf'), 'a') as config:
config.write("include = '/etc/postgresql-common/common-vauxoo.conf'")
call(["/etc/init.d/postgresql", "start"])
call(["su", "postgres", "-c /config_db.py"])
call(["/etc/init.d/postgresql", "stop"])
call(["/usr/bin/supervisord"])
if __name__ == '__main__':
main()
[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervidord.log
logfile_maxbytes=20MB
[program:postgresql]
user = postgres
command=/usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf
stderr_logfile=/var/log/supervisor/postgres_stderr.log
stdout_logfile=/var/log/supervisor/postgres_stdout.log
autostart=true
autorestart=unexpected
exitcodes=0,2
startsecs=10
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment