Skip to content

Commit

Permalink
Add a state to manage replication slots
Browse files Browse the repository at this point in the history
  • Loading branch information
dstufft committed Jan 4, 2015
1 parent 49e9bd7 commit 59d744e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
34 changes: 34 additions & 0 deletions salt/_states/postgres_replica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def slot(name):
ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}

sql = "SELECT * FROM pg_replication_slots WHERE slot_name = '%s'" % name
if __salt__["postgres.psql_query"](sql):
ret["result"] = True
ret["comment"] = "Replication slot '{}' already exists.".format(
name,
)
return ret

if __opts__['test']:
ret['comment'] = 'Replication slot "{0}" will be created.'.format(name)
ret['changes'] = {
'old': None,
'new': name,
}
ret["result"] = None
return ret

__salt__["postgres.psql_query"](
""" SELECT * FROM
pg_create_physical_replication_slot('%s');
""" % name
)

ret["result"] = True
ret["comment"] = "Created replication slot: '{}'.".format(name)
ret["changes"] = {
"old": None,
"new": name,
}

return ret
8 changes: 8 additions & 0 deletions salt/postgresql/server/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ postgresql-psf-cluster:

{% if salt["match.compound"](pillar["roles"]["postgresql-primary"]) %}

{% for replica in salt["mine.get"](pillar["roles"]["postgresql-replica"], "psf_internal").keys() %}
replication-slot-{{ replica|split(".")|first }}:
postgres_replica.slot:
- name: {{ replica|split(".")|first }}
- require:
- service: postgresql-server
{% endfor %}

replicator:
postgres_user.present:
- replication: True
Expand Down

0 comments on commit 59d744e

Please sign in to comment.