Skip to content

Commit

Permalink
Adjusted site-up example to be aligned with latest blog post changes
Browse files Browse the repository at this point in the history
See:
cfengine/website#972

Signed-off-by: Ole Herman Schumacher Elgesem <[email protected]>
  • Loading branch information
olehermanse committed Mar 7, 2024
1 parent 75ecda1 commit aa4b062
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
14 changes: 6 additions & 8 deletions examples/site-up/site_up.cf
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ promise agent site_up
}

bundle agent site_up_autorun
# @brief Example illustrating use of site_up promise type
{
meta:

"tags" slist => { "autorun" };
"tags" slist => { "autorun" };

site_up:
"http://172.28.128.10";
"https://cfengine.com/";
"https://cfengine2.com/";
"https://unavailable.com"
skip_ssl_verification => "true";
"http://172.28.128.10";
"https://cfengine.com/";
"https://cfengine2.com/";
"https://unavailable.com"
skip_ssl_verification => "true";
}
18 changes: 9 additions & 9 deletions examples/site-up/site_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

class SiteUpPromiseTypeModule(PromiseModule):
def __init__(self):
super().__init__("site_up_promise_module", "0.0.2")
super().__init__("site_up_promise_module", "0.0.3")

def is_url_valid(self, url):
regex = re.compile(
r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)",
re.IGNORECASE,
)
return re.match(regex, url) is not None

def validate_promise(self, promiser, attributes, metadata):
if not self.is_url_valid(promiser):
Expand All @@ -24,7 +31,7 @@ def evaluate_promise(self, url, attributes, metadata):
error = None
try:
code = urllib.request.urlopen(url, context=ssl_ctx).getcode()
self.log_info(f"Site '{url}' is UP!")
self.log_verbose(f"Site '{url}' is UP!")
return Result.KEPT
except urllib.error.HTTPError as e:
# HTTPError exception returns response code and useful when handling exotic HTTP errors
Expand All @@ -39,13 +46,6 @@ def evaluate_promise(self, url, attributes, metadata):
self.log_error(error)
return Result.NOT_KEPT

def is_url_valid(self, url):
regex = re.compile(
r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)",
re.IGNORECASE,
)
return re.match(regex, url) is not None


if __name__ == "__main__":
SiteUpPromiseTypeModule().start()

0 comments on commit aa4b062

Please sign in to comment.