Esempio n. 1
0
def update_postgresql_conf():
    settings = assemble_postgresql_conf()
    path = postgresql.postgresql_conf_path()

    with open(path, "r") as f:
        pg_conf = f.read()

    start_mark = "### BEGIN JUJU SETTINGS ###"
    end_mark = "### END JUJU SETTINGS ###"

    # Strip the existing settings section, including the markers.
    pg_conf = re.sub(
        r"^\s*{}.*^\s*{}\s*$".format(re.escape(start_mark), re.escape(end_mark)),
        "",
        pg_conf,
        flags=re.I | re.M | re.DOTALL,
    )

    for k in settings:
        # Comment out conflicting options. We could just allow later
        # options to override earlier ones, but this is less surprising.
        pg_conf = re.sub(
            r"^\s*({}[\s=].*)$".format(re.escape(k)),
            r"# juju # \1",
            pg_conf,
            flags=re.M | re.I,
        )

    # Store the updated charm options. This is compared with the
    # live config to detect if a restart is required.
    store = unitdata.kv()
    current_prefix = "postgresql.cluster.pgconf.current."
    store.unsetrange(prefix=current_prefix)
    store.update(settings, prefix=current_prefix)

    # Generate the charm config section, adding it to the end of the
    # config file.
    simple_re = re.compile(r"^[-.\w]+$")
    override_section = [start_mark]
    for k, v in settings.items():
        v = str(v)
        assert "\n" not in v, "Invalid config value {!r}".format(v)
        if simple_re.search(v) is None:
            v = "'{}'".format(v.replace("'", "''"))
        override_section.append("{} = {}".format(k, v))
    if postgresql.has_version("12"):
        override_section.append("include_if_exists '{}'".format(postgresql.hot_standby_conf_path()))
    override_section.append(end_mark)
    pg_conf += "\n" + "\n".join(override_section)

    helpers.rewrite(path, pg_conf)
Esempio n. 2
0
def update_pg_hba_conf():
    # grab the needed current state
    config = hookenv.config()
    rels = context.Relations()
    path = postgresql.pg_hba_conf_path()
    with open(path, "r") as f:
        pg_hba = f.read()

    # generate the new state
    pg_hba_content = generate_pg_hba_conf(pg_hba, config, rels)

    # write out the new state
    helpers.rewrite(path, pg_hba_content)

    # Use @when_file_changed for this when Issue #44 is resolved.
    if reactive.helpers.any_file_changed([path]) and reactive.is_state("postgresql.cluster.is_running"):
        hookenv.log("pg_hba.conf has changed. PostgreSQL needs reload.")
        reactive.set_state("postgresql.cluster.needs_reload")
Esempio n. 3
0
def update_pg_hba_conf():
    # grab the needed current state
    config = hookenv.config()
    rels = context.Relations()
    path = postgresql.pg_hba_conf_path()
    with open(path, 'r') as f:
        pg_hba = f.read()

    # generate the new state
    pg_hba_content = generate_pg_hba_conf(pg_hba, config, rels)

    # write out the new state
    helpers.rewrite(path, pg_hba_content)

    # Use @when_file_changed for this when Issue #44 is resolved.
    if (reactive.helpers.any_file_changed([path]) and
            reactive.is_state('postgresql.cluster.is_running')):
        hookenv.log('pg_hba.conf has changed. PostgreSQL needs reload.')
        reactive.set_state('postgresql.cluster.needs_reload')
Esempio n. 4
0
def update_postgresql_conf():
    settings = assemble_postgresql_conf()
    path = postgresql.postgresql_conf_path()

    with open(path, 'r') as f:
        pg_conf = f.read()

    start_mark = '### BEGIN JUJU SETTINGS ###'
    end_mark = '### END JUJU SETTINGS ###'

    # Strip the existing settings section, including the markers.
    pg_conf = re.sub(r'^\s*{}.*^\s*{}\s*$'.format(re.escape(start_mark),
                                                  re.escape(end_mark)),
                     '', pg_conf, flags=re.I | re.M | re.DOTALL)

    for k in settings:
        # Comment out conflicting options. We could just allow later
        # options to override earlier ones, but this is less surprising.
        pg_conf = re.sub(r'^\s*({}[\s=].*)$'.format(re.escape(k)),
                         r'# juju # \1', pg_conf, flags=re.M | re.I)

    # Store the updated charm options. This is compared with the
    # live config to detect if a restart is required.
    store = unitdata.kv()
    current_prefix = 'postgresql.cluster.pgconf.current.'
    store.unsetrange(prefix=current_prefix)
    store.update(settings, prefix=current_prefix)

    # Generate the charm config section, adding it to the end of the
    # config file.
    simple_re = re.compile(r'^[-.\w]+$')
    override_section = [start_mark]
    for k, v in settings.items():
        v = str(v)
        assert '\n' not in v, "Invalid config value {!r}".format(v)
        if simple_re.search(v) is None:
            v = "'{}'".format(v.replace("'", "''"))
        override_section.append('{} = {}'.format(k, v))
    override_section.append(end_mark)
    pg_conf += '\n' + '\n'.join(override_section)

    helpers.rewrite(path, pg_conf)