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

feat: Add the possibility to specify the used PostgreSQL version #75

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The complete list of available properties can be found in the [spec](jobs/postgr

Property | Description
-------- | -------------
databases.version | Define the used PostgreSQL major version. Default: 15
databases.port | The database port. Default: 5432
databases.databases | A list of databases and associated properties to create when Postgres starts
databases.databases[n].name | Database name
Expand Down
20 changes: 19 additions & 1 deletion ci/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ jobs:
options:
access_key_id: ((postgres-release-blobstore-user.username))
secret_access_key: ((postgres-release-blobstore-user.password))
- task: bump-yq-package
file: postgres-release/ci/tasks/bump-yq-packages/task.yml
image: bosh-cli-registry-image
input_mapping:
yq-src: yq-4-src
params:
MAJOR_VERSION: 1
ZPascal marked this conversation as resolved.
Show resolved Hide resolved
PRIVATE_YML: |
blobstore:
options:
access_key_id: ((postgres-release-blobstore-user.username))
secret_access_key: ((postgres-release-blobstore-user.password))
- put: postgres-release
params:
repository: postgres-release
Expand Down Expand Up @@ -232,6 +244,13 @@ resources:
regex: 'href="v(?P<version>16\.[0-9.]+)/"'
uri: "https://ftp.postgresql.org/pub/source/v{version}/postgresql-{version}.tar.gz"

- name: yq-4-src
type: http-resource
source:
index: "https://api.github.com/repos/mikefarah/yq/releases"
regex: '"tag_name":"v(?P<version>4\.[0-9.]+)/"'
uri: "https://github.com/mikefarah/yq/releases/download/v{version}/yq_linux_amd64"

- name: postgres-release
type: git
source:
Expand Down Expand Up @@ -263,4 +282,3 @@ resource_types:
type: docker-image
source:
repository: aequitas/http-resource

11 changes: 3 additions & 8 deletions ci/tasks/bump-postgres-packages/task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ pushd postgres-release
fi

if ${NEED_COMMIT}; then
eval $(grep "^current_version=" jobs/postgres/templates/pgconfig.sh.erb)
new_version=$(echo "${FILENAME}" | egrep -o "[0-9.]+[0-9]")

# If new version is greater than current version, update the current_version variables
if printf '%s\n' "$current_version" "$new_version" | sort -V -C; then
sed -i "s/^current_version=.*/current_version=\"${new_version}\"/" jobs/postgres/templates/pgconfig.sh.erb
sed -i "s/^current_version=.*/current_version=\"${new_version}\"/" jobs/bbr-postgres-db/templates/config.sh.erb
fi
echo "-----> $(date): Update the PostgreSQL version inside the used_postgresql_versions.yml file"
current_minor_version=$(cat config/blobs.yml | grep "postgresql-${MAJOR_VERSION}" | cut -f1 -d: | sed "s/postgres\/postgresql-//g" | sed "s/.tar.gz//g")
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
ZPascal marked this conversation as resolved.
Show resolved Hide resolved

echo "-----> $(date): Creating git commit"
git config user.name "$GIT_USER_NAME"
Expand Down
34 changes: 34 additions & 0 deletions ci/tasks/bump-yq-packages/task.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
NEED_COMMIT=false

set -euo pipefail

echo "${PRIVATE_YML}" > postgres-release/config/private.yml

pushd postgres-release
CURRENT_BLOBS=$(bosh blobs)
CURRENT_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)
ZPascal marked this conversation as resolved.
Show resolved Hide resolved
mv ../yq-src/yq_darwin_amd64 ../yq-src/yq-${CURRENT_VERSION}
ZPascal marked this conversation as resolved.
Show resolved Hide resolved
BLOB_PATH=$(ls ../yq-src/yq-${CURRENT_VERSION})
FILENAME=$( basename ${BLOB_PATH} )
OLD_BLOB_PATH=$(cat config/blobs.yml | grep "yq-${MAJOR_VERSION}" | cut -f1 -d:)
if ! echo "${CURRENT_BLOBS}" | grep "${FILENAME}" ; then
NEED_COMMIT=true
echo "adding ${FILENAME}"
bosh add-blob --sha2 "${BLOB_PATH}" "yq/${FILENAME}"
bosh remove-blob ${OLD_BLOB_PATH}
bosh upload-blobs
fi

if ${NEED_COMMIT}; then
echo "-----> $(date): Creating git commit"
git config user.name "$GIT_USER_NAME"
git config user.email "$GIT_USER_EMAIL"
git add .

git --no-pager diff --cached
if [[ "$( git status --porcelain )" != "" ]]; then
git commit -am "Bump packages"
fi
fi
popd
17 changes: 17 additions & 0 deletions ci/tasks/bump-yq-packages/task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
platform: linux

inputs:
- name: yq-src

outputs:
- name: postgres-release

run:
path: postgres-release/ci/tasks/bump-yq-packages/task.sh

params:
PRIVATE_YML:
MAJOR_VERSION:
GIT_USER_NAME: CI Bot
GIT_USER_EMAIL: bots@cloudfoundry.org
8 changes: 6 additions & 2 deletions ci/tasks/create-final-release/task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ set +x
echo "$RELEASE_PRIVATE_YML" >> "config/private.yml"
set -x

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)
curl -s -L https://github.com/mikefarah/yq/releases/download/v${latest_yq_version}/yq_linux_amd64 -o /tmp/yq && chmod +x /tmp/yq

bosh -n create-release --final
new_release_version="$(find releases -regex ".*postgres-[0-9]*.yml" | egrep -o "[0-9]+" | sort -n | tail -n 1)"

