cluster matrix: make remote execution of cluster show more configurable

- use the remote user setting, like other SSH-based remote operations
  (avoid hardcoding the user name)
- enable `repmgr cluster show` to accept the cluster name and the
  database connection information instead of requiring repmgr.conf;
  this means we don't have to assume that repmgr.conf is in one
  of the default locations
This commit is contained in:
Ian Barwick
2016-09-29 00:54:33 +09:00
parent 41ec45a4cc
commit f775750334
2 changed files with 36 additions and 9 deletions

View File

@@ -184,6 +184,7 @@ static char repmgr_slot_name[MAXLEN] = "";
static char *repmgr_slot_name_ptr = NULL; static char *repmgr_slot_name_ptr = NULL;
static char path_buf[MAXLEN] = ""; static char path_buf[MAXLEN] = "";
static char barman_command_buf[MAXLEN] = ""; static char barman_command_buf[MAXLEN] = "";
static char repmgr_cluster[MAXLEN] = "";
/* Collate command line errors and warnings here for friendlier reporting */ /* Collate command line errors and warnings here for friendlier reporting */
ItemList cli_errors = { NULL, NULL }; ItemList cli_errors = { NULL, NULL };
@@ -221,7 +222,6 @@ main(int argc, char **argv)
{"help", no_argument, NULL, OPT_HELP}, {"help", no_argument, NULL, OPT_HELP},
{"check-upstream-config", no_argument, NULL, OPT_CHECK_UPSTREAM_CONFIG}, {"check-upstream-config", no_argument, NULL, OPT_CHECK_UPSTREAM_CONFIG},
{"recovery-min-apply-delay", required_argument, NULL, OPT_RECOVERY_MIN_APPLY_DELAY}, {"recovery-min-apply-delay", required_argument, NULL, OPT_RECOVERY_MIN_APPLY_DELAY},
{"config-archive-dir", required_argument, NULL, OPT_CONFIG_ARCHIVE_DIR},
{"pg_rewind", optional_argument, NULL, OPT_PG_REWIND}, {"pg_rewind", optional_argument, NULL, OPT_PG_REWIND},
{"pwprompt", optional_argument, NULL, OPT_PWPROMPT}, {"pwprompt", optional_argument, NULL, OPT_PWPROMPT},
{"csv", no_argument, NULL, OPT_CSV}, {"csv", no_argument, NULL, OPT_CSV},
@@ -231,6 +231,9 @@ main(int argc, char **argv)
{"copy-external-config-files", optional_argument, NULL, OPT_COPY_EXTERNAL_CONFIG_FILES}, {"copy-external-config-files", optional_argument, NULL, OPT_COPY_EXTERNAL_CONFIG_FILES},
{"wait-sync", optional_argument, NULL, OPT_REGISTER_WAIT}, {"wait-sync", optional_argument, NULL, OPT_REGISTER_WAIT},
{"version", no_argument, NULL, 'V'}, {"version", no_argument, NULL, 'V'},
/* Following options for internal use */
{"cluster", required_argument, NULL, OPT_CLUSTER},
{"config-archive-dir", required_argument, NULL, OPT_CONFIG_ARCHIVE_DIR},
/* Following options deprecated */ /* Following options deprecated */
{"local-port", required_argument, NULL, 'l'}, {"local-port", required_argument, NULL, 'l'},
{"initdb-no-pwprompt", no_argument, NULL, OPT_INITDB_NO_PWPROMPT}, {"initdb-no-pwprompt", no_argument, NULL, OPT_INITDB_NO_PWPROMPT},
@@ -502,9 +505,7 @@ main(int argc, char **argv)
} }
} }
break; break;
case OPT_CONFIG_ARCHIVE_DIR:
strncpy(runtime_options.config_archive_dir, optarg, MAXLEN);
break;
case OPT_PG_REWIND: case OPT_PG_REWIND:
if (optarg != NULL) if (optarg != NULL)
{ {
@@ -534,6 +535,12 @@ main(int argc, char **argv)
runtime_options.wait_register_sync_seconds = repmgr_atoi(optarg, "--wait-sync", &cli_errors, false); runtime_options.wait_register_sync_seconds = repmgr_atoi(optarg, "--wait-sync", &cli_errors, false);
} }
break; break;
case OPT_CONFIG_ARCHIVE_DIR:
strncpy(runtime_options.config_archive_dir, optarg, MAXLEN);
break;
case OPT_CLUSTER:
strncpy(repmgr_cluster, optarg, MAXLEN);
break;
/* deprecated options - output a warning */ /* deprecated options - output a warning */
case 'l': case 'l':
/* -l/--local-port is deprecated */ /* -l/--local-port is deprecated */
@@ -887,8 +894,12 @@ main(int argc, char **argv)
} }
/* Initialise the repmgr schema name */ /* Initialise the repmgr schema name */
maxlen_snprintf(repmgr_schema, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX, if (strlen(repmgr_cluster))
options.cluster_name); maxlen_snprintf(repmgr_schema, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
repmgr_cluster);
else
maxlen_snprintf(repmgr_schema, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
options.cluster_name);
/* /*
* Initialise slot name, if required (9.4 and later) * Initialise slot name, if required (9.4 and later)
@@ -979,7 +990,12 @@ do_cluster_show(void)
/* Connect to local database to obtain cluster connection data */ /* Connect to local database to obtain cluster connection data */
log_info(_("connecting to database\n")); log_info(_("connecting to database\n"));
conn = establish_db_connection(options.conninfo, true);
if (strlen(options.conninfo))
conn = establish_db_connection(options.conninfo, true);
else
conn = establish_db_connection_by_params((const char**)source_conninfo.keywords,
(const char**)source_conninfo.values, true);
sqlquery_snprintf(sqlquery, sqlquery_snprintf(sqlquery,
"SELECT conninfo, type, name, upstream_node_name, id" "SELECT conninfo, type, name, upstream_node_name, id"
@@ -1156,6 +1172,11 @@ do_cluster_matrix(void)
char *host; char *host;
initialize_conninfo_params(&remote_conninfo, false); initialize_conninfo_params(&remote_conninfo, false);
parse_conninfo_string(PQgetvalue(res, i, 0),
&remote_conninfo,
NULL,
false);
host = param_get(&remote_conninfo, "host"); host = param_get(&remote_conninfo, "host");
conn = establish_db_connection(PQgetvalue(res, i, 0), false); conn = establish_db_connection(PQgetvalue(res, i, 0), false);
@@ -1173,13 +1194,16 @@ do_cluster_matrix(void)
continue; continue;
maxlen_snprintf(command, maxlen_snprintf(command,
"repmgr cluster show --csv"); "\"%s -d '%s' --cluster '%s' cluster show --csv\"",
make_pg_path("repmgr"),
PQgetvalue(res, i, 0),
options.cluster_name);
initPQExpBuffer(&command_output); initPQExpBuffer(&command_output);
(void)remote_command( (void)remote_command(
host, host,
"postgres", runtime_options.remote_user,
command, command,
&command_output); &command_output);
@@ -6367,6 +6391,8 @@ check_parameters_for_action(const int action)
break; break;
case CLUSTER_SHOW: case CLUSTER_SHOW:
/* host parameters can be supplied */
config_file_required = false;
/* allow all parameters to be supplied */ /* allow all parameters to be supplied */
break; break;

View File

@@ -61,6 +61,7 @@
#define OPT_WITHOUT_BARMAN 10 #define OPT_WITHOUT_BARMAN 10
#define OPT_NO_UPSTREAM_CONNECTION 11 #define OPT_NO_UPSTREAM_CONNECTION 11
#define OPT_REGISTER_WAIT 12 #define OPT_REGISTER_WAIT 12
#define OPT_CLUSTER 13
/* deprecated command line options */ /* deprecated command line options */
#define OPT_INITDB_NO_PWPROMPT 999 #define OPT_INITDB_NO_PWPROMPT 999