Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
716 changes: 475 additions & 241 deletions docs/guides/SpecialTopic_Editing.texinfo

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions docs/reference/edit_template_example.texinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@


@verbatim
#This is a template file /templates/input.tmpl

These lines apply to anyone

[%CFEngine solaris.Monday:: %]
Everything after here applies only to solaris on Mondays
until overridden...

[%CFEngine linux:: %]
Everything after here now applies now to linux only.

[%CFEngine BEGIN %]
This is a block of text
That contains list variables: $(some.list)
With text before and after.
[%CFEngine END %]

nameserver $(some.list)
@end verbatim

For example:

@verbatim
[%CFEngine any:: %]
<VirtualHost $(sys.ipv4[eth0]):80>
ServerAdmin $(stage_file.params[apache_mail_address][1])
DocumentRoot /var/www/htdocs
ServerName $(stage_file.params[apache_server_name][1])
AddHandler cgi-script cgi
ErrorLog /var/log/httpd/error.log
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLEngine off
CustomLog /var/log/httpd/access.log
</VirtualHost>

[%CFEngine webservers_prod:: %]
[%CFEngine BEGIN %]
<VirtualHost $(sys.ipv4[$(bundle.interfaces)]):443>
ServerAdmin $(stage_file.params[apache_mail_address][1])
DocumentRoot /var/www/htdocs
ServerName $(stage_file.params[apache_server_name][1])
AddHandler cgi-script cgi
ErrorLog /var/log/httpd/error.log
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLEngine on
SSLCertificateFile $(stage_file.params[apache_ssl_crt][1])
SSLCertificateKeyFile $(stage_file.params[apache_ssl_key][1])
CustomLog /var/log/httpd/access.log
</VirtualHost>
[%CFEngine END %]
@end verbatim
26 changes: 26 additions & 0 deletions docs/reference/edit_template_notes.texinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

@i{History}: Was introduced in 3.3.0, Nova 2.2.0, Constellation 1.0.0 (2012)

The template format uses inline tags to mark regions and classes.
Each line represents an @code{insert_lines} promise, unless the promises
are grouped into a block using:

@verbatim
[%CFEngine BEGIN %]
...
[%CFEngine END %]
@end verbatim

@noindent Variables, scalars and list variables are expanded within each
promise; so, if lines are grouped into a block, the whole block is repeated
when lists are expanded (see the Special Topics Guide on editing).

If a class-context modified is used:

@verbatim
[%CFEngine class-expression:: %]
@end verbatim
@noindent then the lines that follow are only inserted if the context
matches the agent's current context. This allows conditional insertion.


6 changes: 5 additions & 1 deletion src/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void SetChecksumUpdates(bool enabled)
CHECKSUMUPDATES = enabled;
}

/*******************************************************************/

Attributes GetFilesAttributes(Promise *pp)
{
Attributes attr = { {0} };
Expand All @@ -61,9 +63,11 @@ Attributes GetFilesAttributes(Promise *pp)
attr.havechange = GetBooleanConstraint("changes", pp);
attr.havecopy = GetBooleanConstraint("copy_from", pp);
attr.havelink = GetBooleanConstraint("link_from", pp);

attr.template = (char *)GetConstraintValue("edit_template", pp, CF_SCALAR);
attr.haveeditline = GetBundleConstraint("edit_line", pp);
attr.haveeditxml = GetBundleConstraint("edit_xml", pp);
attr.haveedit = attr.haveeditline || attr.haveeditxml;
attr.haveedit = attr.haveeditline || attr.haveeditxml || attr.template;

/* Files, specialist */

Expand Down
1 change: 1 addition & 0 deletions src/cf3.defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,7 @@ typedef struct
char *transformer;
char *pathtype;
char *repository;
char *template;
int touch;
int create;
int move_obstructions;
Expand Down
4 changes: 4 additions & 0 deletions src/env_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void MonitorInitialize(void)
LOCALAV.Q[i].expect = 0.0;
LOCALAV.Q[i].var = 0.0;
LOCALAV.Q[i].q = 0.0;
LOCALAV.Q[i].dq = 0.0;
}

for (i = 0; i < 7; i++)
Expand Down Expand Up @@ -369,6 +370,9 @@ static Averages EvalAvQ(char *t)
newvals.Q[i].expect = WAverage(This[i], currentvals->Q[i].expect, WAGE);
LOCALAV.Q[i].expect = WAverage(newvals.Q[i].expect, LOCALAV.Q[i].expect, ITER);

newvals.Q[i].dq = newvals.Q[i].q - currentvals->Q[i].q;
LOCALAV.Q[i].dq = newvals.Q[i].q - currentvals->Q[i].q;

