MXS-922: Add server serialization function

The function serializes a server to a file. This is intended to be used
with dynamically created servers. The output of the server serialization
will eventually be stored in the configuration file directory (default is
/etc/maxscale.cnf.d/) so that created servers persist even after a
restart.
This commit is contained in:
Markus Makela
2016-11-06 21:16:19 +02:00
parent ea0dcea5d6
commit 28fc3d2b83
3 changed files with 195 additions and 0 deletions

View File

@ -91,6 +91,7 @@ typedef struct server
char *protocol; /**< Protocol module to use */
char *authenticator; /**< Authenticator module name */
void *auth_instance; /**< Authenticator instance */
char *auth_options; /**< Authenticator options */
SSL_LISTENER *server_ssl; /**< SSL data structure for server, if any */
unsigned int status; /**< Status flag bitmap for the server */
char *monuser; /**< User name to use to monitor the db */
@ -225,4 +226,17 @@ extern RESULTSET *serverGetList();
extern unsigned int server_map_status(char *str);
extern bool server_set_version_string(SERVER* server, const char* string);
/**
* @brief Serialize a server to a file
*
* This converts @c server into an INI format file. This allows created servers
* to be persisted to disk. A new file is only created if the file pointed by
* @c filename does not exist at the time this function is called.
*
* @param server Server to serialize
* @param filename Path to a file where the server is persisted
* @return False if the serialization of the server fails, true if it was successful
*/
bool server_serialize(SERVER *server, const char *filename);
MXS_END_DECLS

View File

