Skip to content

Commit eccfbbf

Browse files
committed
Added top_down evaluation order option
Ticket: ENT-10184 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent b8e4c70 commit eccfbbf

File tree

10 files changed

+96
-0
lines changed

10 files changed

+96
-0
lines changed

cf-agent/cf-agent.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,15 @@ static void AllClassesReport(const EvalContext *ctx)
15591559

15601560
PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp)
15611561
// NB - this function can be called recursively through "methods"
1562+
{
1563+
if (EvalContextGetEvalOption(ctx, EVAL_OPTION_TOP_DOWN_EVALUATION))
1564+
{
1565+
return ScheduleAgentOperationsTopDownOrder(ctx, bp);
1566+
}
1567+
return ScheduleAgentOperationsNormalOrder(ctx, bp);
1568+
}
1569+
1570+
PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle *bp)
15621571
{
15631572
assert(bp != NULL);
15641573

@@ -1652,6 +1661,59 @@ PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp)
16521661
return result;
16531662
}
16541663

1664+
PromiseResult ScheduleAgentOperationsTopDownOrder(EvalContext *ctx, const Bundle *bp)
1665+
{
1666+
assert(bp != NULL);
1667+
1668+
int save_pr_kept = PR_KEPT;
1669+
int save_pr_repaired = PR_REPAIRED;
1670+
int save_pr_notkept = PR_NOTKEPT;
1671+
struct timespec start = BeginMeasure();
1672+
1673+
if (PROCESSREFRESH == NULL || (PROCESSREFRESH && IsRegexItemIn(ctx, PROCESSREFRESH, bp->name)))
1674+
{
1675+
ClearProcessTable();
1676+
}
1677+
1678+
// maybe 3 passes?
1679+
PromiseResult result = PROMISE_RESULT_SKIPPED;
1680+
const size_t sections = SeqLength(bp->all_sections);
1681+
EvalContextSetPass(ctx, 1);
1682+
for (size_t i = 0; i < sections; ++i)
1683+
{
1684+
BundleSection *section = SeqAt(bp->all_sections, i);
1685+
1686+
if (!section || SeqLength(section->promises) == 0)
1687+
{
1688+
continue;
1689+
}
1690+
1691+
SpecialTypeBannerFromString(section->promise_type, 1);
1692+
EvalContextStackPushBundleSectionFrame(ctx, section);
1693+
1694+
const size_t promises = SeqLength(section->promises);
1695+
for (size_t ppi = 0; ppi < promises; ppi++)
1696+
{
1697+
Promise *pp = SeqAt(section->promises, ppi);
1698+
1699+
1700+
PromiseResult promise_result = ExpandPromise(ctx, pp, KeepAgentPromise, NULL);
1701+
result = PromiseResultUpdate(result, promise_result);
1702+
1703+
if (EvalAborted(ctx) || BundleAbort(ctx))
1704+
{
1705+
EvalContextStackPopFrame(ctx);
1706+
NoteBundleCompliance(bp, save_pr_kept, save_pr_repaired, save_pr_notkept, start);
1707+
return result;
1708+
}
1709+
}
1710+
EvalContextStackPopFrame(ctx);
1711+
}
1712+
1713+
NoteBundleCompliance(bp, save_pr_kept, save_pr_repaired, save_pr_notkept, start);
1714+
return result;
1715+
}
1716+
16551717
/*********************************************************************/
16561718

16571719
#ifdef __MINGW32__

libpromises/cf3.defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ typedef enum
441441
COMMON_CONTROL_TLS_MIN_VERSION,
442442
COMMON_CONTROL_PACKAGE_INVENTORY,
443443
COMMON_CONTROL_PACKAGE_MODULE,
444+
COMMON_CONTROL_EVALUATION_ORDER,
444445
COMMON_CONTROL_MAX
445446
} CommonControl;
446447

libpromises/eval_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ typedef enum
108108

109109
EVAL_OPTION_EVAL_FUNCTIONS = 1 << 0,
110110
EVAL_OPTION_CACHE_SYSTEM_FUNCTIONS = 1 << 1,
111+
EVAL_OPTION_TOP_DOWN_EVALUATION = 1 << 2,
111112

112113
EVAL_OPTION_FULL = 0xFFFFFFFF
113114
} EvalContextOption;

libpromises/expand.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,15 @@ static void ResolveControlBody(EvalContext *ctx, GenericAgentConfig *config,
10251025
/* Ignored */
10261026
}
10271027

