-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_example.py
46 lines (36 loc) · 1.63 KB
/
git_example.py
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
import sys
import os
from cfengine import PromiseModule, ValidationError, Result
# This is an example implementation of the git promise type.
# To make your own promise type, you will need to replace the code
# in validate_promise() and evaluate_promise().
class GitExamplePromiseTypeModule(PromiseModule):
def validate_promise(self, promiser, attributes, metadata):
if not promiser.startswith("/"):
raise ValidationError(f"File path '{promiser}' must be absolute")
for name, value in attributes.items():
if name != "repository":
raise ValidationError(
f"Unknown attribute '{name}' for git_example promises"
)
if name == "repository" and type(value) is not str:
raise ValidationError(
f"'repository' must be string for git_example promises"
)
def evaluate_promise(self, promiser, attributes, metadata):
if not promiser.startswith("/"):
raise ValidationError("File path must be absolute")
folder = promiser
url = attributes["repository"]
if os.path.exists(folder):
return Result.KEPT
self.log_info(f"Cloning '{url}' -> '{folder}'...")
os.system(f"git clone {url} {folder} 1>/dev/null 2>/dev/null")
if os.path.exists(folder):
self.log_info(f"Successfully cloned '{url}' -> '{folder}'")
return Result.REPAIRED
else:
self.log_error(f"Failed to clone '{url}' -> '{folder}'")
return Result.REPAIRED
if __name__ == "__main__":
GitExamplePromiseTypeModule().start()