Skip to content

Commit 9e0c19a

Browse files
committed
Added this.calling_bundle variable
Ticket: ENT-12724 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 6e7aec4 commit 9e0c19a

File tree

12 files changed

+31
-21
lines changed

12 files changed

+31
-21
lines changed

cf-agent/cf-agent.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ static void KeepPromiseBundles(EvalContext *ctx, const Policy *policy, GenericAg
15471547
if (bp)
15481548
{
15491549
BundleBanner(bp,args);
1550-
EvalContextStackPushBundleFrame(ctx, bp, args, false);
1550+
EvalContextStackPushBundleFrame(ctx, bp, args, false, NULL);
15511551
ScheduleAgentOperations(ctx, bp);
15521552
EvalContextStackPopFrame(ctx);
15531553
EndBundleBanner(bp);

cf-agent/verify_files.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
894894

895895
a.haveeditline = true;
896896

897-
EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit);
897+
EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit, NULL);
898898
BundleResolve(ctx, bp);
899899

900900
*save_file = ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext);
@@ -1154,7 +1154,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
11541154
const Bundle *bp = EvalContextResolveBundleExpression(ctx, policy, edit_bundle_name, "edit_line");
11551155
if (bp)
11561156
{
1157-
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit);
1157+
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit, NULL);
11581158

11591159
BundleResolve(ctx, bp);
11601160

@@ -1192,7 +1192,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
11921192
const Bundle *bp = EvalContextResolveBundleExpression(ctx, policy, edit_bundle_name, "edit_xml");
11931193
if (bp)
11941194
{
1195-
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit);
1195+
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit, NULL);
11961196
BundleResolve(ctx, bp);
11971197

11981198
ScheduleEditXmlOperations(ctx, bp, a, pp, edcontext);

