|
| 1 | +import os |
| 2 | +from cfengine import PromiseModule, ValidationError, Result |
| 3 | + |
| 4 | + |
| 5 | +class SymlinksPromiseTypeModule(PromiseModule): |
| 6 | + |
| 7 | + def __init__(self, **kwargs): |
| 8 | + super(SymlinksPromiseTypeModule, self).__init__( |
| 9 | + name="symlinks_promise_module", |
| 10 | + version="0.0.1", |
| 11 | + **kwargs, |
| 12 | + ) |
| 13 | + |
| 14 | + def is_absolute_dir(v): |
| 15 | + if not os.path.isabs(v): |
| 16 | + raise ValidationError("must be an absolute path, not '{v}'".format(v=v)) |
| 17 | + if not os.path.exists(v): |
| 18 | + raise ValidationError("dir must exists") |
| 19 | + if not os.path.isdir(v): |
| 20 | + raise ValidationError("must be a dir") |
| 21 | + |
| 22 | + def is_absolute_file(v): |
| 23 | + if not os.path.isabs(v): |
| 24 | + raise ValidationError("must be an absolute path, not '{v}'".format(v=v)) |
| 25 | + if not os.path.exists(v): |
| 26 | + raise ValidationError("file must exists") |
| 27 | + if not os.path.isfile(v): |
| 28 | + raise ValidationError("must be a file") |
| 29 | + |
| 30 | + self.add_attribute("directory", str, validator=is_absolute_dir) |
| 31 | + self.add_attribute("file", str, validator=is_absolute_file) |
| 32 | + |
| 33 | + def validate_promise(self, promiser, attributes, metadata): |
| 34 | + model = self.create_attribute_object(promiser, attributes) |
| 35 | + |
| 36 | + if not model.file and not model.directory: |
| 37 | + raise ValidationError("missing 'file' or 'directory' attribute") |
| 38 | + |
| 39 | + if model.file and model.directory: |
| 40 | + raise ValidationError("must specify either 'file' or 'directory', not both") |
| 41 | + |
| 42 | + def evaluate_promise(self, promiser, attributes, metadata): |
| 43 | + model = self.create_attribute_object(promiser, attributes) |
| 44 | + link_target = model.file if model.file else model.directory |
| 45 | + |
| 46 | + if not os.path.exists(promiser): |
| 47 | + try: |
| 48 | + os.symlink( |
| 49 | + link_target, promiser, target_is_directory=bool(model.directory) |
| 50 | + ) |
| 51 | + except FileExistsError: |
| 52 | + self.log_error( |
| 53 | + "Couldn't symlink '{}' to '{}'. A link already exists".format( |
| 54 | + link_target, promiser |
| 55 | + ) |
| 56 | + ) |
| 57 | + return (Result.NOT_KEPT, ["old_link"]) |
| 58 | + except: |
| 59 | + self.log_error( |
| 60 | + "Couldn't symlink '{}' to '{}'".format(link_target, promiser) |
| 61 | + ) |
| 62 | + return (Result.NOT_KEPT, ["unknown"]) |
| 63 | + return (Result.REPAIRED, ["link_created"]) |
| 64 | + |
| 65 | + if not os.path.islink(promiser): |
| 66 | + self.log_error("Symlink '{}' is already a path".format(promiser)) |
| 67 | + return (Result.NOT_KEPT, ["link_is_path"]) |
| 68 | + |
| 69 | + if os.path.realpath(promiser) != link_target: |
| 70 | + try: |
| 71 | + os.unlink(promiser) |
| 72 | + os.symlink( |
| 73 | + link_target, promiser, target_is_directory=bool(model.directory) |
| 74 | + ) |
| 75 | + except FileExistsError: |
| 76 | + self.log_error( |
| 77 | + "Couldn't symlink '{}' to '{}'. A link already exists".format( |
| 78 | + link_target, promiser |
| 79 | + ) |
| 80 | + ) |
| 81 | + return (Result.NOT_KEPT, ["old_link"]) |
| 82 | + except: |
| 83 | + self.log_error( |
| 84 | + "Couldn't symlink '{}' to '{}'".format(link_target, promiser) |
| 85 | + ) |
| 86 | + return (Result.NOT_KEPT, ["unknown"]) |
| 87 | + self.log_info( |
| 88 | + "Symlink '{}' had wrong target. Updated from '{}' to '{}'".format( |
| 89 | + promiser, os.path.realpath(promiser), link_target |
| 90 | + ) |
| 91 | + ) |
| 92 | + return (Result.REPAIRED, ["changed_target"]) |
| 93 | + |
| 94 | + return (Result.KEPT, ["already_existing_link"]) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + SymlinksPromiseTypeModule().start() |
0 commit comments