From cc77f7d26d8d73390f9c58b9fd1965a38aa73ef7 Mon Sep 17 00:00:00 2001 From: Riccardo Carlesso Date: Sun, 25 Feb 2024 12:45:15 +0100 Subject: [PATCH] Improve loading and creation of gcloud configurations --- bin/auto_import_config.py | 76 ++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/bin/auto_import_config.py b/bin/auto_import_config.py index 3f53820..67e5f27 100755 --- a/bin/auto_import_config.py +++ b/bin/auto_import_config.py @@ -1,29 +1,68 @@ #!/usr/bin/env python -# TODO rename to `auto_import_config.py ` -# TODO make it su0per NON verbose and when it is auto load it on every CD in the shell :) -# .. like direnv works. +# TODO make it super NON verbose and when it is auto load it on every CD in the shell :) +# .. like direnv works. But first lets make it work :) import yaml import os +ProgVersion = '1.1' +ConfigFileName = '.gcloudconfig.yaml' +SampleConfig = '''\ +######################################################## +# This is a sample from auto_import_config.py v{version} +# Code: https://github.com/palladius/sakura/blob/master/bin/auto_import_config.py +######################################################## +# Once done you can do: gcloud config configurations activate YOUR_VAVORITE_CONFIG +local_config: + auto: true +configurations: + default: + project: my-default-personal-project + compute/region: us-central1 + compute/zone: us-central1-b + account: your.personal.email@gmail.com + # Note: gcloud wont accept this config name if it starts with a number (#btdt) + {this_folder}: + project: my-local-work-project + compute/region: us-central1 + compute/zone: us-central1-b + account: your.work.email@my-job.example.com +''' def inject_all_configs(config_data): 'Given a hash from YAML it creates one gcloud config per key' - # injects config as per tree. + # injects config as per tree. for config_name, properties in config_data['configurations'].items(): - # Create configuration if it doesn't exist + # First verifies if it exists. + ret = os.system(f"gcloud config configurations describe {config_name} >/dev/null") + #print(f"ret = {ret}") + if ret == 0: + print(f"🦘 Config '{config_name}' found: skipping") + # TODO: configurable FORCE: true + #break + #next + continue + print(f"➕ Config '{config_name}' NOT found: creating") + + # Create configuration if it doesn't exist=== os.system(f"gcloud config configurations create {config_name}") for property_name, value in properties.items(): os.system(f"gcloud config set {property_name} {value} --configuration {config_name}") +def local_dirname(): + '''Returns the latest folder of `pwd`.''' + full_dir = os.getcwd() + local_dir = os.path.basename(full_dir) + return local_dir + def parse_local_config(config_data): '''parses the autoconfig: optional stanza.''' if 'auto' in config_data['local_config'] and config_data['local_config']['auto'] == True: # The dictionary has two key children named `local_config` and `auto`, and the value of the `auto` key is equal to `True`. #if local_config is not None and local_config['auto'] == True: full_dir = os.getcwd() - local_dir = os.path.basename(full_dir) + local_dir = local_dirname() # os.path.basename(full_dir) # Alternative: # last_part = current_dir.rsplit('/', 1)[1] print(f'Setting config with same name as local dir: {local_dir}') @@ -31,10 +70,27 @@ def parse_local_config(config_data): else: print(f"local_config[auto] = '{ config_data['local_config']['auto'] }'") +def create_sample_config_name(): + '''Create a reasonable sample filename for me.''' + local_pwd = local_dirname() + ground_config = SampleConfig.format(version=ProgVersion, this_folder=local_pwd) + print (f"sample_config. Try something like: \n$ cat > {ConfigFileName}\n{ground_config}\n# ... Now its time to CRL-D\n") + def main(): print("💛 Riccardo, the intent of this script is to AUTO-load on every `cd SOMEDIR`. This means this script should be SUPER silent and only produce output if something happens.") - with open('.gcloudconfig.yaml', 'r') as file: - config_data = yaml.safe_load(file) + # check if file ConfigFileName exists.. + config_data = {} + if os.path.isfile(ConfigFileName): + with open(ConfigFileName, 'r') as file: + config_data = yaml.safe_load(file) + else: + #except FileNotFoundError: + print(f"{ConfigFileName} not found. Would you like me to create one for you?") + create_sample_config_name() + exit(0) + + # with open(ConfigFileName, 'r') as file: + # config_data = yaml.safe_load(file) inject_all_configs(config_data) @@ -46,6 +102,8 @@ def main(): print('no AUTOCONFIG defined') # todo deleteme. # Final - os.system(f"gcloud config list") + #os.system(f"gcloud config list") + os.system(f"gcloud config configurations list | grep True") + main()