2019-01-18 21:32:51 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Protocol Helper
|
|
|
|
***********************************************************************************************************************************/
|
2019-04-26 08:08:23 -04:00
|
|
|
#include "build.auto.h"
|
|
|
|
|
2019-04-17 08:04:22 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-10 13:27:30 +02:00
|
|
|
#include "common/crypto/common.h"
|
2019-01-18 21:32:51 +02:00
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/exec.h"
|
|
|
|
#include "common/memContext.h"
|
2020-11-23 15:55:46 -05:00
|
|
|
#include "config/config.intern.h"
|
2019-01-18 21:32:51 +02:00
|
|
|
#include "config/exec.h"
|
2020-12-17 09:32:31 -05:00
|
|
|
#include "config/parse.h"
|
2019-02-19 20:57:38 +02:00
|
|
|
#include "config/protocol.h"
|
2020-02-12 15:47:07 -07:00
|
|
|
#include "postgres/version.h"
|
2019-01-18 21:32:51 +02:00
|
|
|
#include "protocol/helper.h"
|
2020-02-12 15:47:07 -07:00
|
|
|
#include "version.h"
|
2019-01-18 21:32:51 +02:00
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Constants
|
|
|
|
***********************************************************************************************************************************/
|
2019-02-27 22:34:21 +02:00
|
|
|
STRING_EXTERN(PROTOCOL_SERVICE_LOCAL_STR, PROTOCOL_SERVICE_LOCAL);
|
2019-01-18 21:32:51 +02:00
|
|
|
STRING_EXTERN(PROTOCOL_SERVICE_REMOTE_STR, PROTOCOL_SERVICE_REMOTE);
|
|
|
|
|
2020-01-08 18:59:02 -07:00
|
|
|
STRING_STATIC(PROTOCOL_REMOTE_TYPE_PG_STR, PROTOCOL_REMOTE_TYPE_PG);
|
|
|
|
STRING_STATIC(PROTOCOL_REMOTE_TYPE_REPO_STR, PROTOCOL_REMOTE_TYPE_REPO);
|
2019-11-23 10:22:11 -05:00
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Local variables
|
|
|
|
***********************************************************************************************************************************/
|
2019-02-27 22:34:21 +02:00
|
|
|
typedef struct ProtocolHelperClient
|
|
|
|
{
|
|
|
|
Exec *exec; // Executed client
|
|
|
|
ProtocolClient *client; // Protocol client
|
|
|
|
} ProtocolHelperClient;
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
MemContext *memContext; // Mem context for protocol helper
|
|
|
|
|
2019-08-21 14:14:30 -04:00
|
|
|
unsigned int clientRemoteSize; // Remote clients
|
2019-02-27 22:34:21 +02:00
|
|
|
ProtocolHelperClient *clientRemote;
|
|
|
|
|
2019-08-21 14:14:30 -04:00
|
|
|
unsigned int clientLocalSize; // Local clients
|
2019-02-27 22:34:21 +02:00
|
|
|
ProtocolHelperClient *clientLocal;
|
2019-01-18 21:32:51 +02:00
|
|
|
} protocolHelper;
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Init local mem context and data structure
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static void
|
|
|
|
protocolHelperInit(void)
|
|
|
|
{
|
|
|
|
// In the protocol helper has not been initialized
|
|
|
|
if (protocolHelper.memContext == NULL)
|
|
|
|
{
|
|
|
|
// Create a mem context to store protocol objects
|
|
|
|
MEM_CONTEXT_BEGIN(memContextTop())
|
|
|
|
{
|
2020-01-17 11:58:41 -07:00
|
|
|
MEM_CONTEXT_NEW_BEGIN("ProtocolHelper")
|
|
|
|
{
|
|
|
|
protocolHelper.memContext = MEM_CONTEXT_NEW();
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_NEW_END();
|
2019-02-27 22:34:21 +02:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
/**********************************************************************************************************************************/
|
2019-01-18 21:32:51 +02:00
|
|
|
bool
|
2020-11-23 15:55:46 -05:00
|
|
|
repoIsLocal(unsigned int repoIdx)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2020-11-23 15:55:46 -05:00
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
|
|
|
FUNCTION_LOG_PARAM(UINT, repoIdx);
|
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN(BOOL, !cfgOptionIdxTest(cfgOptRepoHost, repoIdx));
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
/**********************************************************************************************************************************/
|
2019-08-21 11:41:36 -04:00
|
|
|
void
|
|
|
|
repoIsLocalVerify(void)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_VOID();
|
|
|
|
|
2021-01-21 15:21:50 -05:00
|
|
|
repoIsLocalVerifyIdx(cfgOptionGroupIdxDefault(cfgOptGrpRepo));
|
|
|
|
|
|
|
|
FUNCTION_TEST_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
repoIsLocalVerifyIdx(unsigned int repoIdx)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_VOID();
|
|
|
|
|
|
|
|
if (!repoIsLocal(repoIdx))
|
2019-08-21 11:41:36 -04:00
|
|
|
THROW_FMT(HostInvalidError, "%s command must be run on the repository host", cfgCommandName(cfgCommand()));
|
|
|
|
|
|
|
|
FUNCTION_TEST_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
/**********************************************************************************************************************************/
|
2019-07-17 14:09:50 -04:00
|
|
|
bool
|
2020-10-26 10:25:16 -04:00
|
|
|
pgIsLocal(unsigned int pgIdx)
|
2019-07-17 14:09:50 -04:00
|
|
|
{
|
2019-07-31 20:44:49 -04:00
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
2020-10-26 10:25:16 -04:00
|
|
|
FUNCTION_LOG_PARAM(UINT, pgIdx);
|
2019-07-31 20:44:49 -04:00
|
|
|
FUNCTION_LOG_END();
|
2019-07-17 14:09:50 -04:00
|
|
|
|
2020-11-23 15:55:46 -05:00
|
|
|
FUNCTION_LOG_RETURN(BOOL, !cfgOptionIdxTest(cfgOptPgHost, pgIdx));
|
2019-07-17 14:09:50 -04:00
|
|
|
}
|
|
|
|
|
2020-02-12 15:47:07 -07:00
|
|
|
/**********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
pgIsLocalVerify(void)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_VOID();
|
|
|
|
|
2020-11-23 15:55:46 -05:00
|
|
|
if (!pgIsLocal(cfgOptionGroupIdxDefault(cfgOptGrpPg)))
|
2020-02-12 15:47:07 -07:00
|
|
|
THROW_FMT(HostInvalidError, "%s command must be run on the " PG_NAME " host", cfgCommandName(cfgCommand()));
|
|
|
|
|
|
|
|
FUNCTION_TEST_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
/***********************************************************************************************************************************
|
2019-02-27 22:34:21 +02:00
|
|
|
Get the command line required for local protocol execution
|
2019-01-18 21:32:51 +02:00
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static StringList *
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolLocalParam(ProtocolStorageType protocolStorageType, unsigned int hostIdx, unsigned int processId)
|
2019-02-27 22:34:21 +02:00
|
|
|
{
|
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
2020-10-26 10:25:16 -04:00
|
|
|
FUNCTION_LOG_PARAM(UINT, hostIdx);
|
|
|
|
FUNCTION_LOG_PARAM(UINT, processId);
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
|
|
|
StringList *result = NULL;
|
|
|
|
|
|
|
|
MEM_CONTEXT_TEMP_BEGIN()
|
|
|
|
{
|
|
|
|
// Option replacements
|
|
|
|
KeyValue *optionReplace = kvNew();
|
|
|
|
|
|
|
|
// Add the process id -- used when more than one process will be called
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_PROCESS), VARUINT(processId));
|
2019-02-27 22:34:21 +02:00
|
|
|
|
2021-01-21 15:21:50 -05:00
|
|
|
// Add the pg default. Don't do this for repos because the repo default should come from the user or the local should
|
|
|
|
// handle all the repos equally. Repos don't get special handling like pg primaries or standbys.
|
2020-12-31 08:12:35 -05:00
|
|
|
if (protocolStorageType == protocolStorageTypePg)
|
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_PG), VARUINT(cfgOptionGroupIdxToKey(cfgOptGrpPg, hostIdx)));
|
2019-02-27 22:34:21 +02:00
|
|
|
|
2020-01-09 11:56:03 -07:00
|
|
|
// Add the remote type
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_REMOTE_TYPE), VARSTR(protocolStorageTypeStr(protocolStorageType)));
|
2019-02-27 22:34:21 +02:00
|
|
|
|
2019-03-16 15:00:02 +04:00
|
|
|
// Only enable file logging on the local when requested
|
|
|
|
kvPut(
|
2021-04-22 19:10:13 -04:00
|
|
|
optionReplace, VARSTRDEF(CFGOPT_LOG_LEVEL_FILE),
|
2019-04-17 08:04:22 -04:00
|
|
|
cfgOptionBool(cfgOptLogSubprocess) ? cfgOption(cfgOptLogLevelFile) : VARSTRDEF("off"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
|
|
|
// Always output errors on stderr for debugging purposes
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_LOG_LEVEL_STDERR), VARSTRDEF("error"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
2020-01-15 12:24:58 -07:00
|
|
|
// Disable output to stdout since it is used by the protocol
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_LOG_LEVEL_CONSOLE), VARSTRDEF("off"));
|
2020-01-15 12:24:58 -07:00
|
|
|
|
2020-01-17 13:29:49 -07:00
|
|
|
result = strLstMove(cfgExecParam(cfgCommand(), cfgCmdRoleLocal, optionReplace, true, false), memContextPrior());
|
2019-02-27 22:34:21 +02:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_TEMP_END();
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN(STRING_LIST, result);
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
/**********************************************************************************************************************************/
|
2019-02-27 22:34:21 +02:00
|
|
|
ProtocolClient *
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolLocalGet(ProtocolStorageType protocolStorageType, unsigned int hostIdx, unsigned int processId)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
2020-10-26 10:25:16 -04:00
|
|
|
FUNCTION_LOG_PARAM(UINT, hostIdx);
|
|
|
|
FUNCTION_LOG_PARAM(UINT, processId);
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_END();
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperInit();
|
|
|
|
|
|
|
|
// Allocate the client cache
|
|
|
|
if (protocolHelper.clientLocalSize == 0)
|
|
|
|
{
|
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
|
|
|
{
|
2019-12-05 22:34:38 -05:00
|
|
|
protocolHelper.clientLocalSize = cfgOptionUInt(cfgOptProcessMax) + 1;
|
2020-01-23 14:15:58 -07:00
|
|
|
protocolHelper.clientLocal = memNew(protocolHelper.clientLocalSize * sizeof(ProtocolHelperClient));
|
|
|
|
|
|
|
|
for (unsigned int clientIdx = 0; clientIdx < protocolHelper.clientLocalSize; clientIdx++)
|
|
|
|
protocolHelper.clientLocal[clientIdx] = (ProtocolHelperClient){.exec = NULL};
|
2019-02-27 22:34:21 +02:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
2020-10-26 10:25:16 -04:00
|
|
|
ASSERT(processId <= protocolHelper.clientLocalSize);
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
// Create protocol object
|
2020-10-26 10:25:16 -04:00
|
|
|
ProtocolHelperClient *protocolHelperClient = &protocolHelper.clientLocal[processId - 1];
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
if (protocolHelperClient->client == NULL)
|
|
|
|
{
|
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
|
|
|
{
|
|
|
|
// Execute the protocol command
|
|
|
|
protocolHelperClient->exec = execNew(
|
2020-10-26 10:25:16 -04:00
|
|
|
cfgExe(), protocolLocalParam(protocolStorageType, hostIdx, processId),
|
2020-12-09 08:59:51 -05:00
|
|
|
strNewFmt(PROTOCOL_SERVICE_LOCAL "-%u process", processId), cfgOptionUInt64(cfgOptProtocolTimeout));
|
2019-02-27 22:34:21 +02:00
|
|
|
execOpen(protocolHelperClient->exec);
|
|
|
|
|
|
|
|
// Create protocol object
|
|
|
|
protocolHelperClient->client = protocolClientNew(
|
2020-10-26 10:25:16 -04:00
|
|
|
strNewFmt(PROTOCOL_SERVICE_LOCAL "-%u protocol", processId),
|
2019-02-27 22:34:21 +02:00
|
|
|
PROTOCOL_SERVICE_LOCAL_STR, execIoRead(protocolHelperClient->exec), execIoWrite(protocolHelperClient->exec));
|
|
|
|
|
|
|
|
protocolClientMove(protocolHelperClient->client, execMemContext(protocolHelperClient->exec));
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN(PROTOCOL_CLIENT, protocolHelperClient->client);
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:15:33 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Free the protocol client and underlying exec'd process. Log any errors as warnings since it is not worth terminating the process
|
|
|
|
while closing a local/remote that has already completed its work. The warning will be an indication that something is not right.
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static void
|
|
|
|
protocolHelperClientFree(ProtocolHelperClient *protocolHelperClient)
|
|
|
|
{
|
|
|
|
FUNCTION_LOG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_LOG_PARAM_P(VOID, protocolHelperClient);
|
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
|
|
|
if (protocolHelperClient->client != NULL)
|
|
|
|
{
|
|
|
|
// Try to shutdown the protocol but only warn on error
|
|
|
|
TRY_BEGIN()
|
|
|
|
{
|
|
|
|
protocolClientFree(protocolHelperClient->client);
|
|
|
|
}
|
|
|
|
CATCH_ANY()
|
|
|
|
{
|
|
|
|
LOG_WARN(errorMessage());
|
|
|
|
}
|
|
|
|
TRY_END();
|
|
|
|
|
|
|
|
// Try to end the child process but only warn on error
|
|
|
|
TRY_BEGIN()
|
|
|
|
{
|
|
|
|
execFree(protocolHelperClient->exec);
|
|
|
|
}
|
|
|
|
CATCH_ANY()
|
|
|
|
{
|
|
|
|
LOG_WARN(errorMessage());
|
|
|
|
}
|
|
|
|
TRY_END();
|
|
|
|
|
|
|
|
protocolHelperClient->client = NULL;
|
|
|
|
protocolHelperClient->exec = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************************************************************************/
|
|
|
|
void
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolLocalFree(unsigned int processId)
|
2020-07-28 12:15:33 -04:00
|
|
|
{
|
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
2020-10-26 10:25:16 -04:00
|
|
|
FUNCTION_LOG_PARAM(UINT, processId);
|
2020-07-28 12:15:33 -04:00
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
|
|
|
if (protocolHelper.clientLocal != NULL)
|
|
|
|
{
|
2020-10-26 10:25:16 -04:00
|
|
|
ASSERT(processId <= protocolHelper.clientLocalSize);
|
|
|
|
protocolHelperClientFree(&protocolHelper.clientLocal[processId - 1]);
|
2020-07-28 12:15:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Get the command line required for remote protocol execution
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static StringList *
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolRemoteParam(ProtocolStorageType protocolStorageType, unsigned int hostIdx)
|
2019-02-27 22:34:21 +02:00
|
|
|
{
|
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
2019-07-31 20:44:49 -04:00
|
|
|
FUNCTION_LOG_PARAM(UINT, hostIdx);
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
// Is this a repo remote?
|
|
|
|
bool isRepo = protocolStorageType == protocolStorageTypeRepo;
|
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
// Fixed parameters for ssh command
|
|
|
|
StringList *result = strLstNew();
|
|
|
|
strLstAddZ(result, "-o");
|
|
|
|
strLstAddZ(result, "LogLevel=error");
|
|
|
|
strLstAddZ(result, "-o");
|
|
|
|
strLstAddZ(result, "Compression=no");
|
|
|
|
strLstAddZ(result, "-o");
|
|
|
|
strLstAddZ(result, "PasswordAuthentication=no");
|
|
|
|
|
|
|
|
// Append port if specified
|
2020-11-23 15:55:46 -05:00
|
|
|
ConfigOption optHostPort = isRepo ? cfgOptRepoHostPort : cfgOptPgHostPort;
|
2019-07-17 14:09:50 -04:00
|
|
|
|
2020-11-23 15:55:46 -05:00
|
|
|
if (cfgOptionIdxTest(optHostPort, hostIdx))
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
|
|
|
strLstAddZ(result, "-p");
|
2020-11-23 15:55:46 -05:00
|
|
|
strLstAdd(result, strNewFmt("%u", cfgOptionIdxUInt(optHostPort, hostIdx)));
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append user/host
|
2019-07-17 14:09:50 -04:00
|
|
|
strLstAdd(
|
|
|
|
result,
|
|
|
|
strNewFmt(
|
2020-11-23 15:55:46 -05:00
|
|
|
"%s@%s", strZ(cfgOptionIdxStr(isRepo ? cfgOptRepoHostUser : cfgOptPgHostUser, hostIdx)),
|
|
|
|
strZ(cfgOptionIdxStr(isRepo ? cfgOptRepoHost : cfgOptPgHost, hostIdx))));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
// Option replacements
|
2019-01-18 21:32:51 +02:00
|
|
|
KeyValue *optionReplace = kvNew();
|
|
|
|
|
|
|
|
// Replace config options with the host versions
|
2020-11-23 15:55:46 -05:00
|
|
|
unsigned int optConfig = isRepo ? cfgOptRepoHostConfig : cfgOptPgHostConfig;
|
2019-07-17 14:09:50 -04:00
|
|
|
|
2020-11-23 15:55:46 -05:00
|
|
|
kvPut(
|
2021-04-22 19:10:13 -04:00
|
|
|
optionReplace, VARSTRDEF(CFGOPT_CONFIG),
|
2020-11-23 15:55:46 -05:00
|
|
|
cfgOptionIdxSource(optConfig, hostIdx) != cfgSourceDefault ? VARSTR(cfgOptionIdxStr(optConfig, hostIdx)) : NULL);
|
2019-07-17 14:09:50 -04:00
|
|
|
|
2020-11-23 15:55:46 -05:00
|
|
|
unsigned int optConfigIncludePath = isRepo ? cfgOptRepoHostConfigIncludePath : cfgOptPgHostConfigIncludePath;
|
2019-07-17 14:09:50 -04:00
|
|
|
|
2019-07-26 08:37:58 -04:00
|
|
|
kvPut(
|
2021-04-22 19:10:13 -04:00
|
|
|
optionReplace, VARSTRDEF(CFGOPT_CONFIG_INCLUDE_PATH),
|
2020-11-23 15:55:46 -05:00
|
|
|
cfgOptionIdxSource(optConfigIncludePath, hostIdx) != cfgSourceDefault ?
|
|
|
|
VARSTR(cfgOptionIdxStr(optConfigIncludePath, hostIdx)) : NULL);
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2020-11-23 15:55:46 -05:00
|
|
|
unsigned int optConfigPath = isRepo ? cfgOptRepoHostConfigPath : cfgOptPgHostConfigPath;
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-07-26 08:37:58 -04:00
|
|
|
kvPut(
|
2021-04-22 19:10:13 -04:00
|
|
|
optionReplace, VARSTRDEF(CFGOPT_CONFIG_PATH),
|
2020-11-23 15:55:46 -05:00
|
|
|
cfgOptionIdxSource(optConfigPath, hostIdx) != cfgSourceDefault ? VARSTR(cfgOptionIdxStr(optConfigPath, hostIdx)) : NULL);
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2020-01-15 12:24:58 -07:00
|
|
|
// Update/remove repo/pg options that are sent to the remote
|
2020-01-09 12:20:13 -07:00
|
|
|
for (ConfigOption optionId = 0; optionId < CFG_OPTION_TOTAL; optionId++)
|
2019-07-17 14:09:50 -04:00
|
|
|
{
|
2020-11-23 15:55:46 -05:00
|
|
|
// Skip options that are not part of a group
|
|
|
|
if (!cfgOptionGroup(optionId))
|
|
|
|
continue;
|
|
|
|
|
2020-01-15 12:24:58 -07:00
|
|
|
bool remove = false;
|
2020-11-23 15:55:46 -05:00
|
|
|
bool skipHostZero = false;
|
2020-01-09 12:20:13 -07:00
|
|
|
|
2020-12-29 15:49:37 -05:00
|
|
|
// Remove repo options when the remote type is pg since they won't be used
|
|
|
|
if (cfgOptionGroupId(optionId) == cfgOptGrpRepo)
|
2020-01-15 12:24:58 -07:00
|
|
|
{
|
2020-12-29 15:49:37 -05:00
|
|
|
remove = protocolStorageType == protocolStorageTypePg;
|
2020-01-15 12:24:58 -07:00
|
|
|
}
|
2020-11-23 15:55:46 -05:00
|
|
|
// Remove pg host options that are not needed on the remote
|
|
|
|
else
|
2020-01-09 12:20:13 -07:00
|
|
|
{
|
2020-12-29 15:49:37 -05:00
|
|
|
ASSERT(cfgOptionGroupId(optionId) == cfgOptGrpPg);
|
2020-11-23 15:55:46 -05:00
|
|
|
|
2020-05-21 16:09:23 -04:00
|
|
|
// Remove unrequired/defaulted pg options when the remote type is repo since they won't be used
|
2020-01-15 12:24:58 -07:00
|
|
|
if (protocolStorageType == protocolStorageTypeRepo)
|
|
|
|
{
|
2020-12-17 09:32:31 -05:00
|
|
|
remove = !cfgParseOptionRequired(cfgCommand(), optionId) || cfgParseOptionDefault(cfgCommand(), optionId) != NULL;
|
2020-11-23 15:55:46 -05:00
|
|
|
}
|
|
|
|
// Move pg options to host index 0 (key 1) so they will be in the default index on the remote host
|
|
|
|
else
|
2020-01-09 12:20:13 -07:00
|
|
|
{
|
2020-11-23 15:55:46 -05:00
|
|
|
if (hostIdx != 0)
|
2020-01-15 12:24:58 -07:00
|
|
|
{
|
|
|
|
kvPut(
|
2020-11-23 15:55:46 -05:00
|
|
|
optionReplace, VARSTRZ(cfgOptionIdxName(optionId, 0)),
|
|
|
|
cfgOptionIdxSource(optionId, hostIdx) != cfgSourceDefault ? cfgOptionIdx(optionId, hostIdx) : NULL);
|
2020-01-15 12:24:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
remove = true;
|
2020-11-23 15:55:46 -05:00
|
|
|
skipHostZero = true;
|
2020-01-09 12:20:13 -07:00
|
|
|
}
|
2020-01-15 12:24:58 -07:00
|
|
|
}
|
2020-01-09 12:20:13 -07:00
|
|
|
|
2020-01-15 12:24:58 -07:00
|
|
|
// Remove options that have been marked for removal if they are not already null or invalid. This is more efficient because
|
|
|
|
// cfgExecParam() won't have to search through as large a list looking for overrides.
|
2020-11-23 15:55:46 -05:00
|
|
|
if (remove)
|
|
|
|
{
|
|
|
|
// Loop through option indexes
|
|
|
|
for (unsigned int optionIdx = 0; optionIdx < cfgOptionIdxTotal(optionId); optionIdx++)
|
|
|
|
{
|
|
|
|
if (cfgOptionIdxTest(optionId, optionIdx) && !(skipHostZero && optionIdx == 0))
|
|
|
|
kvPut(optionReplace, VARSTRZ(cfgOptionIdxName(optionId, optionIdx)), NULL);
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 14:09:50 -04:00
|
|
|
}
|
|
|
|
|
2021-01-21 15:21:50 -05:00
|
|
|
// Set repo default so the remote only operates on a single repo
|
|
|
|
if (protocolStorageType == protocolStorageTypeRepo)
|
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_REPO), VARUINT(cfgOptionGroupIdxToKey(cfgOptGrpRepo, hostIdx)));
|
|
|
|
|
2020-10-26 10:25:16 -04:00
|
|
|
// Add the process id if not set. This means that the remote is being started from the main process and should always get a
|
|
|
|
// process id of 0.
|
2019-02-28 09:51:19 +02:00
|
|
|
if (!cfgOptionTest(cfgOptProcess))
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_PROCESS), VARINT(0));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-03-16 15:00:02 +04:00
|
|
|
// Don't pass log-path or lock-path since these are host specific
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_LOG_PATH), NULL);
|
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_LOCK_PATH), NULL);
|
2019-03-16 15:00:02 +04:00
|
|
|
|
|
|
|
// Only enable file logging on the remote when requested
|
|
|
|
kvPut(
|
2021-04-22 19:10:13 -04:00
|
|
|
optionReplace, VARSTRDEF(CFGOPT_LOG_LEVEL_FILE),
|
2019-04-17 08:04:22 -04:00
|
|
|
cfgOptionBool(cfgOptLogSubprocess) ? cfgOption(cfgOptLogLevelFile) : VARSTRDEF("off"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
|
|
|
// Always output errors on stderr for debugging purposes
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_LOG_LEVEL_STDERR), VARSTRDEF("error"));
|
2019-03-16 15:00:02 +04:00
|
|
|
|
2020-01-15 12:24:58 -07:00
|
|
|
// Disable output to stdout since it is used by the protocol
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_LOG_LEVEL_CONSOLE), VARSTRDEF("off"));
|
2020-01-15 12:24:58 -07:00
|
|
|
|
2020-01-09 11:56:03 -07:00
|
|
|
// Add the remote type
|
2021-04-22 19:10:13 -04:00
|
|
|
kvPut(optionReplace, VARSTRDEF(CFGOPT_REMOTE_TYPE), VARSTR(protocolStorageTypeStr(protocolStorageType)));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2020-01-15 12:24:58 -07:00
|
|
|
StringList *commandExec = cfgExecParam(cfgCommand(), cfgCmdRoleRemote, optionReplace, false, true);
|
2020-11-23 15:55:46 -05:00
|
|
|
strLstInsert(commandExec, 0, cfgOptionIdxStr(isRepo ? cfgOptRepoHostCmd : cfgOptPgHostCmd, hostIdx));
|
2019-01-18 21:32:51 +02:00
|
|
|
strLstAdd(result, strLstJoin(commandExec, " "));
|
|
|
|
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_RETURN(STRING_LIST, result);
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
/**********************************************************************************************************************************/
|
2019-01-18 21:32:51 +02:00
|
|
|
ProtocolClient *
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolRemoteGet(ProtocolStorageType protocolStorageType, unsigned int hostIdx)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_PARAM(ENUM, protocolStorageType);
|
2020-10-26 10:25:16 -04:00
|
|
|
FUNCTION_LOG_PARAM(UINT, hostIdx);
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_END();
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-07-17 14:09:50 -04:00
|
|
|
// Is this a repo remote?
|
|
|
|
bool isRepo = protocolStorageType == protocolStorageTypeRepo;
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperInit();
|
|
|
|
|
|
|
|
// Allocate the client cache
|
|
|
|
if (protocolHelper.clientRemoteSize == 0)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-02-27 22:34:21 +02:00
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2020-12-14 14:37:23 -05:00
|
|
|
protocolHelper.clientRemoteSize = cfgOptionGroupIdxTotal(isRepo ? cfgOptGrpRepo : cfgOptGrpPg) + 1;
|
2020-01-23 14:15:58 -07:00
|
|
|
protocolHelper.clientRemote = memNew(protocolHelper.clientRemoteSize * sizeof(ProtocolHelperClient));
|
|
|
|
|
|
|
|
for (unsigned int clientIdx = 0; clientIdx < protocolHelper.clientRemoteSize; clientIdx++)
|
|
|
|
protocolHelper.clientRemote[clientIdx] = (ProtocolHelperClient){.exec = NULL};
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
2019-07-31 20:44:49 -04:00
|
|
|
// Determine protocol id for the remote. If the process option is set then use that since we want the remote protocol id to
|
|
|
|
// match the local protocol id. Otherwise set to 0 since the remote is being started from a main process and there should only
|
|
|
|
// be one remote per host.
|
2020-10-26 10:25:16 -04:00
|
|
|
unsigned int processId = 0;
|
2019-02-28 09:51:19 +02:00
|
|
|
|
|
|
|
if (cfgOptionTest(cfgOptProcess))
|
2020-10-26 10:25:16 -04:00
|
|
|
processId = cfgOptionUInt(cfgOptProcess);
|
2019-02-28 09:51:19 +02:00
|
|
|
|
2020-10-26 10:25:16 -04:00
|
|
|
CHECK(hostIdx < protocolHelper.clientRemoteSize);
|
2019-02-27 22:34:21 +02:00
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
// Create protocol object
|
2020-10-26 10:25:16 -04:00
|
|
|
ProtocolHelperClient *protocolHelperClient = &protocolHelper.clientRemote[hostIdx];
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
if (protocolHelperClient->client == NULL)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
|
|
|
MEM_CONTEXT_BEGIN(protocolHelper.memContext)
|
|
|
|
{
|
2020-11-23 15:55:46 -05:00
|
|
|
unsigned int optHost = isRepo ? cfgOptRepoHost : cfgOptPgHost;
|
2019-07-17 14:09:50 -04:00
|
|
|
|
2019-01-18 21:32:51 +02:00
|
|
|
// Execute the protocol command
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperClient->exec = execNew(
|
2020-10-26 10:25:16 -04:00
|
|
|
cfgOptionStr(cfgOptCmdSsh), protocolRemoteParam(protocolStorageType, hostIdx),
|
2020-11-23 15:55:46 -05:00
|
|
|
strNewFmt(PROTOCOL_SERVICE_REMOTE "-%u process on '%s'", processId, strZ(cfgOptionIdxStr(optHost, hostIdx))),
|
2020-12-09 08:59:51 -05:00
|
|
|
cfgOptionUInt64(cfgOptProtocolTimeout));
|
2019-02-27 22:34:21 +02:00
|
|
|
execOpen(protocolHelperClient->exec);
|
2019-01-18 21:32:51 +02:00
|
|
|
|
|
|
|
// Create protocol object
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolHelperClient->client = protocolClientNew(
|
2020-11-23 15:55:46 -05:00
|
|
|
strNewFmt(PROTOCOL_SERVICE_REMOTE "-%u protocol on '%s'", processId, strZ(cfgOptionIdxStr(optHost, hostIdx))),
|
2019-02-27 22:34:21 +02:00
|
|
|
PROTOCOL_SERVICE_REMOTE_STR, execIoRead(protocolHelperClient->exec), execIoWrite(protocolHelperClient->exec));
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-19 20:57:38 +02:00
|
|
|
// Get cipher options from the remote if none are locally configured
|
2021-01-21 15:21:50 -05:00
|
|
|
if (isRepo && strEq(cfgOptionIdxStr(cfgOptRepoCipherType, hostIdx), CIPHER_TYPE_NONE_STR))
|
2019-02-19 20:57:38 +02:00
|
|
|
{
|
|
|
|
// Options to query
|
|
|
|
VariantList *param = varLstNew();
|
2021-01-21 15:21:50 -05:00
|
|
|
varLstAdd(param, varNewStrZ(cfgOptionIdxName(cfgOptRepoCipherType, hostIdx)));
|
|
|
|
varLstAdd(param, varNewStrZ(cfgOptionIdxName(cfgOptRepoCipherPass, hostIdx)));
|
2019-02-19 20:57:38 +02:00
|
|
|
|
2021-03-16 13:09:34 -04:00
|
|
|
VariantList *optionList = configOptionRemote(protocolHelperClient->client, param);
|
2019-02-19 20:57:38 +02:00
|
|
|
|
|
|
|
if (!strEq(varStr(varLstGet(optionList, 0)), CIPHER_TYPE_NONE_STR))
|
|
|
|
{
|
2021-01-21 15:21:50 -05:00
|
|
|
cfgOptionIdxSet(cfgOptRepoCipherType, hostIdx, cfgSourceConfig, varLstGet(optionList, 0));
|
|
|
|
cfgOptionIdxSet(cfgOptRepoCipherPass, hostIdx, cfgSourceConfig, varLstGet(optionList, 1));
|
2019-02-19 20:57:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
protocolClientMove(protocolHelperClient->client, execMemContext(protocolHelperClient->exec));
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
FUNCTION_LOG_RETURN(PROTOCOL_CLIENT, protocolHelperClient->client);
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 17:48:53 -05:00
|
|
|
/**********************************************************************************************************************************/
|
|
|
|
void
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolRemoteFree(unsigned int hostIdx)
|
2019-12-07 17:48:53 -05:00
|
|
|
{
|
|
|
|
FUNCTION_LOG_BEGIN(logLevelDebug);
|
2020-10-26 10:25:16 -04:00
|
|
|
FUNCTION_LOG_PARAM(UINT, hostIdx);
|
2019-12-07 17:48:53 -05:00
|
|
|
FUNCTION_LOG_END();
|
|
|
|
|
|
|
|
if (protocolHelper.clientRemote != NULL)
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolHelperClientFree(&protocolHelper.clientRemote[hostIdx]);
|
2019-12-07 17:48:53 -05:00
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
/**********************************************************************************************************************************/
|
2019-03-27 20:59:28 +00:00
|
|
|
void
|
|
|
|
protocolKeepAlive(void)
|
|
|
|
{
|
|
|
|
FUNCTION_LOG_VOID(logLevelTrace);
|
|
|
|
|
|
|
|
if (protocolHelper.memContext != NULL)
|
|
|
|
{
|
|
|
|
for (unsigned int clientIdx = 0; clientIdx < protocolHelper.clientRemoteSize; clientIdx++)
|
|
|
|
{
|
|
|
|
if (protocolHelper.clientRemote[clientIdx].client != NULL)
|
|
|
|
protocolClientNoOp(protocolHelper.clientRemote[clientIdx].client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_LOG_RETURN_VOID();
|
|
|
|
}
|
|
|
|
|
2019-11-23 10:22:11 -05:00
|
|
|
/***********************************************************************************************************************************
|
2020-04-03 18:15:32 -04:00
|
|
|
Getters/Setters
|
2019-11-23 10:22:11 -05:00
|
|
|
***********************************************************************************************************************************/
|
|
|
|
ProtocolStorageType
|
|
|
|
protocolStorageTypeEnum(const String *type)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_BEGIN();
|
|
|
|
FUNCTION_TEST_PARAM(STRING, type);
|
|
|
|
FUNCTION_TEST_END();
|
|
|
|
|
|
|
|
ASSERT(type != NULL);
|
|
|
|
|
2020-01-08 18:59:02 -07:00
|
|
|
if (strEq(type, PROTOCOL_REMOTE_TYPE_PG_STR))
|
2019-11-23 10:22:11 -05:00
|
|
|
FUNCTION_TEST_RETURN(protocolStorageTypePg);
|
2020-01-08 18:59:02 -07:00
|
|
|
else if (strEq(type, PROTOCOL_REMOTE_TYPE_REPO_STR))
|
2019-11-23 10:22:11 -05:00
|
|
|
FUNCTION_TEST_RETURN(protocolStorageTypeRepo);
|
|
|
|
|
2020-07-30 07:49:06 -04:00
|
|
|
THROW_FMT(AssertError, "invalid protocol storage type '%s'", strZ(type));
|
2019-11-23 10:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const String *
|
|
|
|
protocolStorageTypeStr(ProtocolStorageType type)
|
|
|
|
{
|
|
|
|
FUNCTION_TEST_BEGIN();
|
|
|
|
FUNCTION_TEST_PARAM(ENUM, type);
|
|
|
|
FUNCTION_TEST_END();
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case protocolStorageTypePg:
|
2020-01-08 18:59:02 -07:00
|
|
|
FUNCTION_TEST_RETURN(PROTOCOL_REMOTE_TYPE_PG_STR);
|
2019-11-23 10:22:11 -05:00
|
|
|
|
|
|
|
case protocolStorageTypeRepo:
|
2020-01-08 18:59:02 -07:00
|
|
|
FUNCTION_TEST_RETURN(PROTOCOL_REMOTE_TYPE_REPO_STR);
|
2019-11-23 10:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
THROW_FMT(AssertError, "invalid protocol storage type %u", type);
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:01:28 -04:00
|
|
|
/**********************************************************************************************************************************/
|
2019-01-18 21:32:51 +02:00
|
|
|
void
|
|
|
|
protocolFree(void)
|
|
|
|
{
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_VOID(logLevelTrace);
|
2019-01-18 21:32:51 +02:00
|
|
|
|
2019-02-27 22:34:21 +02:00
|
|
|
if (protocolHelper.memContext != NULL)
|
2019-01-18 21:32:51 +02:00
|
|
|
{
|
2019-02-27 22:34:21 +02:00
|
|
|
// Free remotes
|
2019-12-07 17:48:53 -05:00
|
|
|
for (unsigned int clientIdx = 0; clientIdx < protocolHelper.clientRemoteSize; clientIdx++)
|
2020-10-26 10:25:16 -04:00
|
|
|
protocolRemoteFree(clientIdx);
|
2019-02-27 22:34:21 +02:00
|
|
|
|
|
|
|
// Free locals
|
2020-07-28 12:15:33 -04:00
|
|
|
for (unsigned int clientIdx = 1; clientIdx <= protocolHelper.clientLocalSize; clientIdx++)
|
|
|
|
protocolLocalFree(clientIdx);
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:41:59 +02:00
|
|
|
FUNCTION_LOG_RETURN_VOID();
|
2019-01-18 21:32:51 +02:00
|
|
|
}
|