-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_example.py
More file actions
62 lines (51 loc) · 2.21 KB
/
git_example.py
File metadata and controls
62 lines (51 loc) · 2.21 KB
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
import subprocess, os
from typing import Dict, Union
from cfengine_module_library import PromiseModule, ValidationError, Result
# NOTE: cfengine_module_library can be found here: https://github.com/cfengine/modules/blob/master/libraries/python/cfengine_module_library.py
# 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: str,
attributes: Dict[str, Union[str, int, bool]],
metadata: Dict[str, Dict[str, Union[str, int, bool]]],
):
if not promiser.startswith("/"):
raise ValidationError("File path '{}' must be absolute".format(promiser))
for name, value in attributes.items():
if name != "repository":
raise ValidationError(
"Unknown attribute '{}' for git_example promises".format(name)
)
if name == "repository" and type(value) is not str:
raise ValidationError(
"'repository' must be string for git_example promises"
)
def evaluate_promise(
self,
promiser: str,
attributes: Dict[str, Union[str, int, bool]],
metadata: Dict[str, Dict[str, Union[str, int, bool]]],
):
if not promiser.startswith("/"):
raise ValidationError("File path must be absolute")
folder = promiser
url = attributes["repository"]
assert type(url) is str # Ensured in validate_promise
if os.path.exists(folder):
return Result.KEPT
self.log_info("Cloning '{}' -> '{}'...".format(url, folder))
_ = subprocess.run(
["git", "clone", str(url), folder],
capture_output=True
)
if os.path.exists(folder):
self.log_info("Successfully cloned '{}' -> '{}'".format(url, folder))
return Result.REPAIRED
else:
self.log_error("Failed to clone '{}' -> '{}'".format(url, folder))
return Result.REPAIRED
if __name__ == "__main__":
GitExamplePromiseTypeModule().start()