delta2 = (This[i] - currentvals->Q[i].expect) * (This[i] - currentvals->Q[i].expect);

if (currentvals->Q[i].var > delta2 * 2.0)
Expand Down
126 changes: 126 additions & 0 deletions src/files_editline.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,132 @@ int ScheduleEditLineOperations(char *filename, Bundle *bp, Attributes a, Promise
return true;
}

/*****************************************************************************/

Bundle *MakeTemporaryBundleFromTemplate(Attributes a, Promise *pp)
{
char bundlename[CF_MAXVARSIZE], buffer[CF_BUFSIZE];
char *sp, *promiser, context[CF_BUFSIZE] = "any";
Bundle *bp;
Promise *np;
SubType *tp;
FILE *fp;
int level = 0, size, lineno = 0;
Item *ip, *lines = NULL;

snprintf(bundlename, CF_MAXVARSIZE, "temp_cf_bundle_%s", CanonifyName(a.template));

bp = xcalloc(1, sizeof(Bundle));
bp->name = xstrdup(bundlename);
bp->type = xstrdup("edit_line");
bp->args = NULL;
bp->next = NULL;

tp = AppendSubType(bp, "insert_lines");

// Now parse the template file

if ((fp = fopen(a.template,"r")) == NULL)
{
cfPS(cf_error, CF_INTERPT, "", pp, a, " !! Unable to open template file \"%s\" to make \"%s\"", a.template, pp->promiser);
return NULL;
}

while(!feof(fp))
{
buffer[0] = '\0';
fgets(buffer, CF_BUFSIZE-1, fp);
lineno++;

// Check closing syntax

// Get Action operator
if (strncmp(buffer, "[%CFEngine", strlen("[%CFEngine")) == 0)
{
char operator[CF_BUFSIZE], brack[CF_SMALLBUF];

sscanf(buffer+strlen("[%CFEngine"), "%1024s %s", operator, brack);

if (strcmp(brack, "%]") != 0)
{
cfPS(cf_error, CF_INTERPT, "", pp, a, " !! Template file \"%s\" syntax error, missing close \"%%]\" at line %d", a.template, lineno);
return NULL;
}

if (strcmp(operator, "BEGIN") == 0)
{
// start new buffer

if (++level > 1)
{
cfPS(cf_error, CF_INTERPT, "", pp, a, " !! Template file \"%s\" contains nested blocks which are not allowed, near line %d", a.template, lineno);
return NULL;
}

continue;
}

if (strcmp(operator, "END") == 0)
{
// install buffer
level--;
}

if (strcmp(operator+strlen(operator)-2, "::") == 0)
{
*(operator+strlen(operator)-2) = '\0';
strcpy(context, operator);
continue;
}

// In all these cases, we should start a new promise

promiser = NULL;
size = 0;

for (ip = lines; ip != NULL; ip = ip->next)
{
size += strlen(ip->name);
}

sp = promiser = xcalloc(1, size+1);

for (ip = lines; ip != NULL; ip = ip->next)
{
int len = strlen(ip->name);
strncpy(sp, ip->name, len);
sp += len;
}

*(sp-1) = '\0'; // StripTrailingNewline(promiser) and terminate

np = AppendPromise(tp, promiser, (Rval) { NULL, CF_NOPROMISEE }, context, bundlename, "edit_line");
AppendConstraint(&(np->conlist), "insert_type", (Rval) { xstrdup("preserve_block"), CF_SCALAR }, "any", false);

DeleteItemList(lines);
free(promiser);
lines = NULL;
}
else
{
if (level > 0)
{
AppendItem(&lines, buffer, NULL);
}
else
{
//install independent promise line
StripTrailingNewline(buffer);
np = AppendPromise(tp, buffer, (Rval) { NULL, CF_NOPROMISEE }, context, bundlename, "edit_line");
AppendConstraint(&(np->conlist), "insert_type", (Rval) { xstrdup("preserve_block"), CF_SCALAR }, "any", false);
}
}
}

fclose(fp);
return bp;
}

/***************************************************************************/
/* Level */
/***************************************************************************/
Expand Down
18 changes: 18 additions & 0 deletions src/files_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,24 @@ int ScheduleEditOperation(char *filename, Attributes a, Promise *pp)
}
}

if (a.template)
{
if ((bp = MakeTemporaryBundleFromTemplate(a,pp)))
{
BannerSubBundle(bp,params);

DeleteScope(bp->name);
NewScope(bp->name);
HashVariables(bp->name);

PushPrivateClassContext();
retval = ScheduleEditLineOperations(filename,bp,a,pp);
PopPrivateClassContext();
DeleteScope(bp->name);
}
// FIXME: why it crashes? DeleteBundles(bp);
}

