postgresql_version = node.metadata['postgresql']['version']

pkg_apt = {
    'postgresql-common': {},
    'postgresql-client-common': {},
    'postgresql-{}'.format(postgresql_version): {},
    'postgresql-client-{}'.format(postgresql_version): {},
    'postgresql-server-dev-{}'.format(postgresql_version): {}
}

directories = {
    '/etc/postgresql': {
        'owner': None,
        'group': None,
        'mode': None,
        # Keeping old configs messes with cluster-auto-detection.
        'purge': True,
    },
    # This is needed so the above purge does not remove the version
    # currently installed.
    '/etc/postgresql/{}/main'.format(postgresql_version): {
        'owner': 'postgres',
        'group': 'postgres',
        'mode': '0755',
        'needs': {f'pkg_apt:{i}' for i in pkg_apt.keys()},
    },
}

files = {
    "/etc/postgresql/{}/main/pg_hba.conf".format(postgresql_version): {
        'content_type': 'mako',
        'owner': 'postgres',
        'group': 'postgres',
        'triggers': {
            'svc_systemd:postgresql:restart',
        },
    },
    "/etc/postgresql/{}/main/postgresql.conf".format(postgresql_version): {
        'content_type': 'mako',
        'context': {
            'version_list': [int(i) for i in node.metadata['postgresql']['version'].split('.')],
            **node.metadata['postgresql'],
        },
        'owner': 'postgres',
        'group': 'postgres',
        'needs': {
            # postgresql won't start if the configured locale isn't available
            'action:locale-gen',
        } if node.has_bundle('basic') else set(),
        'triggers': {
            'svc_systemd:postgresql:restart',
        },
    },
}

postgres_roles = {
    'root': {
        'password': repo.vault.password_for('{} postgresql root'.format(node.name)),
        'superuser': True,
        'needs': {
            'svc_systemd:postgresql',
        },
    },
}

restart_deps = {
    f'file:/etc/postgresql/{postgresql_version}/main/pg_hba.conf',
    f'file:/etc/postgresql/{postgresql_version}/main/postgresql.conf',
    *{f'pkg_apt:{i}' for i in pkg_apt.keys()},
}

svc_systemd = {
    'postgresql': {
        'needs': restart_deps,
        'triggers': {
            'action:postgresql_wait_after_restart',
        },
    },
}

actions = {
    'postgresql_wait_after_restart': {
        # postgresql doesn't accept connections immediately after restarting
        'command': 'sleep 10',
        'triggered': True,
        'before': {
            'postgres_role:',
            'postgres_db:',
        },
    },
}

for user, config in node.metadata.get('postgresql/roles', {}).items():
    postgres_roles[user] = {
        'password': config['password'],
        'needs': {
            'svc_systemd:postgresql',
        },
    }

for database, config in node.metadata.get('postgresql/databases', {}).items():
    postgres_dbs[database] = config