Esempio n. 1
0
def version():
    """PostgreSQL version. major.minor, as a string."""
    # Use a cached version if available, to ensure this
    # method returns the same version consistently, even
    # across OS release upgrades.
    version = unitdata.kv().get("postgresql.pg_version")
    if version:
        return version

    # If no cached version, use the version reported by
    # pg_lsclusters
    version = lsclusters_version()
    if version:
        unitdata.kv().set("postgresql.pg_version", version)
        return version

    # We use the charm configuration here, as multiple versions
    # of PostgreSQL may be installed.
    config = hookenv.config()
    version = config.get("version")
    if version:
        unitdata.kv().set("postgresql.pg_version", version)
        return version

    # If the version wasn't set, we are using the default version for
    # the distro release.
    version_map = dict(xenial="9.5", bionic="10", focal="12")
    try:
        version = version_map[helpers.distro_codename()]
    except KeyError:
        raise NotImplementedError("No default version for distro {}".format(helpers.distro_codename()))
    unitdata.kv().set("postgresql.pg_version", version)
    return version
Esempio n. 2
0
def configure_sources():
    '''Add the PGDB apt sources, if selected.'''
    config = hookenv.config()

    # Shortcut for the PGDG archive.
    if config['pgdg']:
        pgdg_url = 'http://apt.postgresql.org/pub/repos/apt/'
        pgdg_src = 'deb {} {}-pgdg main'.format(pgdg_url,
                                                helpers.distro_codename())
        pgdg_key_path = os.path.join(hookenv.charm_dir(), 'lib', 'pgdg.key')
        with open(pgdg_key_path, 'r') as f:
            hookenv.log('Adding PGDG archive')
            apt.add_source(pgdg_src, f.read())
Esempio n. 3
0
def version():
    '''PostgreSQL version. major.minor, as a string.'''
    # We use the charm configuration here, as multiple versions
    # of PostgreSQL may be installed.
    config = hookenv.config()
    version = config.get('version')
    if version:
        return version

    # If the version wasn't set, we are using the default version for
    # the distro release.
    version_map = dict(precise='9.1', trusty='9.3', xenial='9.5')
    return version_map[helpers.distro_codename()]
Esempio n. 4
0
def add_pgdg_source():
    pgdg_url = "http://apt.postgresql.org/pub/repos/apt/"
    pgdg_src = "deb {} {}-pgdg main".format(pgdg_url, helpers.distro_codename())
    pgdg_key_path = os.path.join(hookenv.charm_dir(), "lib", "pgdg.key")
    kv = unitdata.kv()
    k = "postgresql.service.pgdg_src"
    # Add the source if the key has changed, or if the src has
    # changed such as a distro upgrade. Check key change first,
    # or short circuiting will add the source twice.
    if reactive.helpers.any_file_changed([pgdg_key_path]) or pgdg_src != kv.get(k):
        with open(pgdg_key_path, "r") as f:
            hookenv.log("Adding PGDG archive")
            apt.add_source(pgdg_src, f.read())
        kv.set(k, pgdg_src)