MXS-922: Serialize created servers
When a server is created via MaxAdmin, it will be serialized to disk. This allows created servers to be retained through a restart of MaxScale. Currently, all serialized objects are stored in one folder and there is no structure in the created files. In the future, servers could be created under a `servers` subdirectory so that it is easier to see what was added. Whether there is a need for this will be seen.
This commit is contained in:
@ -47,6 +47,7 @@
|
||||
#include <maxscale/gw_ssl.h>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxscale/modules.h>
|
||||
#include <maxscale/gwdirs.h>
|
||||
|
||||
static SPINLOCK server_spin = SPINLOCK_INIT;
|
||||
static SERVER *allServers = NULL;
|
||||
@ -1062,7 +1063,14 @@ bool server_set_version_string(SERVER* server, const char* string)
|
||||
return rval;
|
||||
}
|
||||
|
||||
bool server_serialize(SERVER *server, const char *filename)
|
||||
/**
|
||||
* Creates a server configuration at the location pointed by @c filename
|
||||
*
|
||||
* @param server Server to serialize into a configuration
|
||||
* @param filename Filename where configuration is written
|
||||
* @return True on success, false on error
|
||||
*/
|
||||
static bool create_server_config(SERVER *server, const char *filename)
|
||||
{
|
||||
int file = open(filename, O_EXCL | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
|
||||
@ -1162,6 +1170,43 @@ bool server_serialize(SERVER *server, const char *filename)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool server_serialize(SERVER *server)
|
||||
{
|
||||
bool rval = false;
|
||||
char filename[PATH_MAX];
|
||||
snprintf(filename, sizeof(filename), "%s/%s.cnf.tmp", get_config_persistdir(),
|
||||
server->unique_name);
|
||||
|
||||
if (unlink(filename) == -1 && errno != ENOENT)
|
||||
{
|
||||
char err[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("Failed to remove temporary server configuration at '%s': %d, %s",
|
||||
filename, errno, strerror_r(errno, err, sizeof(err)));
|
||||
}
|
||||
else if (create_server_config(server, filename))
|
||||
{
|
||||
char final_filename[PATH_MAX];
|
||||
strcpy(final_filename, filename);
|
||||
|
||||
char *dot = strrchr(final_filename, '.');
|
||||
ss_dassert(dot);
|
||||
*dot = '\0';
|
||||
|
||||
if (rename(filename, final_filename) == 0)
|
||||
{
|
||||
rval = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
char err[MXS_STRERROR_BUFLEN];
|
||||
MXS_ERROR("Failed to rename temporary server configuration at '%s': %d, %s",
|
||||
filename, errno, strerror_r(errno, err, sizeof(err)));
|
||||
}
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
bool server_is_ssl_parameter(const char *key)
|
||||
{
|
||||
// TODO: Implement this
|
||||
|
||||
@ -140,31 +140,37 @@ bool test_load_config(const char *input, SERVER *server)
|
||||
bool test_serialize()
|
||||
{
|
||||
char name[] = "serialized-server";
|
||||
char config_name[] = "serialized-server.cnf";
|
||||
char old_config_name[] = "serialized-server.cnf.old";
|
||||
char *persist_dir = MXS_STRDUP_A("./");
|
||||
set_config_persistdir(persist_dir);
|
||||
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");
|
||||
/** Make sure the files don't exist */
|
||||
unlink(config_name);
|
||||
unlink(old_config_name);
|
||||
|
||||
/** Serialize server to disk */
|
||||
TEST(server_serialize(server, "./server.cnf"), "Failed to synchronize original server");
|
||||
TEST(server_serialize(server), "Failed to synchronize original server");
|
||||
|
||||
/** Load it again */
|
||||
TEST(test_load_config("./server.cnf", server), "Failed to load the serialized server");
|
||||
TEST(test_load_config(config_name, 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");
|
||||
rename(config_name, old_config_name);
|
||||
|
||||
/** Serialize the loaded server to disk */
|
||||
TEST(server_serialize(created, "./server-created.cnf"), "Failed to synchronize the copied server");
|
||||
TEST(server_serialize(created), "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");
|
||||
char cmd[1024];
|
||||
sprintf(cmd, "diff ./%s ./%s", config_name, old_config_name);
|
||||
TEST(system(cmd) == 0, "The files are not identical");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user