Skip to content

Commit 6f91866

Browse files
author
Joseph Palermo
authored
Merge pull request #75 from ZPascal/issue-74
feat: Add the possibility to specify the used PostgreSQL version
2 parents 26f31d3 + 69346f0 commit 6f91866

File tree

16 files changed

+154
-19
lines changed

16 files changed

+154
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The complete list of available properties can be found in the [spec](jobs/postgr
7676

7777
Property | Description
7878
-------- | -------------
79+
databases.version | Define the used PostgreSQL major version. Default: 16
7980
databases.port | The database port. Default: 5432
8081
databases.databases | A list of databases and associated properties to create when Postgres starts
8182
databases.databases[n].name | Database name

ci/pipeline.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ jobs:
8686
options:
8787
access_key_id: ((postgres-release-blobstore-user.username))
8888
secret_access_key: ((postgres-release-blobstore-user.password))
89+
- task: bump-yq-package
90+
file: postgres-release/ci/tasks/bump-yq-packages/task.yml
91+
image: bosh-cli-registry-image
92+
input_mapping:
93+
yq-src: postgres-yq-4-src
94+
params:
95+
MAJOR_VERSION: 4
96+
PRIVATE_YML: |
97+
blobstore:
98+
options:
99+
access_key_id: ((postgres-release-blobstore-user.username))
100+
secret_access_key: ((postgres-release-blobstore-user.password))
89101
- put: postgres-release
90102
params:
91103
repository: postgres-release
@@ -232,6 +244,13 @@ resources:
232244
regex: 'href="v(?P<version>16\.[0-9.]+)/"'
233245
uri: "https://ftp.postgresql.org/pub/source/v{version}/postgresql-{version}.tar.gz"
234246

247+
- name: postgres-yq-4-src
248+
type: http-resource
249+
source:
250+
index: "https://api.github.com/repos/mikefarah/yq/releases"
251+
regex: '"tag_name":"v(?P<version>4\.[0-9.]+)/"'
252+
uri: "https://github.com/mikefarah/yq/releases/download/v{version}/yq_linux_amd64"
253+
235254
- name: postgres-release
236255
type: git
237256
source:
@@ -263,4 +282,3 @@ resource_types:
263282
type: docker-image
264283
source:
265284
repository: aequitas/http-resource
266-

ci/tasks/bump-postgres-packages/task.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ pushd postgres-release
1919
fi
2020

2121
if ${NEED_COMMIT}; then
22-
eval $(grep "^current_version=" jobs/postgres/templates/pgconfig.sh.erb)
23-
new_version=$(echo "${FILENAME}" | egrep -o "[0-9.]+[0-9]")
22+
latest_yq_version=$(curl -s -L https://api.github.com/repos/mikefarah/yq/releases/latest | grep "tag_name" | sed s/\"tag_name\":\//g | sed s/\"//g | sed s/\,//g | sed s/v//g | xargs)
23+
curl -s -L https://github.com/mikefarah/yq/releases/download/v${latest_yq_version}/yq_linux_amd64 -o /tmp/yq && chmod +x /tmp/yq
2424

25-
# If new version is greater than current version, update the current_version variables
26-
if printf '%s\n' "$current_version" "$new_version" | sort -V -C; then
27-
sed -i "s/^current_version=.*/current_version=\"${new_version}\"/" jobs/postgres/templates/pgconfig.sh.erb
28-
sed -i "s/^current_version=.*/current_version=\"${new_version}\"/" jobs/bbr-postgres-db/templates/config.sh.erb
29-
fi
25+
echo "-----> $(date): Update the PostgreSQL version inside the used_postgresql_versions.yml file"
26+
current_minor_version=$(cat config/blobs.yml | grep "postgresql-${MAJOR_VERSION}" | cut -f1 -d: | sed "s/postgres\/postgresql-//g" | sed "s/.tar.gz//g")
27+
CURRENT_MINOR_VERSION=$current_minor_version /tmp/yq -i '.postgresql.major_version[env(MAJOR_VERSION)].minor_version = strenv(CURRENT_MINOR_VERSION)' jobs/postgres/config/used_postgresql_versions.yml
3028

3129
echo "-----> $(date): Creating git commit"
3230
git config user.name "$GIT_USER_NAME"

ci/tasks/bump-yq-packages/task.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
NEED_COMMIT=false
3+
4+
set -euo pipefail
5+
6+
echo "${PRIVATE_YML}" > postgres-release/config/private.yml
7+
8+
pushd postgres-release
9+
CURRENT_BLOBS=$(bosh blobs)
10+
CURRENT_VERSION=$(cat ../yq-src/version)
11+
mv ../yq-src/yq_linux_amd64 ../yq-src/postgres-yq-${CURRENT_VERSION}
12+
BLOB_PATH=$(ls ../yq-src/postgres-yq-${CURRENT_VERSION})
13+
FILENAME=$( basename ${BLOB_PATH} )
14+
OLD_BLOB_PATH=$(cat config/blobs.yml | grep "postgres-yq-${MAJOR_VERSION}" | cut -f1 -d:)
15+
if ! echo "${CURRENT_BLOBS}" | grep "${FILENAME}" ; then
16+
NEED_COMMIT=true
17+
echo "adding ${FILENAME}"
18+
bosh add-blob --sha2 "${BLOB_PATH}" "yq/postgres-${FILENAME}"
19+
bosh remove-blob ${OLD_BLOB_PATH}
20+
bosh upload-blobs
21+
fi
22+
23+
if ${NEED_COMMIT}; then
24+
echo "-----> $(date): Creating git commit"
25+
git config user.name "$GIT_USER_NAME"
26+
git config user.email "$GIT_USER_EMAIL"
27+
git add .
28+
29+
git --no-pager diff --cached
30+
if [[ "$( git status --porcelain )" != "" ]]; then
31+
git commit -am "Bump packages"
32+
fi
33+
fi
34+
popd

ci/tasks/bump-yq-packages/task.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
platform: linux
3+
4+
inputs:
5+
- name: yq-src
6+
7+
outputs:
8+
- name: postgres-release
9+
10+
run:
11+
path: postgres-release/ci/tasks/bump-yq-packages/task.sh
12+
13+
params:
14+
PRIVATE_YML:
15+
MAJOR_VERSION:
16+
GIT_USER_NAME: CI Bot
17+
GIT_USER_EMAIL: [email protected]

ci/tasks/create-final-release/task.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ set +x
88
echo "$RELEASE_PRIVATE_YML" >> "config/private.yml"
99
set -x
1010

11+
latest_yq_version=$(curl -s -L https://api.github.com/repos/mikefarah/yq/releases/latest | grep "tag_name" | sed s/\"tag_name\":\//g | sed s/\"//g | sed s/\,//g | sed s/v//g | xargs)
12+
curl -s -L https://github.com/mikefarah/yq/releases/download/v${latest_yq_version}/yq_linux_amd64 -o /tmp/yq && chmod +x /tmp/yq
13+
1114
bosh -n create-release --final
1215
new_release_version="$(find releases -regex ".*postgres-[0-9]*.yml" | egrep -o "[0-9]+" | sort -n | tail -n 1)"
1316

14-
eval $(grep "^current_version=" jobs/postgres/templates/pgconfig.sh.erb)
15-
sed -i "/^versions:/a\ \ ${new_release_version}: \"PostgreSQL ${current_version}\"" versions.yml
17+
current_major_version=$(/tmp/yq '.postgresql.default' jobs/postgres/config/used_postgresql_versions.yml)
18+
current_minor_version=$(CURRENT_MAJOR_VERSION=$current_major_version /tmp/yq '.postgresql.major_version[env(CURRENT_MAJOR_VERSION)].minor_version' jobs/postgres/config/used_postgresql_versions.yml)
19+
sed -i "/^versions:/a\ \ ${new_release_version}: \"PostgreSQL ${current_minor_version}\"" versions.yml
1620
git add versions.yml
1721

1822
git add .final_builds releases

ci/tasks/run-acceptance-tests/task.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
root="${PWD}"
44

5+
function setup_yq() {
6+
latest_yq_version=$(curl -s -L https://api.github.com/repos/mikefarah/yq/releases/latest | grep "tag_name" | sed s/\"tag_name\":\//g | sed s/\"//g | sed s/\,//g | sed s/v//g | xargs)
7+
curl -s -L https://github.com/mikefarah/yq/releases/download/v${latest_yq_version}/yq_linux_amd64 -o /tmp/yq && chmod +x /tmp/yq
8+
}
9+
510
function setup_bosh() {
611
source start-bosh
712
source /tmp/local-bosh/director/env
@@ -43,10 +48,13 @@ function upload_release() {
4348

4449
function main() {
4550
setup_bosh
51+
setup_yq
4652
install_bbr
4753
upload_release
4854
bosh upload-stemcell stemcell/stemcell.tgz
49-
cat ${root}/postgres-release/jobs/postgres/templates/pgconfig.sh.erb | grep current_version > ${root}/pgconfig.sh
55+
current_major_version=$(/tmp/yq '.postgresql.default' jobs/postgres/config/used_postgresql_versions.yml)
56+
current_minor_version=$(CURRENT_MAJOR_VERSION=$current_major_version /tmp/yq '.postgresql.major_version[env(CURRENT_MAJOR_VERSION)].minor_version' jobs/postgres/config/used_postgresql_versions.yml)
57+
echo "current_version=${current_minor_version}" > ${root}/pgconfig.sh
5058
source ${root}/pgconfig.sh
5159
config_file="${root}/pgats_config.yml"
5260
create_config_file > $config_file

jobs/bbr-postgres-db/spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ packages:
1818
- postgres-13
1919
- postgres-15
2020
- postgres-16
21+
- postgres-yq-4
2122

2223
consumes:
2324
- name: database

jobs/bbr-postgres-db/templates/config.sh.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ if_link("database") do |data|
2222
end
2323

2424
%>
25-
current_version="16.3"
25+
2626
JOB_DIR="/var/vcap/jobs/bbr-postgres-db"
27-
PACKAGE_DIR="/var/vcap/packages/postgres-${current_version%.*}"
27+
current_version=postgres-$(MAJOR_VERSION=<%=p('databases.version')%> ${JOB_DIR}/packages/postgres-yq-4/bin/yq '.postgresql.major_version[env(MAJOR_VERSION)].minor_version' /var/vcap/jobs/postgres/config/used_postgresql_versions.yml)
28+
29+
PACKAGE_DIR="/var/vcap/packages/${current_version%.*}"
2830
PORT="<%= port %>"
2931
DATABASES=(<%= databases.map{|d| d["name"]}.join(' ')%>)
3032
DBHOST=<%= dbhost %>

jobs/postgres/spec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ templates:
2424
hooks/postgres-pre-stop.sh.erb: bin/hooks/postgres-pre-stop.sh
2525
hooks/postgres-post-start.sh.erb: bin/hooks/postgres-post-start.sh
2626
hooks/postgres-post-stop.sh.erb: bin/hooks/postgres-post-stop.sh
27+
used_postgresql_versions.yml: config/used_postgresql_versions.yml
2728

2829
packages:
2930
- postgres-common
3031
- postgres-11
3132
- postgres-13
3233
- postgres-15
3334
- postgres-16
35+
- postgres-yq-4
3436

3537
provides:
3638
- name: postgres
@@ -42,6 +44,9 @@ provides:
4244
- databases.tls.ca
4345

4446
properties:
47+
databases.version:
48+
description: "The database version e.g. 11, 13, 15 or 16"
49+
default: 16
4550
databases.port:
4651
description: "The database port"
4752
default: 5432

jobs/postgres/templates/pgconfig.sh.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ set -x # if you want tracing disabled, set 'databases.enable_traces: false' in t
66
ENABLE_TRACE=0
77
# set -x # uncomment it if you want to enable tracing in all control scripts
88
<% end %>
9-
current_version="16.3"
10-
pgversion_current="postgres-${current_version}"
11-
129
JOB_DIR=/var/vcap/jobs/postgres
10+
current_version="<%=p('databases.version')%>"
11+
pgversion_current=postgres-$(MAJOR_VERSION=<%=p('databases.version')%> ${JOB_DIR}/packages/postgres-yq-4/bin/yq '.postgresql.major_version[env(MAJOR_VERSION)].minor_version' /var/vcap/jobs/postgres/config/used_postgresql_versions.yml)
12+
1313
PACKAGE_DIR="/var/vcap/packages/${pgversion_current%.*}"
1414
STORE_DIR=/var/vcap/store
1515
PG_STORE_DIR=${STORE_DIR}/postgres
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
postgresql:
2+
default: 16
3+
major_version:
4+
16:
5+
minor_version: "16.3"
6+
15:
7+
minor_version: "15.6"
8+
13:
9+
minor_version: "13.14"
10+
11:
11+
minor_version: "11.22"

jobs/postgres/templates/utils.sh.erb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ function is_major() {
6868
[ "${pgversion_current%.*}" != "${pgversion_upgrade_from%.*}" ]
6969
}
7070

71+
function check_postgresql_versions(){
72+
if [[ $(echo -e "$pgversion_upgrade_from\n$pgversion_current" | sort --version-sort | head --lines=1) != $pgversion_upgrade_from ]]; then
73+
echo "The downgrade of the database instance is not supported."
74+
exit 1
75+
fi
76+
}
77+
7178
function init_data_dir(){
7279
if [ ! -f "${DATA_DIR}/postgresql.conf" ]; then
7380
# initdb creates data directories
@@ -77,7 +84,7 @@ function init_data_dir(){
7784
}
7885

7986
function run_major_upgrade(){
80-
if is_major; then
87+
if is_major && check_postgresql_versions; then
8188
rm -rf ${DATA_DIR_PREVIOUS}
8289
echo "Running a PostgreSQL major upgrade from ${pgversion_upgrade_from} to ${pgversion_current}"
8390
touch "$POSTGRES_UPGRADE_LOCK"
@@ -105,7 +112,7 @@ EOF
105112
fi
106113
}
107114
function run_minor_upgrade(){
108-
if ! is_major; then
115+
if ! is_major && check_postgresql_versions; then
109116
rm -rf ${DATA_DIR_PREVIOUS}
110117
echo "Running a PostgreSQL minor upgrade from ${pgversion_upgrade_from} to ${pgversion_current}"
111118
<% if p("databases.skip_data_copy_in_minor") %>

packages/postgres-yq-4/packaging

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash -exu
2+
3+
function main() {
4+
install
5+
6+
}
7+
8+
function install() {
9+
10+
echo "Install yq..."
11+
mkdir -p "${BOSH_INSTALL_TARGET}/bin"
12+
cp -a yq/postgres-yq-4.* "${BOSH_INSTALL_TARGET}/bin/yq"
13+
chmod +x "${BOSH_INSTALL_TARGET}/bin/yq"
14+
15+
}
16+
17+
main

packages/postgres-yq-4/spec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
name: postgres-yq-4
3+
files:
4+
- yq/postgres-yq-4.*

test/test_major_or_minor_version.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ pgversion_current=$2
55
# From postgres-x.y.z, it's major if x and y are not the same
66
# in $pgversion_current and $pgversion_upgrade_from
77

8+
function check_postgresql_versions(){
9+
if [[ $(echo -e "$pgversion_upgrade_from\n$pgversion_current" | sort --version-sort | head --lines=1) != $pgversion_upgrade_from ]]; then
10+
echo "The downgrade of the database instance is not supported."
11+
exit 1
12+
fi
13+
}
14+
815
function is_major() {
916
[ "${pgversion_current%.*}" != "${pgversion_upgrade_from%.*}" ]
1017
}
11-
if is_major; then
18+
19+
if is_major && check_postgresql_versions; then
1220
echo is major
1321
else
1422
echo is minor

0 commit comments

Comments
 (0)