Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement defaults: and meta: promises for bundles

  • Loading branch information...
commit b37c4fda91b778d4ec4f3eb6b290b23a07634992 1 parent bb9f8bc
Mark Burgess authored
View
84 examples/unit_defaults.cf
@@ -0,0 +1,84 @@
+
+# Copyright (C) Cfengine AS
+
+# This file is part of Cfengine 3 - written and maintained by Cfengine 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 Commerical Open Source License
+# (COSL) may apply to this file if you as a licensee so wish it. See
+# included file COSL.txt.
+
+#
+# Default values for variables and parameters, introduced 3.4.0
+#
+
+body common control
+
+{
+bundlesequence => { "example" };
+}
+
+###########################################################
+
+bundle agent example
+
+{
+defaults:
+
+ "X" string => "I am a default value";
+ "Y" slist => { "I am a default list item 1", "I am a default list item 2" };
+
+methods:
+
+ "example" usebundle => mymethod("","bbb");
+
+reports:
+
+ !xyz::
+
+ "The default value of X is $(X)";
+ "The default value of Y is $(Y)";
+}
+
+###########################################################
+
+bundle agent mymethod(a,b)
+
+{
+vars:
+
+ "no_return" string => "ok"; # readfile("/dont/exist","123");
+
+defaults:
+
+ "a" string => "AAAAAAAAA", if_match_regex => "";
+
+ "b" string => "BBBBBBBBB", if_match_regex => "";
+
+ "no_return" string => "no such file";
+
+reports:
+
+ !xyz::
+
+ "The value of a is $(a)";
+ "The value of b is $(b)";
+
+ "The value of no_return is $(no_return)";
+
+}
+
+
View
65 examples/unit_meta.cf
@@ -0,0 +1,65 @@
+
+# Copyright (C) Cfengine AS
+
+# This file is part of Cfengine 3 - written and maintained by Cfengine 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 Commerical Open Source License
+# (COSL) may apply to this file if you as a licensee so wish it. See
+# included file COSL.txt.
+
+#
+# Default values for variables and parameters, introduced 3.4.0
+#
+
+body common control
+
+{
+bundlesequence => { "example" };
+}
+
+###########################################################
+
+bundle agent example
+
+{
+meta:
+
+ "bundle_version" string => "1.2.3";
+ "works_with_cfengine" string => "3.4.0";
+
+vars:
+
+ "testvar" string => "x";
+
+reports:
+
+ !xyz::
+
+ "Not a local variable: $(bundle_version)";
+ "Not a local variable: $(other_bundle.something)";
+ "Meta data (variable): $(example_meta.bundle_version)";
+
+}
+
+
+bundle agent other_bundle
+
+{
+vars:
+
+ "something" string => "I am defined in another bundle";
+}
View
21 src/cf-agent.c
@@ -43,7 +43,9 @@ extern int PR_NOTKEPT;
enum typesequence
{
+ kp_meta,
kp_vars,
+ kp_defaults,
kp_classes,
kp_outputs,
kp_interfaces,
@@ -62,7 +64,9 @@ enum typesequence
char *TYPESEQUENCE[] =
{
+ "meta",
"vars",
+ "defaults",
"classes", /* Maelstrom order 2 */
"outputs",
"interfaces",
@@ -859,6 +863,10 @@ static void KeepPromiseBundles(Policy *policy, Rlist *bundlesequence)
if ((bp = GetBundle(policy, name, "agent")) || (bp = GetBundle(policy, name, "common")))
{
+ char namespace[CF_BUFSIZE];
+ snprintf(namespace,CF_BUFSIZE,"%s_meta",bp->name);
+ NewScope(namespace);
+
SetBundleOutputs(bp->name);
AugmentScope(bp->name, bp->args, params);
BannerBundle(bp, params);
@@ -880,7 +888,7 @@ int ScheduleAgentOperations(Bundle *bp)
SubType *sp;
Promise *pp;
enum typesequence type;
- int pass, retval;
+ int pass;
int save_pr_kept = PR_KEPT;
int save_pr_repaired = PR_REPAIRED;
int save_pr_notkept = PR_NOTKEPT;
@@ -1035,12 +1043,6 @@ static void KeepAgentPromise(Promise *pp)
// Record promises examined for efficiency calc
- if (strcmp("vars", pp->agentsubtype) == 0)
- {
- ConvergeVarHashPromise(pp->bundle, pp, true);
- return;
- }
-
if (strcmp("meta", pp->agentsubtype) == 0)
{
char namespace[CF_BUFSIZE];
@@ -1049,6 +1051,11 @@ static void KeepAgentPromise(Promise *pp)
return;
}
+ if (strcmp("vars", pp->agentsubtype) == 0)
+ {
+ ConvergeVarHashPromise(pp->bundle, pp, true);
+ return;
+ }
if (strcmp("defaults", pp->agentsubtype) == 0)
{
View
7 src/expand.c
@@ -730,6 +730,13 @@ void ExpandPromiseAndDo(enum cfagenttype agent, const char *scopeid, Promise *pp
ConvergeVarHashPromise(pp->bundle, pexp, true);
}
+ if (strcmp(pp->agentsubtype, "meta") == 0)
+ {
+ char namespace[CF_BUFSIZE];
+ snprintf(namespace,CF_BUFSIZE,"%s_meta",pp->bundle);
+ ConvergeVarHashPromise(namespace, pp, true);
+ }
+
DeletePromise(pexp);
/* End thread monitor */
View
1  src/mod_common.c
@@ -116,6 +116,7 @@ const BodySyntax CF_METABODY[] =
const BodySyntax CF_DEFAULTSBODY[] =
{
+ {"if_match_regex", cf_str, "", "If this regular expression matches the current value of the variable, replace it with default"},
{"string", cf_str, "", "A scalar string"},
{"slist", cf_slist, "", "A list of scalar strings"},
{NULL, cf_notype, NULL, NULL}
View
57 src/vars.c
@@ -24,6 +24,7 @@
*/
#include "vars.h"
+#include "constraints.h"
static int IsCf3Scalar(char *str);
static int CompareVariableValue(Rval rval, CfAssoc *ap);
@@ -274,7 +275,63 @@ static int CompareVariableValue(Rval rval, CfAssoc *ap)
/*******************************************************************/
void DefaultVarPromise(Promise *pp)
+
{
+ char *regex = GetConstraintValue("if_match_regex", pp, CF_SCALAR);
+ Rval rval;
+ enum cfdatatype dt;
+ Rlist *rp;
+ bool okay = true;
+
+ dt = GetVariable("this", pp->promiser, &rval);
+
+ switch (dt)
+ {
+ case cf_str:
+ case cf_int:
+ case cf_real:
+
+ if (regex && !FullTextMatch(regex,rval.item))
+ {
+ return;
+ }
+
+ if (regex == NULL)
+ {
+ return;
+ }
+
+ break;
+
+ case cf_slist:
+ case cf_ilist:
+ case cf_rlist:
+
+ if (regex)
+ {
+ for (rp = (Rlist *) rval.item; rp != NULL; rp = rp->next)
+ {
+ if (FullTextMatch(regex,rp->item))
+ {
+ okay = false;
+ break;
+ }
+ }
+
+ if (okay)
+ {
+ return;
+ }
+ }
+
+ break;
+
+ default:
+ break;
+ }
+
+ ConvergeVarHashPromise(pp->bundle, pp, true);
+
}
/*******************************************************************/
Please sign in to comment.
Something went wrong with that request. Please try again.