|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import logging |
| 3 | +import os.path |
| 4 | + |
| 5 | +import click |
| 6 | +import yaml |
| 7 | + |
| 8 | +from plugin_kind import PluginKind |
| 9 | +from plugin_static import PluginStatic |
| 10 | + |
| 11 | +PLUGIN_LOOKUP = { |
| 12 | + "kind": PluginKind, |
| 13 | + "static": PluginStatic, |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +def init_plugin(plugin_kind, config_path): |
| 18 | + plugin_maker = PLUGIN_LOOKUP.get(plugin_kind) |
| 19 | + if plugin_maker is None: |
| 20 | + raise ValueError(f"unknown plugin '{plugin_kind}'") |
| 21 | + return plugin_maker(config_path) |
| 22 | + |
| 23 | + |
| 24 | +def load_spec(clusterspec_path): |
| 25 | + with open(clusterspec_path, "rb") as fileobj: |
| 26 | + return yaml.load(fileobj, Loader=yaml.SafeLoader) |
| 27 | + |
| 28 | + |
| 29 | +@click.group() |
| 30 | +def cli(): |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +@cli.command() |
| 35 | +@click.argument('plugin_kind', type=click.Choice(list(PLUGIN_LOOKUP), case_sensitive=False)) |
| 36 | +@click.argument('plugin_config', type=click.Path(exists=True, dir_okay=False)) |
| 37 | +@click.argument('clusterspec_path', type=click.Path(exists=True, dir_okay=False)) |
| 38 | +@click.argument('cluster_id', type=str, default="default") |
| 39 | +def create(plugin_kind, plugin_config, clusterspec_path, cluster_id): |
| 40 | + clusterspec = load_spec(clusterspec_path)['clusters'] |
| 41 | + plugin = init_plugin(plugin_kind, plugin_config) |
| 42 | + clusterinfo = clusterspec[cluster_id] |
| 43 | + plugin.create_cluster(cluster_id, clusterinfo['branch'], os.path.abspath(clusterinfo['kubeconfig'])) |
| 44 | + |
| 45 | + |
| 46 | +@cli.command() |
| 47 | +@click.argument('plugin_kind', type=click.Choice(list(PLUGIN_LOOKUP), case_sensitive=False)) |
| 48 | +@click.argument('plugin_config', type=click.Path(exists=True, dir_okay=False)) |
| 49 | +@click.argument('clusterspec_path', type=click.Path(exists=True, dir_okay=False)) |
| 50 | +@click.argument('cluster_id', type=str, default="default") |
| 51 | +def delete(plugin_kind, plugin_config, clusterspec_path, cluster_id): |
| 52 | + plugin = init_plugin(plugin_kind, plugin_config) |
| 53 | + plugin.delete_cluster(cluster_id) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) |
| 58 | + cli() |
0 commit comments