1028+
if (strcmp(lval, CFG_CONTROLBODY[COMMON_CONTROL_EVALUATION_ORDER].lval) == 0)
1029+
{
1030+
Log(LOG_LEVEL_VERBOSE, "SET evaluation %s",
1031+
RvalScalarValue(evaluated_rval));
1032+
1033+
bool is_top_down = (StringEqual(RvalScalarValue(evaluated_rval), "top_down"));
1034+
EvalContextSetEvalOption(ctx, EVAL_OPTION_TOP_DOWN_EVALUATION, is_top_down);
1035+
}
1036+
10281037
RvalDestroy(evaluated_rval);
10291038
}
10301039

libpromises/mod_common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ const ConstraintSyntax CFG_CONTROLBODY[COMMON_CONTROL_MAX + 1] =
268268
ConstraintSyntaxNewString("tls_min_version", "", "Minimum acceptable TLS version for outgoing connections, defaults to OpenSSL's default", SYNTAX_STATUS_NORMAL),
269269
ConstraintSyntaxNewStringList("package_inventory", ".*", "Name of the package manager used for software inventory management", SYNTAX_STATUS_NORMAL),
270270
ConstraintSyntaxNewString("package_module", ".*", "Name of the default package manager", SYNTAX_STATUS_NORMAL),
271+
ConstraintSyntaxNewString("evaluation_order", "(normal|top_down)", "Order of evaluation of promises", SYNTAX_STATUS_NORMAL),
271272
ConstraintSyntaxNewNull()
272273
};
273274

libpromises/ornaments.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ void SpecialTypeBanner(TypeSequence type, int pass)
130130
}
131131
}
132132

133+
void SpecialTypeBannerFromString(char *type, int pass)
134+
{
135+
if (StringEqual(type, "classes"))
136+
{
137+
Log(LOG_LEVEL_VERBOSE, "C: .........................................................");
138+
Log(LOG_LEVEL_VERBOSE, "C: BEGIN classes / conditions (pass %d)", pass);
139+
}
140+
if (StringEqual(type, "vars"))
141+
{
142+
Log(LOG_LEVEL_VERBOSE, "V: .........................................................");
143+
Log(LOG_LEVEL_VERBOSE, "V: BEGIN variables (pass %d)", pass);
144+
}
145+
}
146+
133147
void PromiseBanner(EvalContext *ctx, const Promise *pp)
134148
{
135149
char handle[CF_MAXVARSIZE];

libpromises/ornaments.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <eval_context.h>
3535

3636
void SpecialTypeBanner(TypeSequence type, int pass);
37+
void SpecialTypeBannerFromString(char *type, int pass);
3738
void PromiseBanner(EvalContext *ctx, const Promise *pp);
3839
void Banner(const char *s);
3940
void Legend();

libpromises/policy.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ static bool PolicyCheckBundle(const Bundle *bundle, Seq *errors)
727727

728728
success &= PolicyCheckBundleSections(bundle->sections, errors);
729729
success &= PolicyCheckBundleSections(bundle->custom_sections, errors);
730+
success &= PolicyCheckBundleSections(bundle->all_sections, errors);
730731

731732
return success;
732733
}
@@ -1322,6 +1323,7 @@ Bundle *PolicyAppendBundle(Policy *policy,
13221323
bundle->source_path = SafeStringDuplicate(source_path);
13231324
bundle->sections = SeqNew(10, BundleSectionDestroy);
13241325
bundle->custom_sections = SeqNew(10, BundleSectionDestroy);
1326+
bundle->all_sections = SeqNew(10, NULL);
13251327

13261328
return bundle;
13271329
}
@@ -1427,6 +1429,7 @@ BundleSection *BundleAppendSection(Bundle *bundle, const char *promise_type)
14271429
{
14281430
SeqAppend(bundle->custom_sections, section);
14291431
}
1432+
SeqAppend(bundle->all_sections, section);
14301433

14311434
return section;
14321435
}
@@ -1479,6 +1482,7 @@ static void BundleDestroy(Bundle *bundle)
14791482
RlistDestroy(bundle->args);
14801483
SeqDestroy(bundle->sections);
14811484
SeqDestroy(bundle->custom_sections);
1485+
SeqDestroy(bundle->all_sections);
14821486

14831487
free(bundle);
14841488
}

libpromises/policy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct Bundle_
7979

8080
Seq *sections;
8181
Seq *custom_sections;
82+
Seq *all_sections; // kinda wasting space?
8283

8384
char *source_path;
8485
SourceOffset offset;

libpromises/prototypes3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ void yyerror(const char *s);
4444
/* agent.c */
4545

4646
PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp);
47+
PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle *bp);
48+
PromiseResult ScheduleAgentOperationsTopDownOrder(EvalContext *ctx, const Bundle *bp);
4749

4850
/* Only for agent.c */
4951

0 commit comments

Comments
 (0)