~pjdc/charms/trusty/postgresql/nagios-additional-servicegroups

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Charles Butler
  • Date: 2015-01-13 16:14:28 UTC
  • mfrom: (105.2.4 manual-replication)
  • Revision ID: charles.butler@ubuntu.com-20150113161428-imt9za8j4gmrvr8a
    [r=lazypower]Stuart Bishop 2014-12-05 [merge] Merge trunk
    Stuart Bishop 2014-11-04 Publish credentials if manually replicated hot standby, even if th...
    Stuart Bishop 2014-11-04 Manual replication mode

Show diffs side-by-side

added added

removed removed

Lines of Context:
646
646
 
647
647
 
648
648
def create_recovery_conf(master_host, master_port, restart_on_change=False):
 
649
    if hookenv.config('manual_replication'):
 
650
        log('manual_replication; should not be here', CRITICAL)
 
651
        raise RuntimeError('manual_replication; should not be here')
 
652
 
649
653
    version = pg_version()
650
654
    cluster_name = hookenv.config('cluster_name')
651
655
    postgresql_cluster_dir = os.path.join(
1087
1091
        return False
1088
1092
 
1089
1093
 
 
1094
def reset_manual_replication_state():
 
1095
    '''In manual replication mode, the state of the local database cluster
 
1096
    is outside of Juju's control. We need to detect and update the charm
 
1097
    state to match reality.
 
1098
    '''
 
1099
    if hookenv.config('manual_replication'):
 
1100
        if os.path.exists('recovery.conf'):
 
1101
            local_state['state'] = 'hot standby'
 
1102
        elif slave_count():
 
1103
            local_state['state'] = 'master'
 
1104
        else:
 
1105
            local_state['state'] = 'standalone'
 
1106
        local_state.publish()
 
1107
 
 
1108
 
1090
1109
@hooks.hook()
1091
1110
def config_changed(force_restart=False, mount_point=None):
1092
1111
    validate_config()
1109
1128
                "(config_changed_volume_apply failure)", ERROR)
1110
1129
            sys.exit(1)
1111
1130
 
 
1131
    reset_manual_replication_state()
 
1132
 
1112
1133
    postgresql_config_dir = _get_postgresql_config_dir(config_data)
1113
1134
    postgresql_config = os.path.join(postgresql_config_dir, "postgresql.conf")
1114
1135
    postgresql_hba = os.path.join(postgresql_config_dir, "pg_hba.conf")
1578
1599
 
1579
1600
@hooks.hook('db-relation-joined', 'db-relation-changed')
1580
1601
def db_relation_joined_changed():
 
1602
    reset_manual_replication_state()
1581
1603
    if local_state['state'] == 'hot standby':
1582
1604
        publish_hot_standby_credentials()
1583
1605
        return
1630
1652
 
1631
1653
@hooks.hook('db-admin-relation-joined', 'db-admin-relation-changed')
1632
1654
def db_admin_relation_joined_changed():
 
1655
    reset_manual_replication_state()
1633
1656
    if local_state['state'] == 'hot standby':
1634
1657
        publish_hot_standby_credentials()
1635
1658
        return
2001
2024
        authorized_units.add(unit)
2002
2025
    local_state['authorized'] = authorized_units
2003
2026
 
 
2027
    if hookenv.config('manual_replication'):
 
2028
        log('manual_replication, nothing to do')
 
2029
        return
 
2030
 
2004
2031
    master = elected_master()
2005
2032
 
2006
2033
    # Handle state changes:
2150
2177
 
2151
2178
        # Block until users and database has replicated, so we know the
2152
2179
        # connection details we publish are actually valid. This will
2153
 
        # normally be pretty much instantaneous.
2154
 
        timeout = 60
2155
 
        start = time.time()
2156
 
        while time.time() < start + timeout:
2157
 
            cur = db_cursor(autocommit=True)
2158
 
            cur.execute('select datname from pg_database')
2159
 
            if cur.fetchone() is not None:
2160
 
                break
2161
 
            del cur
2162
 
            log('Waiting for database {} to be replicated'.format(
2163
 
                connection_settings['database']))
2164
 
            time.sleep(10)
 
2180
        # normally be pretty much instantaneous. Do not block if we are
 
2181
        # running in manual replication mode, as it is outside of juju's
 
2182
        # control when replication is actually setup and running.
 
2183
        if not hookenv.config('manual_replication'):
 
2184
            timeout = 60
 
2185
            start = time.time()
 
2186
            while time.time() < start + timeout:
 
2187
                cur = db_cursor(autocommit=True)
 
2188
                cur.execute('select datname from pg_database')
 
2189
                if cur.fetchone() is not None:
 
2190
                    break
 
2191
                del cur
 
2192
                log('Waiting for database {} to be replicated'.format(
 
2193
                    connection_settings['database']))
 
2194
                time.sleep(10)
2165
2195
 
2166
2196
        log("Relation {} connection settings {!r}".format(
2167
2197
            client_relation, connection_settings), DEBUG)