cf-agent/verify_methods.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ PromiseResult VerifyMethod(EvalContext *ctx, const Rval call, const Attributes *
148148
{
149149
BundleBanner(bp, args);
150150
EvalContextSetBundleArgs(ctx, args);
151-
EvalContextStackPushBundleFrame(ctx, bp, args, a->inherit);
151+
EvalContextStackPushBundleFrame(ctx, bp, args, a->inherit, PromiseGetBundle(pp));
152152

153153
/* Clear all array-variables that are already set in the sub-bundle.
154154
Otherwise, array-data accumulates between multiple bundle evaluations.

cf-monitord/env_monitor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ static void GatherPromisedMeasures(EvalContext *ctx, const Policy *policy)
11431143
for (size_t i = 0; i < SeqLength(policy->bundles); i++)
11441144
{
11451145
const Bundle *bp = SeqAt(policy->bundles, i);
1146-
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
1146+
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);
11471147

11481148
if ((strcmp(bp->type, CF_AGENTTYPES[AGENT_TYPE_MONITOR]) == 0) || (strcmp(bp->type, CF_AGENTTYPES[AGENT_TYPE_COMMON]) == 0))
11491149
{

cf-serverd/server_transform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ static bool IsPromiseTypeNotInTypeSequence(const char *promise_type,
641641

642642
static void EvaluateBundle(EvalContext *ctx, const Bundle *bp, const char * const *seq)
643643
{
644-
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
644+
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);
645645

646646
for (int type = 0; seq[type] != NULL; type++)
647647
{

libpromises/eval_context.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,13 +1355,14 @@ static StackFrame *StackFrameNew(StackFrameType type, bool inherit_previous)
13551355
return frame;
13561356
}
13571357

1358-
static StackFrame *StackFrameNewBundle(const Bundle *owner, bool inherit_previous, bool profiling)
1358+
static StackFrame *StackFrameNewBundle(const Bundle *owner, bool inherit_previous, bool profiling, const Bundle *calling_bundle)
13591359
{
13601360
StackFrame *frame = StackFrameNew(STACK_FRAME_TYPE_BUNDLE, inherit_previous);
13611361

13621362
frame->data.bundle.owner = owner;
13631363
frame->data.bundle.classes = ClassTableNew();
13641364
frame->data.bundle.vars = VariableTableNew();
1365+
frame->data.bundle.calling_bundle = calling_bundle;
13651366
frame->event = (profiling) ? BundleToEventFrame(owner) : NULL;
13661367

13671368
return frame;
@@ -1438,12 +1439,12 @@ static void EvalContextStackPushFrame(EvalContext *ctx, StackFrame *frame)
14381439
STACK_FRAME_TYPE_STR[frame->type]);
14391440
}
14401441

1441-
void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous)
1442+
void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous, const Bundle *calling_bundle)
14421443
{
14431444
assert(ctx != NULL);
14441445
assert(!LastStackFrame(ctx, 0) || LastStackFrame(ctx, 0)->type == STACK_FRAME_TYPE_PROMISE_ITERATION);
14451446

1446-
EvalContextStackPushFrame(ctx, StackFrameNewBundle(owner, inherits_previous, ctx->profiling));
1447+
EvalContextStackPushFrame(ctx, StackFrameNewBundle(owner, inherits_previous, ctx->profiling, calling_bundle));
14471448

14481449
if (RlistLen(args) > 0)
14491450
{
@@ -1567,6 +1568,14 @@ void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner)
15671568
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "bundle", PromiseGetBundle(owner)->name, CF_DATA_TYPE_STRING, "source=promise");
15681569
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "namespace", PromiseGetNamespace(owner), CF_DATA_TYPE_STRING, "source=promise");
15691570

1571+
StackFrame *bundle_frame = LastStackFrameByType(ctx, STACK_FRAME_TYPE_BUNDLE);
1572+
if (bundle_frame != NULL && bundle_frame->data.bundle.calling_bundle != NULL)
1573+
{
1574+
char *calling_bundle_name = StringFormat("%s:%s",bundle_frame->data.bundle.calling_bundle->ns, bundle_frame->data.bundle.calling_bundle->name);
1575+
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "calling_bundle", calling_bundle_name, CF_DATA_TYPE_STRING, "source=promise");
1576+
free(calling_bundle_name);
1577+
}
1578+
15701579
// Recompute `with`
15711580
for (size_t i = 0; i < SeqLength(owner->conlist); i++)
15721581
{

libpromises/eval_context.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef enum
6262
typedef struct
6363
{
6464
const Bundle *owner;
65+
const Bundle *calling_bundle;
6566

6667
ClassTable *classes;
6768
VariableTable *vars;
@@ -183,7 +184,7 @@ void EvalContextClear(EvalContext *ctx);
183184

184185
Rlist *EvalContextGetPromiseCallerMethods(EvalContext *ctx);
185186

186-
void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous);
187+
void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous, const Bundle *calling_bundle);
187188
void EvalContextStackPushBodyFrame(EvalContext *ctx, const Promise *caller, const Body *body, const Rlist *args);
188189
void EvalContextStackPushBundleSectionFrame(EvalContext *ctx, const BundleSection *owner);
189190
void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner);

libpromises/expand.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ void PolicyResolve(EvalContext *ctx, const Policy *policy,
11181118
Bundle *bundle = SeqAt(policy->bundles, i);
11191119
if (strcmp("common", bundle->type) == 0)
11201120
{
1121-
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false);
1121+
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false, NULL);
11221122
BundleResolve(ctx, bundle); /* PRE-EVAL classes,vars */
11231123
EvalContextStackPopFrame(ctx);
11241124
}
@@ -1136,7 +1136,7 @@ void PolicyResolve(EvalContext *ctx, const Policy *policy,
11361136
Bundle *bundle = SeqAt(policy->bundles, i);
11371137
if (strcmp("common", bundle->type) != 0)
11381138
{
1139-
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false);
1139+
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false, NULL);
11401140
BundleResolve(ctx, bundle); /* PRE-EVAL vars */
11411141
EvalContextStackPopFrame(ctx);
11421142
}

libpromises/loading.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ Policy *LoadPolicy(EvalContext *ctx, GenericAgentConfig *config)
573573
for (size_t i = 0; i < SeqLength(policy->bundles); i++)
574574
{
575575
Bundle *bp = SeqAt(policy->bundles, i);
576-
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
576+
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);
577577

578578
for (size_t j = 0; j < SeqLength(bp->sections); j++)
579579
{

tests/unit/eval_context_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void test_class_persistence(void)
5555
Policy *p = PolicyNew();
5656
Bundle *bp = PolicyAppendBundle(p, "ns1", "bundle1", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
5757

58-
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
58+
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);
5959
EvalContextHeapPersistentSave(ctx, "class2", 5, CONTEXT_STATE_POLICY_PRESERVE, "x");
6060
EvalContextStackPopFrame(ctx);
6161

0 commit comments

Comments
 (0)