1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-02 09:47:23 +03:00

ctdb/tools: Update ctdb CLI tool to call ctdb_natgw

The "natgwlist" command is no longer marked "auto all" and is also
marked "without daemon".  That latter is not strictly true because
ctdb_natgw needs the daemon so a subsequent invocation of "ctdb
nodestatus" will work.  However, "without daemon" is used here because
the top-level "ctdb natgwlist" does not need to open a connection to
the daemon.  It just needs to invoke ctdb_natgw.

Update tests to suit.

It would make sense to make "ctdb natgw" generally call out to
ctdb_natgw, passing all argument.  However, that can be done later.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
This commit is contained in:
Martin Schwenke 2016-01-21 12:53:43 +11:00 committed by Amitay Isaacs
parent e515968a51
commit 411ccb98c3
4 changed files with 40 additions and 178 deletions

View File

@ -38,16 +38,26 @@ define_test ()
setup_natgw ()
{
debug "Setting up NAT gateway"
debug "Setting up NAT gateway"
natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
mkdir -p "$natgw_config_dir"
# Use in-tree binaries if running against local daemons.
# Otherwise CTDB need to be installed on all nodes.
if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
if [ -z "$CTDB_NATGW_HELPER" ] ; then
export CTDB_NATGW_HELPER="${ctdb_dir}/tools/ctdb_natgw"
fi
# Only want to find functions file, so this is OK
export CTDB_BASE="${ctdb_dir}/config"
fi
# These will accumulate, 1 per test... but will be cleaned up at
# the end.
export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
natgw_config_dir="${TEST_VAR_DIR}/natgw_config"
mkdir -p "$natgw_config_dir"
cat >"$CTDB_NATGW_NODES"
# These will accumulate, 1 per test... but will be cleaned up
# at the end.
export CTDB_NATGW_NODES=$(mktemp --tmpdir="$natgw_config_dir")
cat >"$CTDB_NATGW_NODES"
}
setup_nodes ()

View File

@ -2,10 +2,10 @@
. "${TEST_SCRIPTS_DIR}/unit.sh"
define_test "3 nodes, node 0 missing NATGW capability, all stopped"
define_test "3 nodes, node 0 is slave-only, all stopped"
setup_natgw <<EOF
192.168.20.41
192.168.20.41 slave-only
192.168.20.42
192.168.20.43
EOF
@ -20,7 +20,7 @@ EOF
simple_test <<EOF
NODEMAP
0 192.168.20.41 0x20 -CTDB_CAP_NATGW
0 192.168.20.41 0x20
1 192.168.20.42 0x20 CURRENT RECMASTER
2 192.168.20.43 0x20

View File

@ -2,12 +2,12 @@
. "${TEST_SCRIPTS_DIR}/unit.sh"
define_test "3 nodes, node 0 missing NATGW capability, all stopped"
define_test "3 nodes, all nodes are slave-only, all stopped"
setup_natgw <<EOF
192.168.20.41
192.168.20.42
192.168.20.43
192.168.20.41 slave-only
192.168.20.42 slave-only
192.168.20.43 slave-only
EOF
required_result 2 <<EOF
@ -20,9 +20,9 @@ EOF
simple_test <<EOF
NODEMAP
0 192.168.20.41 0x20 -CTDB_CAP_NATGW
1 192.168.20.42 0x20 CURRENT RECMASTER -CTDB_CAP_NATGW
2 192.168.20.43 0x20 -CTDB_CAP_NATGW
0 192.168.20.41 0x20
1 192.168.20.42 0x20 CURRENT RECMASTER
2 192.168.20.43 0x20
VNNMAP
654321

View File

