Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL v13 installation #2330

Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5e2c40b
Removed auto generated vars
atsikham May 18, 2021
38926c2
Updated postgresql default configuration with correct settings
atsikham May 18, 2021
92d877f
Updated tests according to the new version
atsikham May 18, 2021
129f59e
Increased postgresql, pgaudit, pgpool, repmgr versions
atsikham May 18, 2021
5881f1d
Postgres installation
atsikham May 18, 2021
a48d2cb
Merge branch 'develop' of https://github.com/epiphany-platform/epipha…
atsikham May 18, 2021
e63cb7f
Repmgr RedHat installation
atsikham May 18, 2021
e9780fd
Commented pgbouncer package not to fail installation
atsikham May 18, 2021
173ce55
Merge branch 'develop' of https://github.com/epiphany-platform/epipha…
atsikham May 19, 2021
54640cc
Ubuntu installation except pgbouncer
atsikham May 23, 2021
6aee001
Updated components
atsikham May 23, 2021
e70b123
Added molecule tests and documentation
atsikham May 24, 2021
da57e0c
Modified vars
atsikham May 24, 2021
6938d72
Asciidoc to markdown
atsikham May 27, 2021
f313962
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
33f5fe8
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
717bfa8
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
67f3550
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
e01b89f
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
d72a72b
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
66278d9
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
808e932
Applied requested changes after review
atsikham Jun 1, 2021
ffec4e0
Merge branch 'feature/upgrade-postgresql-install' of https://github.c…
atsikham Jun 1, 2021
1e7a1df
Update core/src/epicli/data/common/ansible/playbooks/roles/postgresql…
atsikham Jun 1, 2021
aba72f1
Merge branch 'feature/upgrade-postgresql-install' of https://github.c…
atsikham Jun 1, 2021
7924b3f
Fixed repmgr check
atsikham Jun 1, 2021
6709b9c
Require 2 hosts for replication
to-bar Jun 2, 2021
f1669cc
Improve molecule tests
to-bar Jun 2, 2021
71e8282
Add {{ ansible_managed }} to templates
to-bar Jun 2, 2021
8a0aff3
Remove unused templates
to-bar Jun 2, 2021
87d5d9c
Improve task names
to-bar Jun 2, 2021
57113a4
Do not hide all failures of "Get cluster status" task
to-bar Jun 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- name: Check if database is running on node0 database server
become: true
become_user: postgres
command: "{{ repmgr.pg_bindir[ansible_os_family] }}/pg_isready"
command: "{{ pg.bindir[ansible_os_family] }}/pg_isready"
register: node0
ignore_errors: True
when: groups['postgresql'][0] == inventory_hostname
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
---
pg:
bindir:
RedHat: /usr/pgsql-13/bin
Debian: /usr/lib/postgresql/13/bin
service_name:
RedHat: postgresql-10
RedHat: postgresql-13
Debian: postgresql
instantiated_service_name:
RedHat: null
Debian: postgresql@10-main
Debian: postgresql@13-main
base_dir:
RedHat: /var/lib/pgsql
Debian: /var/lib/postgresql
config_dir:
RedHat: /var/lib/pgsql/10/data
Debian: /etc/postgresql/10/main
RedHat: /var/lib/pgsql/13/data
Debian: /etc/postgresql/13/main
data_dir:
RedHat: /var/lib/pgsql/10/data
Debian: /var/lib/postgresql/10/main
RedHat: /var/lib/pgsql/13/data
Debian: /var/lib/postgresql/13/main

repmgr:
bindir:
RedHat: /usr/pgsql-10/bin
RedHat: /usr/pgsql-13/bin
Debian: /usr/bin
pg_bindir:
RedHat: /usr/pgsql-10/bin
Debian: /usr/lib/postgresql/10/bin
service_name:
RedHat: repmgr10
RedHat: repmgr13
Debian: repmgrd
config_dir:
RedHat: /etc/repmgr/10
Debian: /etc/postgresql/10/main
RedHat: /etc/repmgr/13
Debian: /etc/postgresql/13/main

pgbouncer:
logfile:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
= Installation guide
atsikham marked this conversation as resolved.
Show resolved Hide resolved

== Requirements

* Internet connection
* Molecule link:https://molecule.readthedocs.io/en/latest/installation.html[dependencies]
* Docker
* ansible-lint

== Installation

