|
| 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("directory 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 | + try: |
| 47 | + os.symlink(link_target, promiser, target_is_directory=bool(model.directory)) |
| 48 | + self.log_info("Created symlink '{}' -> '{}'".format(promiser, link_target)) |
| 49 | + return Result.REPAIRED |
| 50 | + except FileExistsError: |
| 51 | + |
| 52 | + if not os.path.islink(promiser): |
| 53 | + self.log_error("Symlink '{}' is already a path".format(promiser)) |
| 54 | + return Result.NOT_KEPT |
| 55 | + |
| 56 | + if os.path.realpath(promiser) != link_target: |
| 57 | + self.log_warning( |
| 58 | + "Symlink '{}' already exists but has wrong target '{}'".format( |
| 59 | + promiser, os.path.realpath(promiser) |
| 60 | + ) |
| 61 | + ) |
| 62 | + try: |
| 63 | + os.unlink(promiser) |
| 64 | + except FileNotFoundError: |
| 65 | + self.log_error( |
| 66 | + "'{}' is already unlinked from its old target".format(promiser) |
| 67 | + ) |
| 68 | + return Result.NOT_KEPT |
| 69 | + except Exception: |
| 70 | + self.log_error( |
| 71 | + "'{}' has wrong target but couldn't be unlinked: {}".format( |
| 72 | + promiser, e |
| 73 | + ) |
| 74 | + ) |
| 75 | + return Result.NOT_KEPT |
| 76 | + try: |
| 77 | + os.symlink( |
| 78 | + link_target, promiser, target_is_directory=bool(model.directory) |
| 79 | + ) |
| 80 | + except FileExistsError: |
| 81 | + self.log_error( |
| 82 | + "Couldn't symlink '{}' to '{}'. A symlink already exists".format( |
| 83 | + link_target, promiser |
| 84 | + ) |
| 85 | + ) |
| 86 | + return Result.NOT_KEPT |
| 87 | + except FileNotFoundError: |
| 88 | + self.log_error("'{}' doesn't exist".format(link_target)) |
| 89 | + return Result.NOT_KEPT |
| 90 | + except Exception as e: |
| 91 | + self.log_error( |
| 92 | + "Couldn't symlink '{}' to '{}': {}".format( |
| 93 | + link_target, promiser, e |
| 94 | + ) |
| 95 | + ) |
| 96 | + return Result.NOT_KEPT |
| 97 | + |
| 98 | + self.log_info( |
| 99 | + "Corrected symlink '{}' -> '{}'".format(promiser, link_target) |
| 100 | + ) |
| 101 | + return Result.REPAIRED |
| 102 | + |
| 103 | + return Result.KEPT |
| 104 | + |
| 105 | + except FileNotFoundError: |
| 106 | + self.log_error("'{}' doesn't exist".format(promiser)) |
| 107 | + return Result.NOT_KEPT |
| 108 | + |
| 109 | + except Exception as e: |
| 110 | + self.log_error( |
| 111 | + "Couldn't symlink '{}' to '{}': {}".format(link_target, promiser, e) |
| 112 | + ) |
| 113 | + return Result.NOT_KEPT |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + SymlinksPromiseTypeModule().start() |
0 commit comments