pacemaker  1.1.19-c3c624ea3d
Scalable High-Availability cluster resource manager
container.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <crm_internal.h>
20 
21 #include <ctype.h>
22 
23 #include <crm/pengine/rules.h>
24 #include <crm/pengine/status.h>
25 #include <crm/pengine/internal.h>
26 #include <unpack.h>
27 #include <crm/msg_xml.h>
28 
29 #define VARIANT_CONTAINER 1
30 #include "./variant.h"
31 
32 void tuple_free(container_grouping_t *tuple);
33 
34 static char *
35 next_ip(const char *last_ip)
36 {
37  unsigned int oct1 = 0;
38  unsigned int oct2 = 0;
39  unsigned int oct3 = 0;
40  unsigned int oct4 = 0;
41  int rc = sscanf(last_ip, "%u.%u.%u.%u", &oct1, &oct2, &oct3, &oct4);
42 
43  if (rc != 4) {
44  /*@ TODO check for IPv6 */
45  return NULL;
46 
47  } else if (oct3 > 253) {
48  return NULL;
49 
50  } else if (oct4 > 253) {
51  ++oct3;
52  oct4 = 1;
53 
54  } else {
55  ++oct4;
56  }
57 
58  return crm_strdup_printf("%u.%u.%u.%u", oct1, oct2, oct3, oct4);
59 }
60 
61 static int
62 allocate_ip(container_variant_data_t *data, container_grouping_t *tuple, char *buffer, int max)
63 {
64  if(data->ip_range_start == NULL) {
65  return 0;
66 
67  } else if(data->ip_last) {
68  tuple->ipaddr = next_ip(data->ip_last);
69 
70  } else {
71  tuple->ipaddr = strdup(data->ip_range_start);
72  }
73 
74  data->ip_last = tuple->ipaddr;
75 #if 0
76  return snprintf(buffer, max, " --add-host=%s-%d:%s --link %s-docker-%d:%s-link-%d",
77  data->prefix, tuple->offset, tuple->ipaddr,
78  data->prefix, tuple->offset, data->prefix, tuple->offset);
79 #else
80  if (data->type == PE_CONTAINER_TYPE_DOCKER) {
81  return snprintf(buffer, max, " --add-host=%s-%d:%s",
82  data->prefix, tuple->offset, tuple->ipaddr);
83  } else if (data->type == PE_CONTAINER_TYPE_RKT) {
84  return snprintf(buffer, max, " --hosts-entry=%s=%s-%d",
85  tuple->ipaddr, data->prefix, tuple->offset);
86  } else {
87  return 0;
88  }
89 #endif
90 }
91 
92 static xmlNode *
93 create_resource(const char *name, const char *provider, const char *kind)
94 {
95  xmlNode *rsc = create_xml_node(NULL, XML_CIB_TAG_RESOURCE);
96 
97  crm_xml_add(rsc, XML_ATTR_ID, name);
99  crm_xml_add(rsc, XML_AGENT_ATTR_PROVIDER, provider);
100  crm_xml_add(rsc, XML_ATTR_TYPE, kind);
101 
102  return rsc;
103 }
104 
117 static bool
118 valid_network(container_variant_data_t *data)
119 {
120  if(data->ip_range_start) {
121  return TRUE;
122  }
123  if(data->control_port) {
124  if(data->replicas_per_host > 1) {
125  pe_err("Specifying the 'control-port' for %s requires 'replicas-per-host=1'", data->prefix);
126  data->replicas_per_host = 1;
127  /* @TODO to be sure: clear_bit(rsc->flags, pe_rsc_unique); */
128  }
129  return TRUE;
130  }
131  return FALSE;
132 }
133 
134 static bool
135 create_ip_resource(
136  resource_t *parent, container_variant_data_t *data, container_grouping_t *tuple,
137  pe_working_set_t * data_set)
138 {
139  if(data->ip_range_start) {
140  char *id = NULL;
141  xmlNode *xml_ip = NULL;
142  xmlNode *xml_obj = NULL;
143 
144  id = crm_strdup_printf("%s-ip-%s", data->prefix, tuple->ipaddr);
146  xml_ip = create_resource(id, "heartbeat", "IPaddr2");
147  free(id);
148 
149  xml_obj = create_xml_node(xml_ip, XML_TAG_ATTR_SETS);
150  crm_xml_set_id(xml_obj, "%s-attributes-%d", data->prefix, tuple->offset);
151 
152  crm_create_nvpair_xml(xml_obj, NULL, "ip", tuple->ipaddr);
153  if(data->host_network) {
154  crm_create_nvpair_xml(xml_obj, NULL, "nic", data->host_network);
155  }
156 
157  if(data->host_netmask) {
158  crm_create_nvpair_xml(xml_obj, NULL,
159  "cidr_netmask", data->host_netmask);
160 
161  } else {
162  crm_create_nvpair_xml(xml_obj, NULL, "cidr_netmask", "32");
163  }
164 
165  xml_obj = create_xml_node(xml_ip, "operations");
166  crm_create_op_xml(xml_obj, ID(xml_ip), "monitor", "60s", NULL);
167 
168  // TODO: Other ops? Timeouts and intervals from underlying resource?
169 
170  crm_log_xml_trace(xml_ip, "Container-ip");
171  if (common_unpack(xml_ip, &tuple->ip, parent, data_set) == false) {
172  return FALSE;
173  }
174 
175  parent->children = g_list_append(parent->children, tuple->ip);
176  }
177  return TRUE;
178 }
179 
180 static bool
181 create_docker_resource(
182  resource_t *parent, container_variant_data_t *data, container_grouping_t *tuple,
183  pe_working_set_t * data_set)
184 {
185  int offset = 0, max = 4096;
186  char *buffer = calloc(1, max+1);
187 
188  int doffset = 0, dmax = 1024;
189  char *dbuffer = calloc(1, dmax+1);
190 
191  char *id = NULL;
192  xmlNode *xml_docker = NULL;
193  xmlNode *xml_obj = NULL;
194 
195  id = crm_strdup_printf("%s-docker-%d", data->prefix, tuple->offset);
197  xml_docker = create_resource(id, "heartbeat", "docker");
198  free(id);
199 
200  xml_obj = create_xml_node(xml_docker, XML_TAG_ATTR_SETS);
201  crm_xml_set_id(xml_obj, "%s-attributes-%d", data->prefix, tuple->offset);
202 
203  crm_create_nvpair_xml(xml_obj, NULL, "image", data->image);
204  crm_create_nvpair_xml(xml_obj, NULL, "allow_pull", XML_BOOLEAN_TRUE);
205  crm_create_nvpair_xml(xml_obj, NULL, "force_kill", XML_BOOLEAN_FALSE);
206  crm_create_nvpair_xml(xml_obj, NULL, "reuse", XML_BOOLEAN_FALSE);
207 
208  offset += snprintf(buffer+offset, max-offset, " --restart=no");
209 
210  /* Set a container hostname only if we have an IP to map it to.
211  * The user can set -h or --uts=host themselves if they want a nicer
212  * name for logs, but this makes applications happy who need their
213  * hostname to match the IP they bind to.
214  */
215  if (data->ip_range_start != NULL) {
216  offset += snprintf(buffer+offset, max-offset, " -h %s-%d",
217  data->prefix, tuple->offset);
218  }
219 
220  offset += snprintf(buffer+offset, max-offset, " -e PCMK_stderr=1");
221 
222  if(data->docker_network) {
223 // offset += snprintf(buffer+offset, max-offset, " --link-local-ip=%s", tuple->ipaddr);
224  offset += snprintf(buffer+offset, max-offset, " --net=%s", data->docker_network);
225  }
226 
227  if(data->control_port) {
228  offset += snprintf(buffer+offset, max-offset, " -e PCMK_remote_port=%s", data->control_port);
229  } else {
230  offset += snprintf(buffer+offset, max-offset, " -e PCMK_remote_port=%d", DEFAULT_REMOTE_PORT);
231  }
232 
233  for(GListPtr pIter = data->mounts; pIter != NULL; pIter = pIter->next) {
234  container_mount_t *mount = pIter->data;
235 
236  if(mount->flags) {
237  char *source = crm_strdup_printf(
238  "%s/%s-%d", mount->source, data->prefix, tuple->offset);
239 
240  if(doffset > 0) {
241  doffset += snprintf(dbuffer+doffset, dmax-doffset, ",");
242  }
243  doffset += snprintf(dbuffer+doffset, dmax-doffset, "%s", source);
244  offset += snprintf(buffer+offset, max-offset, " -v %s:%s", source, mount->target);
245  free(source);
246 
247  } else {
248  offset += snprintf(buffer+offset, max-offset, " -v %s:%s", mount->source, mount->target);
249  }
250  if(mount->options) {
251  offset += snprintf(buffer+offset, max-offset, ":%s", mount->options);
252  }
253  }
254 
255  for(GListPtr pIter = data->ports; pIter != NULL; pIter = pIter->next) {
256  container_port_t *port = pIter->data;
257 
258  if(tuple->ipaddr) {
259  offset += snprintf(buffer+offset, max-offset, " -p %s:%s:%s",
260  tuple->ipaddr, port->source, port->target);
261  } else if(safe_str_neq(data->docker_network, "host")) {
262  // No need to do port mapping if net=host
263  offset += snprintf(buffer+offset, max-offset, " -p %s:%s", port->source, port->target);
264  }
265  }
266 
267  if(data->docker_run_options) {
268  offset += snprintf(buffer+offset, max-offset, " %s", data->docker_run_options);
269  }
270 
271  if(data->docker_host_options) {
272  offset += snprintf(buffer+offset, max-offset, " %s", data->docker_host_options);
273  }
274 
275  crm_create_nvpair_xml(xml_obj, NULL, "run_opts", buffer);
276  free(buffer);
277 
278  crm_create_nvpair_xml(xml_obj, NULL, "mount_points", dbuffer);
279  free(dbuffer);
280 
281  if(tuple->child) {
282  if(data->docker_run_command) {
283  crm_create_nvpair_xml(xml_obj, NULL,
284  "run_cmd", data->docker_run_command);
285  } else {
286  crm_create_nvpair_xml(xml_obj, NULL,
287  "run_cmd", SBIN_DIR "/pacemaker_remoted");
288  }
289 
290  /* TODO: Allow users to specify their own?
291  *
292  * We just want to know if the container is alive, we'll
293  * monitor the child independently
294  */
295  crm_create_nvpair_xml(xml_obj, NULL, "monitor_cmd", "/bin/true");
296  /* } else if(child && data->untrusted) {
297  * Support this use-case?
298  *
299  * The ability to have resources started/stopped by us, but
300  * unable to set attributes, etc.
301  *
302  * Arguably better to control API access this with ACLs like
303  * "normal" remote nodes
304  *
305  * crm_create_nvpair_xml(xml_obj, NULL,
306  * "run_cmd", "/usr/libexec/pacemaker/lrmd");
307  * crm_create_nvpair_xml(xml_obj, NULL, "monitor_cmd",
308  * "/usr/libexec/pacemaker/lrmd_internal_ctl -c poke");
309  */
310  } else {
311  if(data->docker_run_command) {
312  crm_create_nvpair_xml(xml_obj, NULL,
313  "run_cmd", data->docker_run_command);
314  }
315 
316  /* TODO: Allow users to specify their own?
317  *
318  * We don't know what's in the container, so we just want
319  * to know if it is alive
320  */
321  crm_create_nvpair_xml(xml_obj, NULL, "monitor_cmd", "/bin/true");
322  }
323 
324 
325  xml_obj = create_xml_node(xml_docker, "operations");
326  crm_create_op_xml(xml_obj, ID(xml_docker), "monitor", "60s", NULL);
327 
328  // TODO: Other ops? Timeouts and intervals from underlying resource?
329  crm_log_xml_trace(xml_docker, "Container-docker");
330  if (common_unpack(xml_docker, &tuple->docker, parent, data_set) == FALSE) {
331  return FALSE;
332  }
333  parent->children = g_list_append(parent->children, tuple->docker);
334  return TRUE;
335 }
336 
337 static bool
338 create_rkt_resource(
339  resource_t *parent, container_variant_data_t *data, container_grouping_t *tuple,
340  pe_working_set_t * data_set)
341 {
342  int offset = 0, max = 4096;
343  char *buffer = calloc(1, max+1);
344 
345  int doffset = 0, dmax = 1024;
346  char *dbuffer = calloc(1, dmax+1);
347 
348  char *id = NULL;
349  xmlNode *xml_docker = NULL;
350  xmlNode *xml_obj = NULL;
351 
352  int volid = 0;
353 
354  id = crm_strdup_printf("%s-rkt-%d", data->prefix, tuple->offset);
356  xml_docker = create_resource(id, "heartbeat", "rkt");
357  free(id);
358 
359  xml_obj = create_xml_node(xml_docker, XML_TAG_ATTR_SETS);
360  crm_xml_set_id(xml_obj, "%s-attributes-%d", data->prefix, tuple->offset);
361 
362  crm_create_nvpair_xml(xml_obj, NULL, "image", data->image);
363  crm_create_nvpair_xml(xml_obj, NULL, "allow_pull", "true");
364  crm_create_nvpair_xml(xml_obj, NULL, "force_kill", "false");
365  crm_create_nvpair_xml(xml_obj, NULL, "reuse", "false");
366 
367  /* Set a container hostname only if we have an IP to map it to.
368  * The user can set -h or --uts=host themselves if they want a nicer
369  * name for logs, but this makes applications happy who need their
370  * hostname to match the IP they bind to.
371  */
372  if (data->ip_range_start != NULL) {
373  offset += snprintf(buffer+offset, max-offset, " --hostname=%s-%d",
374  data->prefix, tuple->offset);
375  }
376 
377  offset += snprintf(buffer+offset, max-offset, " --environment=PCMK_stderr=1");
378 
379  if(data->docker_network) {
380 // offset += snprintf(buffer+offset, max-offset, " --link-local-ip=%s", tuple->ipaddr);
381  offset += snprintf(buffer+offset, max-offset, " --net=%s", data->docker_network);
382  }
383 
384  if(data->control_port) {
385  offset += snprintf(buffer+offset, max-offset, " --environment=PCMK_remote_port=%s", data->control_port);
386  } else {
387  offset += snprintf(buffer+offset, max-offset, " --environment=PCMK_remote_port=%d", DEFAULT_REMOTE_PORT);
388  }
389 
390  for(GListPtr pIter = data->mounts; pIter != NULL; pIter = pIter->next) {
391  container_mount_t *mount = pIter->data;
392 
393  if(mount->flags) {
394  char *source = crm_strdup_printf(
395  "%s/%s-%d", mount->source, data->prefix, tuple->offset);
396 
397  if(doffset > 0) {
398  doffset += snprintf(dbuffer+doffset, dmax-doffset, ",");
399  }
400  doffset += snprintf(dbuffer+doffset, dmax-doffset, "%s", source);
401  offset += snprintf(buffer+offset, max-offset, " --volume vol%d,kind=host,source=%s", volid, source);
402  if(mount->options) {
403  offset += snprintf(buffer+offset, max-offset, ",%s", mount->options);
404  }
405  offset += snprintf(buffer+offset, max-offset, " --mount volume=vol%d,target=%s", volid, mount->target);
406  free(source);
407 
408  } else {
409  offset += snprintf(buffer+offset, max-offset, " --volume vol%d,kind=host,source=%s", volid, mount->source);
410  if(mount->options) {
411  offset += snprintf(buffer+offset, max-offset, ",%s", mount->options);
412  }
413  offset += snprintf(buffer+offset, max-offset, " --mount volume=vol%d,target=%s", volid, mount->target);
414  }
415  volid++;
416  }
417 
418  for(GListPtr pIter = data->ports; pIter != NULL; pIter = pIter->next) {
419  container_port_t *port = pIter->data;
420 
421  if(tuple->ipaddr) {
422  offset += snprintf(buffer+offset, max-offset, " --port=%s:%s:%s",
423  port->target, tuple->ipaddr, port->source);
424  } else {
425  offset += snprintf(buffer+offset, max-offset, " --port=%s:%s", port->target, port->source);
426  }
427  }
428 
429  if(data->docker_run_options) {
430  offset += snprintf(buffer+offset, max-offset, " %s", data->docker_run_options);
431  }
432 
433  if(data->docker_host_options) {
434  offset += snprintf(buffer+offset, max-offset, " %s", data->docker_host_options);
435  }
436 
437  crm_create_nvpair_xml(xml_obj, NULL, "run_opts", buffer);
438  free(buffer);
439 
440  crm_create_nvpair_xml(xml_obj, NULL, "mount_points", dbuffer);
441  free(dbuffer);
442 
443  if(tuple->child) {
444  if(data->docker_run_command) {
445  crm_create_nvpair_xml(xml_obj, NULL, "run_cmd", data->docker_run_command);
446  } else {
447  crm_create_nvpair_xml(xml_obj, NULL, "run_cmd", SBIN_DIR"/pacemaker_remoted");
448  }
449 
450  /* TODO: Allow users to specify their own?
451  *
452  * We just want to know if the container is alive, we'll
453  * monitor the child independently
454  */
455  crm_create_nvpair_xml(xml_obj, NULL, "monitor_cmd", "/bin/true");
456  /* } else if(child && data->untrusted) {
457  * Support this use-case?
458  *
459  * The ability to have resources started/stopped by us, but
460  * unable to set attributes, etc.
461  *
462  * Arguably better to control API access this with ACLs like
463  * "normal" remote nodes
464  *
465  * crm_create_nvpair_xml(xml_obj, NULL,
466  * "run_cmd", "/usr/libexec/pacemaker/lrmd");
467  * crm_create_nvpair_xml(xml_obj, NULL, "monitor_cmd",
468  * "/usr/libexec/pacemaker/lrmd_internal_ctl -c poke");
469  */
470  } else {
471  if(data->docker_run_command) {
472  crm_create_nvpair_xml(xml_obj, NULL, "run_cmd",
473  data->docker_run_command);
474  }
475 
476  /* TODO: Allow users to specify their own?
477  *
478  * We don't know what's in the container, so we just want
479  * to know if it is alive
480  */
481  crm_create_nvpair_xml(xml_obj, NULL, "monitor_cmd", "/bin/true");
482  }
483 
484 
485  xml_obj = create_xml_node(xml_docker, "operations");
486  crm_create_op_xml(xml_obj, ID(xml_docker), "monitor", "60s", NULL);
487 
488  // TODO: Other ops? Timeouts and intervals from underlying resource?
489 
490  crm_log_xml_trace(xml_docker, "Container-rkt");
491  if (common_unpack(xml_docker, &tuple->docker, parent, data_set) == FALSE) {
492  return FALSE;
493  }
494  parent->children = g_list_append(parent->children, tuple->docker);
495  return TRUE;
496 }
497 
504 static void
505 disallow_node(resource_t *rsc, const char *uname)
506 {
507  gpointer match = g_hash_table_lookup(rsc->allowed_nodes, uname);
508 
509  if (match) {
510  ((pe_node_t *) match)->weight = -INFINITY;
511  ((pe_node_t *) match)->rsc_discover_mode = pe_discover_never;
512  }
513  if (rsc->children) {
514  GListPtr child;
515 
516  for (child = rsc->children; child != NULL; child = child->next) {
517  disallow_node((resource_t *) (child->data), uname);
518  }
519  }
520 }
521 
522 static bool
523 create_remote_resource(
524  resource_t *parent, container_variant_data_t *data, container_grouping_t *tuple,
525  pe_working_set_t * data_set)
526 {
527  if (tuple->child && valid_network(data)) {
528  GHashTableIter gIter;
529  GListPtr rsc_iter = NULL;
530  node_t *node = NULL;
531  xmlNode *xml_remote = NULL;
532  char *id = crm_strdup_printf("%s-%d", data->prefix, tuple->offset);
533  char *port_s = NULL;
534  const char *uname = NULL;
535  const char *connect_name = NULL;
536 
537  if (remote_id_conflict(id, data_set)) {
538  free(id);
539  // The biggest hammer we have
540  id = crm_strdup_printf("pcmk-internal-%s-remote-%d", tuple->child->id, tuple->offset);
541  CRM_ASSERT(remote_id_conflict(id, data_set) == FALSE);
542  }
543 
544  /* REMOTE_CONTAINER_HACK: Using "#uname" as the server name when the
545  * connection does not have its own IP is a magic string that we use to
546  * support nested remotes (i.e. a bundle running on a remote node).
547  */
548  connect_name = (tuple->ipaddr? tuple->ipaddr : "#uname");
549 
550  if (data->control_port == NULL) {
551  port_s = crm_itoa(DEFAULT_REMOTE_PORT);
552  }
553 
554  /* This sets tuple->docker as tuple->remote's container, which is
555  * similar to what happens with guest nodes. This is how the PE knows
556  * that the bundle node is fenced by recovering docker, and that
557  * remote should be ordered relative to docker.
558  */
559  xml_remote = pe_create_remote_xml(NULL, id, tuple->docker->id,
560  NULL, NULL, "60s", NULL,
561  NULL, connect_name,
562  (data->control_port?
563  data->control_port : port_s));
564  free(port_s);
565 
566  /* Abandon our created ID, and pull the copy from the XML, because we
567  * need something that will get freed during data set cleanup to use as
568  * the node ID and uname.
569  */
570  free(id);
571  id = NULL;
572  uname = ID(xml_remote);
573 
574  /* Ensure a node has been created for the guest (it may have already
575  * been, if it has a permanent node attribute), and ensure its weight is
576  * -INFINITY so no other resources can run on it.
577  */
578  node = pe_find_node(data_set->nodes, uname);
579  if (node == NULL) {
580  node = pe_create_node(uname, uname, "remote", "-INFINITY",
581  data_set);
582  } else {
583  node->weight = -INFINITY;
584  }
586 
587  /* unpack_remote_nodes() ensures that each remote node and guest node
588  * has a pe_node_t entry. Ideally, it would do the same for bundle nodes.
589  * Unfortunately, a bundle has to be mostly unpacked before it's obvious
590  * what nodes will be needed, so we do it just above.
591  *
592  * Worse, that means that the node may have been utilized while
593  * unpacking other resources, without our weight correction. The most
594  * likely place for this to happen is when common_unpack() calls
595  * resource_location() to set a default score in symmetric clusters.
596  * This adds a node *copy* to each resource's allowed nodes, and these
597  * copies will have the wrong weight.
598  *
599  * As a hacky workaround, fix those copies here.
600  *
601  * @TODO Possible alternative: ensure bundles are unpacked before other
602  * resources, so the weight is correct before any copies are made.
603  */
604  for (rsc_iter = data_set->resources; rsc_iter; rsc_iter = rsc_iter->next) {
605  disallow_node((resource_t *) (rsc_iter->data), uname);
606  }
607 
608  tuple->node = node_copy(node);
609  tuple->node->weight = 500;
610  tuple->node->rsc_discover_mode = pe_discover_exclusive;
611 
612  /* Ensure the node shows up as allowed and with the correct discovery set */
613  if (tuple->child->allowed_nodes != NULL) {
614  g_hash_table_destroy(tuple->child->allowed_nodes);
615  }
616  tuple->child->allowed_nodes = g_hash_table_new_full(crm_str_hash, g_str_equal, NULL, g_hash_destroy_str);
617  g_hash_table_insert(tuple->child->allowed_nodes, (gpointer) tuple->node->details->id, node_copy(tuple->node));
618 
619  {
620  node_t *copy = node_copy(tuple->node);
621  copy->weight = -INFINITY;
622  g_hash_table_insert(tuple->child->parent->allowed_nodes, (gpointer) tuple->node->details->id, copy);
623  }
624  crm_log_xml_trace(xml_remote, "Container-remote");
625  if (common_unpack(xml_remote, &tuple->remote, parent, data_set) == FALSE) {
626  return FALSE;
627  }
628 
629  g_hash_table_iter_init(&gIter, tuple->remote->allowed_nodes);
630  while (g_hash_table_iter_next(&gIter, NULL, (void **)&node)) {
631  if(is_remote_node(node)) {
632  /* Remote resources can only run on 'normal' cluster node */
633  node->weight = -INFINITY;
634  }
635  }
636 
637  tuple->node->details->remote_rsc = tuple->remote;
638  tuple->remote->container = tuple->docker; // Ensures is_container_remote_node() functions correctly immediately
639 
640  /* A bundle's #kind is closer to "container" (guest node) than the
641  * "remote" set by pe_create_node().
642  */
643  g_hash_table_insert(tuple->node->details->attrs,
644  strdup(CRM_ATTR_KIND), strdup("container"));
645 
646  /* One effect of this is that setup_container() will add
647  * tuple->remote to tuple->docker's fillers, which will make
648  * rsc_contains_remote_node() true for tuple->docker.
649  *
650  * tuple->child does NOT get added to tuple->docker's fillers.
651  * The only noticeable effect if it did would be for its fail count to
652  * be taken into account when checking tuple->docker's migration
653  * threshold.
654  */
655  parent->children = g_list_append(parent->children, tuple->remote);
656  }
657  return TRUE;
658 }
659 
660 static bool
661 create_container(
662  resource_t *parent, container_variant_data_t *data, container_grouping_t *tuple,
663  pe_working_set_t * data_set)
664 {
665 
666  if (data->type == PE_CONTAINER_TYPE_DOCKER &&
667  create_docker_resource(parent, data, tuple, data_set) == FALSE) {
668  return FALSE;
669  }
670  if (data->type == PE_CONTAINER_TYPE_RKT &&
671  create_rkt_resource(parent, data, tuple, data_set) == FALSE) {
672  return FALSE;
673  }
674 
675  if(create_ip_resource(parent, data, tuple, data_set) == FALSE) {
676  return FALSE;
677  }
678  if(create_remote_resource(parent, data, tuple, data_set) == FALSE) {
679  return FALSE;
680  }
681  if(tuple->child && tuple->ipaddr) {
682  add_hash_param(tuple->child->meta, "external-ip", tuple->ipaddr);
683  }
684 
685  if(tuple->remote) {
686  /*
687  * Allow the remote connection resource to be allocated to a
688  * different node than the one on which the docker container
689  * is active.
690  *
691  * Makes it possible to have remote nodes, running docker
692  * containers with pacemaker_remoted inside in order to start
693  * services inside those containers.
694  */
695  set_bit(tuple->remote->flags, pe_rsc_allow_remote_remotes);
696  }
697 
698  return TRUE;
699 }
700 
701 static void
702 mount_add(container_variant_data_t *container_data, const char *source,
703  const char *target, const char *options, int flags)
704 {
705  container_mount_t *mount = calloc(1, sizeof(container_mount_t));
706 
707  mount->source = strdup(source);
708  mount->target = strdup(target);
709  if (options) {
710  mount->options = strdup(options);
711  }
712  mount->flags = flags;
713  container_data->mounts = g_list_append(container_data->mounts, mount);
714 }
715 
716 static void mount_free(container_mount_t *mount)
717 {
718  free(mount->source);
719  free(mount->target);
720  free(mount->options);
721  free(mount);
722 }
723 
724 static void port_free(container_port_t *port)
725 {
726  free(port->source);
727  free(port->target);
728  free(port);
729 }
730 
731 static container_grouping_t *
732 tuple_for_remote(resource_t *remote)
733 {
734  resource_t *top = remote;
735  container_variant_data_t *container_data = NULL;
736 
737  if (top == NULL) {
738  return NULL;
739  }
740 
741  while (top->parent != NULL) {
742  top = top->parent;
743  }
744 
745  get_container_variant_data(container_data, top);
746  for (GListPtr gIter = container_data->tuples; gIter != NULL; gIter = gIter->next) {
747  container_grouping_t *tuple = (container_grouping_t *)gIter->data;
748  if(tuple->remote == remote) {
749  return tuple;
750  }
751  }
752  CRM_LOG_ASSERT(FALSE);
753  return NULL;
754 }
755 
756 bool
758 {
759  const char *name;
760  const char *value;
761  const char *attr_list[] = {
765  };
766  const char *value_list[] = {
767  "remote",
769  "pacemaker"
770  };
771 
772  if(rsc == NULL) {
773  return FALSE;
774  }
775 
776  name = "addr";
777  value = g_hash_table_lookup(rsc->parameters, name);
778  if (safe_str_eq(value, "#uname") == FALSE) {
779  return FALSE;
780  }
781 
782  for (int lpc = 0; lpc < DIMOF(attr_list); lpc++) {
783  name = attr_list[lpc];
784  value = crm_element_value(rsc->xml, attr_list[lpc]);
785  if (safe_str_eq(value, value_list[lpc]) == FALSE) {
786  return FALSE;
787  }
788  }
789  return TRUE;
790 }
791 
792 const char *
793 container_fix_remote_addr_in(resource_t *rsc, xmlNode *xml, const char *field)
794 {
795  // REMOTE_CONTAINER_HACK: Allow remote nodes that start containers with pacemaker remote inside
796 
797  pe_node_t *node = NULL;
798  container_grouping_t *tuple = NULL;
799 
800  if(container_fix_remote_addr(rsc) == FALSE) {
801  return NULL;
802  }
803 
804  tuple = tuple_for_remote(rsc);
805  if(tuple == NULL) {
806  return NULL;
807  }
808 
809  node = tuple->docker->allocated_to;
810  if (node == NULL) {
811  /* If it won't be running anywhere after the
812  * transition, go with where it's running now.
813  */
814  node = pe__current_node(tuple->docker);
815  }
816 
817  if(node == NULL) {
818  crm_trace("Cannot fix address for %s", tuple->remote->id);
819  return NULL;
820  }
821 
822  crm_trace("Fixing addr for %s on %s", rsc->id, node->details->uname);
823  if(xml != NULL && field != NULL) {
824  crm_xml_add(xml, field, node->details->uname);
825  }
826 
827  return node->details->uname;
828 }
829 
830 gboolean
832 {
833  const char *value = NULL;
834  xmlNode *xml_obj = NULL;
835  xmlNode *xml_resource = NULL;
836  container_variant_data_t *container_data = NULL;
837 
838  CRM_ASSERT(rsc != NULL);
839  pe_rsc_trace(rsc, "Processing resource %s...", rsc->id);
840 
841  container_data = calloc(1, sizeof(container_variant_data_t));
842  rsc->variant_opaque = container_data;
843  container_data->prefix = strdup(rsc->id);
844 
845  xml_obj = first_named_child(rsc->xml, "docker");
846  if (xml_obj != NULL) {
847  container_data->type = PE_CONTAINER_TYPE_DOCKER;
848  } else {
849  xml_obj = first_named_child(rsc->xml, "rkt");
850  if (xml_obj != NULL) {
851  container_data->type = PE_CONTAINER_TYPE_RKT;
852  } else {
853  return FALSE;
854  }
855  }
856 
857  value = crm_element_value(xml_obj, "masters");
858  container_data->masters = crm_parse_int(value, "0");
859  if (container_data->masters < 0) {
860  pe_err("'masters' for %s must be nonnegative integer, using 0",
861  rsc->id);
862  container_data->masters = 0;
863  }
864 
865  value = crm_element_value(xml_obj, "replicas");
866  if ((value == NULL) && (container_data->masters > 0)) {
867  container_data->replicas = container_data->masters;
868  } else {
869  container_data->replicas = crm_parse_int(value, "1");
870  }
871  if (container_data->replicas < 1) {
872  pe_err("'replicas' for %s must be positive integer, using 1", rsc->id);
873  container_data->replicas = 1;
874  }
875 
876  /*
877  * Communication between containers on the same host via the
878  * floating IPs only works if docker is started with:
879  * --userland-proxy=false --ip-masq=false
880  */
881  value = crm_element_value(xml_obj, "replicas-per-host");
882  container_data->replicas_per_host = crm_parse_int(value, "1");
883  if (container_data->replicas_per_host < 1) {
884  pe_err("'replicas-per-host' for %s must be positive integer, using 1",
885  rsc->id);
886  container_data->replicas_per_host = 1;
887  }
888  if (container_data->replicas_per_host == 1) {
890  }
891 
892  container_data->docker_run_command = crm_element_value_copy(xml_obj, "run-command");
893  container_data->docker_run_options = crm_element_value_copy(xml_obj, "options");
894  container_data->image = crm_element_value_copy(xml_obj, "image");
895  container_data->docker_network = crm_element_value_copy(xml_obj, "network");
896 
897  xml_obj = first_named_child(rsc->xml, "network");
898  if(xml_obj) {
899 
900  container_data->ip_range_start = crm_element_value_copy(xml_obj, "ip-range-start");
901  container_data->host_netmask = crm_element_value_copy(xml_obj, "host-netmask");
902  container_data->host_network = crm_element_value_copy(xml_obj, "host-interface");
903  container_data->control_port = crm_element_value_copy(xml_obj, "control-port");
904 
905  for (xmlNode *xml_child = __xml_first_child_element(xml_obj); xml_child != NULL;
906  xml_child = __xml_next_element(xml_child)) {
907 
908  container_port_t *port = calloc(1, sizeof(container_port_t));
909  port->source = crm_element_value_copy(xml_child, "port");
910 
911  if(port->source == NULL) {
912  port->source = crm_element_value_copy(xml_child, "range");
913  } else {
914  port->target = crm_element_value_copy(xml_child, "internal-port");
915  }
916 
917  if(port->source != NULL && strlen(port->source) > 0) {
918  if(port->target == NULL) {
919  port->target = strdup(port->source);
920  }
921  container_data->ports = g_list_append(container_data->ports, port);
922 
923  } else {
924  pe_err("Invalid port directive %s", ID(xml_child));
925  port_free(port);
926  }
927  }
928  }
929 
930  xml_obj = first_named_child(rsc->xml, "storage");
931  for (xmlNode *xml_child = __xml_first_child_element(xml_obj); xml_child != NULL;
932  xml_child = __xml_next_element(xml_child)) {
933 
934  const char *source = crm_element_value(xml_child, "source-dir");
935  const char *target = crm_element_value(xml_child, "target-dir");
936  const char *options = crm_element_value(xml_child, "options");
937  int flags = 0;
938 
939  if (source == NULL) {
940  source = crm_element_value(xml_child, "source-dir-root");
941  flags = 1;
942  }
943 
944  if (source && target) {
945  mount_add(container_data, source, target, options, flags);
946  } else {
947  pe_err("Invalid mount directive %s", ID(xml_child));
948  }
949  }
950 
951  xml_obj = first_named_child(rsc->xml, "primitive");
952  if (xml_obj && valid_network(container_data)) {
953  char *value = NULL;
954  xmlNode *xml_set = NULL;
955 
956  if(container_data->masters > 0) {
957  xml_resource = create_xml_node(NULL, XML_CIB_TAG_MASTER);
958 
959  } else {
960  xml_resource = create_xml_node(NULL, XML_CIB_TAG_INCARNATION);
961  }
962 
963  crm_xml_set_id(xml_resource, "%s-%s", container_data->prefix, xml_resource->name);
964 
965  xml_set = create_xml_node(xml_resource, XML_TAG_META_SETS);
966  crm_xml_set_id(xml_set, "%s-%s-meta", container_data->prefix, xml_resource->name);
967 
968  crm_create_nvpair_xml(xml_set, NULL,
970 
971  value = crm_itoa(container_data->replicas);
972  crm_create_nvpair_xml(xml_set, NULL,
974  free(value);
975 
976  value = crm_itoa(container_data->replicas_per_host);
977  crm_create_nvpair_xml(xml_set, NULL,
979  free(value);
980 
982  (container_data->replicas_per_host > 1)?
984 
985  if(container_data->masters) {
986  value = crm_itoa(container_data->masters);
987  crm_create_nvpair_xml(xml_set, NULL,
988  XML_RSC_ATTR_MASTER_MAX, value);
989  free(value);
990  }
991 
992  //crm_xml_add(xml_obj, XML_ATTR_ID, container_data->prefix);
993  add_node_copy(xml_resource, xml_obj);
994 
995  } else if(xml_obj) {
996  pe_err("Cannot control %s inside %s without either ip-range-start or control-port",
997  rsc->id, ID(xml_obj));
998  return FALSE;
999  }
1000 
1001  if(xml_resource) {
1002  int lpc = 0;
1003  GListPtr childIter = NULL;
1004  resource_t *new_rsc = NULL;
1005  container_port_t *port = NULL;
1006 
1007  int offset = 0, max = 1024;
1008  char *buffer = NULL;
1009 
1010  if (common_unpack(xml_resource, &new_rsc, rsc, data_set) == FALSE) {
1011  pe_err("Failed unpacking resource %s", ID(rsc->xml));
1012  if (new_rsc != NULL && new_rsc->fns != NULL) {
1013  new_rsc->fns->free(new_rsc);
1014  }
1015  return FALSE;
1016  }
1017 
1018  container_data->child = new_rsc;
1019 
1020  /* Currently, we always map the default authentication key location
1021  * into the same location inside the container.
1022  *
1023  * Ideally, we would respect the host's PCMK_authkey_location, but:
1024  * - it may be different on different nodes;
1025  * - the actual connection will do extra checking to make sure the key
1026  * file exists and is readable, that we can't do here on the DC
1027  * - tools such as crm_resource and crm_simulate may not have the same
1028  * environment variables as the cluster, causing operation digests to
1029  * differ
1030  *
1031  * Always using the default location inside the container is fine,
1032  * because we control the pacemaker_remote environment, and it avoids
1033  * having to pass another environment variable to the container.
1034  *
1035  * @TODO A better solution may be to have only pacemaker_remote use the
1036  * environment variable, and have the cluster nodes use a new
1037  * cluster option for key location. This would introduce the limitation
1038  * of the location being the same on all cluster nodes, but that's
1039  * reasonable.
1040  */
1041  mount_add(container_data, DEFAULT_REMOTE_KEY_LOCATION,
1042  DEFAULT_REMOTE_KEY_LOCATION, NULL, 0);
1043 
1044  mount_add(container_data, CRM_LOG_DIR "/bundles", "/var/log", NULL, 1);
1045 
1046  port = calloc(1, sizeof(container_port_t));
1047  if(container_data->control_port) {
1048  port->source = strdup(container_data->control_port);
1049  } else {
1050  /* If we wanted to respect PCMK_remote_port, we could use
1051  * crm_default_remote_port() here and elsewhere in this file instead
1052  * of DEFAULT_REMOTE_PORT.
1053  *
1054  * However, it gains nothing, since we control both the container
1055  * environment and the connection resource parameters, and the user
1056  * can use a different port if desired by setting control-port.
1057  */
1058  port->source = crm_itoa(DEFAULT_REMOTE_PORT);
1059  }
1060  port->target = strdup(port->source);
1061  container_data->ports = g_list_append(container_data->ports, port);
1062 
1063  buffer = calloc(1, max+1);
1064  for(childIter = container_data->child->children; childIter != NULL; childIter = childIter->next) {
1065  container_grouping_t *tuple = calloc(1, sizeof(container_grouping_t));
1066  tuple->child = childIter->data;
1067  tuple->child->exclusive_discover = TRUE;
1068  tuple->offset = lpc++;
1069 
1070  // Ensure the child's notify gets set based on the underlying primitive's value
1071  if(is_set(tuple->child->flags, pe_rsc_notify)) {
1072  set_bit(container_data->child->flags, pe_rsc_notify);
1073  }
1074 
1075  offset += allocate_ip(container_data, tuple, buffer+offset, max-offset);
1076  container_data->tuples = g_list_append(container_data->tuples, tuple);
1077  container_data->attribute_target = g_hash_table_lookup(tuple->child->meta, XML_RSC_ATTR_TARGET);
1078  }
1079  container_data->docker_host_options = buffer;
1080  if(container_data->attribute_target) {
1081  g_hash_table_replace(rsc->meta, strdup(XML_RSC_ATTR_TARGET), strdup(container_data->attribute_target));
1082  g_hash_table_replace(container_data->child->meta, strdup(XML_RSC_ATTR_TARGET), strdup(container_data->attribute_target));
1083  }
1084 
1085  } else {
1086  // Just a naked container, no pacemaker-remote
1087  int offset = 0, max = 1024;
1088  char *buffer = calloc(1, max+1);
1089 
1090  for(int lpc = 0; lpc < container_data->replicas; lpc++) {
1091  container_grouping_t *tuple = calloc(1, sizeof(container_grouping_t));
1092  tuple->offset = lpc;
1093  offset += allocate_ip(container_data, tuple, buffer+offset, max-offset);
1094  container_data->tuples = g_list_append(container_data->tuples, tuple);
1095  }
1096 
1097  container_data->docker_host_options = buffer;
1098  }
1099 
1100  for (GListPtr gIter = container_data->tuples; gIter != NULL; gIter = gIter->next) {
1101  container_grouping_t *tuple = (container_grouping_t *)gIter->data;
1102  if (create_container(rsc, container_data, tuple, data_set) == FALSE) {
1103  pe_err("Failed unpacking resource %s", rsc->id);
1104  rsc->fns->free(rsc);
1105  return FALSE;
1106  }
1107  }
1108 
1109  if(container_data->child) {
1110  rsc->children = g_list_append(rsc->children, container_data->child);
1111  }
1112  return TRUE;
1113 }
1114 
1115 static int
1116 tuple_rsc_active(resource_t *rsc, gboolean all)
1117 {
1118  if (rsc) {
1119  gboolean child_active = rsc->fns->active(rsc, all);
1120 
1121  if (child_active && !all) {
1122  return TRUE;
1123  } else if (!child_active && all) {
1124  return FALSE;
1125  }
1126  }
1127  return -1;
1128 }
1129 
1130 gboolean
1131 container_active(resource_t * rsc, gboolean all)
1132 {
1133  container_variant_data_t *container_data = NULL;
1134  GListPtr iter = NULL;
1135 
1136  get_container_variant_data(container_data, rsc);
1137  for (iter = container_data->tuples; iter != NULL; iter = iter->next) {
1138  container_grouping_t *tuple = (container_grouping_t *)(iter->data);
1139  int rsc_active;
1140 
1141  rsc_active = tuple_rsc_active(tuple->ip, all);
1142  if (rsc_active >= 0) {
1143  return (gboolean) rsc_active;
1144  }
1145 
1146  rsc_active = tuple_rsc_active(tuple->child, all);
1147  if (rsc_active >= 0) {
1148  return (gboolean) rsc_active;
1149  }
1150 
1151  rsc_active = tuple_rsc_active(tuple->docker, all);
1152  if (rsc_active >= 0) {
1153  return (gboolean) rsc_active;
1154  }
1155 
1156  rsc_active = tuple_rsc_active(tuple->remote, all);
1157  if (rsc_active >= 0) {
1158  return (gboolean) rsc_active;
1159  }
1160  }
1161 
1162  /* If "all" is TRUE, we've already checked that no resources were inactive,
1163  * so return TRUE; if "all" is FALSE, we didn't find any active resources,
1164  * so return FALSE.
1165  */
1166  return all;
1167 }
1168 
1178 resource_t *
1179 find_container_child(const resource_t *bundle, const node_t *node)
1180 {
1181  container_variant_data_t *container_data = NULL;
1182  CRM_ASSERT(bundle && node);
1183 
1184  get_container_variant_data(container_data, bundle);
1185  for (GListPtr gIter = container_data->tuples; gIter != NULL;
1186  gIter = gIter->next) {
1187  container_grouping_t *tuple = (container_grouping_t *)gIter->data;
1188 
1189  CRM_ASSERT(tuple && tuple->node);
1190  if (tuple->node->details == node->details) {
1191  return tuple->child;
1192  }
1193  }
1194  return NULL;
1195 }
1196 
1197 static void
1198 print_rsc_in_list(resource_t *rsc, const char *pre_text, long options,
1199  void *print_data)
1200 {
1201  if (rsc != NULL) {
1202  if (options & pe_print_html) {
1203  status_print("<li>");
1204  }
1205  rsc->fns->print(rsc, pre_text, options, print_data);
1206  if (options & pe_print_html) {
1207  status_print("</li>\n");
1208  }
1209  }
1210 }
1211 
1212 static const char*
1213 container_type_as_string(enum container_type t)
1214 {
1215  if (t == PE_CONTAINER_TYPE_DOCKER) {
1216  return PE_CONTAINER_TYPE_DOCKER_S;
1217  } else if (t == PE_CONTAINER_TYPE_RKT) {
1218  return PE_CONTAINER_TYPE_RKT_S;
1219  } else {
1220  return PE_CONTAINER_TYPE_UNKNOWN_S;
1221  }
1222 }
1223 
1224 static void
1225 container_print_xml(resource_t * rsc, const char *pre_text, long options, void *print_data)
1226 {
1227  container_variant_data_t *container_data = NULL;
1228  char *child_text = NULL;
1229  CRM_CHECK(rsc != NULL, return);
1230 
1231  if (pre_text == NULL) {
1232  pre_text = "";
1233  }
1234  child_text = crm_concat(pre_text, " ", ' ');
1235 
1236  get_container_variant_data(container_data, rsc);
1237 
1238  status_print("%s<bundle ", pre_text);
1239  status_print("id=\"%s\" ", rsc->id);
1240 
1241  // Always lowercase the container technology type for use as XML value
1242  status_print("type=\"");
1243  for (const char *c = container_type_as_string(container_data->type);
1244  *c; ++c) {
1245  status_print("%c", tolower(*c));
1246  }
1247  status_print("\" ");
1248 
1249  status_print("image=\"%s\" ", container_data->image);
1250  status_print("unique=\"%s\" ", is_set(rsc->flags, pe_rsc_unique)? "true" : "false");
1251  status_print("managed=\"%s\" ", is_set(rsc->flags, pe_rsc_managed) ? "true" : "false");
1252  status_print("failed=\"%s\" ", is_set(rsc->flags, pe_rsc_failed) ? "true" : "false");
1253  status_print(">\n");
1254 
1255  for (GListPtr gIter = container_data->tuples; gIter != NULL; gIter = gIter->next) {
1256  container_grouping_t *tuple = (container_grouping_t *)gIter->data;
1257 
1258  CRM_ASSERT(tuple);
1259  status_print("%s <replica id=\"%d\">\n", pre_text, tuple->offset);
1260  print_rsc_in_list(tuple->ip, child_text, options, print_data);
1261  print_rsc_in_list(tuple->child, child_text, options, print_data);
1262  print_rsc_in_list(tuple->docker, child_text, options, print_data);
1263  print_rsc_in_list(tuple->remote, child_text, options, print_data);
1264  status_print("%s </replica>\n", pre_text);
1265  }
1266  status_print("%s</bundle>\n", pre_text);
1267  free(child_text);
1268 }
1269 
1270 static void
1271 tuple_print(container_grouping_t * tuple, const char *pre_text, long options, void *print_data)
1272 {
1273  node_t *node = NULL;
1274  resource_t *rsc = tuple->child;
1275 
1276  int offset = 0;
1277  char buffer[LINE_MAX];
1278 
1279  if(rsc == NULL) {
1280  rsc = tuple->docker;
1281  }
1282 
1283  if(tuple->remote) {
1284  offset += snprintf(buffer + offset, LINE_MAX - offset, "%s", rsc_printable_id(tuple->remote));
1285  } else {
1286  offset += snprintf(buffer + offset, LINE_MAX - offset, "%s", rsc_printable_id(tuple->docker));
1287  }
1288  if(tuple->ipaddr) {
1289  offset += snprintf(buffer + offset, LINE_MAX - offset, " (%s)", tuple->ipaddr);
1290  }
1291 
1292  node = pe__current_node(tuple->docker);
1293  common_print(rsc, pre_text, buffer, node, options, print_data);
1294 }
1295 
1296 void
1297 container_print(resource_t * rsc, const char *pre_text, long options, void *print_data)
1298 {
1299  container_variant_data_t *container_data = NULL;
1300  char *child_text = NULL;
1301  CRM_CHECK(rsc != NULL, return);
1302 
1303  if (options & pe_print_xml) {
1304  container_print_xml(rsc, pre_text, options, print_data);
1305  return;
1306  }
1307 
1308  get_container_variant_data(container_data, rsc);
1309 
1310  if (pre_text == NULL) {
1311  pre_text = " ";
1312  }
1313 
1314  status_print("%s%s container%s: %s [%s]%s%s\n",
1315  pre_text, container_type_as_string(container_data->type),
1316  container_data->replicas>1?" set":"", rsc->id, container_data->image,
1317  is_set(rsc->flags, pe_rsc_unique) ? " (unique)" : "",
1318  is_set(rsc->flags, pe_rsc_managed) ? "" : " (unmanaged)");
1319  if (options & pe_print_html) {
1320  status_print("<br />\n<ul>\n");
1321  }
1322 
1323 
1324  for (GListPtr gIter = container_data->tuples; gIter != NULL; gIter = gIter->next) {
1325  container_grouping_t *tuple = (container_grouping_t *)gIter->data;
1326 
1327  CRM_ASSERT(tuple);
1328  if (options & pe_print_html) {
1329  status_print("<li>");
1330  }
1331 
1332  if (is_set(options, pe_print_implicit)) {
1333  child_text = crm_strdup_printf(" %s", pre_text);
1334  if(g_list_length(container_data->tuples) > 1) {
1335  status_print(" %sReplica[%d]\n", pre_text, tuple->offset);
1336  }
1337  if (options & pe_print_html) {
1338  status_print("<br />\n<ul>\n");
1339  }
1340  print_rsc_in_list(tuple->ip, child_text, options, print_data);
1341  print_rsc_in_list(tuple->docker, child_text, options, print_data);
1342  print_rsc_in_list(tuple->remote, child_text, options, print_data);
1343  print_rsc_in_list(tuple->child, child_text, options, print_data);
1344  if (options & pe_print_html) {
1345  status_print("</ul>\n");
1346  }
1347  } else {
1348  child_text = crm_strdup_printf("%s ", pre_text);
1349  tuple_print(tuple, child_text, options, print_data);
1350  }
1351  free(child_text);
1352 
1353  if (options & pe_print_html) {
1354  status_print("</li>\n");
1355  }
1356  }
1357  if (options & pe_print_html) {
1358  status_print("</ul>\n");
1359  }
1360 }
1361 
1362 void
1363 tuple_free(container_grouping_t *tuple)
1364 {
1365  if(tuple == NULL) {
1366  return;
1367  }
1368 
1369  if(tuple->node) {
1370  free(tuple->node);
1371  tuple->node = NULL;
1372  }
1373 
1374  if(tuple->ip) {
1375  free_xml(tuple->ip->xml);
1376  tuple->ip->xml = NULL;
1377  tuple->ip->fns->free(tuple->ip);
1378  tuple->ip = NULL;
1379  }
1380  if(tuple->docker) {
1381  free_xml(tuple->docker->xml);
1382  tuple->docker->xml = NULL;
1383  tuple->docker->fns->free(tuple->docker);
1384  tuple->docker = NULL;
1385  }
1386  if(tuple->remote) {
1387  free_xml(tuple->remote->xml);
1388  tuple->remote->xml = NULL;
1389  tuple->remote->fns->free(tuple->remote);
1390  tuple->remote = NULL;
1391  }
1392  free(tuple->ipaddr);
1393  free(tuple);
1394 }
1395 
1396 void
1398 {
1399  container_variant_data_t *container_data = NULL;
1400  CRM_CHECK(rsc != NULL, return);
1401 
1402  get_container_variant_data(container_data, rsc);
1403  pe_rsc_trace(rsc, "Freeing %s", rsc->id);
1404 
1405  free(container_data->prefix);
1406  free(container_data->image);
1407  free(container_data->control_port);
1408  free(container_data->host_network);
1409  free(container_data->host_netmask);
1410  free(container_data->ip_range_start);
1411  free(container_data->docker_network);
1412  free(container_data->docker_run_options);
1413  free(container_data->docker_run_command);
1414  free(container_data->docker_host_options);
1415 
1416  g_list_free_full(container_data->tuples, (GDestroyNotify)tuple_free);
1417  g_list_free_full(container_data->mounts, (GDestroyNotify)mount_free);
1418  g_list_free_full(container_data->ports, (GDestroyNotify)port_free);
1419  g_list_free(rsc->children);
1420 
1421  if(container_data->child) {
1422  free_xml(container_data->child->xml);
1423  container_data->child->xml = NULL;
1424  container_data->child->fns->free(container_data->child);
1425  }
1426  common_free(rsc);
1427 }
1428 
1429 enum rsc_role_e
1430 container_resource_state(const resource_t * rsc, gboolean current)
1431 {
1432  enum rsc_role_e container_role = RSC_ROLE_UNKNOWN;
1433  return container_role;
1434 }
1435 
1443 int
1445 {
1446  if ((rsc == NULL) || (rsc->variant != pe_container)) {
1447  return 0;
1448  } else {
1449  container_variant_data_t *container_data = NULL;
1450 
1451  get_container_variant_data(container_data, rsc);
1452  return container_data->replicas;
1453  }
1454 }
bool remote_id_conflict(const char *remote_name, pe_working_set_t *data)
Definition: unpack.c:424
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:164
GListPtr nodes
Definition: status.h:111
const char * uname
Definition: status.h:143
xmlNode * xml
Definition: status.h:263
gboolean safe_str_neq(const char *a, const char *b)
Definition: strings.c:150
#define INFINITY
Definition: crm.h:73
int pe_bundle_replicas(const resource_t *rsc)
Get the number of configured replicas in a bundle.
Definition: container.c:1444
#define CRM_ATTR_KIND
Definition: crm.h:90
node_t * node_copy(const node_t *this_node)
Definition: utils.c:127
int weight
Definition: status.h:179
node_t * pe_create_node(const char *id, const char *uname, const char *type, const char *score, pe_working_set_t *data_set)
Definition: unpack.c:362
#define XML_ATTR_TYPE
Definition: msg_xml.h:105
void(* free)(resource_t *)
Definition: complex.h:51
#define XML_BOOLEAN_FALSE
Definition: msg_xml.h:118
gboolean common_unpack(xmlNode *xml_obj, resource_t **rsc, resource_t *parent, pe_working_set_t *data_set)
Definition: complex.c:454
enum pe_obj_types variant
Definition: status.h:269
void common_free(resource_t *rsc)
Definition: complex.c:908
#define status_print(fmt, args...)
Definition: unpack.h:79
int crm_parse_int(const char *text, const char *default_text)
Definition: strings.c:125
char * crm_element_value_copy(xmlNode *data, const char *name)
Definition: xml.c:3902
GListPtr resources
Definition: status.h:112
node_t * pe_find_node(GListPtr node_list, const char *uname)
Definition: status.c:301
#define CRM_LOG_ASSERT(expr)
Definition: logging.h:150
#define clear_bit(word, bit)
Definition: crm_internal.h:191
#define XML_RSC_ATTR_INCARNATION_MAX
Definition: msg_xml.h:213
GListPtr children
Definition: status.h:305
#define XML_RSC_ATTR_TARGET
Definition: msg_xml.h:204
#define pe_rsc_allow_remote_remotes
Definition: status.h:204
void crm_xml_sanitize_id(char *id)
Sanitize a string so it is usable as an XML ID.
Definition: xml.c:3054
char * id
Definition: status.h:261
GHashTable * parameters
Definition: status.h:302
#define DEFAULT_REMOTE_PORT
Definition: lrmd.h:54
#define DEFAULT_REMOTE_KEY_LOCATION
Definition: lrmd.h:52
#define CRM_LOG_DIR
Definition: config.h:59
#define XML_TAG_ATTR_SETS
Definition: msg_xml.h:185
char uname[MAX_NAME]
Definition: internal.h:53
gboolean is_remote_node(node_t *node)
Definition: remote.c:62
struct node_shared_s * details
Definition: status.h:182
#define set_bit(word, bit)
Definition: crm_internal.h:190
#define PCMK_RESOURCE_CLASS_OCF
Definition: services.h:57
xmlNode * pe_create_remote_xml(xmlNode *parent, const char *uname, const char *container_id, const char *migrateable, const char *is_managed, const char *interval, const char *monitor_timeout, const char *start_timeout, const char *server, const char *port)
Definition: remote.c:158
#define XML_ATTR_ID
Definition: msg_xml.h:102
#define XML_CIB_TAG_RESOURCE
Definition: msg_xml.h:196
#define XML_BOOLEAN_TRUE
Definition: msg_xml.h:117
#define pe_rsc_failed
Definition: status.h:206
resource_object_functions_t * fns
Definition: status.h:270
resource_t * find_container_child(const resource_t *bundle, const node_t *node)
Definition: container.c:1179
GHashTable * allowed_nodes
Definition: status.h:296
void * variant_opaque
Definition: status.h:268
#define crm_trace(fmt, args...)
Definition: logging.h:254
xmlNode * add_node_copy(xmlNode *new_parent, xmlNode *xml_node)
Definition: xml.c:2438
xmlNode * crm_create_op_xml(xmlNode *parent, const char *prefix, const char *task, const char *interval, const char *timeout)
Create a CIB XML element for an operation.
Definition: operations.c:455
#define XML_AGENT_ATTR_PROVIDER
Definition: msg_xml.h:255
#define XML_RSC_ATTR_ORDERED
Definition: msg_xml.h:210
#define XML_TAG_META_SETS
Definition: msg_xml.h:186
xmlNode * create_xml_node(xmlNode *parent, const char *name)
Definition: xml.c:2621
const char * crm_element_value(xmlNode *data, const char *name)
Definition: xml.c:5224
unsigned long long flags
Definition: status.h:285
#define XML_RSC_ATTR_INCARNATION_NODEMAX
Definition: msg_xml.h:215
resource_t * parent
Definition: status.h:267
void free_xml(xmlNode *child)
Definition: xml.c:2739
bool container_fix_remote_addr(resource_t *rsc)
Definition: container.c:757
#define XML_RSC_ATTR_UNIQUE
Definition: msg_xml.h:221
gboolean(* active)(resource_t *, gboolean)
Definition: complex.h:48
void common_print(resource_t *rsc, const char *pre_text, const char *name, node_t *node, long options, void *print_data)
Definition: native.c:487
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Definition: xml.c:2523
#define XML_RSC_ATTR_MASTER_MAX
Definition: msg_xml.h:216
void(* print)(resource_t *, const char *, long, void *)
Definition: complex.h:47
#define pe_rsc_unique
Definition: status.h:194
gboolean container_unpack(resource_t *rsc, pe_working_set_t *data_set)
Definition: container.c:831
#define SBIN_DIR
Definition: config.h:685
GHashTable * meta
Definition: status.h:301
enum rsc_role_e container_resource_state(const resource_t *rsc, gboolean current)
Definition: container.c:1430
void tuple_free(container_grouping_t *tuple)
Definition: container.c:1363
#define XML_CIB_TAG_INCARNATION
Definition: msg_xml.h:198
void add_hash_param(GHashTable *hash, const char *name, const char *value)
Definition: common.c:423
void crm_xml_set_id(xmlNode *xml, const char *format,...) __attribute__((__format__(__printf__
gboolean container_active(resource_t *rsc, gboolean all)
Definition: container.c:1131
xmlNode * crm_create_nvpair_xml(xmlNode *parent, const char *id, const char *name, const char *value)
Create an XML name/value pair.
Definition: xml.c:4890
#define DIMOF(a)
Definition: crm.h:29
#define pe_rsc_managed
Definition: status.h:189
#define crm_str_hash
Definition: util.h:73
#define CRM_ASSERT(expr)
Definition: error.h:35
char data[0]
Definition: internal.h:58
rsc_role_e
Definition: common.h:81
#define XML_CIB_TAG_MASTER
Definition: msg_xml.h:199
int rsc_discover_mode
Definition: status.h:183
xmlNode * first_named_child(xmlNode *parent, const char *name)
Definition: xml.c:5112
#define crm_log_xml_trace(xml, text)
Definition: logging.h:262
Definition: status.h:178
#define pe_rsc_trace(rsc, fmt, args...)
Definition: internal.h:16
char * crm_concat(const char *prefix, const char *suffix, char join)
Definition: strings.c:32
#define ID(x)
Definition: msg_xml.h:447
#define pe_err(fmt...)
Definition: internal.h:18
char * crm_itoa(int an_int)
Definition: strings.c:60
#define safe_str_eq(a, b)
Definition: util.h:72
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
const char * container_fix_remote_addr_in(resource_t *rsc, xmlNode *xml, const char *field)
Definition: container.c:793
void container_print(resource_t *rsc, const char *pre_text, long options, void *print_data)
Definition: container.c:1297
GList * GListPtr
Definition: crm.h:210
#define pe_rsc_notify
Definition: status.h:193
void g_hash_destroy_str(gpointer data)
Definition: strings.c:74
const char * rsc_printable_id(resource_t *rsc)
Definition: utils.c:2139
uint64_t flags
Definition: remote.c:156
#define XML_AGENT_ATTR_CLASS
Definition: msg_xml.h:254
void container_free(resource_t *rsc)
Definition: container.c:1397