[source:bash]
----
pip install 'molecule[docker]'
pip install ansible-lint
----

== Execution

=== General

[source:bash]
----
molecule test -s <scenario-name>
----

=== Separate steps

[source:bash]
----
molecule lint -s <scenario-name>
molecule converge -s <scenario-name>
molecule idempotence -s <scenario-name>
molecule verify -s <scenario-name>
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash -eu

apt update && apt -y install wget gpg-agent

wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list

wget -qO - https://dl.2ndquadrant.com/gpg-key.asc | apt-key add -
echo "deb https://dl.2ndquadrant.com/default/release/apt bionic-2ndquadrant main" | tee /etc/apt/sources.list.d/2ndquadrant-dl-default-release.list

apt update
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env bash

set -euo pipefail

CREATE_LOGFILE='no'

# params: <repo_id> <config_file_content>
add_repo_as_file() {
local repo_id="$1"
local config_file_content="$2"
local config_file_name="$repo_id.repo"

if ! is_repo_enabled "$repo_id"; then
echol "Adding repository: $repo_id"
cat <<<"$config_file_content" >"/etc/yum.repos.d/$config_file_name" ||
exit_with_error "Function add_repo_as_file failed for repo: $repo_id"
local -a gpg_key_urls
IFS=" " read -r -a gpg_key_urls \
<<<"$(grep -i --only-matching --perl-regexp '(?<=^gpgkey=)http[^#\n]+' <<<"$config_file_content")"
if ((${#gpg_key_urls[@]} > 0)); then
import_repo_gpg_keys "${gpg_key_urls[@]}" 3
fi
# to accept import of repo's GPG key (for repo_gpgcheck=1)
yum -y repolist >/dev/null || exit_with_error "Command failed: yum -y repolist"
fi
}

# params: <script_url>
add_repo_from_script() {
local script_url="$1"

echol "Running: curl $script_url | bash"
curl $script_url | bash
}

# params: <repo_id>
disable_repo() {
local repo_id="$1"

if yum repolist enabled | grep --quiet "$repo_id"; then
echol "Disabling repository: $repo_id"
yum-config-manager --disable "$repo_id" ||
exit_with_error "Command failed: yum-config-manager --disable \"$repo_id\""
fi
}

echol() {
echo -e "$@"
if [[ $CREATE_LOGFILE == 'yes' ]]; then
local timestamp=$(date +"%b %e %H:%M:%S")
echo -e "${timestamp}: $@" >>"$LOG_FILE_PATH"
fi
}

# params: <url(s)> <retries>
import_repo_gpg_keys() {
local retries=${!#} # get last arg
local urls=("${@:1:$#-1}") # remove last arg

for url in "${urls[@]}"; do
run_cmd_with_retries rpm --import "$url" "$retries"
done
}

# params: <repo_id>
is_repo_enabled() {
local repo_id="$1"

if yum repolist | grep --quiet "$repo_id"; then
echol "Repository $repo_id already enabled"
return 0
else
return 1
fi
}

# Runs command as array with printing it, doesn't support commands with shell operators (such as pipe or redirection)
# params: <command to execute> [--no-exit-on-error]
run_cmd() {
local cmd_arr=("$@")

local exit_on_error=1
if [[ ${cmd_arr[-1]} == '--no-exit-on-error' ]]; then
exit_on_error=0
cmd_arr=("${cmd_arr[@]:0:$#-1}") # remove last item
fi

local escaped_string return_code
escaped_string=$(_print_array_as_shell_escaped_string "${cmd_arr[@]}")
echol "Executing: ${escaped_string}"
"${cmd_arr[@]}"
return_code=$?
if ((return_code != 0)) && ((exit_on_error)); then
exit_with_error "Command failed: ${escaped_string}"
else
return $return_code
fi
}

# Runs command with retries, doesn't support commands with shell operators (such as pipe or redirection)
# params: <command to execute> <retries>
run_cmd_with_retries() {
# pop 'retries' argument
local retries=${!#} # get last arg (indirect expansion)
set -- "${@:1:$#-1}" # set new "$@"

local cmd_arr=("$@")
(# sub-shell is used to limit scope for 'set +e'
set +e
trap - ERR # disable global trap locally
for ((i = 0; i <= retries; i++)); do
run_cmd "${cmd_arr[@]}" '--no-exit-on-error'
return_code=$?
if ((return_code == 0)); then
break
elif ((i < retries)); then
sleep 1
echol "retrying ($((i + 1))/${retries})"
else
echol "ERROR: all attempts failed"
local escaped_string
escaped_string=$(_print_array_as_shell_escaped_string "${cmd_arr[@]}")
exit_with_error "Command failed: ${escaped_string}"
fi
done
return $return_code
)
}

POSTGRESQL_REPO_CONF=$(
cat <<'EOF'
[pgdg13]
name=PostgreSQL 13 for RHEL/CentOS $releasever - $basearch
baseurl=https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-$releasever-$basearch
enabled=1
gpgcheck=1
gpgkey=https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG
EOF
)

add_repo_as_file 'postgresql-13' "$POSTGRESQL_REPO_CONF"
add_repo_from_script 'https://dl.2ndquadrant.com/default/release/get/13/rpm' # for repmgr
disable_repo '2ndquadrant-dl-default-release-pg13-debug' # script adds 2 repositories, only 1 is required
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- name: Converge
hosts: postgresql
become: true
become_method: sudo
module_defaults:
shell:
executable: /bin/bash
tasks:
- name: Include 'configuration/postgresql' variables
include_vars:
file: ../vars-repmgr.yml

- name: Include postgresql role
include_role:
name: postgresql
vars:
yum_lock_timeout: 300 # https://github.com/ansible/ansible/issues/57189
roles_with_generated_vars: [ ] # don't execute PgPool related tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
dependency:
enabled: false
lint: ansible-lint --exclude molecule
driver:
name: docker
platforms:
- name: instance-1
groups:
- postgresql
image: geerlingguy/docker-ubuntu1804-ansible:python2
pre_build_image: true
capabilities:
- SYS_ADMIN
command: /lib/systemd/systemd
tmpfs:
- /run
- /tmp
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: instance-2
groups:
- postgresql
image: geerlingguy/docker-ubuntu1804-ansible:python2
pre_build_image: true
capabilities:
- SYS_ADMIN
command: /lib/systemd/systemd
tmpfs:
- /run
- /tmp
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
provisioner:
name: ansible
playbooks:
prepare: prepare.yml
converge: converge.yml
verify: verify.yml
inventory:
group_vars:
postgresql:
ansible_python_interpreter: /usr/bin/python2
verifier:
name: ansible
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Converge
atsikham marked this conversation as resolved.
Show resolved Hide resolved
hosts: postgresql
tasks:
- name: Add repositories for dependencies downloading
script: ../add-repos-debian.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
- name: Verify
hosts: postgresql
gather_facts: false
tasks:
- name: Check that DB port is open
block:
- name: Ensure that DB port is open
wait_for:
host: 0.0.0.0
port: 5432
state: started
delay: 0
timeout: 3
register: port_status

- name: Verify that port status is not changed
assert:
that:
- not port_status.changed
atsikham marked this conversation as resolved.
Show resolved Hide resolved

- name: Check that PostgreSQL services are started and enabled
block:
- name: Ensure that postgresql service is started and enabled
systemd:
name: postgresql
enabled: true
atsikham marked this conversation as resolved.
Show resolved Hide resolved
register: svc_status

- name: Ensure that postgresql@13-main service is started and enabled
systemd:
name: postgresql
atsikham marked this conversation as resolved.
Show resolved Hide resolved
enabled: true
register: svc_main_status

- name: Verify that service statuses are not changed
assert:
that:
- not svc_status.changed
- not svc_main_status.changed

- name: Check that repmgrd service is started and enabled
atsikham marked this conversation as resolved.
Show resolved Hide resolved
block:
- name: Ensure that repmgrd service is started and enabled
systemd:
name: repmgrd
enabled: true
register: svc_repmgr_status

- name: Verify that service status is not changed
assert:
that:
- not svc_repmgr_status.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- name: Converge
hosts: postgresql
become: true
become_method: sudo
module_defaults:
shell:
executable: /bin/bash
tasks:
- name: Include 'configuration/postgresql' variables
include_vars:
file: ../vars-single-node.yml

- name: Include postgresql role
include_role:
name: postgresql
vars:
yum_lock_timeout: 300 # https://github.com/ansible/ansible/issues/57189
roles_with_generated_vars: [ ] # don't execute PgPool related tasks
Loading