set_quoted_values: second field separator in line produces undesired result

12 views
Skip to first unread message

Bernd Kalbfuss-Zimmermann

unread,
Jun 16, 2017, 3:50:43 PM6/16/17
to help-c...@googlegroups.com
Hello everyone,

I am pretty new to cfengine and am currently taken my first steps. So
please forgive my ignorance. As a first challenge I am trying to install
an apache2 web server, mysql and the vimbadmin mail adminstration
frontend. So far so good. I have been able to install the apache2, mysql
and vimbadmin. I am able to create the vimbadmin configuration file, but
I am stuck with editing the ini style configuration file.

I am using a copy of the set_quoted_values edit_line bundle of the
standard library (v3.5) in order to adjust parameter settings (see end
of message). The only change in the edit_line bundle I have made is to
replace the "#" for an ";" to indicate comments in the file. Works
pretty well, however, I am having troubles with a single parameter.
Specifically, I am trying to edit "defaults.mailbox.maildir" to match
the following value:

defaults.mailbox.maildir = "maildir:/srv/vmail/%d/%u/mail:LAYOUT=fs"

What I get instead is:

defaults.mailbox.maildir = "maildir:/srv/vmail/%d/%u/mail:LAYOUT=fs"=fs"

When I checked the distribution config file, from which the
configuration is intialized, I found that the parameter is already set
to the string I intend it to set to. What seems to be happening is that
the second "=" in the string is recognized as additional field
separater. Consequently, col("=","2",' "$($(v)[$(index)])"',"set") only
replaces the part between the two equal signs and not the complete
string on the RHS. So I believe I identified the root cause, but how can
I fix?

I assume I am not the first person who has stumbled over this problem.
Any recommendations?

Cheers,

Bernd

--

bundle edit_line va_set_quoted_values(v)
{
# Sets the RHS of variables in shell-like files
# that is:
# LHS="RHS"
# Adds a new line if no LHS exists
# repairs RHS values if one does exist
# If the line is commented out with ;, it gets uncommented first.
#
# 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:
# If the line is there, but commented out, first uncomment it
";+\s*$(index)\s*=.*"
edit_field => col("=","1","$(index)","set");

# 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";

replace_patterns:
"\s*$(index)\s*=.*"
replace_with => value('$(index) = "$($(v)[$(index)])"');

insert_lines:
'$(index) = "$($(v)[$(index)])"'
comment => "Insert a variable definition",
ifvarclass => "!$(cindex[$(index)])_in_file";
}


Bernd Kalbfuss-Zimmermann

unread,
Jun 16, 2017, 3:54:36 PM6/16/17
to help-c...@googlegroups.com
Apologies. Please ignore the "replace_patterns" section in va_set_quoted_values at the end of my previous message. It results from an experiment and was not there initially.

Neil H. Watson

unread,
Jun 16, 2017, 4:18:54 PM6/16/17
to help-c...@googlegroups.com
Welcome to CFEngine,

It's a good best practice to not try and edit files line by line. It's better
to promise the whole file using a template:

http://watson-wilson.ca/blog/2015/03/26/mustache-template-in-cfengine/index.html

--
Neil H Watson @neil_h_watson
CFEngine reporting: https://github.com/neilhwatson/delta_reporting
CFEngine policy: https://github.com/neilhwatson/evolve_cfengine_freelib
CFEngine and vim: https://github.com/neilhwatson/vim_cf3
Reply all
Reply to author
Forward
0 new messages