Skip to content

Commit ac12fcd

Browse files
authored
Fix/debian cluster for the monitor (#681)
* Fix pg_autoctl do pgsetup ... with reading the configuration file. That fixes a bug where we expect port 5432 (default) to be found in the Postgres pid file even though the pg_autoctl node has been created with a specific port number (such as --pgport 6000). * Discover and fix debian clusters for the monitor too. In passing, review the test suite for the debian integration and make it work smoothly when it was almost broken before.
1 parent 81be864 commit ac12fcd

7 files changed

Lines changed: 123 additions & 38 deletions

File tree

src/bin/pg_autoctl/cli_do_misc.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,19 @@ keeper_cli_pgsetup_pg_ctl(int argc, char **argv)
234234
void
235235
keeper_cli_pgsetup_discover(int argc, char **argv)
236236
{
237-
bool missingPgdataOk = true;
238-
PostgresSetup pgSetup = { 0 };
237+
ConfigFilePaths pathnames = { 0 };
238+
LocalPostgresServer postgres = { 0 };
239+
PostgresSetup *pgSetup = &(postgres.postgresSetup);
239240

240-
if (!pg_setup_init(&pgSetup, &keeperOptions.pgSetup, true, true))
241+
if (!cli_common_pgsetup_init(&pathnames, pgSetup))
241242
{
242-
exit(EXIT_CODE_PGCTL);
243+
/* errors have already been logged */
244+
exit(EXIT_CODE_BAD_CONFIG);
243245
}
244246

245-
if (!pg_controldata(&pgSetup, missingPgdataOk))
247+
bool missingPgdataOk = true;
248+
249+
if (!pg_controldata(pgSetup, missingPgdataOk))
246250
{
247251
exit(EXIT_CODE_PGCTL);
248252
}
@@ -252,7 +256,7 @@ keeper_cli_pgsetup_discover(int argc, char **argv)
252256
fformat(stdout, "Node Name: %s\n", keeperOptions.hostname);
253257
}
254258

255-
fprintf_pg_setup(stdout, &pgSetup);
259+
fprintf_pg_setup(stdout, pgSetup);
256260
}
257261

258262

