~bloodearnest/charms/trusty/postgresql/trunk

« back to all changes in this revision

Viewing changes to actions/actions.py

  • Committer: Matthew Bruzek
  • Date: 2015-11-11 19:37:09 UTC
  • mfrom: (121.4.130 rewrite)
  • Revision ID: matthew.bruzek@canonical.com-20151111193709-fn9vb1jd865frl9t
[stub] A rewrite to make use of modern Juju and improve the reliability of the postgresql charm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    sys.path.append(hooks_dir)
26
26
 
27
27
from charmhelpers.core import hookenv
28
 
import hooks
 
28
 
 
29
import postgresql
29
30
 
30
31
 
31
32
def replication_pause(params):
32
 
    offset = hooks.postgresql_wal_received_offset()
33
 
    if offset is None:
 
33
    if not postgresql.is_secondary():
34
34
        hookenv.action_fail('Not a hot standby')
35
35
        return
 
36
 
 
37
    offset = postgresql.wal_received_offset()
36
38
    hookenv.action_set(dict(offset=offset))
37
39
 
38
 
    cur = hooks.db_cursor(autocommit=True)
 
40
    con = postgresql.connect()
 
41
    con.autocommit = True
 
42
    cur = con.cursor()
39
43
    cur.execute('SELECT pg_is_xlog_replay_paused()')
40
44
    if cur.fetchone()[0] is True:
41
45
        hookenv.action_fail('Already paused')
45
49
 
46
50
 
47
51
def replication_resume(params):
48
 
    offset = hooks.postgresql_wal_received_offset()
49
 
    if offset is None:
 
52
    if not postgresql.is_secondary():
50
53
        hookenv.action_fail('Not a hot standby')
51
54
        return
 
55
 
 
56
    offset = postgresql.wal_received_offset()
52
57
    hookenv.action_set(dict(offset=offset))
53
58
 
54
 
    cur = hooks.db_cursor(autocommit=True)
 
59
    con = postgresql.connect()
 
60
    con.autocommit = True
 
61
    cur = con.cursor()
55
62
    cur.execute('SELECT pg_is_xlog_replay_paused()')
56
63
    if cur.fetchone()[0] is False:
57
64
        hookenv.action_fail('Already resumed')
60
67
    hookenv.action_set(dict(result='Resumed'))
61
68
 
62
69
 
 
70
# Revisit this when actions are more mature. Per Bug #1483525, it seems
 
71
# impossible to return filenames in our results.
 
72
#
 
73
# def backup(params):
 
74
#     assert params['type'] == 'dump'
 
75
#     script = os.path.join(helpers.scripts_dir(), 'pg_backup_job')
 
76
#     cmd = ['sudo', '-u', 'postgres', '-H', script, str(params['retention'])]
 
77
#     hookenv.action_set(dict(command=' '.join(cmd)))
 
78
#
 
79
#     try:
 
80
#         subprocess.check_call(cmd)
 
81
#     except subprocess.CalledProcessError as x:
 
82
#         hookenv.action_fail(str(x))
 
83
#         return
 
84
#
 
85
#     backups = {}
 
86
#     for filename in os.listdir(backups_dir):
 
87
#         path = os.path.join(backups_dir, filename)
 
88
#         if not is.path.isfile(path):
 
89
#             continue
 
90
#         backups['{}.{}'.format(filename
 
91
#         backups[filename] = dict(name=filename,
 
92
#                                  size=os.path.getsize(path),
 
93
#                                  path=path,
 
94
#                                  scp_path='{}:{}'.format(unit, path))
 
95
#     hookenv.action_set(dict(backups=backups))
 
96
 
 
97
 
63
98
def main(argv):
64
99
    action = os.path.basename(argv[0])
65
100
    params = hookenv.action_get()