Skip to content

Commit

Permalink
Refactor checking/discovering --nodename. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimCitus committed May 23, 2019
1 parent 0b4558b commit 4b9c3ba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/bin/pg_autoctl/cli_common.h
Expand Up @@ -100,6 +100,7 @@ void exit_unless_role_is_keeper(KeeperConfig *kconfig);
/* cli_create_drop_node.c */
bool cli_create_config(Keeper *keeper, KeeperConfig *config);
void cli_create_pg(Keeper *keeper, KeeperConfig *config);
bool check_or_discover_nodename(KeeperConfig *config);



Expand Down
93 changes: 55 additions & 38 deletions src/bin/pg_autoctl/cli_create_drop_node.c
Expand Up @@ -229,45 +229,10 @@ cli_create_postgres(int argc, char **argv)
config.pgSetup.pgKind = NODE_KIND_STANDALONE;
strlcpy(config.nodeKind, "standalone", NAMEDATALEN);

/* take care of the nodename */
if (IS_EMPTY_STRING_BUFFER(config.nodename))
{
char monitorHostname[_POSIX_HOST_NAME_MAX];
int monitorPort = 0;

if (!hostname_from_uri(config.monitor_pguri,
monitorHostname, _POSIX_HOST_NAME_MAX,
&monitorPort))
{
log_fatal("Failed to determine monitor hostname when parsing "
"Postgres URI \"%s\"", config.monitor_pguri);
exit(EXIT_CODE_BAD_ARGS);
}

if (!discover_nodename((char *) &(config.nodename),
_POSIX_HOST_NAME_MAX,
monitorHostname,
monitorPort))
{
log_fatal("Failed to auto-detect the hostname of this machine, "
"please provide one via --nodename");
exit(EXIT_CODE_BAD_ARGS);
}
}
else
if (!check_or_discover_nodename(&config))
{
/*
* When provided with a --nodename option, we run some checks on the
* user provided value based on Postgres usage for the hostname in its
* HBA setup. Both forward and reverse DNS needs to return meaningful
* values for the connections to be granted when using a hostname.
*
* That said network setup is something complex and we don't pretend we
* are able to avoid any and all false negatives in our checks, so we
* only WARN when finding something that might be fishy, and proceed
* with the setup of the local node anyway.
*/
(void) check_nodename(config.nodename);
/* errors have already been logged */
exit(EXIT_CODE_BAD_ARGS);
}

if (!cli_create_config(&keeper, &config))
Expand Down Expand Up @@ -585,6 +550,58 @@ cli_drop_node(int argc, char **argv)
}


/*
* check_or_discover_nodename checks given --nodename or attempt to discover a
* suitable default value for the current node when it's not been provided on
* the command line.
*/
bool
check_or_discover_nodename(KeeperConfig *config)
{
/* take care of the nodename */
if (IS_EMPTY_STRING_BUFFER(config->nodename))
{
char monitorHostname[_POSIX_HOST_NAME_MAX];
int monitorPort = 0;

if (!hostname_from_uri(config->monitor_pguri,
monitorHostname, _POSIX_HOST_NAME_MAX,
&monitorPort))
{
log_fatal("Failed to determine monitor hostname when parsing "
"Postgres URI \"%s\"", config->monitor_pguri);
return false;
}

if (!discover_nodename((char *) &(config->nodename),
_POSIX_HOST_NAME_MAX,
monitorHostname,
monitorPort))
{
log_fatal("Failed to auto-detect the hostname of this machine, "
"please provide one via --nodename");
return false;
}
}
else
{
/*
* When provided with a --nodename option, we run some checks on the
* user provided value based on Postgres usage for the hostname in its
* HBA setup. Both forward and reverse DNS needs to return meaningful
* values for the connections to be granted when using a hostname.
*
* That said network setup is something complex and we don't pretend we
* are able to avoid any and all false negatives in our checks, so we
* only WARN when finding something that might be fishy, and proceed
* with the setup of the local node anyway.
*/
(void) check_nodename(config->nodename);
}
return true;
}


/*
* discover_nodename discovers a suitable --nodename default value in three
* steps:
Expand Down

0 comments on commit 4b9c3ba

Please sign in to comment.