1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
# Copyright 2021 Northern.tech AS
# This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
# To the extent this program is licensed as part of the Enterprise
# versions of Cfengine, the applicable Commercial Open Source License
# (COSL) may apply to this file if you as a licensee so wish it. See
# included file COSL.txt.
#+begin_src cfengine3
body common control
{
bundlesequence => { run };
}
bundle agent run
{
vars:
# you can also readjson() from a file
"groups" data => parsejson('{
"ByGroup":
{
"App1":
[
"GrpA",
"GrpB",
"GrpC"
],
"App2":
[
"GrpX",
"GrpY",
"GrpZ"
]
},
"ByApp":
{
"App1":
[
"Host1",
"Host2",
"Host3"
],
"App2":
[
"Host1",
"Host3"
]
}
}');
methods:
# use the first one on the client
#"go" usebundle => appgroups($(sys.uqhost), @(groups));
"go" usebundle => appgroups("Host1", @(groups));
"go" usebundle => appgroups("Host2", @(groups));
}
bundle agent appgroups(name, g)
{
classes:
# stage (3) now for each APP, define have_app_APP if the host is in APP's host list
"have_app_$(apps)" expression => strcmp($(name), "$(hosts_$(apps))");
# stage (4) define the class have_group_GROUP for every GROUP belonging to APP
"have_group_$(groups_$(apps))" expression => "have_app_$(apps)";
# stage (5) define the class have_group if we found any groups
"have_group" not => strcmp("0", length("$(cname)_list"));
vars:
# stage (1) start here: we get the apps from the list of "by app" keys
"apps" slist => getindices("g[ByApp]");
"apps_str" string => format("%S", "apps");
# stage (2) now for each app, we collect the hosts assigned to it
"hosts_$(apps)" slist => getvalues("g[ByApp][$(apps)]");
"hosts_$(apps)_str" string => format("%S", "hosts_$(apps)");
# stage (2) now for each app, we collect the groups assigned to it
"groups_$(apps)" slist => getvalues("g[ByGroup][$(apps)]");
"groups_$(apps)_str" string => format("%S", "groups_$(apps)");
# stage (5) collect the space-separated group names from an intermediate array
"cname" string => canonify($(name));
"$(cname)_grouplist[$(groups_$(apps))]" string => "1",
if => "have_app_$(apps)";
"$(cname)_grouplist_slist" slist => getvalues("$(cname)_grouplist");
# get the keys of the array and sort them, then join with spaces
"$(cname)_list" slist => getindices("$(cname)_grouplist");
"$(cname)_list_sorted" slist => sort("$(cname)_list", "lex");
"$(cname)_list_spaces" string => join(" ", "$(cname)_list_sorted");
reports:
# stage (1)
"$(this.bundle): looking for $(name)";
"$(this.bundle): apps: $(apps_str)";
# stage (2)
"$(this.bundle): hosts for $(apps): $(hosts_$(apps)_str)";
"$(this.bundle): groups for $(apps): $(groups_$(apps)_str)";
# stage (3)
"$(this.bundle): $(name) is assigned $(apps)"
if => "have_app_$(apps)";
# stage (4)
"$(this.bundle): all groups for $(name) = $(groups_$(apps))"
if => "have_group_$(groups_$(apps))";
# stage (5)
have_group::
"$(this.bundle): space-separated groups for $(name) = $($(cname)_list_spaces)";
}
#+end_src
###############################################################################
#+begin_src example_output
#@ ```
#@ R: appgroups: looking for Host1
#@ R: appgroups: apps: { "App1", "App2" }
#@ R: appgroups: hosts for App1: { "Host1", "Host2", "Host3" }
#@ R: appgroups: hosts for App2: { "Host1", "Host3" }
#@ R: appgroups: groups for App1: { "GrpA", "GrpB", "GrpC" }
#@ R: appgroups: groups for App2: { "GrpX", "GrpY", "GrpZ" }
#@ R: appgroups: Host1 is assigned App1
#@ R: appgroups: Host1 is assigned App2
#@ R: appgroups: all groups for Host1 = GrpA
#@ R: appgroups: all groups for Host1 = GrpB
#@ R: appgroups: all groups for Host1 = GrpC
#@ R: appgroups: all groups for Host1 = GrpX
#@ R: appgroups: all groups for Host1 = GrpY
#@ R: appgroups: all groups for Host1 = GrpZ
#@ R: appgroups: space-separated groups for Host1 = GrpA GrpB GrpC GrpX GrpY GrpZ
#@ R: appgroups: looking for Host2
#@ R: appgroups: Host2 is assigned App1
#@ R: appgroups: all groups for Host2 = GrpA
#@ R: appgroups: all groups for Host2 = GrpB
#@ R: appgroups: all groups for Host2 = GrpC
#@ R: appgroups: space-separated groups for Host2 = GrpA GrpB GrpC
#@ ```
#+end_src
|