1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <cf3.defs.h>
#include <files_lib.h>
#include <actuator.h>
#include <eval_context.h>
#include <promises.h>
#include <vars.h>
#include <conversion.h>
#include <attributes.h>
#include <locks.h>
#include <policy.h>
#include <scope.h>
#include <ornaments.h>
#include <verify_environments.h>
#ifdef HAVE_LIBVIRT
/*****************************************************************************/
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
/*****************************************************************************/
enum cfhypervisors
{
cfv_virt_xen,
cfv_virt_kvm,
cfv_virt_esx,
cfv_virt_vbox,
cfv_virt_test,
cfv_virt_xen_net,
cfv_virt_kvm_net,
cfv_virt_esx_net,
cfv_virt_test_net,
cfv_zone,
cfv_ec2,
cfv_eucalyptus,
cfv_none
};
/*****************************************************************************/
virConnectPtr CFVC[cfv_none];
#define CF_MAX_CONCURRENT_ENVIRONMENTS 256
int CF_RUNNING[CF_MAX_CONCURRENT_ENVIRONMENTS];
char *CF_SUSPENDED[CF_MAX_CONCURRENT_ENVIRONMENTS];
/*****************************************************************************/
static bool EnvironmentsSanityChecks(const Attributes *a, const Promise *pp);
static PromiseResult VerifyEnvironments(EvalContext *ctx, const Attributes *a, const Promise *pp);
static PromiseResult VerifyVirtDomain(EvalContext *ctx, char *uri, enum cfhypervisors envtype, const Attributes *a, const Promise *pp);
static PromiseResult VerifyVirtNetwork(EvalContext *ctx, char *uri, enum cfhypervisors envtype, const Attributes *a, const Promise *pp);
static PromiseResult CreateVirtDom(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp);
static PromiseResult DeleteVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp);
static PromiseResult RunningVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp);
static PromiseResult SuspendedVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp);
static PromiseResult DownVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp);
static void EnvironmentErrorHandler(void);
static void ShowRunList(virConnectPtr vc);
static void ShowDormant(void);
static PromiseResult CreateVirtNetwork(EvalContext *ctx, virConnectPtr vc, char **networks, const Attributes *a, const Promise *pp);
static PromiseResult DeleteVirtNetwork(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp);
static enum cfhypervisors Str2Hypervisors(char *s);
/*****************************************************************************/
void NewEnvironmentsContext(void)
{
int i;
for (i = 0; i < cfv_none; i++)
{
CFVC[i] = NULL;
}
}
void DeleteEnvironmentsContext(void)
{
int i;
for (i = 0; i < cfv_none; i++)
{
if (CFVC[i] != NULL)
{
virConnectClose(CFVC[i]);
CFVC[i] = NULL;
}
}
}
/*****************************************************************************/
PromiseResult VerifyEnvironmentsPromise(EvalContext *ctx, const Promise *pp)
{
CfLock thislock;
Attributes a = GetEnvironmentsAttributes(ctx, pp);
PromiseResult result = PROMISE_RESULT_NOOP;
if (EnvironmentsSanityChecks(&a, pp))
{
thislock = AcquireLock(ctx, "virtual", VUQNAME, CFSTARTTIME, a.transaction.ifelapsed, a.transaction.expireafter, pp, false);
if (thislock.lock == NULL)
{
return PROMISE_RESULT_NOOP;
}
PromiseBanner(ctx, pp);
bool excluded = false;
Promise *pexp = ExpandDeRefPromise(ctx, pp, &excluded);
if (excluded)
{
result = PROMISE_RESULT_SKIPPED;
}
else
{
result = VerifyEnvironments(ctx, &a, pp);
}
PromiseDestroy(pexp);
YieldCurrentLock(thislock);
}
return result;
}
/*****************************************************************************/
static bool EnvironmentsSanityChecks(const Attributes *a, const Promise *pp)
{
assert(a != NULL);
const Environments env = a->env;
if (env.spec)
{
if (env.cpus != CF_NOINT || env.memory != CF_NOINT || env.disk != CF_NOINT)
{
Log(LOG_LEVEL_ERR, "Conflicting promise of both a spec and cpu/memory/disk resources");
return false;
}
}
if (env.host == NULL)
{
Log(LOG_LEVEL_ERR, "No environment_host defined for environment promise");
PromiseRef(LOG_LEVEL_ERR, pp);
return false;
}
switch (Str2Hypervisors(env.type))
{
case cfv_virt_xen_net:
case cfv_virt_kvm_net:
case cfv_virt_esx_net:
case cfv_virt_test_net:
if (env.cpus != CF_NOINT || env.memory != CF_NOINT || env.disk != CF_NOINT || env.name
|| env.addresses)
{
Log(LOG_LEVEL_ERR, "Network environment promises computational resources (%d,%d,%d,%s)", env.cpus,
env.memory, env.disk, env.name);
PromiseRef(LOG_LEVEL_ERR, pp);
}
break;
default:
break;
}
return true;
}
/*****************************************************************************/
static PromiseResult VerifyEnvironments(EvalContext *ctx, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
char hyper_uri[CF_MAXVARSIZE];
enum cfhypervisors envtype = cfv_none;
Environments env= a->env;
switch (Str2Hypervisors(env.type))
{
case cfv_virt_xen:
case cfv_virt_xen_net:
snprintf(hyper_uri, CF_MAXVARSIZE - 1, "xen:///");
envtype = cfv_virt_xen;
break;
case cfv_virt_kvm:
case cfv_virt_kvm_net:
snprintf(hyper_uri, CF_MAXVARSIZE - 1, "qemu:///session");
envtype = cfv_virt_kvm;
break;
case cfv_virt_esx:
case cfv_virt_esx_net:
snprintf(hyper_uri, CF_MAXVARSIZE - 1, "esx://127.0.0.1");
envtype = cfv_virt_esx;
break;
case cfv_virt_test:
case cfv_virt_test_net:
snprintf(hyper_uri, CF_MAXVARSIZE - 1, "test:///default");
envtype = cfv_virt_test;
break;
case cfv_virt_vbox:
snprintf(hyper_uri, CF_MAXVARSIZE - 1, "vbox:///session");
envtype = cfv_virt_vbox;
break;
case cfv_zone:
snprintf(hyper_uri, CF_MAXVARSIZE - 1, "solaris_zone");
envtype = cfv_zone;
break;
default:
Log(LOG_LEVEL_ERR, "Environment type '%s' not currently supported", env.type);
return PROMISE_RESULT_NOOP;
break;
}
Log(LOG_LEVEL_VERBOSE, "Selecting environment type '%s' '%s'", env.type, hyper_uri);
ClassRef environment_host_ref = ClassRefParse(env.host);
if (!EvalContextClassGet(ctx, environment_host_ref.ns, environment_host_ref.name))
{
switch (env.state)
{
case ENVIRONMENT_STATE_CREATE:
case ENVIRONMENT_STATE_RUNNING:
Log(LOG_LEVEL_VERBOSE,
"This host ''%s' is not the promised host for the environment '%s', so setting its intended state to 'down'",
VFQNAME, env.host);
env.state = ENVIRONMENT_STATE_DOWN;
break;
default:
Log(LOG_LEVEL_VERBOSE,
"This is not the promised host for the environment, but it does not promise a run state, so take promise as valid");
}
}
ClassRefDestroy(environment_host_ref);
virInitialize();
PromiseResult result = PROMISE_RESULT_NOOP;
#if defined(__linux__)
switch (Str2Hypervisors(env.type))
{
case cfv_virt_xen:
case cfv_virt_kvm:
case cfv_virt_esx:
case cfv_virt_vbox:
case cfv_virt_test:
result = PromiseResultUpdate(result, VerifyVirtDomain(ctx, hyper_uri, envtype, a, pp));
break;
case cfv_virt_xen_net:
case cfv_virt_kvm_net:
case cfv_virt_esx_net:
case cfv_virt_test_net:
result = PromiseResultUpdate(result, VerifyVirtNetwork(ctx, hyper_uri, envtype, a, pp));
break;
case cfv_ec2:
break;
case cfv_eucalyptus:
break;
default:
break;
}
#elif defined(__APPLE__)
switch (Str2Hypervisors(env.type))
{
case cfv_virt_vbox:
case cfv_virt_test:
result = PromiseResultUpdate(result, VerifyVirtDomain(ctx, hyper_uri, envtype, a, pp));
break;
case cfv_virt_xen_net:
case cfv_virt_kvm_net:
case cfv_virt_esx_net:
case cfv_virt_test_net:
result = PromiseResultUpdate(result, VerifyVirtNetwork(ctx, hyper_uri, envtype, a, pp));
break;
default:
break;
}
#elif defined(__sun)
switch (Str2Hypervisors(env.type))
{
case cfv_zone:
result = PromiseResultUpdate(result, VerifyZone(a, pp));
break;
default:
break;
}
#else
Log(LOG_LEVEL_VERBOSE, "Unable to resolve an environment supervisor/monitor for this platform, aborting");
#endif
return result;
}
/*****************************************************************************/
/* Level */
/*****************************************************************************/
static PromiseResult VerifyVirtDomain(EvalContext *ctx, char *uri, enum cfhypervisors envtype, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
int num, i;
/* set up the library error handler */
virSetErrorFunc(NULL, (void *) EnvironmentErrorHandler);
if (CFVC[envtype] == NULL)
{
if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL)
{
Log(LOG_LEVEL_ERR, "Failed to connect to virtualization monitor '%s'", uri);
return PROMISE_RESULT_NOOP;
}
}
for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++)
{
CF_RUNNING[i] = -1;
CF_SUSPENDED[i] = NULL;
}
num = virConnectListDomains(CFVC[envtype], CF_RUNNING, CF_MAX_CONCURRENT_ENVIRONMENTS);
Log(LOG_LEVEL_VERBOSE, "Found %d running guest environments on this host (including enclosure)", num);
ShowRunList(CFVC[envtype]);
num = virConnectListDefinedDomains(CFVC[envtype], CF_SUSPENDED, CF_MAX_CONCURRENT_ENVIRONMENTS);
Log(LOG_LEVEL_VERBOSE, "Found %d dormant guest environments on this host", num);
ShowDormant();
PromiseResult result = PROMISE_RESULT_NOOP;
switch (a->env.state)
{
case ENVIRONMENT_STATE_CREATE:
result = PromiseResultUpdate(result, CreateVirtDom(ctx, CFVC[envtype], a, pp));
break;
case ENVIRONMENT_STATE_DELETE:
result = PromiseResultUpdate(result, DeleteVirt(ctx, CFVC[envtype], a, pp));
break;
case ENVIRONMENT_STATE_RUNNING:
result = PromiseResultUpdate(result, RunningVirt(ctx, CFVC[envtype], a, pp));
break;
case ENVIRONMENT_STATE_SUSPENDED:
result = PromiseResultUpdate(result, SuspendedVirt(ctx, CFVC[envtype], a, pp));
break;
case ENVIRONMENT_STATE_DOWN:
result = PromiseResultUpdate(result, DownVirt(ctx, CFVC[envtype], a, pp));
break;
default:
Log(LOG_LEVEL_INFO, "No state specified for this environment");
break;
}
return result;
}
/*****************************************************************************/
static PromiseResult VerifyVirtNetwork(EvalContext *ctx, char *uri, enum cfhypervisors envtype, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
int num, i;
char *networks[CF_MAX_CONCURRENT_ENVIRONMENTS];
if (CFVC[envtype] == NULL)
{
if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL)
{
Log(LOG_LEVEL_ERR, "Failed to connect to virtualization monitor '%s'", uri);
return PROMISE_RESULT_NOOP;
}
}
for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++)
{
networks[i] = NULL;
}
num = virConnectListNetworks(CFVC[envtype], networks, CF_MAX_CONCURRENT_ENVIRONMENTS);
Log(LOG_LEVEL_VERBOSE, "Detected %d active networks", num);
PromiseResult result = PROMISE_RESULT_NOOP;
switch (a->env.state)
{
case ENVIRONMENT_STATE_CREATE:
result = PromiseResultUpdate(result, CreateVirtNetwork(ctx, CFVC[envtype], networks, a, pp));
break;
case ENVIRONMENT_STATE_DELETE:
result = PromiseResultUpdate(result, DeleteVirtNetwork(ctx, CFVC[envtype], a, pp));
break;
default:
Log(LOG_LEVEL_INFO, "No recognized state specified for this network environment");
break;
}
return result;
}
/*****************************************************************************/
/* Level */
/*****************************************************************************/
static PromiseResult CreateVirtDom(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
int alloc_file = false;
char *xml_file;
const char *name;
char defaultxml[CF_MAXVARSIZE];
virDomainPtr dom;
int i;
snprintf(defaultxml, CF_MAXVARSIZE - 1,
"<domain type='test'>"
" <name>%s</name>"
" <memory>8388608</memory>"
" <currentMemory>2097152</currentMemory>"
" <vcpu>2</vcpu>" " <os>" " <type>hvm</type>" " </os>" "</domain>", pp->promiser);
for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++)
{
if (CF_RUNNING[i] > 0)
{
dom = virDomainLookupByID(vc, CF_RUNNING[i]);
name = virDomainGetName(dom);
if (name && strcmp(name, pp->promiser) == 0)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Found a running environment called '%s' - promise kept",
name);
return PROMISE_RESULT_NOOP;
}
virDomainFree(dom);
}
}
for (i = 0; CF_SUSPENDED[i] != NULL; i++)
{
if (strcmp(CF_SUSPENDED[i], pp->promiser) == 0)
{
Log(LOG_LEVEL_INFO, "Found an existing, but suspended, environment id = %s, called '%s'",
CF_SUSPENDED[i], CF_SUSPENDED[i]);
}
}
const Environments env = a->env;
if (env.spec)
{
xml_file = xstrdup(env.spec);
alloc_file = true;
}
else
{
Log(LOG_LEVEL_VERBOSE, "No spec file is promised, so reverting to default settings");
xml_file = defaultxml;
}
PromiseResult result = PROMISE_RESULT_NOOP;
if ((dom = virDomainCreateXML(vc, xml_file, 0)))
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Created a virtual domain '%s'", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
if (env.cpus != CF_NOINT)
{
int maxcpus;
if ((maxcpus = virConnectGetMaxVcpus(vc, virConnectGetType(vc))) == -1)
{
Log(LOG_LEVEL_VERBOSE, "Can't determine the available CPU resources");
}
else
{
if (env.cpus > maxcpus)
{
Log(LOG_LEVEL_INFO,
"The promise to allocate %d CPUs in domain '%s' cannot be kept - only %d exist on the host",
env.cpus, pp->promiser, maxcpus);
}
else if (virDomainSetVcpus(dom, (unsigned int) env.cpus) == -1)
{
Log(LOG_LEVEL_INFO, "Unable to adjust CPU count to %d", env.cpus);
}
else
{
Log(LOG_LEVEL_INFO, "Verified that environment CPU count is now %d", env.cpus);
}
}
}
if (env.memory != CF_NOINT)
{
unsigned long maxmem;
if ((maxmem = virDomainGetMaxMemory(dom)) == -1)
{
Log(LOG_LEVEL_VERBOSE, "Can't determine the available CPU resources");
}
else
{
if (virDomainSetMaxMemory(dom, (unsigned long) env.memory) == -1)
{
Log(LOG_LEVEL_INFO, " Unable to set the memory limit to %d", env.memory);
}
else
{
Log(LOG_LEVEL_INFO, "Setting the memory limit to %d", env.memory);
}
if (virDomainSetMemory(dom, (unsigned long) env.memory) == -1)
{
Log(LOG_LEVEL_INFO, " Unable to set the current memory to %d", env.memory);
}
}
}
if (env.disk != CF_NOINT)
{
Log(LOG_LEVEL_VERBOSE, "Info: env_disk parameter is not currently supported on this platform");
}
virDomainFree(dom);
}
else
{
virErrorPtr vp;
vp = virGetLastError();
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a,
"Failed to create a virtual domain '%s' - check spec for errors: '%s'", pp->promiser, vp->message);
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
Log(LOG_LEVEL_VERBOSE, "Quoted spec file: %s", xml_file);
}
if (alloc_file)
{
free(xml_file);
}
return result;
}
/*****************************************************************************/
static PromiseResult DeleteVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
virDomainPtr dom;
dom = virDomainLookupByName(vc, pp->promiser);
PromiseResult result = PROMISE_RESULT_NOOP;
if (dom)
{
if (virDomainDestroy(dom) == -1)
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Failed to delete virtual domain '%s'", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
}
else
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Deleted virtual domain '%s'", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
}
virDomainFree(dom);
}
else
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "No such virtual domain called '%s' - promise kept", pp->promiser);
}
return result;
}
/*****************************************************************************/
static PromiseResult RunningVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
virDomainPtr dom;
virDomainInfo info;
dom = virDomainLookupByName(vc, pp->promiser);
PromiseResult result = PROMISE_RESULT_NOOP;
if (dom)
{
if (virDomainGetInfo(dom, &info) == -1)
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to probe virtual domain '%s'", pp->promiser);
virDomainFree(dom);
return PROMISE_RESULT_FAIL;
}
switch (info.state)
{
case VIR_DOMAIN_RUNNING:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' running - promise kept", pp->promiser);
break;
case VIR_DOMAIN_BLOCKED:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a,
"Virtual domain '%s' running but waiting for a resource - promise kept as far as possible",
pp->promiser);
break;
case VIR_DOMAIN_SHUTDOWN:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is shutting down", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
Log(LOG_LEVEL_VERBOSE,
"It is currently impossible to know whether it will reboot or not - deferring promise check until it has completed its shutdown");
break;
case VIR_DOMAIN_PAUSED:
if (virDomainResume(dom) == -1)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' failed to resume after suspension",
pp->promiser);
virDomainFree(dom);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
return result;
}
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' was suspended, resuming", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
break;
case VIR_DOMAIN_SHUTOFF:
if (virDomainCreate(dom) == -1)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' failed to resume after halting",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
virDomainFree(dom);
return result;
}
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' was inactive, booting...", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
break;
case VIR_DOMAIN_CRASHED:
if (virDomainReboot(dom, 0) == -1)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' has crashed and rebooting failed",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
virDomainFree(dom);
return result;
}
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' has crashed, rebooting...", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
break;
default:
Log(LOG_LEVEL_VERBOSE, "Virtual domain '%s' is reported as having no state, whatever that means",
pp->promiser);
break;
}
const Environments env = a->env;
if (env.cpus > 0)
{
if (virDomainSetVcpus(dom, env.cpus) == -1)
{
Log(LOG_LEVEL_INFO, " Unable to set the number of cpus to %d", env.cpus);
}
else
{
Log(LOG_LEVEL_INFO, "Setting the number of virtual cpus to %d", env.cpus);
}
}
if (env.memory != CF_NOINT)
{
if (virDomainSetMaxMemory(dom, (unsigned long) env.memory) == -1)
{
Log(LOG_LEVEL_INFO, " Unable to set the memory limit to %d", env.memory);
}
else
{
Log(LOG_LEVEL_INFO, "Setting the memory limit to %d", env.memory);
}
if (virDomainSetMemory(dom, (unsigned long) env.memory) == -1)
{
Log(LOG_LEVEL_INFO, " Unable to set the current memory to %d", env.memory);
}
}
if (env.disk != CF_NOINT)
{
Log(LOG_LEVEL_VERBOSE, "Info: env_disk parameter is not currently supported on this platform");
}
virDomainFree(dom);
}
else
{
Log(LOG_LEVEL_VERBOSE, "Virtual domain '%s' cannot be located, attempting to recreate", pp->promiser);
result = PromiseResultUpdate(result, CreateVirtDom(ctx, vc, a, pp));
}
return result;
}
/*****************************************************************************/
static PromiseResult SuspendedVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
virDomainPtr dom;
virDomainInfo info;
dom = virDomainLookupByName(vc, pp->promiser);
PromiseResult result = PROMISE_RESULT_NOOP;
if (dom)
{
if (virDomainGetInfo(dom, &info) == -1)
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to probe virtual domain '%s'", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
virDomainFree(dom);
return result;
}
switch (info.state)
{
case VIR_DOMAIN_BLOCKED:
case VIR_DOMAIN_RUNNING:
if (virDomainSuspend(dom) == -1)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' failed to suspend", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
virDomainFree(dom);
return result;
}
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' running, suspending", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
break;
case VIR_DOMAIN_SHUTDOWN:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is shutting down", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
Log(LOG_LEVEL_VERBOSE,
"It is currently impossible to know whether it will reboot or not - deferring promise check until it has completed its shutdown");
break;
case VIR_DOMAIN_PAUSED:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' is suspended - promise kept",
pp->promiser);
break;
case VIR_DOMAIN_SHUTOFF:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' is down - promise kept", pp->promiser);
break;
case VIR_DOMAIN_CRASHED:
if (virDomainSuspend(dom) == -1)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is crashed has failed to suspend",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
virDomainFree(dom);
return result;
}
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' is in a crashed state, suspending",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
break;
default:
Log(LOG_LEVEL_VERBOSE, "Virtual domain '%s' is reported as having no state, whatever that means",
pp->promiser);
break;
}
virDomainFree(dom);
}
else
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' cannot be found - take promise as kept",
pp->promiser);
}
return result;
}
/*****************************************************************************/
static PromiseResult DownVirt(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp)
{
virDomainPtr dom;
virDomainInfo info;
dom = virDomainLookupByName(vc, pp->promiser);
PromiseResult result = PROMISE_RESULT_NOOP;
if (dom)
{
if (virDomainGetInfo(dom, &info) == -1)
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to probe virtual domain '%s'", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
virDomainFree(dom);
return result;
}
switch (info.state)
{
case VIR_DOMAIN_BLOCKED:
case VIR_DOMAIN_RUNNING:
if (virDomainShutdown(dom) == -1)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' failed to shutdown",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
virDomainFree(dom);
return result;
}
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' running, terminating", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
break;
case VIR_DOMAIN_SHUTOFF:
case VIR_DOMAIN_SHUTDOWN:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' is down - promise kept", pp->promiser);
break;
case VIR_DOMAIN_PAUSED:
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is suspended - ignoring promise",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
break;
case VIR_DOMAIN_CRASHED:
if (virDomainSuspend(dom) == -1)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_INTERRUPTED, pp, a, "Virtual domain '%s' is crashed and failed to shutdown",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_INTERRUPTED);
virDomainFree(dom);
return result;
}
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_CHANGE, pp, a, "Virtual domain '%s' is in a crashed state, terminating",
pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
break;
default:
Log(LOG_LEVEL_VERBOSE, "Virtual domain '%s' is reported as having no state, whatever that means",
pp->promiser);
break;
}
virDomainFree(dom);
}
else
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Virtual domain '%s' cannot be found - take promise as kept",
pp->promiser);
}
return result;
}
/*****************************************************************************/
static PromiseResult CreateVirtNetwork(EvalContext *ctx, virConnectPtr vc, char **networks, const Attributes *a, const Promise *pp)
{
assert(a != NULL);
virNetworkPtr network;
char *xml_file;
char defaultxml[CF_MAXVARSIZE];
int i, found = false;
snprintf(defaultxml, CF_MAXVARSIZE - 1,
"<network>"
"<name>%s</name>"
"<bridge name=\"virbr0\" />"
"<forward mode=\"nat\"/>"
"<ip address=\"192.168.122.1\" netmask=\"255.255.255.0\">"
"<dhcp>"
"<range start=\"192.168.122.2\" end=\"192.168.122.254\" />" "</dhcp>" "</ip>" "</network>", pp->promiser);
for (i = 0; networks[i] != NULL; i++)
{
Log(LOG_LEVEL_VERBOSE, "Discovered a running network '%s'", networks[i]);
if (strcmp(networks[i], pp->promiser) == 0)
{
found = true;
}
}
if (found)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Network '%s' exists - promise kept", pp->promiser);
return PROMISE_RESULT_NOOP;
}
if (a->env.spec)
{
xml_file = xstrdup(a->env.spec);
}
else
{
xml_file = xstrdup(defaultxml);
}
PromiseResult result = PROMISE_RESULT_NOOP;
if ((network = virNetworkCreateXML(vc, xml_file)) == NULL)
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Unable to create network '%s'", pp->promiser);
free(xml_file);
return PROMISE_RESULT_FAIL;
}
else
{
cfPS(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_CHANGE, pp, a, "Created network '%s' - promise repaired", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
}
free(xml_file);
virNetworkFree(network);
return result;
}
/*****************************************************************************/
static PromiseResult DeleteVirtNetwork(EvalContext *ctx, virConnectPtr vc, const Attributes *a, const Promise *pp)
{
virNetworkPtr network;
if ((network = virNetworkLookupByName(vc, pp->promiser)) == NULL)
{
cfPS(ctx, LOG_LEVEL_VERBOSE, PROMISE_RESULT_NOOP, pp, a, "Couldn't find a network called '%s' - promise assumed kept",
pp->promiser);
return PROMISE_RESULT_NOOP;
}
PromiseResult result = PROMISE_RESULT_NOOP;
if (virNetworkDestroy(network) == 0)
{
cfPS(ctx, LOG_LEVEL_INFO, PROMISE_RESULT_CHANGE, pp, a, "Deleted network '%s' - promise repaired", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
}
else
{
cfPS(ctx, LOG_LEVEL_ERR, PROMISE_RESULT_FAIL, pp, a, "Network deletion of '%s' failed", pp->promiser);
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
}
virNetworkFree(network);
return result;
}
/*****************************************************************************/
/* Level */
/*****************************************************************************/
static void EnvironmentErrorHandler()
{
}
/*****************************************************************************/
static void ShowRunList(virConnectPtr vc)
{
int i;
virDomainPtr dom;
const char *name;
for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++)
{
if (CF_RUNNING[i] > 0)
{
if ((dom = virDomainLookupByID(vc, CF_RUNNING[i])))
{
Log(LOG_LEVEL_VERBOSE, "Found a running virtual domain with id %d", CF_RUNNING[i]);
}
if ((name = virDomainGetName(dom)))
{
Log(LOG_LEVEL_VERBOSE, "Found a running virtual domain called '%s'", name);
}
virDomainFree(dom);
}
}
}
/*****************************************************************************/
static void ShowDormant(void)
{
int i;
for (i = 0; CF_SUSPENDED[i] != NULL; i++)
{
Log(LOG_LEVEL_VERBOSE, "Found a suspended, domain environment called '%s'", CF_SUSPENDED[i]);
}
}
/*****************************************************************************/
static enum cfhypervisors Str2Hypervisors(char *s)
{
static char *names[] = { "xen", "kvm", "esx", "vbox", "test",
"xen_net", "kvm_net", "esx_net", "test_net",
"zone", "ec2", "eucalyptus", NULL
};
int i;
if (s == NULL)
{
return cfv_virt_test;
}
for (i = 0; names[i] != NULL; i++)
{
if (s && (strcmp(s, names[i]) == 0))
{
return (enum cfhypervisors) i;
}
}
return (enum cfhypervisors) i;
}
/*****************************************************************************/
#else /* !HAVE_LIBVIRT */
void NewEnvironmentsContext(void)
{
}
void DeleteEnvironmentsContext(void)
{
}
PromiseResult VerifyEnvironmentsPromise(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Promise *pp)
{
return PROMISE_RESULT_NOOP;
}
#endif
|