Skip to content

Commit

Permalink
Improve our docker compose file a little. (#724)
Browse files Browse the repository at this point in the history
* Improve our docker compose file a little.

Use environment variables rather than very long command lines where it makes
sense, making it easier to see that we're doing the same thing in the three
Postgres nodes.

* Add support for env variables for pg_autoctl do demo run.
  • Loading branch information
DimCitus committed Aug 16, 2022
1 parent 7d351de commit 8754aab
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 42 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ build-check:
build-i386:
docker build -t i386:latest -f Dockerfile.i386 .

build-demo:
docker build -t citusdata/pg_auto_failover:demo .

# expected to be run from within the i386 docker container
installcheck-i386:
pg_autoctl run &
Expand Down
59 changes: 20 additions & 39 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,41 @@ services:
image: citusdata/pg_auto_failover:demo
environment:
PGDATA: /tmp/pgaf
PG_AUTOCTL_DEBUG: 1
command: [
"pg_autoctl", "create", "postgres",
"--ssl-self-signed",
"--auth", "trust",
"--pg-hba-lan",
"--username", "ad",
"--dbname", "analytics",
"--monitor", "postgresql://autoctl_node@monitor/pg_auto_failover",
"--run"]
PGUSER: ad
PGDATABASE: analytics
PG_AUTOCTL_MONITOR: "postgresql://autoctl_node@monitor/pg_auto_failover"
command: pg_autoctl create postgres --ssl-self-signed --auth trust --pg-hba-lan --run
expose:
- 5432
node2:
image: citusdata/pg_auto_failover:demo
environment:
PGDATA: /tmp/pgaf
PG_AUTOCTL_DEBUG: 1
command: [
"pg_autoctl", "create", "postgres",
"--ssl-self-signed",
"--auth", "trust",
"--pg-hba-lan",
"--username", "ad",
"--dbname", "analytics",
"--monitor", "postgresql://autoctl_node@monitor/pg_auto_failover",
"--run"]
PGUSER: ad
PGDATABASE: analytics
PG_AUTOCTL_MONITOR: "postgresql://autoctl_node@monitor/pg_auto_failover"
command: pg_autoctl create postgres --ssl-self-signed --auth trust --pg-hba-lan --run
expose:
- 5432
node3:
image: citusdata/pg_auto_failover:demo
environment:
PGDATA: /tmp/pgaf
PG_AUTOCTL_DEBUG: 1
command: [
"pg_autoctl", "create", "postgres",
"--ssl-self-signed",
"--auth", "trust",
"--pg-hba-lan",
"--username", "ad",
"--dbname", "analytics",
"--monitor", "postgresql://autoctl_node@monitor/pg_auto_failover",
"--run"]
PGUSER: ad
PGDATABASE: analytics
PG_AUTOCTL_MONITOR: "postgresql://autoctl_node@monitor/pg_auto_failover"
command: pg_autoctl create postgres --ssl-self-signed --auth trust --pg-hba-lan --run
expose:
- 5432
demo-app:
image: citusdata/pg_auto_failover:demo
environment:
PGDATA: /tmp/pgaf
PG_AUTOCTL_DEBUG: 1
command: [
"pg_autoctl", "do", "demo", "run",
"--username", "ad",
"--clients", "10",
"--duration", "125",
"--first-failover", "45",
"--failover-freq", "30",
"--monitor", "postgresql://autoctl_node@monitor/pg_auto_failover"]
PGUSER: ad
PGDATABASE: analytics
PG_AUTOCTL_MONITOR: "postgresql://autoctl_node@monitor/pg_auto_failover"
PG_AUTOCTL_DEMO_CLIENTS: 10
PG_AUTOCTL_DEMO_DURATION: 125
PG_AUTOCTL_DEMO_FAILOVER_FREQ: 30
PG_AUTOCTL_DEMO_FAILOVER_FIRST: 45
command: pg_autoctl do demo run
79 changes: 79 additions & 0 deletions src/bin/pg_autoctl/cli_do_demoapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,85 @@ cli_do_demoapp_getopts(int argc, char **argv)
options.doFailover = true;
strlcpy(options.formation, "default", sizeof(options.formation));

/*
* Add support for environment variables for some of those options.
*/
if (env_exists(PG_AUTOCTL_DEMO_CLIENTS))
{
char clients[BUFSIZE] = { 0 };

if (!get_env_copy(PG_AUTOCTL_DEMO_CLIENTS, clients, sizeof(clients)))
{
/* errors have already been logged */
exit(EXIT_CODE_BAD_ARGS);
}

if (!stringToInt(clients, &(options.clientsCount)))
{
log_fatal("Failed to parse \"%s\" integer value \"%s\"",
PG_AUTOCTL_DEMO_CLIENTS,
clients);
exit(EXIT_CODE_BAD_ARGS);
}
}

if (env_exists(PG_AUTOCTL_DEMO_DURATION))
{
char duration[BUFSIZE] = { 0 };

if (!get_env_copy(PG_AUTOCTL_DEMO_DURATION, duration, sizeof(duration)))
{
/* errors have already been logged */
exit(EXIT_CODE_BAD_ARGS);
}

if (!stringToInt(duration, &(options.duration)))
{
log_fatal("Failed to parse \"%s\" integer value \"%s\"",
PG_AUTOCTL_DEMO_DURATION,
duration);
exit(EXIT_CODE_BAD_ARGS);
}
}

if (env_exists(PG_AUTOCTL_DEMO_FAILOVER_FIRST))
{
char first[BUFSIZE] = { 0 };

if (!get_env_copy(PG_AUTOCTL_DEMO_FAILOVER_FIRST, first, sizeof(first)))
{
/* errors have already been logged */
exit(EXIT_CODE_BAD_ARGS);
}

if (!stringToInt(first, &(options.firstFailover)))
{
log_fatal("Failed to parse \"%s\" integer value \"%s\"",
PG_AUTOCTL_DEMO_FAILOVER_FIRST,
first);
exit(EXIT_CODE_BAD_ARGS);
}
}

if (env_exists(PG_AUTOCTL_DEMO_FAILOVER_FREQ))
{
char freq[BUFSIZE] = { 0 };

if (!get_env_copy(PG_AUTOCTL_DEMO_FAILOVER_FREQ, freq, sizeof(freq)))
{
/* errors have already been logged */
exit(EXIT_CODE_BAD_ARGS);
}

if (!stringToInt(freq, &(options.failoverFreq)))
{
log_fatal("Failed to parse \"%s\" integer value \"%s\"",
PG_AUTOCTL_DEMO_FAILOVER_FREQ,
freq);
exit(EXIT_CODE_BAD_ARGS);
}
}

/*
* The only command lines that are using cli_do_demoapp_getopts are
* terminal ones: they don't accept subcommands. In that case our option
Expand Down
6 changes: 6 additions & 0 deletions src/bin/pg_autoctl/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
#define PG_AUTOCTL_CANDIDATE_PRIORITY "PG_AUTOCTL_CANDIDATE_PRIORITY"
#define PG_AUTOCTL_REPLICATION_QUORUM "PG_AUTOCTL_REPLICATION_QUORUM"

/* environment variables for `pg_autoctl do demo run` */
#define PG_AUTOCTL_DEMO_CLIENTS "PG_AUTOCTL_DEMO_CLIENTS"
#define PG_AUTOCTL_DEMO_DURATION "PG_AUTOCTL_DEMO_DURATION"
#define PG_AUTOCTL_DEMO_FAILOVER_FREQ "PG_AUTOCTL_DEMO_FAILOVER_FREQ"
#define PG_AUTOCTL_DEMO_FAILOVER_FIRST "PG_AUTOCTL_DEMO_FAILOVER_FIRST"

/* default values for the pg_autoctl settings */
#define POSTGRES_PORT 5432
#define POSTGRES_DEFAULT_LISTEN_ADDRESSES "*"
Expand Down
4 changes: 2 additions & 2 deletions src/bin/pg_autoctl/keeper_pg_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,12 +1016,12 @@ create_database_and_extension(Keeper *keeper)
*/
if (!IS_EMPTY_STRING_BUFFER(pgSetup->username))
{
char pguser[NAMEDATALEN] = { 0 };

/*
* Remove PGUSER from the environment when we want to create that very
* user at bootstrap.
*/
char pguser[NAMEDATALEN] = { 0 };

if (!get_env_copy_with_fallback("PGUSER", pguser, NAMEDATALEN, ""))
{
/* errors have already been logged */
Expand Down
2 changes: 1 addition & 1 deletion src/bin/pg_autoctl/pgsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pg_setup_init(PostgresSetup *pgSetup,
}

/* check or find dbname */
if (options->dbname[0] != '\0')
if (!IS_EMPTY_STRING_BUFFER(options->dbname))
{
strlcpy(pgSetup->dbname, options->dbname, NAMEDATALEN);
}
Expand Down

0 comments on commit 8754aab

Please sign in to comment.