From 8320179f3491bc4ebd5360b3e5c325ecd8755bfe Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Wed, 25 Apr 2018 11:49:22 +0900 Subject: [PATCH] Add configuration file parameter "config_directory" This enables explicit provision of an external configuration file directory, which if set will be passed to "pg_ctl" as the -D parameter. Otherwise "pg_ctl" will default to using the data directory, which will cause some operations to fail if the configuration files are not present there. Note this is implemented primarily for feature completeness and for development/testing purposes. Users who have installed "repmgr" from a package should not rely on "pg_ctl" to stop/start/restart PostgreSQL, instead they should set the appropriate "service_..._command" for their operating system. For more details see: https://repmgr.org/docs/4.0/configuration-service-commands.html Note: in a future release, the presence of "config_directory" in repmgr.conf will be used to implictly set "--copy-external-config-files=samepath" when cloning a standby; this is a behaviour change so will be implemented in the next major realease (repmgr 4.1). Implements GitHub #424. --- configfile.c | 4 ++++ configfile.h | 3 ++- repmgr-action-node.c | 4 ++-- repmgr-action-standby.c | 11 ++++++----- repmgr-client-global.h | 1 + repmgr-client.c | 36 ++++++++++++++++++++++++++++++++++++ repmgr.conf.sample | 14 ++++++++++++-- 7 files changed, 63 insertions(+), 10 deletions(-) diff --git a/configfile.c b/configfile.c index 2ea24aa0..bf8c39fd 100644 --- a/configfile.c +++ b/configfile.c @@ -288,6 +288,7 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList * memset(options->node_name, 0, sizeof(options->node_name)); memset(options->conninfo, 0, sizeof(options->conninfo)); memset(options->data_directory, 0, sizeof(options->data_directory)); + memset(options->config_directory, 0, sizeof(options->data_directory)); memset(options->pg_bindir, 0, sizeof(options->pg_bindir)); options->replication_type = REPLICATION_TYPE_PHYSICAL; @@ -464,6 +465,9 @@ _parse_config(t_configuration_options *options, ItemList *error_list, ItemList * strncpy(options->conninfo, value, MAXLEN); else if (strcmp(name, "data_directory") == 0) strncpy(options->data_directory, value, MAXPGPATH); + else if (strcmp(name, "config_directory") == 0) + strncpy(options->config_directory, value, MAXPGPATH); + else if (strcmp(name, "replication_user") == 0) { if (strlen(value) < NAMEDATALEN) diff --git a/configfile.h b/configfile.h index 7bd9e094..d86aa907 100644 --- a/configfile.h +++ b/configfile.h @@ -73,6 +73,7 @@ typedef struct char conninfo[MAXLEN]; char replication_user[NAMEDATALEN]; char data_directory[MAXPGPATH]; + char config_directory[MAXPGPATH]; char pg_bindir[MAXPGPATH]; int replication_type; @@ -159,7 +160,7 @@ typedef struct #define T_CONFIGURATION_OPTIONS_INITIALIZER { \ /* node information */ \ - UNKNOWN_NODE_ID, "", "", "", "", "", REPLICATION_TYPE_PHYSICAL, \ + UNKNOWN_NODE_ID, "", "", "", "", "", "", REPLICATION_TYPE_PHYSICAL, \ /* log settings */ \ "", "", "", DEFAULT_LOG_STATUS_INTERVAL, \ /* standby clone settings */ \ diff --git a/repmgr-action-node.c b/repmgr-action-node.c index d9fd1e92..22c36f49 100644 --- a/repmgr-action-node.c +++ b/repmgr-action-node.c @@ -1518,7 +1518,7 @@ do_node_service(void) if (data_dir_required_for_action(action)) { - get_node_data_directory(data_dir); + get_node_config_directory(data_dir); if (data_dir[0] == '\0') { @@ -1606,7 +1606,7 @@ _do_node_service_list_actions(t_server_action action) if (data_dir_required == true) { - get_node_data_directory(data_dir); + get_node_config_directory(data_dir); } /* show command for specific action only */ diff --git a/repmgr-action-standby.c b/repmgr-action-standby.c index a1bbc1c9..702598b7 100644 --- a/repmgr-action-standby.c +++ b/repmgr-action-standby.c @@ -73,7 +73,7 @@ static char local_repmgr_tmp_directory[MAXPGPATH]; static char datadir_list_filename[MAXLEN]; static char barman_command_buf[MAXLEN] = ""; -static void _do_standby_promote_internal(PGconn *conn, const char *data_dir); +static void _do_standby_promote_internal(PGconn *conn); static void _do_create_recovery_conf(void); static void check_barman_config(void); @@ -1957,13 +1957,12 @@ do_standby_promote(void) PQfinish(current_primary_conn); - - _do_standby_promote_internal(conn, config_file_options.data_directory); + _do_standby_promote_internal(conn); } static void -_do_standby_promote_internal(PGconn *conn, const char *data_dir) +_do_standby_promote_internal(PGconn *conn) { char script[MAXLEN]; int r, @@ -1975,7 +1974,9 @@ _do_standby_promote_internal(PGconn *conn, const char *data_dir) t_node_info local_node_record = T_NODE_INFO_INITIALIZER; RecordStatus record_status = RECORD_NOT_FOUND; + char data_dir[MAXPGPATH]; + get_node_config_directory(data_dir); /* fetch local node record so we can add detail in log messages */ record_status = get_node_record(conn, @@ -3636,7 +3637,7 @@ do_standby_switchover(void) } /* promote standby (local node) */ - _do_standby_promote_internal(local_conn, config_file_options.data_directory); + _do_standby_promote_internal(local_conn); /* diff --git a/repmgr-client-global.h b/repmgr-client-global.h index cf07c931..d0e78a08 100644 --- a/repmgr-client-global.h +++ b/repmgr-client-global.h @@ -231,6 +231,7 @@ extern void print_help_header(void); /* server control functions */ extern void get_server_action(t_server_action action, char *script, char *data_dir); extern bool data_dir_required_for_action(t_server_action action); +extern void get_node_config_directory(char *config_dir_buf); extern void get_node_data_directory(char *data_dir_buf); extern void init_node_record(t_node_info *node_record); extern bool can_use_pg_rewind(PGconn *conn, const char *data_directory, PQExpBufferData *reason); diff --git a/repmgr-client.c b/repmgr-client.c index 9b455029..a3c56322 100644 --- a/repmgr-client.c +++ b/repmgr-client.c @@ -1328,6 +1328,15 @@ check_cli_parameters(const int action) _("--no-upstream-connection only effective in Barman mode")); } } + + if (strlen(config_file_options.config_directory)) + { + if (runtime_options.copy_external_config_files == false) + { + item_list_append(&cli_warnings, + _("\"config_directory\" set in repmgr.conf, but --copy-external-config-files not provided")); + } + } } break; @@ -2735,6 +2744,33 @@ data_dir_required_for_action(t_server_action action) } +/* + * Copy the location of the configuration file directory into the + * provided buffer; if "config_directory" provided, use that, otherwise + * default to the data directory. + * + * This is primarily intended for use with "pg_ctl" (which itself shouldn't + * be used outside of development environments). + */ +void +get_node_config_directory(char *config_dir_buf) +{ + if (config_file_options.config_directory[0] != '\0') + { + strncpy(config_dir_buf, config_file_options.config_directory, MAXPGPATH); + return; + } + + if (config_file_options.data_directory[0] != '\0') + { + strncpy(config_dir_buf, config_file_options.data_directory, MAXPGPATH); + return; + } + + return; +} + + void get_node_data_directory(char *data_dir_buf) { diff --git a/repmgr.conf.sample b/repmgr.conf.sample index ac9b137c..3e4237aa 100644 --- a/repmgr.conf.sample +++ b/repmgr.conf.sample @@ -40,18 +40,28 @@ # is not running and there's no other way of determining # the data directory. -#replication_user='repmgr' # User to make replication connections with, if not set defaults - # to the user defined in "conninfo". # ============================================================================= # Optional configuration items # ============================================================================= + +#------------------------------------------------------------------------------ +# Server settings +#------------------------------------------------------------------------------ + +#config_directory='' # If configuration files are located outside the data + # directory, specify the directory where the main + # postgresql.conf file is located. + #------------------------------------------------------------------------------ # Replication settings #------------------------------------------------------------------------------ +#replication_user='repmgr' # User to make replication connections with, if not set defaults + # to the user defined in "conninfo". + #replication_type=physical # Must be one of 'physical' or 'bdr'. #location=default # arbitrary string defining the location of the node; this