@@ -263,19 +267,22 @@ keeper_cli_pgsetup_discover(int argc, char **argv)
263267
void
264268
keeper_cli_pgsetup_is_ready(int argc, char **argv)
265269
{
266-
PostgresSetup pgSetup = { 0 };
267-
bool pgIsNotRunningIsOk = false;
270+
ConfigFilePaths pathnames = { 0 };
271+
LocalPostgresServer postgres = { 0 };
272+
PostgresSetup *pgSetup = &(postgres.postgresSetup);
268273

269-
if (!pg_setup_init(&pgSetup, &keeperOptions.pgSetup, true, true))
274+
if (!cli_common_pgsetup_init(&pathnames, pgSetup))
270275
{
271-
exit(EXIT_CODE_PGCTL);
276+
/* errors have already been logged */
277+
exit(EXIT_CODE_BAD_CONFIG);
272278
}
273279

274280
log_debug("Initialized pgSetup, now calling pg_setup_is_ready()");
275281

276-
bool pgIsReady = pg_setup_is_ready(&pgSetup, pgIsNotRunningIsOk);
282+
bool pgIsNotRunningIsOk = false;
283+
bool pgIsReady = pg_setup_is_ready(pgSetup, pgIsNotRunningIsOk);
277284

278-
log_info("Postgres status is: \"%s\"", pmStatusToString(pgSetup.pm_status));
285+
log_info("Postgres status is: \"%s\"", pmStatusToString(pgSetup->pm_status));
279286

280287
if (pgIsReady)
281288
{
@@ -293,18 +300,22 @@ void
293300
keeper_cli_pgsetup_wait_until_ready(int argc, char **argv)
294301
{
295302
int timeout = 30;
296-
PostgresSetup pgSetup = { 0 };
297303

298-
if (!pg_setup_init(&pgSetup, &keeperOptions.pgSetup, true, true))
304+
ConfigFilePaths pathnames = { 0 };
305+
LocalPostgresServer postgres = { 0 };
306+
PostgresSetup *pgSetup = &(postgres.postgresSetup);
307+
308+
if (!cli_common_pgsetup_init(&pathnames, pgSetup))
299309
{
300-
exit(EXIT_CODE_PGCTL);
310+
/* errors have already been logged */
311+
exit(EXIT_CODE_BAD_CONFIG);
301312
}
302313

303314
log_debug("Initialized pgSetup, now calling pg_setup_wait_until_is_ready()");
304315

305-
bool pgIsReady = pg_setup_wait_until_is_ready(&pgSetup, timeout, LOG_INFO);
316+
bool pgIsReady = pg_setup_wait_until_is_ready(pgSetup, timeout, LOG_INFO);
306317

307-
log_info("Postgres status is: \"%s\"", pmStatusToString(pgSetup.pm_status));
318+
log_info("Postgres status is: \"%s\"", pmStatusToString(pgSetup->pm_status));
308319

309320
if (pgIsReady)
310321
{
@@ -320,16 +331,19 @@ keeper_cli_pgsetup_wait_until_ready(int argc, char **argv)
320331
void
321332
keeper_cli_pgsetup_startup_logs(int argc, char **argv)
322333
{
323-
PostgresSetup pgSetup = { 0 };
334+
ConfigFilePaths pathnames = { 0 };
335+
LocalPostgresServer postgres = { 0 };
336+
PostgresSetup *pgSetup = &(postgres.postgresSetup);
324337

325-
if (!pg_setup_init(&pgSetup, &keeperOptions.pgSetup, true, true))
338+
if (!cli_common_pgsetup_init(&pathnames, pgSetup))
326339
{
327-
exit(EXIT_CODE_PGCTL);
340+
/* errors have already been logged */
341+
exit(EXIT_CODE_BAD_CONFIG);
328342
}
329343

330344
log_debug("Initialized pgSetup, now calling pg_log_startup()");
331345

332-
if (!pg_log_startup(pgSetup.pgdata, LOG_INFO))
346+
if (!pg_log_startup(pgSetup->pgdata, LOG_INFO))
333347
{
334348
exit(EXIT_CODE_PGCTL);
335349
}

src/bin/pg_autoctl/debian.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ static bool disableAutoStart(PostgresConfigFiles *pgConfigFiles);
6262
* them from default location and modifies paths inside copied postgresql.conf.
6363
*/
6464
bool
65-
keeper_ensure_pg_configuration_files_in_pgdata(KeeperConfig *config)
65+
keeper_ensure_pg_configuration_files_in_pgdata(PostgresSetup *pgSetup)
6666
{
67-
PostgresSetup *pgSetup = &(config->pgSetup);
6867
PostgresConfigFiles pgConfigFiles = { 0 };
6968

7069
if (!debian_find_postgres_configuration_files(pgSetup, &pgConfigFiles))

src/bin/pg_autoctl/debian.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ typedef struct debian_pathnames
6262
} DebianPathnames;
6363

6464

65-
bool keeper_ensure_pg_configuration_files_in_pgdata(KeeperConfig *config);
65+
bool keeper_ensure_pg_configuration_files_in_pgdata(PostgresSetup *pgSetup);
6666

6767

6868
#endif /* DEBIAN_H */

src/bin/pg_autoctl/keeper_pg_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ keeper_pg_init_and_register(Keeper *keeper)
110110

111111
if (postgresInstanceExists)
112112
{
113-
if (!keeper_ensure_pg_configuration_files_in_pgdata(config))
113+
if (!keeper_ensure_pg_configuration_files_in_pgdata(pgSetup))
114114
{
115115
log_fatal("Failed to setup your Postgres instance "
116116
"the PostgreSQL way, see above for details");

src/bin/pg_autoctl/monitor_pg_init.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "postgres_fe.h"
1414

1515
#include "cli_common.h"
16+
#include "debian.h"
1617
#include "defaults.h"
1718
#include "ipaddr.h"
1819
#include "log.h"
@@ -98,6 +99,14 @@ monitor_pg_init(Monitor *monitor)
9899

99100
return false;
100101
}
102+
103+
/* if we have a debian cluster, re-own the configuration files */
104+
if (!keeper_ensure_pg_configuration_files_in_pgdata(&existingPgSetup))
105+
{
106+
log_fatal("Failed to setup your Postgres instance "
107+
"the PostgreSQL way, see above for details");
108+
return false;
109+
}
101110
}
102111
else
103112
{

tests/pgautofailover_utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,17 @@ def pg_createcluster(self, datadir, port=5432):
148148
create_command = [
149149
"sudo",
150150
shutil.which("pg_createcluster"),
151-
"-U",
151+
"--user",
152152
os.getenv("USER"),
153-
PGVERSION,
154-
datadir,
153+
"--group",
154+
"postgres",
155155
"-p",
156156
str(port),
157+
PGVERSION,
158+
datadir,
159+
"--",
160+
"--auth-local",
161+
"trust",
157162
]
158163

159164
print("%s" % " ".join(create_command))
@@ -1612,14 +1617,14 @@ def __init__(
16121617
else:
16131618
self.hostname = str(self.vnode.address)
16141619

1615-
def create(self, run=False):
1620+
def create(self, level="-v", run=False):
16161621
"""
16171622
Initializes and runs the monitor process.
16181623
"""
16191624
create_args = [
16201625
"create",
16211626
self.role.command(),
1622-
"-vv",
1627+
level,
16231628
"--pgdata",
16241629
self.datadir,
16251630
"--pgport",

tests/test_debian_clusters.py

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pgautofailover_utils as pgautofailover
22
import os.path
3+
import subprocess
34

45
cluster = None
56
monitor = None
@@ -16,10 +17,41 @@ def teardown_module():
1617

1718
def test_000_create_monitor():
1819
global monitor
19-
monitor = cluster.create_monitor("/tmp/debian/monitor")
20-
monitor.run()
20+
21+
print()
22+
monitor_path = cluster.pg_createcluster("monitor", port=6000)
23+
24+
postgres_conf_path = os.path.join(monitor_path, "postgresql.conf")
25+
# verify postgresql.conf is not in data directory
26+
assert not os.path.exists(postgres_conf_path)
27+
28+
monitor = cluster.create_monitor(monitor_path, port=6000)
29+
monitor.create(level="-vv")
30+
monitor.run(port=6000)
2131
monitor.wait_until_pg_is_running()
2232

33+
# verify postgresql.conf is in data directory now
34+
assert os.path.exists(postgres_conf_path)
35+
36+
pgversion = os.getenv("PGVERSION")
37+
38+
p = subprocess.run(
39+
[
40+
"ls",
41+
"-ld",
42+
monitor_path,
43+
"/var/lib/postgresql/%s" % pgversion,
44+
"/etc/postgresql/%s" % pgversion,
45+
"/etc/postgresql/%s/monitor" % pgversion,
46+
"/etc/postgresql/%s/monitor/postgresql.conf" % pgversion,
47+
"/etc/postgresql/%s/monitor/pg_hba.conf" % pgversion,
48+
"/etc/postgresql/%s/monitor/pg_ident.conf" % pgversion,
49+
],
50+
text=True,
51+
capture_output=True,
52+
)
53+
print("%s" % p.stdout)
54+
2355

2456
def test_001_custom_single():
2557
global node1
@@ -37,13 +69,39 @@ def test_001_custom_single():
3769
# verify postgresql.conf is in data directory now
3870
assert os.path.exists(postgres_conf_path)
3971

72+
pgversion = os.getenv("PGVERSION")
73+
74+
p = subprocess.run(
75+
[
76+
"ls",
77+
"-ld",
78+
node1_path,
79+
"/var/lib/postgresql/%s" % pgversion,
80+
"/etc/postgresql/%s" % pgversion,
81+
"/etc/postgresql/%s/debian_node1" % pgversion,
82+
"/etc/postgresql/%s/debian_node1/postgresql.conf" % pgversion,
83+
"/etc/postgresql/%s/debian_node1/pg_hba.conf" % pgversion,
84+
"/etc/postgresql/%s/debian_node1/pg_ident.conf" % pgversion,
85+
],
86+
text=True,
87+
capture_output=True,
88+
)
89+
print("%s" % p.stdout)
90+
4091
monitor.print_state()
4192

42-
node1.destroy()
4393

44-
# node is stuck in init state, destroy won't remove it from monitor
45-
# force remove from monitor
46-
monitor.run_sql_query(
47-
"select pgautofailover.remove_node('%s', %s)"
48-
% (str(node1.vnode.address), str(node1.port))
94+
def test_002_chmod_debian_data_directory():
95+
# debian installs the following ownership and permissions:
96+
#
97+
# drwxr-xr-x 5 postgres postgres ... /var/lib/postgresql/11
98+
# drwx------ 20 docker postgres ... /var/lib/postgresql/11/monitor
99+
# drwx------ 20 docker postgres ... /var/lib/postgresql/11/debian_node1
100+
#
101+
# we need to give the postgres group the w on the top-level directory
102+
pgversion = os.getenv("PGVERSION")
103+
104+
p = subprocess.Popen(
105+
["chmod", "go+w", "/var/lib/postgresql/%s" % pgversion]
49106
)
107+
assert p.wait() == 0

0 commit comments

Comments
 (0)