Skip to content

Commit b1b9bad

Browse files
committed
Allow top-down evaluation for all promises
Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 5038b0c commit b1b9bad

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

cf-agent/cf-agent.c

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,6 @@ PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle
15941594
{
15951595
continue;
15961596
}
1597-
printf("N: Promise type: %s\n", sp->promise_type);
15981597

15991598
NewTypeContext(type);
16001599

@@ -1662,6 +1661,20 @@ PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle
16621661
return result;
16631662
}
16641663

1664+
int PromiseCmpWrapper(const Promise *p1, const Promise *p2, void *user_data)
1665+
{
1666+
UNUSED(user_data);
1667+
if (p1->order < p2->order)
1668+
{
1669+
return -1;
1670+
}
1671+
else if (p1->order > p2->order)
1672+
{
1673+
return 1;
1674+
}
1675+
return 0;
1676+
}
1677+
16651678
PromiseResult ScheduleAgentOperationsTopDownOrder(EvalContext *ctx, const Bundle *bp)
16661679
{
16671680
assert(bp != NULL);
@@ -1676,38 +1689,48 @@ PromiseResult ScheduleAgentOperationsTopDownOrder(EvalContext *ctx, const Bundle
16761689
ClearProcessTable();
16771690
}
16781691

1692+
Seq *promise_sequence = SeqNew(10, NULL);
1693+
1694+
const size_t sections = SeqLength(bp->sections);
1695+
for (size_t i = 0; i < sections; i++)
1696+
{
1697+
BundleSection *section = SeqAt(bp->sections, i);
1698+
const size_t promises = SeqLength(section->promises);
1699+
1700+
for (size_t j = 0; j < promises; j++)
1701+
{
1702+
SeqAppend(promise_sequence, SeqAt(section->promises, j));
1703+
}
1704+
}
1705+
1706+
SeqSort(promise_sequence, PromiseCmpWrapper, NULL);
1707+
16791708
PromiseResult result = PROMISE_RESULT_SKIPPED;
16801709
for (int pass = 1; pass < CF_DONEPASSES; pass++)
16811710
{
1682-
const size_t sections = SeqLength(bp->all_sections);
1683-
EvalContextSetPass(ctx, 1);
1684-
for (size_t i = 0; i < sections; ++i)
1711+
for (size_t ppi = 0; ppi < SeqLength(promise_sequence); ppi++)
16851712
{
1686-
BundleSection *section = SeqAt(bp->all_sections, i);
1687-
1713+
Promise *pp = SeqAt(promise_sequence, ppi);
1714+
BundleSection *parent_section = pp->parent_section;
1715+
printf("TP: %s\n", parent_section->promise_type);
16881716

1689-
SpecialTypeBannerFromString(section->promise_type, pass);
1690-
EvalContextStackPushBundleSectionFrame(ctx, section);
1691-
const size_t promises = SeqLength(section->promises);
1717+
EvalContextStackPushBundleSectionFrame(ctx, parent_section);
16921718

1693-
for (size_t ppi = 0; ppi < promises; ppi++)
1719+
PromiseResult promise_result = ExpandPromise(ctx, pp, KeepAgentPromise, NULL);
1720+
result = PromiseResultUpdate(result, promise_result);
1721+
if (EvalAborted(ctx) || BundleAbort(ctx))
16941722
{
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-
}
1723+
EvalContextStackPopFrame(ctx);
1724+
NoteBundleCompliance(bp, save_pr_kept, save_pr_repaired, save_pr_notkept, start);
1725+
SeqDestroy(promise_sequence);
1726+
return result;
17051727
}
17061728
EvalContextStackPopFrame(ctx);
17071729
}
17081730
}
17091731

17101732
NoteBundleCompliance(bp, save_pr_kept, save_pr_repaired, save_pr_notkept, start);
1733+
SeqDestroy(promise_sequence);
17111734
return result;
17121735
}
17131736

libpromises/policy.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ 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);
731730

732731
return success;
733732
}
@@ -1321,9 +1320,8 @@ Bundle *PolicyAppendBundle(Policy *policy,
13211320
bundle->ns = xstrdup(ns);
13221321
bundle->args = RlistCopy(args);
13231322
bundle->source_path = SafeStringDuplicate(source_path);
1324-
bundle->sections = SeqNew(10, NULL);
1325-
bundle->custom_sections = SeqNew(10, NULL);
1326-
bundle->all_sections = SeqNew(10, BundleSectionDestroy);
1323+
bundle->sections = SeqNew(10, BundleSectionDestroy);
1324+
bundle->custom_sections = SeqNew(10, BundleSectionDestroy);
13271325

13281326
return bundle;
13291327
}
@@ -1397,13 +1395,6 @@ BundleSection *BundleAppendSection(Bundle *bundle, const char *promise_type)
13971395
ProgrammingError("Attempt to add a type without a bundle");
13981396
}
13991397

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-
14071398
// TODO: review SeqLookup
14081399
for (size_t i = 0; i < SeqLength(bundle->sections); i++)
14091400
{
@@ -1422,6 +1413,12 @@ BundleSection *BundleAppendSection(Bundle *bundle, const char *promise_type)
14221413
}
14231414
}
14241415

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+
14251422
if (IsBuiltInPromiseType(promise_type))
14261423
{
14271424
SeqAppend(bundle->sections, section);
@@ -1440,6 +1437,7 @@ Promise *BundleSectionAppendPromise(BundleSection *section, const char *promiser
14401437
{
14411438
assert(promiser && "Missing promiser");
14421439
assert(section != NULL && "Missing promise type");
1440+
assert(section->parent_bundle != NULL);
14431441

14441442
Promise *pp = xcalloc(1, sizeof(Promise));
14451443

@@ -1461,6 +1459,7 @@ Promise *BundleSectionAppendPromise(BundleSection *section, const char *promiser
14611459
pp->promisee = promisee;
14621460
pp->conlist = SeqNew(10, ConstraintDestroy);
14631461
pp->org_pp = pp;
1462+
pp->order = section->parent_bundle->promise_count++;
14641463

14651464
if (varclasses != NULL)
14661465
{
@@ -1482,7 +1481,6 @@ static void BundleDestroy(Bundle *bundle)
14821481
RlistDestroy(bundle->args);
14831482
SeqDestroy(bundle->sections);
14841483
SeqDestroy(bundle->custom_sections);
1485-
SeqDestroy(bundle->all_sections);
14861484

14871485
free(bundle);
14881486
}

libpromises/policy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typedef struct
7171
struct Bundle_
7272
{
7373
Policy *parent_policy;
74+
int promise_count;
7475

7576
char *type;
7677
char *name;
@@ -79,7 +80,6 @@ struct Bundle_
7980

8081
Seq *sections;
8182
Seq *custom_sections;
82-
Seq *all_sections; // kinda wasting space?
8383

8484
char *source_path;
8585
SourceOffset offset;
@@ -114,6 +114,7 @@ struct BundleSection_
114114
struct Promise_
115115
{
116116
BundleSection *parent_section;
117+
int order;
117118

118119
char *classes;
119120
char *comment;

0 commit comments

Comments
 (0)