From 981cbfb5803f6207cac877395fedcaac0a915cce Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Tue, 30 Jun 2026 11:36:20 +0200 Subject: [PATCH] Update parser with event driven cfengine syntax Ticket: ENT-14192 Changelog: Title Signed-off-by: Victor Moene --- libpromises/Makefile.am | 1 + libpromises/mod_common.c | 2 + libpromises/mod_watch.c | 51 +++++++++++++++++++++++++ libpromises/mod_watch.h | 32 ++++++++++++++++ tests/acceptance/32_cf-watchd/syntax.cf | 28 ++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 libpromises/mod_watch.c create mode 100644 libpromises/mod_watch.h create mode 100644 tests/acceptance/32_cf-watchd/syntax.cf diff --git a/libpromises/Makefile.am b/libpromises/Makefile.am index 88387e12bb..8f60e0f501 100644 --- a/libpromises/Makefile.am +++ b/libpromises/Makefile.am @@ -134,6 +134,7 @@ libpromises_la_SOURCES = \ mod_storage.c mod_storage.h \ mod_knowledge.c mod_knowledge.h \ mod_users.c mod_users.h \ + mod_watch.c mod_watch.h \ modes.c \ monitoring_read.c monitoring_read.h \ ornaments.c ornaments.h \ diff --git a/libpromises/mod_common.c b/libpromises/mod_common.c index aa1ac7bf2a..b85ad5b46b 100644 --- a/libpromises/mod_common.c +++ b/libpromises/mod_common.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -549,6 +550,7 @@ const PromiseTypeSyntax *const CF_ALL_PROMISE_TYPES[] = CF_MEASUREMENT_PROMISE_TYPES, /* mod_measurement.c */ CF_KNOWLEDGE_PROMISE_TYPES, /* mod_knowledge.c */ CF_USERS_PROMISE_TYPES, /* mod_users.c */ + CF_WATCH_PROMISE_TYPES, /* mod_watch.c */ }; const int CF3_MODULES = (sizeof(CF_ALL_PROMISE_TYPES) / sizeof(CF_ALL_PROMISE_TYPES[0])); diff --git a/libpromises/mod_watch.c b/libpromises/mod_watch.c new file mode 100644 index 0000000000..ac659dcd58 --- /dev/null +++ b/libpromises/mod_watch.c @@ -0,0 +1,51 @@ +/* + Copyright 2026 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. +*/ + +#include + +#include + +static const ConstraintSyntax when_constraints[] = +{ + CONSTRAINT_SYNTAX_GLOBAL, + + /* Row models */ + ConstraintSyntaxNewStringList("files_deleted", CF_ANYSTRING, "List of files to watch for deletion", SYNTAX_STATUS_NORMAL), + ConstraintSyntaxNewNull() +}; + +static const BodySyntax when_body = BodySyntaxNew("when", when_constraints, NULL, SYNTAX_STATUS_NORMAL); + +static const ConstraintSyntax CF_EVENT_BODIES[] = +{ + ConstraintSyntaxNewBody("when", &when_body, "Event to watch", SYNTAX_STATUS_NORMAL), + ConstraintSyntaxNewBundle("then", "Bundle to run on event", SYNTAX_STATUS_NORMAL), + ConstraintSyntaxNewNull() +}; + +const PromiseTypeSyntax CF_WATCH_PROMISE_TYPES[] = +{ + PromiseTypeSyntaxNew("watch", "events", CF_EVENT_BODIES, NULL, SYNTAX_STATUS_NORMAL), + PromiseTypeSyntaxNewNull() +}; diff --git a/libpromises/mod_watch.h b/libpromises/mod_watch.h new file mode 100644 index 0000000000..2ba6b3a922 --- /dev/null +++ b/libpromises/mod_watch.h @@ -0,0 +1,32 @@ +/* + Copyright 2026 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. +*/ + +#ifndef CFENGINE_MOD_WATCH_H +#define CFENGINE_MOD_WATCH_H + +#include + +extern const PromiseTypeSyntax CF_WATCH_PROMISE_TYPES[]; + +#endif diff --git a/tests/acceptance/32_cf-watchd/syntax.cf b/tests/acceptance/32_cf-watchd/syntax.cf new file mode 100644 index 0000000000..d7adbff151 --- /dev/null +++ b/tests/acceptance/32_cf-watchd/syntax.cf @@ -0,0 +1,28 @@ +bundle agent main +{ + reports: + "$(this.promise_filename) Pass"; +} + +body when file_deleted(filename) +{ + files_deleted => { "$(filename)" }; +} + +bundle agent create_file(filename) +{ + files: + "$(filename)" create => "true"; +} + +bundle watch event_handler +{ + events: + "Create /tmp/a" + when => file_deleted("/tmp/a"), + then => create_file("/tmp/a"); + + "Create /tmp/b" + when => file_deleted("/tmp/b"), + then => create_file("/tmp/b"); +}