0% found this document useful (0 votes)
141 views10 pages

Edit - Line Bundles in CFEngine

This document describes 23 bundles for editing text files in CFEngine. The bundles perform common text processing tasks like inserting, commenting, uncommenting, deleting, and replacing lines; editing fields on a line; expanding templates; and setting variable values in a file. The bundles take parameters like regular expressions, filenames, and associative arrays to customize their behavior.

Uploaded by

Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views10 pages

Edit - Line Bundles in CFEngine

This document describes 23 bundles for editing text files in CFEngine. The bundles perform common text processing tasks like inserting, commenting, uncommenting, deleting, and replacing lines; editing fields on a line; expanding templates; and setting variable values in a file. The bundles take parameters like regular expressions, filenames, and associative arrays to customize their behavior.

Uploaded by

Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

##################################

#################
#
#

edit_line bundles

##################################
#################
1) bundle edit_line insert_lines(lines)
{
insert_lines:
"$(lines)"
comment => "Append lines if they don't exist";

}
##

2) bundle edit_line insert_file(templatefile)


{
insert_lines:
"$(templatefile)"
comment => "Insert the template file into the file being edited",
insert_type => "file";

}
##

3) bundle edit_line comment_lines_matching(regex,comment)


# Comment lines of a file matching a regex
{
replace_patterns:
"^($(regex))$"
replace_with => comment("$(comment)"),
comment => "Search and replace string";
}
##

4) bundle edit_line uncomment_lines_matching(regex,comment)


# Uncomment lines of a file where the regex matches
# the text after the comment string
{
replace_patterns:
"^$(comment)\s?($(regex))$"
replace_with => uncomment,
comment => "Uncomment lines matching a regular expression";
}
##
5) bundle edit_line comment_lines_containing(regex,comment)
# Comment lines of a file containing a regex
{
replace_patterns:
"^(.*$(regex).*)$"
replace_with => comment("$(comment)"),
comment => "Comment out lines in a file";
}
##
6) bundle edit_line uncomment_lines_containing(regex,comment)
# Uncomment lines of a file where the regex matches
# the text after the comment string
{
replace_patterns:
"^$(comment)\s?(.*$(regex).*)$"
replace_with => uncomment,
comment => "Uncomment a line containing a fragment";
}
##

7) bundle edit_line delete_lines_matching(regex)


{
delete_lines:
"$(regex)"
comment => "Delete lines matching regular expressions";

}
##

8) bundle edit_line warn_lines_matching(regex)


{
delete_lines:
"$(regex)"
comment => "Warn about lines in a file",
action => warn_only;
}
##
9) bundle edit_line append_if_no_line(str)
{
insert_lines:
"$(str)"
comment => "Append a line to the file if it doesn't already exist";
}
##
10) bundle edit_line append_if_no_lines(list)
{
insert_lines:
"$(list)"
comment => "Append lines to the file if they don't already exist";
}
##

11) bundle edit_line replace_line_end(start,end)


#
# Lines starting with "$(start)" will get the ending given in "$(end)",
# whitespaces will be left unmodified.
# For example, replace_line_end("ftp", "2121/tcp") would replace
# "ftp
21/tcp"
# with
# "ftp
2121/tcp"
{
field_edits:
"\s*$(start)\s.*"
edit_field => line("(^|\s)$(start)\s*", "2", "$(end)","set");

}
##

12) bundle edit_line append_to_line_end(start,end)


#
# Lines starting with "$(start)" and not ending with "$(end)"
# will get appended with "$(end)", whitespaces will be left unmodified.
# For example, append_to_line_end("kernel", "vga=791") would replace
# "kernel /boot/vmlinuz root=/dev/sda7"
# with
# "kernel /boot/vmlinuz root=/dev/sda7 resume=/dev/sda9 vga=791"
#
# WARNING: Be careful not to have multiple promises matching the same line,
#
which would result in the line growing indefinetively.
{
field_edits:
"\s*$(start)\s.*"
edit_field => line("(^|\s)$(start)\s*", "2", "$(end)","append");
}
##
13) bundle edit_line resolvconf(search,list)
# search is the search domains with space
# list is an slist of nameserver addresses
{
delete_lines:
"search.*"
comment => "Reset search lines from resolver";
"nameserver.*" comment => "Reset nameservers in resolver";

insert_lines:
"search $(search)"
"nameserver $(list)"

comment => "Add search domains to resolver";


comment => "Add name servers to resolver";

##
14) bundle edit_line set_variable_values(v)
# Sets the RHS of variables in the file of the form
#
LHS = RHS
# Adds a new line if no LHS exists, repairs RHS values if one does exist
#
# To use:
#
1) Define an array, where the keys are the LHS and the values are the RHS
#
"stuff[lhs-1]" string => "rhs1";
#
"stuff[lhs-2]" string => "rhs2";
#
2) The parameter passed to the edit_line promise is the fully qualified
#
name of the array (i.e., "bundlename.stuff") WITHOUT any "$" or "@"
{
vars:
"index" slist => getindices("$(v)");
# Be careful if the index string contains funny chars
"cindex[$(index)]" string => canonify("$(index)");
field_edits:
# match a line starting like the key = something
"\s*$(index)\s*=.*"
edit_field => col("=","2","$($(v)[$(index)])","set"),
classes => if_ok("$(cindex[$(index)])_in_file"),
comment => "Match a line starting like key = something";
insert_lines:
"$(index)=$($(v)[$(index)])",

comment => "Insert a variable definition",


ifvarclass => "!$(cindex[$(index)])_in_file";

15) bundle edit_line set_config_values(v)


