Skip to content

Commit a76ce54

Browse files
committed
Allow top-down evaluation with separate bundlesections
Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 5038b0c commit a76ce54

File tree

8 files changed

+88
-55
lines changed

8 files changed

+88
-55
lines changed

cf-agent/cf-agent.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,11 +1562,68 @@ PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp)
15621562
{
15631563
if (EvalContextGetEvalOption(ctx, EVAL_OPTION_NORMAL_EVALUATION))
15641564
{
1565+
DispatchBundleSection(bp);
15651566
return ScheduleAgentOperationsNormalOrder(ctx, bp);
15661567
}
15671568
return ScheduleAgentOperationsTopDownOrder(ctx, bp);
15681569
}
15691570

1571+
/**
1572+
* When parsing, each bundlesection is appended to bundle->all_sections
1573+
* When evaluating with normal order, we dispatch the bundlesections to
1574+
* bundle->sections and bundle->custom_section and remove duplicates
1575+
*/
1576+
void DispatchBundleSection(const Bundle *bp)
1577+
{
1578+
for (size_t spi = 0; spi < SeqLength(bp->all_sections); spi++)
1579+
{
1580+
const BundleSection *sp = SeqAt(bp->all_sections, spi);
1581+
1582+
if (IsBuiltInPromiseType(sp->promise_type))
1583+
{
1584+
MergePromises(bp->sections, sp);
1585+
}
1586+
else
1587+
{
1588+
MergePromises(bp->custom_sections, sp);
1589+
}
1590+
}
1591+
}
1592+
1593+
/**
1594+
* shallow copy each promise to the right bundlesection, removing duplicates
1595+
*/
1596+
void MergePromises(Seq *sections, const BundleSection *sp)
1597+
{
1598+
for (size_t si = 0; si < SeqLength(sections); si++)
1599+
{
1600+
BundleSection *existing = SeqAt(sections, si);
1601+
if (strcmp(existing->promise_type, sp->promise_type) == 0)
1602+
{
1603+
for (size_t pi = 0; pi < SeqLength(sp->promises); pi++)
1604+
{
1605+
Promise *pp = SeqAt(sp->promises, pi);
1606+
SeqAppend(existing->promises, pp);
1607+
}
1608+
return;
1609+
}
1610+
}
1611+
1612+
BundleSection *new_section = xcalloc(1, sizeof(BundleSection));
1613+
1614+
new_section->parent_bundle = sp->parent_bundle;
1615+
new_section->promise_type = xstrdup(sp->promise_type);
1616+
new_section->promises = SeqNew(10, NULL);
1617+
1618+
for (size_t pi = 0; pi < SeqLength(sp->promises); pi++)
1619+
{
1620+
Promise *pp = SeqAt(sp->promises, pi);
1621+
SeqAppend(new_section->promises, pp);
1622+
}
1623+
1624+
SeqAppend(sections, new_section);
1625+
}
1626+
15701627
PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle *bp)
15711628
{
15721629
assert(bp != NULL);
@@ -1594,7 +1651,6 @@ PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle
15941651
{
15951652
continue;
15961653
}
1597-
printf("N: Promise type: %s\n", sp->promise_type);
15981654

15991655
NewTypeContext(type);
16001656

cf-monitord/env_monitor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,9 @@ static void GatherPromisedMeasures(EvalContext *ctx, const Policy *policy)
11471147

11481148
if ((strcmp(bp->type, CF_AGENTTYPES[AGENT_TYPE_MONITOR]) == 0) || (strcmp(bp->type, CF_AGENTTYPES[AGENT_TYPE_COMMON]) == 0))
11491149
{
1150-
for (size_t j = 0; j < SeqLength(bp->sections); j++)
1150+
for (size_t j = 0; j < SeqLength(bp->all_sections); j++)
11511151
{
1152-
BundleSection *sp = SeqAt(bp->sections, j);
1152+
BundleSection *sp = SeqAt(bp->all_sections, j);
11531153

11541154
EvalContextStackPushBundleSectionFrame(ctx, sp);
11551155
for (size_t ppi = 0; ppi < SeqLength(sp->promises); ppi++)

cf-serverd/server_transform.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,9 @@ static void EvaluateBundle(EvalContext *ctx, const Bundle *bp, const char * cons
666666

667667
/* Check if we are having some other promise types which we
668668
* should evaluate. THIS IS ONLY FOR BACKWARD COMPATIBILITY! */
669-
for (size_t j = 0; j < SeqLength(bp->sections); j++)
669+
for (size_t j = 0; j < SeqLength(bp->all_sections); j++)
670670
{
671-
BundleSection *sp = SeqAt(bp->sections, j);
671+
BundleSection *sp = SeqAt(bp->all_sections, j);
672672

673673
/* Skipping evaluation of promise as this was evaluated in
674674
* loop above. */

libpromises/evalfunction.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,10 +1872,10 @@ static Bundle *GetBundleFromPolicy(const Policy *policy, const char *namespace,
18721872
static Promise *GetPromiseFromBundle(const Bundle *bundle, const char *promise_type, const char *promiser)
18731873
{
18741874
assert(bundle != NULL);
1875-
const size_t sections_length = SeqLength(bundle->sections);
1875+
const size_t sections_length = SeqLength(bundle->all_sections);
18761876
for (size_t i = 0; i < sections_length; i++)
18771877
{
1878-
BundleSection *bsection = SeqAt(bundle->sections, i);
1878+
BundleSection *bsection = SeqAt(bundle->all_sections, i);
18791879
if (StringEqual(bsection->promise_type, promise_type))
18801880
{
18811881
const size_t promises_length = SeqLength(bsection->promises);

libpromises/expand.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,9 @@ Rval EvaluateFinalRval(EvalContext *ctx, const Policy *policy,
724724

725725
void BundleResolvePromiseType(EvalContext *ctx, const Bundle *bundle, const char *type, PromiseActuator *actuator)
726726
{
727-
for (size_t j = 0; j < SeqLength(bundle->sections); j++)
727+
for (size_t j = 0; j < SeqLength(bundle->all_sections); j++)
728728
{
729-
BundleSection *section = SeqAt(bundle->sections, j);
729+
BundleSection *section = SeqAt(bundle->all_sections, j);
730730

731731
if (strcmp(section->promise_type, type) == 0)
732732
{

libpromises/loading.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,9 @@ Policy *LoadPolicy(EvalContext *ctx, GenericAgentConfig *config)
575575
Bundle *bp = SeqAt(policy->bundles, i);
576576
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
577577

578-
for (size_t j = 0; j < SeqLength(bp->sections); j++)
578+
for (size_t j = 0; j < SeqLength(bp->all_sections); j++)
579579
{
580-
BundleSection *sp = SeqAt(bp->sections, j);
580+
BundleSection *sp = SeqAt(bp->all_sections, j);
581581
EvalContextStackPushBundleSectionFrame(ctx, sp);
582582

583583
for (size_t ppi = 0; ppi < SeqLength(sp->promises); ppi++)

libpromises/policy.c

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ static unsigned BundleHash(const Bundle *bundle, unsigned seed)
235235
hash = StringHash(bundle->name, hash);
236236
hash = RlistHash(bundle->args, hash);
237237

238-
for (size_t i = 0; i < SeqLength(bundle->sections); i++)
238+
for (size_t i = 0; i < SeqLength(bundle->all_sections); i++)
239239
{
240-
const BundleSection *section = SeqAt(bundle->sections, i);
240+
const BundleSection *section = SeqAt(bundle->all_sections, i);
241241
hash = BundleSectionHash(section, hash);
242242
}
243243

@@ -891,9 +891,9 @@ static bool PolicyCheckUndefinedBodies(const Policy *policy, Seq *errors)
891891
{
892892
Bundle *bundle = SeqAt(policy->bundles, bpi);
893893

894-
for (size_t sti = 0; sti < SeqLength(bundle->sections); sti++)
894+
for (size_t sti = 0; sti < SeqLength(bundle->all_sections); sti++)
895895
{
896-
BundleSection *section = SeqAt(bundle->sections, sti);
896+
BundleSection *section = SeqAt(bundle->all_sections, sti);
897897

898898
for (size_t ppi = 0; ppi < SeqLength(section->promises); ppi++)
899899
{
@@ -945,9 +945,9 @@ static bool PolicyCheckRequiredComments(const EvalContext *ctx, const Policy *po
945945
{
946946
Bundle *bundle = SeqAt(policy->bundles, bpi);
947947

948-
for (size_t sti = 0; sti < SeqLength(bundle->sections); sti++)
948+
for (size_t sti = 0; sti < SeqLength(bundle->all_sections); sti++)
949949
{
950-
BundleSection *section = SeqAt(bundle->sections, sti);
950+
BundleSection *section = SeqAt(bundle->all_sections, sti);
951951

952952
for (size_t ppi = 0; ppi < SeqLength(section->promises); ppi++)
953953
{
@@ -993,9 +993,9 @@ bool PolicyCheckDuplicateHandles(const Policy *policy, Seq *errors)
993993
{
994994
Bundle *bundle = SeqAt(policy->bundles, bpi);
995995

996-
for (size_t sti = 0; sti < SeqLength(bundle->sections); sti++)
996+
for (size_t sti = 0; sti < SeqLength(bundle->all_sections); sti++)
997997
{
998-
BundleSection *section = SeqAt(bundle->sections, sti);
998+
BundleSection *section = SeqAt(bundle->all_sections, sti);
999999

10001000
for (size_t ppi = 0; ppi < SeqLength(section->promises); ppi++)
10011001
{
@@ -1321,8 +1321,8 @@ Bundle *PolicyAppendBundle(Policy *policy,
13211321
bundle->ns = xstrdup(ns);
13221322
bundle->args = RlistCopy(args);
13231323
bundle->source_path = SafeStringDuplicate(source_path);
1324-
bundle->sections = SeqNew(10, NULL);
1325-
bundle->custom_sections = SeqNew(10, NULL);
1324+
bundle->sections = SeqNew(10, BundleSectionDestroy);
1325+
bundle->custom_sections = SeqNew(10, BundleSectionDestroy); // maybe change
13261326
bundle->all_sections = SeqNew(10, BundleSectionDestroy);
13271327

13281328
return bundle;
@@ -1404,33 +1404,6 @@ BundleSection *BundleAppendSection(Bundle *bundle, const char *promise_type)
14041404
section->promises = SeqNew(10, PromiseDestroy);
14051405
SeqAppend(bundle->all_sections, section);
14061406

1407-
// TODO: review SeqLookup
1408-
for (size_t i = 0; i < SeqLength(bundle->sections); i++)
1409-
{
1410-
BundleSection *existing = SeqAt(bundle->sections, i);
1411-
if (strcmp(existing->promise_type, promise_type) == 0)
1412-
{
1413-
return existing;
1414-
}
1415-
}
1416-
for (size_t i = 0; i < SeqLength(bundle->custom_sections); i++)
1417-
{
1418-
BundleSection *existing = SeqAt(bundle->custom_sections, i);
1419-
if (strcmp(existing->promise_type, promise_type) == 0)
1420-
{
1421-
return existing;
1422-
}
1423-
}
1424-
1425-
if (IsBuiltInPromiseType(promise_type))
1426-
{
1427-
SeqAppend(bundle->sections, section);
1428-
}
1429-
else
1430-
{
1431-
SeqAppend(bundle->custom_sections, section);
1432-
}
1433-
14341407
return section;
14351408
}
14361409

@@ -1641,9 +1614,11 @@ const BundleSection *BundleGetSection(const Bundle *bp, const char *promise_type
16411614
return NULL;
16421615
}
16431616

1644-
for (size_t i = 0; i < SeqLength(bp->sections); i++)
1617+
Seq *sections = (SeqAt(bp->sections, 0) == NULL) ? bp->all_sections : bp->sections;
1618+
1619+
for (size_t i = 0; i < SeqLength(sections); i++)
16451620
{
1646-
BundleSection *sp = SeqAt(bp->sections, i);
1621+
BundleSection *sp = SeqAt(sections, i);
16471622

16481623
if (strcmp(promise_type, sp->promise_type) == 0)
16491624
{
@@ -1934,9 +1909,9 @@ JsonElement *BundleToJson(const Bundle *bundle)
19341909
{
19351910
JsonElement *json_promise_types = JsonArrayCreate(10);
19361911

1937-
for (size_t i = 0; i < SeqLength(bundle->sections); i++)
1912+
for (size_t i = 0; i < SeqLength(bundle->all_sections); i++)
19381913
{
1939-
const BundleSection *sp = SeqAt(bundle->sections, i);
1914+
const BundleSection *sp = SeqAt(bundle->all_sections, i);
19401915

19411916
JsonElement *json_promise_type = JsonObjectCreate(10);
19421917

@@ -2115,9 +2090,9 @@ void BundleToString(Writer *writer, Bundle *bundle)
21152090
ArgumentsToString(writer, bundle->args);
21162091
WriterWrite(writer, "\n{");
21172092

2118-
for (size_t i = 0; i < SeqLength(bundle->sections); i++)
2093+
for (size_t i = 0; i < SeqLength(bundle->all_sections); i++)
21192094
{
2120-
BundleSection *section = SeqAt(bundle->sections, i);
2095+
BundleSection *section = SeqAt(bundle->all_sections, i);
21212096

21222097
WriterWriteF(writer, "\n%s:\n", section->promise_type);
21232098

@@ -2160,7 +2135,7 @@ void BundleToString(Writer *writer, Bundle *bundle)
21602135
WriterWriteChar(writer, '\n');
21612136
}
21622137

2163-
if (i == (SeqLength(bundle->sections) - 1))
2138+
if (i == (SeqLength(bundle->all_sections) - 1))
21642139
{
21652140
WriterWriteChar(writer, '\n');
21662141
}

libpromises/prototypes3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ void yyerror(const char *s);
4646
PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp);
4747
PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle *bp);
4848
PromiseResult ScheduleAgentOperationsTopDownOrder(EvalContext *ctx, const Bundle *bp);
49+
void DispatchBundleSection(const Bundle *bp);
50+
void MergePromises(Seq *sections, const BundleSection *sp);
4951

5052
/* Only for agent.c */
5153

0 commit comments

Comments
 (0)