Skip to content

Commit a315b1e

Browse files
DimCitusJelteF
andauthored
Add support for more environment variables. (#846)
* Add support for more environment variables. The parameters --replication-quorum, --candidate-priority, and --name can now also be passed from the environment variables PG_AUTOCTL_REPLICATION_QUORUM, PG_AUTOCTL_CANDIDATE_PRIORITY, and PG_AUTOCTL_NODE_NAME. This allows easier Dockerfile "control-at-a-distance" integration style, where all the containers are the same with the same command, but the environment is where you can inject container/node/pod specific options. Co-authored-by: Jelte Fennema <github-tech@jeltef.nl>
1 parent 0364134 commit a315b1e

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

src/bin/pg_autoctl/cli_common.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,60 @@ cli_common_keeper_getopts(int argc, char **argv,
112112
/* default to a "primary" in citus node_role terms */
113113
LocalOptionConfig.citusRole = CITUS_ROLE_PRIMARY;
114114

115+
/* add support for environment variable for some of the options */
116+
if (env_exists(PG_AUTOCTL_NODE_NAME))
117+
{
118+
if (!get_env_copy(PG_AUTOCTL_NODE_NAME,
119+
LocalOptionConfig.name,
120+
sizeof(LocalOptionConfig.name)))
121+
{
122+
/* errors have already been logged */
123+
exit(EXIT_CODE_BAD_ARGS);
124+
}
125+
}
126+
127+
if (env_exists(PG_AUTOCTL_CANDIDATE_PRIORITY))
128+
{
129+
char prio[BUFSIZE] = { 0 };
130+
131+
if (!get_env_copy(PG_AUTOCTL_CANDIDATE_PRIORITY, prio, sizeof(prio)))
132+
{
133+
/* errors have already been logged */
134+
exit(EXIT_CODE_BAD_ARGS);
135+
}
136+
137+
int candidatePriority = strtol(prio, NULL, 10);
138+
if (errno == EINVAL || candidatePriority < 0 || candidatePriority > 100)
139+
{
140+
log_fatal("PG_AUTOCTL_CANDIDATE_PRIORITY environment variable is not valid."
141+
" Valid values are integers from 0 to 100. ");
142+
exit(EXIT_CODE_BAD_ARGS);
143+
}
144+
145+
LocalOptionConfig.pgSetup.settings.candidatePriority = candidatePriority;
146+
}
147+
148+
if (env_exists(PG_AUTOCTL_REPLICATION_QUORUM))
149+
{
150+
char quorum[BUFSIZE] = { 0 };
151+
152+
if (!get_env_copy(PG_AUTOCTL_REPLICATION_QUORUM, quorum, sizeof(quorum)))
153+
{
154+
/* errors have already been logged */
155+
exit(EXIT_CODE_BAD_ARGS);
156+
}
157+
158+
bool replicationQuorum = false;
159+
if (!parse_bool(quorum, &replicationQuorum))
160+
{
161+
log_fatal("PG_AUTOCTL_REPLICATION_QUORUM environment variable is not valid."
162+
" Valid values are \"true\" or \"false\".");
163+
exit(EXIT_CODE_BAD_ARGS);
164+
}
165+
166+
LocalOptionConfig.pgSetup.settings.replicationQuorum = replicationQuorum;
167+
}
168+
115169
optind = 0;
116170

117171
while ((c = getopt_long(argc, argv,

src/bin/pg_autoctl/defaults.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
/* environment variable for --monitor, when used instead of --pgdata */
3030
#define PG_AUTOCTL_MONITOR "PG_AUTOCTL_MONITOR"
3131

32+
/* environment variable for --candidate-priority and --replication-quorum */
33+
#define PG_AUTOCTL_NODE_NAME "PG_AUTOCTL_NODE_NAME"
34+
#define PG_AUTOCTL_CANDIDATE_PRIORITY "PG_AUTOCTL_CANDIDATE_PRIORITY"
35+
#define PG_AUTOCTL_REPLICATION_QUORUM "PG_AUTOCTL_REPLICATION_QUORUM"
36+
3237
/* default values for the pg_autoctl settings */
3338
#define POSTGRES_PORT 5432
3439
#define POSTGRES_DEFAULT_LISTEN_ADDRESSES "*"

tests/test_auth.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ def test_003_init_secondary():
6868
node1.get_synchronous_standby_names_local(),
6969
"ANY 1 (pgautofailover_standby_2)",
7070
)
71+
72+
# Prevent regression from bug fixed in PR #839
7173
logs = node2.logs("STDERR")
72-
match = re.search(
73-
"Failed to connect to .*, retrying until the server is ready", logs
74-
)
74+
match = re.search("Failed to CHECKPOINT before restart", logs)
7575
if match:
76-
print("Found connection failure in logs: ", match[0])
76+
print("Found CHECKPOINT in logs: ", match[0])
7777
assert not match
7878
node2.run()
7979

@@ -95,8 +95,9 @@ def test_005_logging_of_passwords():
9595
logs = node2.logs()
9696
assert monitor_password not in logs
9797
assert "password=****" in logs
98-
# We are still logging passwords when the pguri is incomplete and when printing settings,
99-
# so assert that it's not there in other cases:
98+
99+
# We are still logging passwords when the pguri is incomplete and when
100+
# printing settings, so assert that it's not there in other cases:
100101
assert not re.search(
101102
"^(?!primary_conninfo|Failed to find).*%s.*$" % replication_password,
102103
logs,

0 commit comments

Comments
 (0)