Skip to content

Commit a66b123

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 a66b123

File tree

10 files changed

+101
-8
lines changed

10 files changed

+101
-8
lines changed

cf-agent/cf-agent.c

Lines changed: 59 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_NORMAL_EVALUATION))
1564+
{
1565+
return ScheduleAgentOperationsNormalOrder(ctx, bp);
1566+
}
1567+
return ScheduleAgentOperationsTopDownOrder(ctx, bp);
1568+
}
1569+
1570+
PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle *bp)
15621571
{
15631572
assert(bp != NULL);
15641573

@@ -1585,6 +1594,7 @@ PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp)
15851594
{
15861595
continue;
15871596
}
1597+
printf("N: Promise type: %s\n", sp->promise_type);
15881598

15891599
NewTypeContext(type);
15901600

@@ -1652,6 +1662,55 @@ PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp)
16521662
return result;
16531663
}
16541664

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

16571716
#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_NORMAL_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_normal = (StringEqual(RvalScalarValue(evaluated_rval), "normal"));
1034+
EvalContextSetEvalOption(ctx, EVAL_OPTION_NORMAL_EVALUATION, is_normal);
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: 12 additions & 8 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
}
@@ -1320,8 +1321,9 @@ Bundle *PolicyAppendBundle(Policy *policy,
13201321
bundle->ns = xstrdup(ns);
13211322
bundle->args = RlistCopy(args);
13221323
bundle->source_path = SafeStringDuplicate(source_path);
1323-
bundle->sections = SeqNew(10, BundleSectionDestroy);
1324-
bundle->custom_sections = SeqNew(10, BundleSectionDestroy);
1324+
bundle->sections = SeqNew(10, NULL);
1325+
bundle->custom_sections = SeqNew(10, NULL);
1326+
bundle->all_sections = SeqNew(10, BundleSectionDestroy);
13251327

13261328
return bundle;
13271329
}
@@ -1395,6 +1397,13 @@ BundleSection *BundleAppendSection(Bundle *bundle, const char *promise_type)
13951397
ProgrammingError("Attempt to add a type without a bundle");
13961398
}
13971399

1400+
BundleSection *section = xcalloc(1, sizeof(BundleSection));
1401+
1402+
section->parent_bundle = bundle;
1403+
section->promise_type = xstrdup(promise_type);
1404+
section->promises = SeqNew(10, PromiseDestroy);
1405+
SeqAppend(bundle->all_sections, section);
1406+
13981407
// TODO: review SeqLookup
13991408
for (size_t i = 0; i < SeqLength(bundle->sections); i++)
14001409
{
@@ -1413,12 +1422,6 @@ BundleSection *BundleAppendSection(Bundle *bundle, const char *promise_type)
14131422
}
14141423
}
14151424

1416-
BundleSection *section = xcalloc(1, sizeof(BundleSection));
1417-
1418-
section->parent_bundle = bundle;
1419-
section->promise_type = xstrdup(promise_type);
1420-
section->promises = SeqNew(10, PromiseDestroy);
1421-
14221425
if (IsBuiltInPromiseType(promise_type))
14231426
{
14241427
SeqAppend(bundle->sections, section);
@@ -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)