Skip to content

Commit 6fb5ff5

Browse files
Jalexchencci-jsmith
Jalexchen
andauthored
feat: implement pg_cron capabilities (CircleCI-Public#83)
- adds code to install pg_cron to template - this extension is becoming increasingly important at circle ci and is not available with the create extension command by default. Co-authored-by: cci-jsmith <[email protected]>
1 parent 7f80be9 commit 6fb5ff5

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ workflows:
4444
IMAGES=$(docker images --format='{{.Repository}}:{{.Tag}}' | grep "ccitest/postgres")
4545
for IMAGE in $IMAGES; do
4646
printf "Booting $IMAGE...\n"
47-
CONTAINER_ID=$(docker run --rm --env POSTGRES_USER=user --env POSTGRES_PASSWORD=passw0rd -p 5432:5432 -d $IMAGE postgres -c 'config_file=/etc/postgresql/postgresql.conf')
47+
CONTAINER_ID=$(docker run --rm --env POSTGRES_USER=user --env POSTGRES_PASSWORD=passw0rd -p 5432:5432 -d $IMAGE postgres -c 'config_file=/var/lib/postgresql/data/postgresql.conf')
4848
for i in {1..20}; do
4949
printf "[$i/20] Checking Postgres is up...\n"
5050
pg_isready -h 127.0.0.1

12.13/Dockerfile

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
# specific reason to use a different one. This means January, April, July, or
77
# October.
88

9-
FROM cimg/base:2023.01
9+
FROM cimg/base:2023.04
1010

1111
LABEL maintainer="Community & Partner Engineering Team <[email protected]>"
1212

1313
ENV PG_VER=12.13
1414
ENV PG_MAJOR=12
1515
ENV POSTGRES_HOST_AUTH_METHOD=trust
16+
ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH
17+
ENV PGDATA /var/lib/postgresql/data
18+
ENV POSTGRES_DB=circle_test
1619

1720
USER root
1821
RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -36,6 +39,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
3639
llvm \
3740
llvm-dev \
3841
locales \
42+
postgresql-server-dev-all \
3943
python3-dev \
4044
tcl-dev \
4145
uuid-dev \
@@ -73,25 +77,24 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
7377
make world && \
7478
make install-world
7579

76-
RUN mkdir /docker-entrypoint-initdb.d
80+
# clones pg_cron extension for local use, this is not available wihout installing
81+
# run this as a separate layer so it caches better
82+
RUN git clone https://github.com/citusdata/pg_cron.git /pg_cron && \
83+
cd /pg_cron && make && PATH=$PATH make install
7784

78-
ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH
79-
ENV PGDATA /var/lib/postgresql/data
80-
81-
RUN useradd postgres -m -s /bin/bash && \
82-
groupadd --force postgres && \
83-
adduser postgres postgres
84-
85-
ENV POSTGRES_DB=circle_test
85+
RUN mkdir /docker-entrypoint-initdb.d
8686

87-
COPY postgresql.conf /etc/postgresql/postgresql.conf
87+
COPY pg_cron.sh /docker-entrypoint-initdb.d/
88+
COPY custom-postgresql.conf /etc/postgresql/custom-postgresql.conf
8889
COPY docker-entrypoint.sh /usr/local/bin/
90+
8991
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
9092

91-
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
93+
RUN chmod +x /usr/local/bin/docker-entrypoint.sh /docker-entrypoint-initdb.d/pg_cron.sh
9294
RUN mkdir -p /var/lib/postgresql && \
9395
chown -R postgres:postgres /var/lib/postgresql && \
94-
chown -R postgres:postgres /usr/local/bin/docker-entrypoint.sh
96+
chown -R postgres:postgres /usr/local/bin/docker-entrypoint.sh && \
97+
rm -rf /pg_cron
9598

9699
ENTRYPOINT ["docker-entrypoint.sh"]
97100
STOPSIGNAL SIGINT

Dockerfile.template

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ LABEL maintainer="Community & Partner Engineering Team <community-partner@circle
1313
ENV PG_VER=%%VERSION_FULL%%
1414
ENV PG_MAJOR=%%VERSION_MAJOR%%
1515
ENV POSTGRES_HOST_AUTH_METHOD=trust
16+
ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH
17+
ENV PGDATA /var/lib/postgresql/data
18+
ENV POSTGRES_DB=circle_test
1619

1720
USER root
1821
RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -36,6 +39,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
3639
llvm \
3740
llvm-dev \
3841
locales \
42+
postgresql-server-dev-all \
3943
python3-dev \
4044
tcl-dev \
4145
uuid-dev \
@@ -73,25 +77,24 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
7377
make world && \
7478
make install-world
7579

76-
RUN mkdir /docker-entrypoint-initdb.d
80+
# clones pg_cron extension for local use, this is not available wihout installing
81+
# run this as a separate layer so it caches better
82+
RUN git clone https://github.com/citusdata/pg_cron.git /pg_cron && \
83+
cd /pg_cron && make && PATH=$PATH make install
7784

78-
ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH
79-
ENV PGDATA /var/lib/postgresql/data
80-
81-
RUN useradd postgres -m -s /bin/bash && \
82-
groupadd --force postgres && \
83-
adduser postgres postgres
84-
85-
ENV POSTGRES_DB=circle_test
85+
RUN mkdir /docker-entrypoint-initdb.d
8686

87-
COPY postgresql.conf /etc/postgresql/postgresql.conf
87+
COPY pg_cron.sh /docker-entrypoint-initdb.d/
88+
COPY custom-postgresql.conf /etc/postgresql/custom-postgresql.conf
8889
COPY docker-entrypoint.sh /usr/local/bin/
90+
8991
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
9092

91-
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
93+
RUN chmod +x /usr/local/bin/docker-entrypoint.sh /docker-entrypoint-initdb.d/pg_cron.sh
9294
RUN mkdir -p /var/lib/postgresql && \
9395
chown -R postgres:postgres /var/lib/postgresql && \
94-
chown -R postgres:postgres /usr/local/bin/docker-entrypoint.sh
96+
chown -R postgres:postgres /usr/local/bin/docker-entrypoint.sh && \
97+
rm -rf /pg_cron
9598

9699
ENTRYPOINT ["docker-entrypoint.sh"]
97100
STOPSIGNAL SIGINT

postgresql.conf renamed to custom-postgresql.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ max_prepared_transactions = 200
44
max_locks_per_transaction = 512
55
max_pred_locks_per_transaction = 256
66
listen_addresses = '*'
7+
shared_preload_libraries = 'pg_cron'
8+
cron.database_name = 'circle_test'

pg_cron.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
# use same db as the one from env
3+
dbname="$POSTGRES_DB"
4+
5+
# create custom config
6+
customconf=/etc/postgresql/custom-postgresql.conf
7+
echo "shared_preload_libraries = 'pg_cron'" >> $customconf
8+
echo "cron.database_name = '$dbname'" >> $customconf
9+
chown postgres $customconf
10+
chgrp postgres $customconf
11+
12+
cat /etc/postgresql/custom-postgresql.conf
13+
14+
# include custom config from main config
15+
conf=/var/lib/postgresql/data/postgresql.conf
16+
found=$(grep "include = '$customconf'" $conf)
17+
if [ -z "$found" ]; then
18+
echo "include = '$customconf'" >> $conf
19+
fi
20+
21+
cat /var/lib/postgresql/data/postgresql.conf | grep shared
22+
cat /var/lib/postgresql/data/postgresql.conf | grep include

0 commit comments

Comments
 (0)