@ -1150,31 +1150,6 @@ static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **
return ret;
}
static struct ctdb_node_map_old *read_natgw_nodes_file(struct ctdb_context *ctdb,
TALLOC_CTX *mem_ctx)
{
const char *natgw_list;
struct ctdb_node_map_old *natgw_nodes = NULL;
natgw_list = getenv("CTDB_NATGW_NODES");
if (natgw_list == NULL) {
natgw_list = talloc_asprintf(mem_ctx, "%s/natgw_nodes",
getenv("CTDB_BASE"));
if (natgw_list == NULL) {
DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
exit(1);
}
}
/* The PNNs/flags will be junk but they're not used */
natgw_nodes = ctdb_read_nodes_file(mem_ctx, natgw_list);
if (natgw_nodes == NULL) {
DEBUG(DEBUG_ERR,
("Failed to load natgw node list '%s'\n", natgw_list));
}
return natgw_nodes;
}
/* talloc off the existing nodemap... */
static struct ctdb_node_map_old *talloc_nodemap(struct ctdb_node_map_old *nodemap)
{
@ -1183,37 +1158,6 @@ static struct ctdb_node_map_old *talloc_nodemap(struct ctdb_node_map_old *nodema
nodemap->num * sizeof(struct ctdb_node_and_flags));
}
static struct ctdb_node_map_old *
filter_nodemap_by_addrs(struct ctdb_context *ctdb,
struct ctdb_node_map_old *nodemap,
struct ctdb_node_map_old *natgw_nodes)
{
int i, j;
struct ctdb_node_map_old *ret;
ret = talloc_nodemap(nodemap);
CTDB_NO_MEMORY_NULL(ctdb, ret);
ret->num = 0;
for (i = 0; i < nodemap->num; i++) {
for(j = 0; j < natgw_nodes->num ; j++) {
if (nodemap->nodes[j].flags & NODE_FLAGS_DELETED) {
continue;
}
if (ctdb_same_ip(&natgw_nodes->nodes[j].addr,
&nodemap->nodes[i].addr)) {
ret->nodes[ret->num] = nodemap->nodes[i];
ret->num++;
break;
}
}
}
return ret;
}
static struct ctdb_node_map_old *
filter_nodemap_by_capabilities(struct ctdb_context *ctdb,
struct ctdb_node_map_old *nodemap,
@ -1290,116 +1234,24 @@ filter_nodemap_by_flags(struct ctdb_context *ctdb,
*/
static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
{
TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
int i, ret;
struct ctdb_node_map_old *natgw_nodes = NULL;
struct ctdb_node_map_old *orig_nodemap=NULL;
struct ctdb_node_map_old *nodemap;
uint32_t mypnn, pnn;
const char *ip;
static char prog[PATH_MAX+1] = "";
/* When we have some nodes that could be the NATGW, make a
* series of attempts to find the first node that doesn't have
* certain status flags set.
*/
uint32_t exclude_flags[] = {
/* Look for a nice healthy node */
NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
/* If not found, an UNHEALTHY/BANNED node will do */
NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
/* If not found, a STOPPED node will do */
NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
0,
};
/* read the natgw nodes file into a linked list */
natgw_nodes = read_natgw_nodes_file(ctdb, tmp_ctx);
if (natgw_nodes == NULL) {
ret = -1;
goto done;
if (argc != 0) {
usage();
}
ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
tmp_ctx, &orig_nodemap);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
talloc_free(tmp_ctx);
return -1;
if (!ctdb_set_helper("NAT gateway helper", prog, sizeof(prog),
"CTDB_NATGW_HELPER", CTDB_HELPER_BINDIR,
"ctdb_natgw")) {
DEBUG(DEBUG_ERR, ("Unable to set NAT gateway helper\n"));
exit(1);
}
/* Get a nodemap that includes only the nodes in the NATGW
* group */
nodemap = filter_nodemap_by_addrs(ctdb, orig_nodemap, natgw_nodes);
if (nodemap == NULL) {
ret = -1;
goto done;
}
execl(prog, prog, "natgwlist", NULL);
ret = 2; /* matches ENOENT */
pnn = -1;
ip = "0.0.0.0";
/* For each flag mask... */
for (i = 0; exclude_flags[i] != 0; i++) {
/* ... get a nodemap that excludes nodes with with
* masked flags... */
struct ctdb_node_map_old *t =
filter_nodemap_by_flags(ctdb, nodemap,
exclude_flags[i]);
if (t == NULL) {
/* No memory */
ret = -1;
goto done;
}
if (t->num > 0) {
/* ... and find the first node with the NATGW
* capability */
struct ctdb_node_map_old *n;
n = filter_nodemap_by_capabilities(ctdb, t,
CTDB_CAP_NATGW,
true);
if (n == NULL) {
/* No memory */
ret = -1;
goto done;
}
if (n->num > 0) {
ret = 0;
pnn = n->nodes[0].pnn;
ip = ctdb_addr_to_str(&n->nodes[0].addr);
break;
}
}
talloc_free(t);
}
if (options.machinereadable) {
printm(":Node:IP:\n");
printm(":%d:%s:\n", pnn, ip);
} else {
printf("%d %s\n", pnn, ip);
}
/* print the pruned list of nodes belonging to this natgw list */
mypnn = getpnn(ctdb);
if (options.machinereadable) {
control_status_header_machine();
} else {
printf("Number of nodes:%d\n", nodemap->num);
}
for(i=0;i<nodemap->num;i++){
if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
continue;
}
if (options.machinereadable) {
control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
} else {
control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
}
}
done:
talloc_free(tmp_ctx);
return ret;
DEBUG(DEBUG_ERR,
("Unable to run NAT gateway helper %s\n", strerror(errno)));
exit(1);
}
/*
@ -6558,7 +6410,7 @@ static const struct {
{ "scriptstatus", control_scriptstatus, true, false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
{ "enablescript", control_enablescript, true, false, "enable an eventscript", "<script>"},
{ "disablescript", control_disablescript, true, false, "disable an eventscript", "<script>"},
{ "natgwlist", control_natgwlist, true, false, "show the nodes belonging to this natgw configuration"},
{ "natgwlist", control_natgwlist, false, true, "show the nodes belonging to this natgw configuration"},
{ "xpnn", control_xpnn, false, true, "find the pnn of the local node without talking to the daemon (unreliable)" },
{ "getreclock", control_getreclock, true, false, "Show the reclock file of a node"},
{ "setreclock", control_setreclock, true, false, "Set/clear the reclock file of a node", "[filename]"},