Last active
September 26, 2019 18:54
-
-
Save squizzi/5bd3969c362b5581edfd366025b5f962 to your computer and use it in GitHub Desktop.
Load a UCP client bundle in python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
load_env is the functional equivalent of loading a UCP client bundle | |
""" | |
def load_env(client_bundle="/client-bundle"): | |
logging.info("Loading UCP client bundle...") | |
try: | |
# Open the extracted, mounted client_bundle directory and use its | |
# contents | |
bundle_env = client_bundle + "/env.sh" | |
with open(bundle_env) as f: | |
content = f.readlines() | |
except FileNotFoundError as e: | |
logging.error("Unable to load UCP client bundle: {0}".format(e)) | |
for line in content: | |
if line.startswith("export DOCKER_") or line.startswith("export KUBECONFIG"): | |
logging.debug("Applying Docker environment variable: {}".format(line)) | |
env_vars = line.split(" ")[1].split("=") | |
os.environ["{0}".format(env_vars[0])] = "{0}".format(env_vars[1].strip()) | |
# Apply kubectl set-* commands | |
if line.startswith("kubectl"): | |
if "kubectl config" in line: | |
try: | |
# Run each given line | |
run('{}'.format(line.replace("$PWD", bundle_dir).strip()), output=False) | |
except RuntimeError as err: | |
logging.error("Unable to set kubectl config: {}".format(e)) | |
# Apply the final DOCKER_CERT_PATH environment var | |
os.environ["DOCKER_CERT_PATH"] = client_bundle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment