Skip to content

Commit

Permalink
Adds init.sql for user defined commands at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
miketonks committed Jul 28, 2015
1 parent d289584 commit 9ce1644
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ RUN pip install -U pip setuptools
RUN pip install python-etcd

RUN mkdir -p /governor/helpers
ADD governor.py /governor/governor.py
ADD helpers /governor/helpers
ADD postgres0.yml /governor/
COPY governor.py /governor/governor.py
COPY helpers /governor/helpers
COPY postgres0.yml /governor/
COPY pg_hba.conf /governor/
COPY init.sql /governor/

RUN mkdir -p /data/postgres && \
chown -R postgres /data && \
Expand All @@ -20,4 +22,3 @@ WORKDIR /governor

CMD gosu postgres /governor/governor.py


3 changes: 2 additions & 1 deletion governor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def shutdown():
logging.info("Governor Shutting Down: Abdicating Leadership")
etcd.abdicate(postgresql.name)

logging.info("Governor Shutting Down: Remiving Membership")
logging.info("Governor Shutting Down: Removing Membership")
etcd.delete_member(postgresql.name)
except:
pass
Expand Down Expand Up @@ -93,6 +93,7 @@ def shutdown():
postgresql.start(master=True)
else:
logging.info("Governor Starting up: Initialisation Race ... LOST")
time.sleep(20)
logging.info("Governor Starting up: Sync Postgres from Leader")
synced_from_leader = False
while not synced_from_leader:
Expand Down
11 changes: 11 additions & 0 deletions helpers/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ def initialize(self):
if os.system("initdb -D %s" % self.data_dir) == 0:
#self.write_pg_hba()
self.copy_pg_hba()

self.start()
self.create_replication_user()
self.run_init_sql()
self.stop()

return True
Expand Down Expand Up @@ -245,6 +247,15 @@ def demote(self, leader):
self.stop()
self.start(master=False)

def run_init_sql(self):
if os.path.exists('init.sql'):
logger.info("Running init.sql")
sql_lines = open("init.sql", "r").read().split("\n")
for sql in sql_lines:
if not sql:
continue
self.query(sql)

def create_replication_user(self):
#logger.info("Governor Starting Up: Running postgres single user mode to create repliaction user")
#os.system("postgres --single -jE << CREATE USER '%s' WITH REPLICATION ENCRYPTED PASSWORD '%s';" % (self.replication["username"], self.replication["password"]))
Expand Down
4 changes: 4 additions & 0 deletions init.sql.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

CREATE USER example WITH ENCRYPTED PASSWORD 'examplepass';

CREATE DATABASE example;

0 comments on commit 9ce1644

Please sign in to comment.