FinishEditContext(pp->edcontext, a, pp);
YieldCurrentLock(thislock);
return retval;
Expand Down
8 changes: 8 additions & 0 deletions src/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static void NotePerformance(char *eventname, time_t t, double value)
lastseen = now - e.t;
newe.t = t;
newe.Q.q = value;
newe.Q.dq = value - e.Q.q;
newe.Q.expect = GAverage(value, e.Q.expect, 0.3);
delta2 = (value - e.Q.expect) * (value - e.Q.expect);
newe.Q.var = GAverage(delta2, e.Q.var, 0.3);
Expand All @@ -133,6 +134,7 @@ static void NotePerformance(char *eventname, time_t t, double value)
lastseen = 0.0;
newe.t = t;
newe.Q.q = value;
newe.Q.dq = 0;
newe.Q.expect = value;
newe.Q.var = 0.001;
}
Expand Down Expand Up @@ -207,6 +209,7 @@ void NoteClassUsage(AlphaList baselist, int purge)
lastseen = now - e.t;
newe.t = now;
newe.Q.q = vtrue;
newe.Q.dq = vtrue - e.Q.q;
newe.Q.expect = GAverage(vtrue, e.Q.expect, 0.7);
delta2 = (vtrue - e.Q.expect) * (vtrue - e.Q.expect);
newe.Q.var = GAverage(delta2, e.Q.var, 0.7);
Expand All @@ -216,6 +219,7 @@ void NoteClassUsage(AlphaList baselist, int purge)
lastseen = 0.0;
newe.t = now;
newe.Q.q = 0.5 * vtrue;
newe.Q.dq = 0;
newe.Q.expect = 0.5 * vtrue; /* With no data it's 50/50 what we can say */
newe.Q.var = 0.000;
}
Expand Down Expand Up @@ -277,6 +281,7 @@ void NoteClassUsage(AlphaList baselist, int purge)
{
newe.t = then;
newe.Q.q = 0;
newe.Q.dq = entry.Q.dq;
newe.Q.expect = GAverage(0.0, av, 0.5);
delta2 = av * av;
newe.Q.var = GAverage(delta2, var, 0.5);
Expand Down Expand Up @@ -385,9 +390,11 @@ static void UpdateLastSawHost(char *rkey, char *ipaddress)
lastseen = 300;
q.Q.expect = 0;
q.Q.var = 0;
q.Q.dq = 0;
}

newq.Q.q = (double) now;
newq.Q.dq = newq.Q.q - q.Q.q;
newq.Q.expect = GAverage(lastseen, q.Q.expect, 0.4);
delta2 = (lastseen - q.Q.expect) * (lastseen - q.Q.expect);
newq.Q.var = GAverage(delta2, q.Q.var, 0.4);
Expand All @@ -397,6 +404,7 @@ static void UpdateLastSawHost(char *rkey, char *ipaddress)
{
lastseen = 0.0;
newq.Q.q = (double) now;
newq.Q.dq = 0;
newq.Q.expect = 0.0;
newq.Q.var = 0.0;
strncpy(newq.address, ipaddress, CF_ADDRSIZE - 1);
Expand Down
3 changes: 2 additions & 1 deletion src/mod_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,10 @@ BodySyntax CF_FILES_BODIES[] =
{"create", cf_opts, CF_BOOL, "true/false whether to create non-existing file", "false"},
{"delete", cf_body, CF_TIDY_BODY, "Criteria for deleting files"},
{"depth_search", cf_body, CF_RECURSION_BODY, "Criteria for file depth searches"},
{"edit_defaults", cf_body, CF_EDITS_BODY, "Default promise details for file edits"},
{"edit_line", cf_bundle, CF_BUNDLE, "Line editing model for file"},
{"edit_template", cf_str, CF_ABSPATHRANGE, "The name of a special CFEngine template file to expand"},
{"edit_xml", cf_bundle, CF_BUNDLE, "XML editing model for file"},
{"edit_defaults", cf_body, CF_EDITS_BODY, "Default promise details for file edits"},
{"file_select", cf_body, CF_FILEFILTER_BODY, "Choose which files select in a search"},
{"link_from", cf_body, CF_LINKTO_BODY, "Criteria for linking file from a source"},
{"move_obstructions", cf_opts, CF_BOOL, "true/false whether to move obstructions to file-object creation", "false"},
Expand Down
1 change: 1 addition & 0 deletions src/prototypes3.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ int AppendIfNoSuchLine(char *filename, char *line);
/* files_editline.c */

int ScheduleEditLineOperations(char *filename, Bundle *bp, Attributes a, Promise *pp);
Bundle *MakeTemporaryBundleFromTemplate(Attributes a,Promise *pp);

/* files_links.c */

Expand Down