# Sets the RHS of configuration items in the file of the form
#
LHS RHS
# If the line is commented out with #, it gets uncommented first.
# Adds a new line if none exists.
# The argument is the fully-qualified name of an associative array containing
v[LHS]="rhs"
{
vars:
"index" slist => getindices("$(v)");
# Be careful if the index string contains funny chars
"cindex[$(index)]" string => canonify("$(index)");
replace_patterns:
# If the line is there, maybe commented out, uncomment and replace with
# the correct value
"^\s*($(index)\s+(?!$($(v)[$(index)])).*|# ?$(index)\s+.*)$"
replace_with => value("$(index) $($(v)[$(index)])"),
classes => always("replace_attempted_$(cindex[$(index)])");
insert_lines:
"$(index) $($(v)[$(index)])"
ifvarclass => "replace_attempted_$(cindex[$(index)])";
}
16) bundle edit_line set_config_values_matching(v,pat)
# Sets the RHS of configuration items in the file of the form
#
LHS RHS
# If the line is commented out with #, it gets uncommented first.
# Adds a new line if none exists.
# Only elements of "v" that match the regex "pat" are used
# The argument is the fully-qualified name of an associative array containing
v[LHS]="rhs"
{
vars:
"allparams" slist => getindices("$(v)");
"index"
slist => grep("$(pat)", "allparams");
# Be careful if the index string contains funny chars
"cindex[$(index)]" string => canonify("$(index)");
replace_patterns:
# If the line is there, maybe commented out, uncomment and replace with
# the correct value
"^\s*($(index)\s+(?!$($(v)[$(index)])).*|# ?$(index)\s+.*)$"

replace_with => value("$(index) $($(v)[$(index)])"),


classes => always("replace_attempted_$(cindex[$(index)])");
insert_lines:
"$(index) $($(v)[$(index)])"
ifvarclass => "replace_attempted_$(cindex[$(index)])";
}
##
17) bundle edit_line set_variable_values2(file,v)
#
# Another implementation of set_variable_values.
# The input and output should be exactly the same (except the file name),
# but in some Cfengine versions there are bugs,
# so this bundle can be used as a workaround.
#
{
vars:
"index" slist => getindices("$(v)");
# Be careful if the index string contains funny chars
"cindex[$(index)]" string => canonify("$(index)");
"fieldc_$(cindex[$(index)])" int => getfields("$(index).*","$
(file)","=","FIELD_$(cindex[$(index)])");
classes:
"$(cindex[$(index)])_in_file" expression => strcmp("$(index)=$($(v)[$
(index)])", "$(FIELD_$(index)[1])=$(FIELD_$(index)[2])");
delete_lines: # delete any lhs when no match
"$(index)=.*",
ifvarclass => "!$(cindex[$(index)])_in_file";
insert_lines:
"$(index)=$($(v)[$(index)])",
comment => "Insert a variable definition",
ifvarclass => "!$(cindex[$(index)])_in_file";
}
##
18) bundle edit_line append_users_starting(v)
# For adding to /etc/passwd or etc/shadow, needs
# an array v[username] string => "line..."

{
vars:
"index"

slist => getindices("$(v)");

classes:
"add_$(index)" not => userexists("$(index)");
insert_lines:
"$($(v)[$(index)])",
comment => "Append users into a password file format",
ifvarclass => "add_$(index)";

}
##

19) bundle edit_line append_groups_starting(v)


# For adding groups to /etc/group, needs
# an array v[groupname] string => "line..."
{
vars:
"index"

slist => getindices("$(v)");

classes:
"add_$(index)" not => groupexists("$(index)");
insert_lines:
"$($(v)[$(index)])",
comment => "Append users into a group file format",
ifvarclass => "add_$(index)";
}
##
20) bundle edit_line set_user_field(user,field,val)
# Set the value of field number "field" in
# a :-field formatted file like /etc/passwd
{
field_edits:
"$(user):.*"
comment => "Edit a user attribute in the password file",
edit_field => col(":","$(field)","$(val)","set");
}

##

21) bundle edit_line append_user_field(group,field,allusers)


# For adding users to to a file like /etc/group
# at field position "field", comma separated subfields
{
vars:
"val" slist => { @(allusers) };
field_edits:
"$(group):.*"
comment => "Append users into a password file format",
edit_field => col(":","$(field)","$(val)","alphanum");
}
##
22) bundle edit_line expand_template(templatefile)
# Read in the named text file and expand $(var)
# inside the file
{
insert_lines:
"$(templatefile)"
insert_type => "file",
comment => "Expand variables in the template file",
expand_scalars => "true";

23) bundle edit_line replace_or_add(pattern,line)


#
#
#
#
#

Replace a pattern in a file with


If the pattern is not found, add
The pattern must match the whole
anchored to the start and end of
ambiguity.

a single line.
the line to the file.
line (it is automatically
the line) to avoid

{
vars:
"cline" string => canonify("$(line)");
replace_patterns:
"^(?!$(line)$)$(pattern)$"
replace_with => value("$(line)"),

classes => always("replace_done_$(cline)");


insert_lines:
"$(line)"
ifvarclass => "replace_done_$(cline)";
}

You might also like