eval $(grep "^current_version=" jobs/postgres/templates/pgconfig.sh.erb)
sed -i "/^versions:/a\ \ ${new_release_version}: \"PostgreSQL ${current_version}\"" versions.yml
current_major_version=$(/tmp/yq '.postgresql.default' jobs/postgres/config/used_postgresql_versions.yml)
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)
sed -i "/^versions:/a\ \ ${new_release_version}: \"PostgreSQL ${current_minor_version}\"" versions.yml
git add versions.yml

git add .final_builds releases
Expand Down
10 changes: 9 additions & 1 deletion ci/tasks/run-acceptance-tests/task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

root="${PWD}"

function setup_yq() {
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)
curl -s -L https://github.com/mikefarah/yq/releases/download/v${latest_yq_version}/yq_linux_amd64 -o /tmp/yq && chmod +x /tmp/yq
}

function setup_bosh() {
source start-bosh
source /tmp/local-bosh/director/env
Expand Down Expand Up @@ -43,10 +48,13 @@ function upload_release() {

function main() {
setup_bosh
setup_yq
install_bbr
upload_release
bosh upload-stemcell stemcell/stemcell.tgz
cat ${root}/postgres-release/jobs/postgres/templates/pgconfig.sh.erb | grep current_version > ${root}/pgconfig.sh
current_major_version=$(/tmp/yq '.postgresql.default' jobs/postgres/config/used_postgresql_versions.yml)
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)
echo "current_version=${current_minor_version}" > ${root}/pgconfig.sh
source ${root}/pgconfig.sh
config_file="${root}/pgats_config.yml"
create_config_file > $config_file
Expand Down
1 change: 1 addition & 0 deletions jobs/bbr-postgres-db/spec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ packages:
- postgres-13
- postgres-15
- postgres-16
- yq-4

consumes:
- name: database
Expand Down
5 changes: 3 additions & 2 deletions jobs/bbr-postgres-db/templates/config.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ if_link("database") do |data|
end

%>
current_version="16.3"

current_version=postgres-$(MAJOR_VERSION=<%=p('databases.version')%> ${JOB_DIR}/packages/yq-4/bin/yq '.postgresql.major_version[env(MAJOR_VERSION)].minor_version' /var/vcap/jobs/postgres/config/used_postgresql_versions.yml)
ZPascal marked this conversation as resolved.
Show resolved Hide resolved
JOB_DIR="/var/vcap/jobs/bbr-postgres-db"
PACKAGE_DIR="/var/vcap/packages/postgres-${current_version%.*}"
PACKAGE_DIR="/var/vcap/packages/${current_version%.*}"
PORT="<%= port %>"
DATABASES=(<%= databases.map{|d| d["name"]}.join(' ')%>)
DBHOST=<%= dbhost %>
Expand Down
5 changes: 5 additions & 0 deletions jobs/postgres/spec
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ templates:
hooks/postgres-pre-stop.sh.erb: bin/hooks/postgres-pre-stop.sh
hooks/postgres-post-start.sh.erb: bin/hooks/postgres-post-start.sh
hooks/postgres-post-stop.sh.erb: bin/hooks/postgres-post-stop.sh
used_postgresql_versions.yml: config/used_postgresql_versions.yml

packages:
- postgres-common
- postgres-11
- postgres-13
- postgres-15
- postgres-16
- yq-4

provides:
- name: postgres
Expand All @@ -42,6 +44,9 @@ provides:
- databases.tls.ca

properties:
databases.version:
description: "The database version e.g. 11, 13, 15 or 16"
default: "16"
ZPascal marked this conversation as resolved.
Show resolved Hide resolved
databases.port:
description: "The database port"
default: 5432
Expand Down
6 changes: 3 additions & 3 deletions jobs/postgres/templates/pgconfig.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ set -x # if you want tracing disabled, set 'databases.enable_traces: false' in t
ENABLE_TRACE=0
# set -x # uncomment it if you want to enable tracing in all control scripts
<% end %>
current_version="16.3"
pgversion_current="postgres-${current_version}"

JOB_DIR=/var/vcap/jobs/postgres
current_version="<%=p('databases.version')%>"
pgversion_current=postgres-$(MAJOR_VERSION=<%=p('databases.version')%> ${JOB_DIR}/packages/yq-4/bin/yq '.postgresql.major_version[env(MAJOR_VERSION)].minor_version' /var/vcap/jobs/postgres/config/used_postgresql_versions.yml)

PACKAGE_DIR="/var/vcap/packages/${pgversion_current%.*}"
STORE_DIR=/var/vcap/store
PG_STORE_DIR=${STORE_DIR}/postgres
Expand Down
11 changes: 11 additions & 0 deletions jobs/postgres/templates/used_postgresql_versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
postgresql:
default: 16
major_version:
16:
minor_version: "16.3"
15:
minor_version: "15.6"
13:
minor_version: "13.14"
11:
minor_version: "11.22"
17 changes: 17 additions & 0 deletions packages/yq-4/packaging
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -exu
ZPascal marked this conversation as resolved.
Show resolved Hide resolved

function main() {
install

}

function install() {

echo "Install yq..."
mkdir -p "${BOSH_INSTALL_TARGET}/bin"
cp -a yq/yq-4.* "${BOSH_INSTALL_TARGET}/bin/yq"
chmod +x "${BOSH_INSTALL_TARGET}/bin/yq"

}

main
4 changes: 4 additions & 0 deletions packages/yq-4/spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
name: yq-4
files:
- yq/yq-4.*