diff --git a/source/250-101-Scalars-0018-scalar.cf b/source/250-101-Scalars-0018-scalar.cf index 90f7865..61c3d9b 100644 --- a/source/250-101-Scalars-0018-scalar.cf +++ b/source/250-101-Scalars-0018-scalar.cf @@ -6,8 +6,8 @@ bundle agent main { "greeting" string => "Hello. My name is $(name). "; - "name" - string => "Inigo Montoya"; +# "name" +# string => "Inigo Montoya"; reports: diff --git a/source/250-101-Scalars-0411-variable_scope_2.cf b/source/250-101-Scalars-0411-variable_scope_2.cf index 6afc2ff..43d97f3 100644 --- a/source/250-101-Scalars-0411-variable_scope_2.cf +++ b/source/250-101-Scalars-0411-variable_scope_2.cf @@ -1,5 +1,10 @@ # Simplify / break in parts +body common control { + bundlesequence => { bundle_0, "bundle_1", bundle_2, bundle_4, }; +} + + bundle agent bundle_0 { vars: "fruit" string => "banana"; reports: "This is bundle_0"; @@ -17,3 +22,4 @@ bundle agent bundle_2 { } bundle agent bundle_4 { vars: "xyzzy" string => "magic"; } + diff --git a/source/250-120-Lists-0260-List_variables_and_implicit_looping.cf b/source/250-120-Lists-0260-List_variables_and_implicit_looping.cf index 41b1987..01efd67 100644 --- a/source/250-120-Lists-0260-List_variables_and_implicit_looping.cf +++ b/source/250-120-Lists-0260-List_variables_and_implicit_looping.cf @@ -12,8 +12,15 @@ bundle agent main { "hamburgers", }; + "second_list" + slist => { + "some item", + @(shopping_list), + "last item", + }; + reports: - "Buy $(shopping_list)"; + "Buy $(second_list)"; } diff --git a/source/250-120-Lists-0290-Lists_of_integers.cf b/source/250-120-Lists-0290-Lists_of_integers.cf index a192395..bed4c19 100644 --- a/source/250-120-Lists-0290-Lists_of_integers.cf +++ b/source/250-120-Lists-0290-Lists_of_integers.cf @@ -5,7 +5,7 @@ bundle agent main { "my_list" comment => "Demonstrate a list of integers", - ilist => { "1", "2", "3" }; + ilist => { 1, 2, 3 }; reports: diff --git a/source/250-120-Lists-0300-Lists_of_real_numbers.cf b/source/250-120-Lists-0300-Lists_of_real_numbers.cf index c5f9a0f..d2abf97 100644 --- a/source/250-120-Lists-0300-Lists_of_real_numbers.cf +++ b/source/250-120-Lists-0300-Lists_of_real_numbers.cf @@ -6,7 +6,7 @@ bundle agent main { handle => "rlist_demo", comment => "Create an rlist", - rlist => { "1.5", "3.0", "4.5" }; + rlist => { 1.5, 3.0, 4.5 }; reports: diff --git a/source/250-130-Data_Structures_Arrays-0346-array_values.cf b/source/250-130-Data_Structures_Arrays-0346-array_values.cf index f6e7258..aa9c2b6 100644 --- a/source/250-130-Data_Structures_Arrays-0346-array_values.cf +++ b/source/250-130-Data_Structures_Arrays-0346-array_values.cf @@ -2,16 +2,36 @@ bundle agent main { vars: - "food_prices[Apple]" + any:: + "price_map" + data => '{ "Apple": 58, "Banana": 30 }'; + + "price_of[Apple]" string => "58c"; - "food_prices[Banana]" + "price_of[Banana]" string => "30c"; "food" - slist => getindices("food_prices"); + slist => getindices("price_of"); + + "prices_only" + slist => getvalues("price_of"); + "for_reporting_only" + string => format("%S", "price_map"); reports: - "Values of 'food_prices' array: $(food): $(food_prices[$(food)])"; + "Values of 'price_of' array: $(food): $(price_of[$(food)])"; + + "Just apples: $(price_of[Apple])"; + + "The result of the getindices call includes: $(food)"; + + "For printing only: $(for_reporting_only)"; + + "The prices (of what we don't know) include: $(prices_only)"; + + "Match everything with everything: $(food), $(prices_only)"; + } diff --git a/source/250-210-Methods-0010-simple_example.cf b/source/250-210-Methods-0010-simple_example.cf index abb4314..3901ee9 100644 --- a/source/250-210-Methods-0010-simple_example.cf +++ b/source/250-210-Methods-0010-simple_example.cf @@ -1,3 +1,9 @@ +body file control { + inputs => { + "$(sys.libdir)/stdlib.cf" + }; +} + bundle agent main { vars: @@ -14,6 +20,13 @@ bundle agent remove_user(user) { commands: "/usr/sbin/userdel $(user)"; - "/bin/rm /var/spool/cron/$(user)"; - "/bin/rm /var/mail/$(user)"; + + files: + any:: + "/var/spool/cron/$(user)" + delete => tidy; + + "/var/mail/$(user)" + delete => tidy; + } diff --git a/source/300-015-Basic_Examples_Classes_2-0134-soft_classes_simple.cf b/source/300-015-Basic_Examples_Classes_2-0134-soft_classes_simple.cf index 9de9cb2..5ba2d3c 100644 --- a/source/300-015-Basic_Examples_Classes_2-0134-soft_classes_simple.cf +++ b/source/300-015-Basic_Examples_Classes_2-0134-soft_classes_simple.cf @@ -4,9 +4,19 @@ bundle agent main { "weekend" expression => "Saturday|Sunday"; + classes: + any:: # Valid even as far back as 3.7 "weekday" expression => "Monday|Tuesday|Wednesday|Thursday|Friday"; + Monday|Tuesday|Wednesday|Thursday|Friday:: + "weekday" + expression => "any"; # 3.7 and earlier require expression attribute + + Monday|Tuesday|Wednesday|Thursday|Friday:: + "weekday"; # In 3.10 and later the expression attribute can be left out. + # When left out, it defaults to "any". + reports: weekend:: "Yay! I get to rest today."; diff --git a/source/300-040-Classes_4-0030-Ensuring_httpd_is_running.cf b/source/300-040-Classes_4-0030-Ensuring_httpd_is_running.cf index 08fb7c4..7b3e2b4 100644 --- a/source/300-040-Classes_4-0030-Ensuring_httpd_is_running.cf +++ b/source/300-040-Classes_4-0030-Ensuring_httpd_is_running.cf @@ -2,10 +2,10 @@ bundle agent main { processes: "httpd" - restart_class => "start_httpd"; + restart_class => "httpd_is_not_running"; commands: - start_httpd:: + httpd_is_not_running:: "/etc/init.d/httpd start"; } diff --git a/source/310-040-Ordering-0020-normal_ordering.cf b/source/310-040-Ordering-0020-normal_ordering.cf index f2bb6b1..020ea50 100644 --- a/source/310-040-Ordering-0020-normal_ordering.cf +++ b/source/310-040-Ordering-0020-normal_ordering.cf @@ -3,20 +3,20 @@ bundle agent main { classes: - "file_exists" + "file_exists" # 4 expression => fileexists("/tmp/newfile"); - "file_absent" + "file_absent" # 1 not => fileexists("/tmp/newfile"); files: - "/tmp/newfile" + "/tmp/newfile" # 2 create => "true"; reports: - file_exists:: + file_exists:: # 5 "file /tmp/newfile exists"; - file_absent:: + file_absent:: # 3 "file /tmp/newfile does not exist"; } diff --git a/source/310-050-Knowledge_Management-0260-depends_on.cf b/source/310-050-Knowledge_Management-0260-depends_on.cf index a229d4b..b602683 100644 --- a/source/310-050-Knowledge_Management-0260-depends_on.cf +++ b/source/310-050-Knowledge_Management-0260-depends_on.cf @@ -5,8 +5,8 @@ bundle agent main reports: - "System Check" - handle => "systems_check"; + "We launched" + depends_on => { "ignition" }; "Launch!!" depends_on => { "fuel_check", "systems_check" }, @@ -14,8 +14,12 @@ bundle agent main comment => "Demonstrate flow control with depends_on"; "Fueling..." + depends_on => { "systems_check" }, handle => "fuel_check"; + "System Check" + handle => "systems_check"; + } # Try to think declaratively (not imperatively), and use depends_on diff --git a/source/320-150-Editing_Files-0360-delete_lines.cf b/source/320-150-Editing_Files-0360-delete_lines.cf index f3e53b0..11b7d2f 100644 --- a/source/320-150-Editing_Files-0360-delete_lines.cf +++ b/source/320-150-Editing_Files-0360-delete_lines.cf @@ -3,11 +3,15 @@ bundle agent main { files: "/etc/motd" - handle => "motd", comment => "Create and populate motd", create => "true", edit_line => my_motd; + + "/tmp/motd_example" + create => "true", + edit_line => my_motd; + } diff --git a/source/320-270-0015.mustache b/source/320-270-0015.mustache index 998c566..a5eeb1b 100644 --- a/source/320-270-0015.mustache +++ b/source/320-270-0015.mustache @@ -19,4 +19,6 @@ my hostname is {{vars.sys.fqhost}} The value of foo is {{vars.main.foo}} +The literal value of foo is {{{vars.main.foo}}} + Have a nice day. diff --git a/source/320-270-Mustache_Templates_with_Datastate-0010.cf b/source/320-270-Mustache_Templates_with_Datastate-0010.cf index a0d1f28..d00dfee 100644 --- a/source/320-270-Mustache_Templates_with_Datastate-0010.cf +++ b/source/320-270-Mustache_Templates_with_Datastate-0010.cf @@ -12,7 +12,7 @@ bundle common g { bundle agent main { - vars: "foo" string => "bar"; + vars: "foo" string => "bar/baz"; files: "/etc/motd" diff --git a/source/400-010-Body_Parts-0540-Parts_Setting_group_ownership_based_on_OS.cf b/source/400-010-Body_Parts-0540-Parts_Setting_group_ownership_based_on_OS.cf index 6cd7c1b..9b6cb38 100644 --- a/source/400-010-Body_Parts-0540-Parts_Setting_group_ownership_based_on_OS.cf +++ b/source/400-010-Body_Parts-0540-Parts_Setting_group_ownership_based_on_OS.cf @@ -11,8 +11,13 @@ bundle agent main { body perms admin_group { - linux:: groups => { "wheel" }; - darwin:: groups => { "admin" }; - sunos:: groups => { "sys" }; + linux:: + groups => { "wheel" }; + + darwin:: + groups => { "admin" }; + + sunos:: + groups => { "sys" }; } diff --git a/source/400-010-Body_Parts-0545-parameterized_body.cf b/source/400-010-Body_Parts-0545-parameterized_body.cf index 1fbd9a5..8566f38 100644 --- a/source/400-010-Body_Parts-0545-parameterized_body.cf +++ b/source/400-010-Body_Parts-0545-parameterized_body.cf @@ -9,11 +9,11 @@ bundle agent main { handle => "set_file_attributes_automagically", comment => "Set appropriate file attributes everywhere", create => "true", - perms => set_mode_700_admin_group_and_specified_user("sam"); + perms => admin_700_file_owned_by("mike"); } -body perms set_mode_700_admin_group_and_specified_user(user) { +body perms admin_700_file_owned_by(user) { mode => "0700"; owners => { "$(user)" }; diff --git a/source/430-100-File_Copying-1070-Local_copy_a_single_file.cf b/source/430-100-File_Copying-1070-Local_copy_a_single_file.cf index d239835..82f7040 100644 --- a/source/430-100-File_Copying-1070-Local_copy_a_single_file.cf +++ b/source/430-100-File_Copying-1070-Local_copy_a_single_file.cf @@ -6,6 +6,7 @@ bundle agent main { "/root/etc_group.bak" + edit_defaults => backup_timestamp, copy_from => local_cp("/etc/group"), comment => "Demonstrate local file copy"; diff --git a/source/440-070-Patterns-0780-Classes_ORing_of_classes_and_fileexists.cf b/source/440-070-Patterns-0780-Classes_ORing_of_classes_and_fileexists.cf index 5a05833..7d37404 100644 --- a/source/440-070-Patterns-0780-Classes_ORing_of_classes_and_fileexists.cf +++ b/source/440-070-Patterns-0780-Classes_ORing_of_classes_and_fileexists.cf @@ -10,9 +10,10 @@ bundle agent main handle => "or_list", comment => "Demonstrate list form of class expression useful for including functions", - or => { "linux", - "solaris", - fileexists("/etc/fstab") + or => { + "linux", + "solaris", + fileexists("/etc/fstab"), }; diff --git a/source/440-070-Patterns-0790-Classes_Set_a_private_class_based_on_hard_classes_expression.cf b/source/440-070-Patterns-0790-Classes_Set_a_private_class_based_on_hard_classes_expression.cf index 9d942e6..531c7a4 100644 --- a/source/440-070-Patterns-0790-Classes_Set_a_private_class_based_on_hard_classes_expression.cf +++ b/source/440-070-Patterns-0790-Classes_Set_a_private_class_based_on_hard_classes_expression.cf @@ -1,15 +1,40 @@ bundle agent main { + classes: + GMT_Hr00|GMT_Hr01:: + "maintenance_window_for_storage_changes" + scope => "namespace"; + + GMT_Hr02|GMT_Hr03:: + "maintenance_window_for_ldap_changes" + scope => "namespace"; + + vars: + any:: + "all_maintenance_window_classes" + slist => classesmatching("maintenance_window.*"); + + vars: + any:: + "mw_numbers" + slist => { "1", "2" }; + + classes: + any:: + "some_mw_is_set" + expression => "maintenance_window_$(mw_numbers)"; + classes: "good_technology" + meta => { "some tag", "report" }, handle => "good_technology_class", comment => "Set a custom class based on built-in classes", - expression => "linux|solaris"; + expression => "linux|(solaris.x86_64)"; reports: - good_technology:: + linux|solaris:: "I love good technology" handle => "show_respect", comment => "Show respect for good technology"; diff --git a/source/440-070-Patterns-0830-Classes_Report_type_of_weekday_Uses_ifvarclass.cf b/source/440-070-Patterns-0830-Classes_Report_type_of_weekday_Uses_ifvarclass.cf index 71c0a47..62acb94 100644 --- a/source/440-070-Patterns-0830-Classes_Report_type_of_weekday_Uses_ifvarclass.cf +++ b/source/440-070-Patterns-0830-Classes_Report_type_of_weekday_Uses_ifvarclass.cf @@ -1,35 +1,34 @@ bundle agent main { vars: + any:: "days" handle => "days", comment => "Build a list of days to report day of the week", - slist => { "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - "Saturday", - "Sunday", + slist => { + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday", }; - - - reports: "Hello world! I love $(days)s!" comment => "Report day of the week", - ifvarclass => "$(days)"; + if => "$(days)"; # The above promise creates 7 promises: # "Hello world! I love Mondays!" # comment => "Report day of the week", -# ifvarclass => "Monday"; +# if => "Monday"; # # ... # # "Hello world! I love Sundays!" # comment => "Report day of the week", -# ifvarclass => "Sunday"; +# if => "Sunday"; } diff --git a/source/440-070-Patterns-0840-Classes_GOTCHA.cf b/source/440-070-Patterns-0840-Classes_GOTCHA.cf index 5b410c6..d66c8d4 100644 --- a/source/440-070-Patterns-0840-Classes_GOTCHA.cf +++ b/source/440-070-Patterns-0840-Classes_GOTCHA.cf @@ -2,14 +2,27 @@ bundle agent main { commands: - linux&Hr08:: - "/bin/echo Linux system AND we are in the 8th hour."; + any:: "/bin/echo hello world"; # this promise is NOT in the class "any" !!! - any:: + "/bin/date"; + + "/bin/date"; + + "/bin/date"; + + something:: + "/bin/ls"; + + "/bin/date"; "/bin/date"; + + "/bin/date"; + + "/bin/something-else"; + } diff --git a/source/440-070-Patterns-0870-Classes_Scope.cf b/source/440-070-Patterns-0870-Classes_Scope.cf index be7f9f1..2d4d340 100644 --- a/source/440-070-Patterns-0870-Classes_Scope.cf +++ b/source/440-070-Patterns-0870-Classes_Scope.cf @@ -1,9 +1,25 @@ +# Standalone example - in a real codebase, you can only have ONE body common control. +body common control { + bundlesequence => { + "example_1", + "example_2", + }; +} + bundle agent example_1 { +# For common bundles, the default scope for classes promises is "namespace". +# For agent bundles, the default scope for classes promises is "bundle". +# Either one can be overridden explicitly. classes: "its_monday" expression => "Monday"; + classes: + "its_wednesday" + scope => "namespace", + expression => "Wednesday"; + classes: "its_thur" expression => "Thursday"; @@ -16,6 +32,10 @@ bundle agent example_2 { its_monday:: "Yay! I love Mondays!"; + reports: + its_wednesday:: + "Yay! I love Wednesdays!"; + reports: its_thur:: "Yay! I love Thursdays!"; diff --git a/source/510-050-Special_Variables-0310-Const.cf b/source/510-050-Special_Variables-0310-Const.cf index 159301e..27c7a3c 100644 --- a/source/510-050-Special_Variables-0310-Const.cf +++ b/source/510-050-Special_Variables-0310-Const.cf @@ -22,4 +22,6 @@ bundle agent main { "A newline with either $(const.n) or with $(const.endl) is ok"; "But a string with \n in it does not have a newline!"; + "This line has a double backslash: \\ let's see how it looks"; + "A backslash doublequote: \" like that"; } diff --git a/source/520-060-File_Selection-0520-Select_by_several_things.cf b/source/520-060-File_Selection-0520-Select_by_several_things.cf index 319867a..74c5a7c 100644 --- a/source/520-060-File_Selection-0520-Select_by_several_things.cf +++ b/source/520-060-File_Selection-0520-Select_by_several_things.cf @@ -16,7 +16,7 @@ body file_select compound_filter search_mode => { "777" }; leaf_name => { ".*\.pdf" }; # leaf_name = regex to match - file_result => "leaf_name&mode"; # this is a class expression + file_result => "(leaf_name|mode).!(leaf_name.mode)"; # this is a class expression } # Exercise: delete world-writable PDF files owned by root from /tmp diff --git a/source/extra-examples/adam.cf b/source/extra-examples/adam.cf new file mode 100644 index 0000000..b930518 --- /dev/null +++ b/source/extra-examples/adam.cf @@ -0,0 +1,65 @@ +bundle agent old_main { + + reports: + + linux:: + "running on LINUX"; + + ubuntu_14:: + "running on ubuntu 14"; + + redhat:: + "running on redhat"; + + July:: + "It is the month of July"; +} + +bundle agent main { + + vars: + any:: + "files" + slist => { + "/etc/passwd", + "/etc/shadow", + "/etc/adamfile", + }; + + classes: + any:: + "file_exists$(files)" + expression => fileexists("$(files)"); + + reports: + file_exists_etc_passwd.file_exists_etc_shadow:: + "Both the files exist"; + + reports: + any:: + "$(files) is missing" + unless => fileexists("$(files)"); + + reports: + any:: + "$(files) is present" + if => fileexists("$(files)"); + + classes: + any:: + "adams_class" + handle => "file_check", + comment => "Checks for existence of a file", + and => { + fileexists("/etc/passwd"), + fileexists("/etc/shadow") + }; + + reports: + adams_class:: + "/etc/password and /etc/shadow exists!"; + + !adams_class:: + "/etc/password and/or /etc/shadow missing!!!!"; + +} diff --git a/source/extra-examples/adam2.cf b/source/extra-examples/adam2.cf new file mode 100644 index 0000000..9526463 --- /dev/null +++ b/source/extra-examples/adam2.cf @@ -0,0 +1,57 @@ +bundle agent main { + + methods: + + "any" + + handle => "group_exists", + comment => "make sure the specified group is always present", + usebundle => groupadd("adam"); + + methods: + any:: + "add a students group" + usebundle => groupadd("students"); + +} + +bundle agent groupadd(groupname) { + + vars: + linux:: + "groupadd" + string => "/usr/sbin/groupadd"; + + aix:: + "groupadd" + string => "/sbin/addgroup"; + + hpux:: + "groupadd" + string => "/usr/sbin/addgroup"; + + classes: + "gname" + expression => groupexists("$(groupname)"); + + commands: + !gname:: + "$(groupadd)" + classes => if_repaired("group_$(groupname)_added"), + args => "$(groupname)"; + + reports: + + gname:: + "Group $(groupname) exists!" + unless => "group_$(groupname)_added"; + + any:: + "Group $(groupname) added!" + if => "group_$(groupname)_added"; + +} + +body file control { + inputs => { "$(sys.libdir)/stdlib.cf" }; +} diff --git a/source/extra-examples/adrians_file.cf b/source/extra-examples/adrians_file.cf new file mode 100644 index 0000000..6c20b91 --- /dev/null +++ b/source/extra-examples/adrians_file.cf @@ -0,0 +1,24 @@ +bundle agent adrian { + + vars: + any:: + "fruit" string => "apples"; + special_class:: + "fruit" string => "lemons"; + + reports: + any:: + "This is bundle adrian"; + +} + +bundle agent luke { + + vars: + "fruit" + string => "oranges"; + + reports: + "I like $(adrian.fruit), Luke likes $(luke.fruit)"; + +} diff --git a/source/extra-examples/backup-sbin.cf b/source/extra-examples/backup-sbin.cf new file mode 100644 index 0000000..696bf00 --- /dev/null +++ b/source/extra-examples/backup-sbin.cf @@ -0,0 +1,17 @@ +bundle agent main { + files: + any:: + "/tmp/mirror/." + depth_search => recursive, + copy_from => sbin; +} + +body copy_from sbin { + source => "/usr/local/sbin"; + preserve => "true"; + stealth => "true"; +} + +body depth_search recursive { + depth => "inf"; +} diff --git a/source/extra-examples/classes.cf b/source/extra-examples/classes.cf new file mode 100644 index 0000000..c80c62a --- /dev/null +++ b/source/extra-examples/classes.cf @@ -0,0 +1,10 @@ +bundle agent main { + classes: + any:: + "This_class_name_is_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long"; + + reports: + This_class_name_is_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long:: + "Wow!"; + +} diff --git a/source/extra-examples/comment-days.cf b/source/extra-examples/comment-days.cf new file mode 100644 index 0000000..8b16c3d --- /dev/null +++ b/source/extra-examples/comment-days.cf @@ -0,0 +1,8 @@ +bundle agent main { + files: + any:: + "/tmp/date.txt" + edit_line => comment_lines_matching("[^#].*(Mon|Tue|Wed|Thur|Fri|Sat|Sun).*" , "#"); +} + +body file control { inputs => { "$(sys.libdir)/stdlib.cf" }; } diff --git a/source/extra-examples/conditional_report.cf b/source/extra-examples/conditional_report.cf new file mode 100644 index 0000000..ee36124 --- /dev/null +++ b/source/extra-examples/conditional_report.cf @@ -0,0 +1,5 @@ +bundle agent main { + reports: + ec2_instance:: + "I am in AWS!"; +} diff --git a/source/extra-examples/data_example1.cf b/source/extra-examples/data_example1.cf new file mode 100644 index 0000000..b3a75ce --- /dev/null +++ b/source/extra-examples/data_example1.cf @@ -0,0 +1,24 @@ +bundle agent main { + + vars: + any:: + + "phone_prices[Pixel]" + string => "$800"; + + "phone_prices[Galaxy]" + string => "$700"; + + "phone_prices[iPhone]" + string => "$999"; + + vars: + any:: + "phones" + slist => getindices("phone_prices"); + + reports: + any:: + "The price of a $(phones) is $(phone_prices[$(phones)])"; + +} diff --git a/source/extra-examples/data_example2.cf b/source/extra-examples/data_example2.cf new file mode 100644 index 0000000..51aceae --- /dev/null +++ b/source/extra-examples/data_example2.cf @@ -0,0 +1,22 @@ +bundle agent main { + + vars: + any:: + "phone_prices" + data => ' + { + "Pixel" : "$800", + "Galaxy": "$700", + "iPhone": "$999", + }'; + + vars: + any:: + "phones" + slist => getindices("phone_prices"); + + reports: + any:: + "The price of a $(phones) is $(phone_prices[$(phones)])"; + +} diff --git a/source/extra-examples/data_example3.cf b/source/extra-examples/data_example3.cf new file mode 100644 index 0000000..edd8d82 --- /dev/null +++ b/source/extra-examples/data_example3.cf @@ -0,0 +1,23 @@ +bundle agent main { + + vars: + any:: + "phone_prices" + data => readjson("$(this.promise_dirname)/phone_prices.json"); + + vars: + any:: + "phones" + slist => getindices("phone_prices"); + + reports: + any:: + "The price of a $(phones) is $(phone_prices[$(phones)])"; + + "Attempt to iterate over a data container directly: $(phone_prices)"; + + files: + any:: + "/etc/motd" + +} diff --git a/source/extra-examples/delme.cf b/source/extra-examples/delme.cf new file mode 100644 index 0000000..48ad086 --- /dev/null +++ b/source/extra-examples/delme.cf @@ -0,0 +1,10 @@ +bundle agent apple { + vars: + any:: + "mikes_var" string => "I am a cool var"; +} + +bundle agent orange { + reports: + "apple.mikes_var is: $(apple.mikes_var)"; +} diff --git a/source/extra-examples/delme1.cf b/source/extra-examples/delme1.cf new file mode 100644 index 0000000..d94050b --- /dev/null +++ b/source/extra-examples/delme1.cf @@ -0,0 +1,12 @@ +body file control { + inputs => { + "delme2.cf", + }; +} + +bundle agent my_name_can_be_anything { + vars: + "myvar" string => "This is the first bundle"; + reports: + "$(myvar)"; +} diff --git a/source/extra-examples/delme2.cf b/source/extra-examples/delme2.cf new file mode 100644 index 0000000..e59a92b --- /dev/null +++ b/source/extra-examples/delme2.cf @@ -0,0 +1,6 @@ +bundle agent second_bundle { + vars: + "myvar" string => "This is the second bundle"; + reports: + "$(myvar)"; +} diff --git a/source/extra-examples/example.cf b/source/extra-examples/example.cf new file mode 100644 index 0000000..2080008 --- /dev/null +++ b/source/extra-examples/example.cf @@ -0,0 +1,30 @@ +body common control { + bundlesequence => { "example" }; +} + +bundle agent example { + + classes: + any:: + "file_compare" + expression => isnewerthan("/tmp/later", "/tmp/earlier"); + + classes: + any:: + "do_it" + and => { + "class1", + "class2", + "class3", + "file_compare", + }; + #expression => isnewerthan("/tmp/later", "/tmp/earlier"); + + reports: + do_it:: + "/tmp/later is older than /tmp/earlier"; + + !class1:: + "Class 1 wasn't set"; + +} diff --git a/source/extra-examples/file-edit-example.cf b/source/extra-examples/file-edit-example.cf new file mode 100644 index 0000000..1cc0737 --- /dev/null +++ b/source/extra-examples/file-edit-example.cf @@ -0,0 +1,18 @@ +bundle agent main { + + files: + any:: + "/etc/motd" + edit_template => "$(this.promise_dirname)/whatever_filename.mustache", + #template_data => '{ + #"org": "AT&T", + #"hostname": "$(sys.fqhost)" + #}', + template_method => "mustache"; + + vars: + any:: + "org" + string => "AT&T"; + +} diff --git a/source/extra-examples/file-owners.cf b/source/extra-examples/file-owners.cf new file mode 100644 index 0000000..0329172 --- /dev/null +++ b/source/extra-examples/file-owners.cf @@ -0,0 +1,34 @@ +bundle agent main { + + vars: + any:: + "file_owners" + data => '{ + "/etc/passwd": "root", + "/etc/shadow": "root", + "/tmp/myfile": "mike", + }'; + + any:: + "files" + slist => getindices("file_owners"); + + users: + any:: + "pete" + policy => "absent"; + + files: + any:: + "$(files)" + if => fileexists("$(files)"), + perms => owner("$(file_owners[$(files)])"); + +} + +# Needs standard library to work standalone +body file control { + inputs => { + "$(sys.libdir)/stdlib.cf", + }; +} diff --git a/source/extra-examples/filetimecompare.cf b/source/extra-examples/filetimecompare.cf new file mode 100644 index 0000000..41b952a --- /dev/null +++ b/source/extra-examples/filetimecompare.cf @@ -0,0 +1,25 @@ +bundle agent main { + + classes: + any:: + "motd_newer_than_passwd" + expression => isnewerthan("/etc/motd", "/etc/passwd"); + + reports: + motd_newer_than_passwd:: + "The motd file is newer than the passwd file."; + +} + +bundle common students { + + classes: + any:: + "student" + expression => "working_on_exercises"; + + any:: + "observer" + not => "working_on_exercises"; + +} diff --git a/source/extra-examples/five_files.cf b/source/extra-examples/five_files.cf new file mode 100644 index 0000000..b40e43e --- /dev/null +++ b/source/extra-examples/five_files.cf @@ -0,0 +1,28 @@ +bundle agent main { + + vars: + any:: + "files_to_create" + slist => { + "file1", + "file2", + "file3", + "file4", + "file5", + }; + + files: + any:: + "/tmp/$(files_to_create)" + create => "true"; + + vars: + any:: + "at_index_2" + string => nth("files_to_create", 2); + + reports: + any:: + "At index 2 is $(at_index_2)"; + +} diff --git a/source/extra-examples/methods.cf b/source/extra-examples/methods.cf new file mode 100644 index 0000000..955284f --- /dev/null +++ b/source/extra-examples/methods.cf @@ -0,0 +1,19 @@ +bundle agent main { + + reports: + any:: + "$(this.bundle)"; + + methods: + any:: + "apple"; + +} + +bundle agent apple { + + reports: + any:: + "$(this.bundle)"; + +} diff --git a/source/extra-examples/motd.cf b/source/extra-examples/motd.cf new file mode 100644 index 0000000..7855781 --- /dev/null +++ b/source/extra-examples/motd.cf @@ -0,0 +1,21 @@ +body file control { + inputs => { "$(sys.libdir)/stdlib.cf" }; +} + +bundle agent main { + + files: + any:: + "/etc/motd" + edit_defaults => backup_timestamp, + edit_line => insert_lines("Unauthorized use is forbidden"); + +} + +body agent control { + default_repository => "/var/cfengine/backups"; +} + +body copy_from my_local_cp(banana) { + source => "$(banana)"; +} diff --git a/source/extra-examples/phone_prices.json b/source/extra-examples/phone_prices.json new file mode 100644 index 0000000..630f465 --- /dev/null +++ b/source/extra-examples/phone_prices.json @@ -0,0 +1,5 @@ +{ + "Pixel" : "$800", + "Galaxy": "$700", + "iPhone": "$999", +} diff --git a/source/extra-examples/random.cf b/source/extra-examples/random.cf new file mode 100644 index 0000000..5fa2c11 --- /dev/null +++ b/source/extra-examples/random.cf @@ -0,0 +1,17 @@ +bundle agent main +{ + vars: + "low" string => "4"; + "high" string => "60"; + + "random" int => randomint($(low), $(high)); + classes: + "isabove" expression => isgreaterthan($(random), 3); + + reports: + isabove:: + "The generated random number was above 3"; + + any:: + "Randomly generated '$(random)'"; +} diff --git a/source/extra-examples/regex-markup-0.10.0-1.x86_64.rpm b/source/extra-examples/regex-markup-0.10.0-1.x86_64.rpm new file mode 100644 index 0000000..87c0c26 Binary files /dev/null and b/source/extra-examples/regex-markup-0.10.0-1.x86_64.rpm differ diff --git a/source/extra-examples/simple-methods.cf b/source/extra-examples/simple-methods.cf new file mode 100644 index 0000000..f065827 --- /dev/null +++ b/source/extra-examples/simple-methods.cf @@ -0,0 +1,31 @@ +bundle agent main { + + reports: + any:: + "This is the main bundle"; + + methods: + any:: + "banana"; + + any:: + "The promiser is ignored when there is a usebundle attribute" + usebundle => apple("baz"); + +} + +bundle agent banana { + + reports: + any:: + "This is the banana bundle."; + +} + +bundle agent apple(foo) { + + reports: + any:: + "The apple bundle received parameter $(foo)"; + +} diff --git a/source/extra-examples/stop_processes.cf b/source/extra-examples/stop_processes.cf new file mode 100644 index 0000000..a2937ec --- /dev/null +++ b/source/extra-examples/stop_processes.cf @@ -0,0 +1,16 @@ +bundle agent main { + + vars: + any:: + "process_blacklist" + slist => { + "eggdrop", + "trn", + }; + + processes: + any:: + "$(process_blacklist)" + signals => { "term", "kill" }; + +} diff --git a/source/extra-examples/vince.cf b/source/extra-examples/vince.cf new file mode 100644 index 0000000..a0cad00 --- /dev/null +++ b/source/extra-examples/vince.cf @@ -0,0 +1,31 @@ +bundle agent main { + + methods: + + "any" + comment => "make sure the specified group is always present", + usebundle => bundle_1; + +} + +bundle agent bundle_1 { + + classes: + any:: + "namespace_context" + scope => "namespace", + expression => "whatever"; + + methods: + any:: + "bundle_2"; + +} + + +bundle agent bundle_2 +{ + reports: + webserver:: + "Bundle bundle_1: I am a Web server"; +} diff --git a/source/extra-examples/webserver.cf b/source/extra-examples/webserver.cf new file mode 100644 index 0000000..a369388 --- /dev/null +++ b/source/extra-examples/webserver.cf @@ -0,0 +1,48 @@ +body file control { + inputs => { "$(sys.libdir)/stdlib.cf" }; +} + +body common control { + package_module => $(package_module_knowledge.platform_default); +} + +bundle agent main { + + methods: + any:: + "Set up as not a webserver" + usebundle => webserver("off"); + +} + +bundle agent webserver(on_or_off) { + + classes: + + any:: + "webserver_on" + expression => strcmp("$(on_or_off)", "on"); + + any:: + "webserver_off" + expression => strcmp("$(on_or_off)", "off"); + + packages: + webserver_on:: + "httpd" + policy => "present"; + + webserver_off:: + "httpd" + policy => "absent"; + + services: + webserver_on:: + "httpd" + service_policy => "start"; + + webserver_off:: + "httpd" + service_policy => "stop"; + +} diff --git a/source/extra-examples/whatever_filename.mustache b/source/extra-examples/whatever_filename.mustache new file mode 100644 index 0000000..ff9dbd1 --- /dev/null +++ b/source/extra-examples/whatever_filename.mustache @@ -0,0 +1,36 @@ +UNAUTHORIZED USE FORBIDDEN + +(This file is managed by CFEngine.) + +The organization is {{{vars.main.org}}} + +New reference to org: {{{org}}} + +This host is {{{vars.sys.fqhost}}} + +New reference to hostname: {{{hostname}}} + +{{#vars.sys.ip_addresses}} + One of its IP addresses is {{{.}}} +{{/vars.sys.ip_addresses}} + +{{#classes.linux}} +This is a linux host. + +{{#classes.ubuntu}} +This is an Ubuntu host. +{{/classes.ubuntu}} +{{^classes.ubuntu}} +This is not an Ubuntu host. +{{/classes.ubuntu}} + +{{#classes.centos}} +This is a CentOS host. +{{/classes.centos}} +{{^classes.centos}} +This is not a CentOS host. +{{/classes.centos}} +{{/classes.linux}} +{{^classes.linux}} +This is not a linux host. +{{/classes.linux}} diff --git a/source/hello_world.cf b/source/hello_world.cf index 44d4381..b0e463d 100644 --- a/source/hello_world.cf +++ b/source/hello_world.cf @@ -1,4 +1,8 @@ -bundle agent main +body common control { + bundlesequence => { "hello_world" }; +} + +bundle agent hello_world { reports: @@ -13,4 +17,20 @@ reports: # # "$(myclasses)"; + methods: + "whatever" + usebundle => second; + + methods: + "whatever" + usebundle => second; + } + +bundle agent second { + + reports: + + "This is a reports promise"; + +}