@ -35,6 +35,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <maxscale/session.h>
#include <maxscale/server.h>
#include <maxscale/spinlock.h>
@ -86,6 +89,12 @@ server_alloc(char *servname, char *protocol, unsigned short port, char *authenti
return NULL;
}
if (auth_options && (auth_options = MXS_STRDUP(auth_options)) == NULL)
{
MXS_FREE(authenticator);
return NULL;
}
servname = MXS_STRNDUP(servname, MAX_SERVER_NAME_LEN);
protocol = MXS_STRDUP(protocol);
@ -108,6 +117,7 @@ server_alloc(char *servname, char *protocol, unsigned short port, char *authenti
server->protocol = protocol;
server->authenticator = authenticator;
server->auth_instance = auth_instance;
server->auth_options = auth_options;
server->port = port;
server->status = SERVER_RUNNING;
server->node_id = -1;
@ -1070,3 +1080,103 @@ bool server_set_version_string(SERVER* server, const char* string)
return rval;
}
bool server_serialize(SERVER *server, const char *filename)
{
int file = open(filename, O_EXCL | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (file == -1)
{
char errbuf[MXS_STRERROR_BUFLEN];
MXS_ERROR("Failed to open file '%s' when serializing server '%s': %d, %s",
filename, server->unique_name, errno, strerror_r(errno, errbuf, sizeof(errbuf)));
return false;
}
// TODO: Check for return values on all of the dprintf calls
dprintf(file, "[%s]\n", server->unique_name);
dprintf(file, "type=server\n");
dprintf(file, "protocol=%s\n", server->protocol);
dprintf(file, "address=%s\n", server->name);
dprintf(file, "port=%u\n", server->port);
dprintf(file, "authenticator=%s\n", server->authenticator);
if (server->auth_options)
{
dprintf(file, "authenticator_options=%s\n", server->auth_options);
}
if (server->monpw && server->monuser)
{
dprintf(file, "monitoruser=%s\n", server->monuser);
dprintf(file, "monitorpw=%s\n", server->monpw);
}
if (server->persistpoolmax)
{
dprintf(file, "persistpoolmax=%ld\n", server->persistpoolmax);
}
if (server->persistmaxtime)
{
dprintf(file, "persistmaxtime=%ld\n", server->persistmaxtime);
}
if (server->server_ssl)
{
dprintf(file, "ssl=required\n");
if (server->server_ssl->ssl_cert)
{
dprintf(file, "ssl_cert=%s\n", server->server_ssl->ssl_cert);
}
if (server->server_ssl->ssl_key)
{
dprintf(file, "ssl_key=%s\n", server->server_ssl->ssl_key);
}
if (server->server_ssl->ssl_ca_cert)
{
dprintf(file, "ssl_ca_cert=%s\n", server->server_ssl->ssl_ca_cert);
}
if (server->server_ssl->ssl_cert_verify_depth)
{
dprintf(file, "ssl_cert_verify_depth=%d\n", server->server_ssl->ssl_cert_verify_depth);
}
const char *version = NULL;
switch (server->server_ssl->ssl_method_type)
{
case SERVICE_TLS10:
version = "TLSV10";
break;
#ifdef OPENSSL_1_0
case SERVICE_TLS11:
version = "TLSV11";
break;
case SERVICE_TLS12:
version = "TLSV12";
break;
#endif
case SERVICE_SSL_TLS_MAX:
version = "MAX";
break;
default:
break;
}
if (version)
{
dprintf(file, "ssl_version=%s\n", version);
}
}
close(file);
return true;
}

View File

@ -37,6 +37,10 @@
#include <maxscale/server.h>
#include <maxscale/log_manager.h>
#include <maxscale/gwdirs.h>
// This is pretty ugly but it's required to test internal functions
#include "../config.c"
/**
* test1 Allocate a server and do lots of other things
*
@ -103,11 +107,78 @@ test1()
}
#define TEST(A, B) do { if(!(A)){ printf(B"\n"); return false; }} while(false)
bool test_load_config(const char *input, SERVER *server)
{
DUPLICATE_CONTEXT dcontext;
if (duplicate_context_init(&dcontext))
{
CONFIG_CONTEXT ccontext = {.object = ""};
if (config_load_single_file(input, &dcontext, &ccontext))
{
CONFIG_CONTEXT *obj = ccontext.next;
CONFIG_PARAMETER *param = obj->parameters;
TEST(strcmp(obj->object, server->unique_name) == 0, "Server names differ");
TEST(strcmp(server->name, config_get_param(param, "address")->value) == 0, "Server addresses differ");
TEST(strcmp(server->protocol, config_get_param(param, "protocol")->value) == 0, "Server protocols differ");
TEST(strcmp(server->authenticator, config_get_param(param, "authenticator")->value) == 0,
"Server authenticators differ");
TEST(strcmp(server->auth_options, config_get_param(param, "authenticator_options")->value) == 0,
"Server authenticator options differ");
TEST(server->port == atoi(config_get_param(param, "port")->value), "Server ports differ");
TEST(create_new_server(obj) == 0, "Failed to create server from loaded config");
}
}
return true;
}
bool test_serialize()
{
char name[] = "serialized-server";
SERVER *server = server_alloc("127.0.0.1", "HTTPD", 9876, "NullAuthAllow", "fake=option");
TEST(server, "Server allocation failed");
server_set_unique_name(server, name);
/** Make sure the file doesn't exist */
unlink("./server.cnf");
/** Serialize server to disk */
TEST(server_serialize(server, "./server.cnf"), "Failed to synchronize original server");
/** Load it again */
TEST(test_load_config("./server.cnf", server), "Failed to load the serialized server");
/** We should have two identical servers */
SERVER *created = server_find_by_unique_name(name);
TEST(created->next == server, "We should end up with two servers");
/** Make sure the file doesn't exist */
unlink("./server-created.cnf");
/** Serialize the loaded server to disk */
TEST(server_serialize(created, "./server-created.cnf"), "Failed to synchronize the copied server");
/** Check that they serialize to identical files */
TEST(system("diff ./server.cnf ./server-created.cnf") == 0, "The files are not identical");
return true;
}
int main(int argc, char **argv)
{
int result = 0;
result += test1();
if (!test_serialize())
{
result++;
}
exit(result);
}