|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import click |
| 4 | +import yaml |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
| 8 | +from subprocess import check_output |
| 9 | +from jsonschema import validate |
| 10 | +from abc import ABC, abstractmethod |
| 11 | + |
| 12 | + |
| 13 | +# The schema of the configuration files we can handle |
| 14 | +schema = """ |
| 15 | +type: object |
| 16 | +properties: |
| 17 | + addons: |
| 18 | + description: "List of addons" |
| 19 | + type: array |
| 20 | + items: |
| 21 | + type: object |
| 22 | + properties: |
| 23 | + name: |
| 24 | + description: "Name of the addon" |
| 25 | + type: string |
| 26 | + status: |
| 27 | + description: "Should the addon be enabled or disabled" |
| 28 | + enum: |
| 29 | + - enable |
| 30 | + - disable |
| 31 | + args: |
| 32 | + description: "Arguments for the addon" |
| 33 | + type: array |
| 34 | + items: |
| 35 | + type: string |
| 36 | + required: ["name"] |
| 37 | +""" |
| 38 | + |
| 39 | + |
| 40 | +class Executor(ABC): |
| 41 | + @abstractmethod |
| 42 | + def Enable(self, addon: str, args: list): |
| 43 | + """ |
| 44 | + Enable an addon |
| 45 | + """ |
| 46 | + pass |
| 47 | + |
| 48 | + @abstractmethod |
| 49 | + def Disable(self, addon: str, args: list): |
| 50 | + """ |
| 51 | + Disable an addon |
| 52 | + """ |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +class EchoCommander(Executor): |
| 57 | + """ |
| 58 | + The class just prints out the commands to be executed. It is used for dry runs and debugging. |
| 59 | + """ |
| 60 | + |
| 61 | + def Enable(self, addon: str, args: list): |
| 62 | + args_str = " ".join(args) |
| 63 | + click.echo(f"microk8s enable {addon} {args_str}") |
| 64 | + |
| 65 | + def Disable(self, addon: str, args: list): |
| 66 | + args_str = " ".join(args) |
| 67 | + click.echo(f"microk8s disable {addon} {args_str}") |
| 68 | + |
| 69 | + |
| 70 | +class CliCommander(Executor): |
| 71 | + """ |
| 72 | + This Executor calls the microk8s wrappers to apply the configuration. |
| 73 | + """ |
| 74 | + |
| 75 | + def __init__(self) -> None: |
| 76 | + super().__init__() |
| 77 | + self.enable_cmd = os.path.expandvars("$SNAP/microk8s-enable.wrapper") |
| 78 | + self.disable_cmd = os.path.expandvars("$SNAP/microk8s-disable.wrapper") |
| 79 | + |
| 80 | + def Enable(self, addon: str, args: list): |
| 81 | + args_str = " ".join(args) |
| 82 | + click.echo(f"microk8s enable {addon} {args_str}") |
| 83 | + command = [self.enable_cmd, addon] |
| 84 | + if len(args) > 0: |
| 85 | + command = command + args |
| 86 | + output = check_output(command) |
| 87 | + click.echo(output) |
| 88 | + |
| 89 | + def Disable(self, addon: str, args: list): |
| 90 | + args_str = " ".join(args) |
| 91 | + click.echo(f"microk8s enable {addon} {args_str}") |
| 92 | + command = [self.disable_cmd, addon] |
| 93 | + if len(args) > 0: |
| 94 | + command = command + args |
| 95 | + output = check_output(command) |
| 96 | + click.echo(output) |
| 97 | + |
| 98 | + |
| 99 | +@click.command("launcher") |
| 100 | +@click.argument("configuration") |
| 101 | +@click.option( |
| 102 | + "--dry", |
| 103 | + is_flag=True, |
| 104 | + required=False, |
| 105 | + default=False, |
| 106 | + help="Do nothing, just print the commands to be executed. (default: false)", |
| 107 | +) |
| 108 | +def launcher(configuration: str, dry): |
| 109 | + """ |
| 110 | + Setup MicroK8s based on the provided CONFIGURATION |
| 111 | +
|
| 112 | + CONFIGURATION is a yaml file |
| 113 | + """ |
| 114 | + |
| 115 | + if not os.path.exists(configuration): |
| 116 | + sys.stderr.write("Please provide a yaml configuration file.\n") |
| 117 | + sys.exit(1) |
| 118 | + |
| 119 | + with open(configuration, "r") as stream: |
| 120 | + try: |
| 121 | + cfg = yaml.safe_load(stream) |
| 122 | + except yaml.YAMLError as exc: |
| 123 | + sys.stderr.write(exc) |
| 124 | + sys.stderr.write("Please provide a valid yaml configuration file.\n") |
| 125 | + sys.exit(2) |
| 126 | + |
| 127 | + validate(cfg, yaml.safe_load(schema)) |
| 128 | + if dry: |
| 129 | + executor = EchoCommander() |
| 130 | + else: |
| 131 | + executor = CliCommander() |
| 132 | + |
| 133 | + for addon in cfg["addons"]: |
| 134 | + args = [] |
| 135 | + if "args" in addon.keys(): |
| 136 | + args = addon["args"] |
| 137 | + if "status" in addon.keys() and addon["status"] == "disable": |
| 138 | + executor.Disable(addon["name"], args) |
| 139 | + else: |
| 140 | + executor.Enable(addon["name"], args) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + launcher() |
0 commit comments