88from plugin_kind import PluginKind
99from plugin_static import PluginStatic
1010
11+
1112PLUGIN_LOOKUP = {
1213 "kind" : PluginKind ,
1314 "static" : PluginStatic ,
1415}
1516
1617
17- def init_plugin (plugin_kind , config_path ):
18+ def load_config (path = None ):
19+ if path :
20+ with open (path , "rb" ) as fileobj :
21+ cfg = yaml .load (fileobj , Loader = yaml .SafeLoader )
22+ if not isinstance (cfg , dict ):
23+ raise RuntimeError ('clusters.yaml must be a YAML dict' )
24+ if 'clusters' not in cfg or not isinstance (cfg ['clusters' ], dict ):
25+ raise RuntimeError ('clusters.yaml must be a YAML dict, and so must be .clusters' )
26+ return cfg
27+ path = os .path .join (os .path .expanduser ('~' ), '.config' , 'scs' , 'clusters.yaml' )
28+ if os .path .exists (path ):
29+ return load_config (path )
30+ raise FileNotFoundError ('config' )
31+
32+
33+ def init_plugin (plugin_kind , config ):
1834 plugin_maker = PLUGIN_LOOKUP .get (plugin_kind )
1935 if plugin_maker is None :
2036 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 )
37+ return plugin_maker (config )
2738
2839
2940@click .group ()
@@ -32,27 +43,25 @@ def cli():
3243
3344
3445@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 ))
3846@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' ]))
47+ @cli .pass_obj
48+ def create (cfg , cluster_id ):
49+ spec = cfg [cluster_id ]
50+ config = spec ['config' ]
51+ config ['name' ] = cluster_id
52+ init_plugin (spec ['kind' ], config ).create_cluster ()
4453
4554
4655@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 ))
5056@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 )
57+ @cli .pass_obj
58+ def delete (cfg , cluster_id ):
59+ spec = cfg [cluster_id ]
60+ config = spec ['config' ]
61+ config ['name' ] = cluster_id
62+ init_plugin (spec ['kind' ], config ).delete_cluster ()
5463
5564
5665if __name__ == '__main__' :
5766 logging .basicConfig (format = '%(levelname)s: %(message)s' , level = logging .INFO )
58- cli ()
67+ cli (obj = load_config () )
0 commit comments