Skip to content

Commit

Permalink
work in progress on script consolidation for kubernetes, #172
Browse files Browse the repository at this point in the history
  • Loading branch information
mmguero committed Apr 18, 2023
1 parent 3e2c208 commit d2e1760
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
43 changes: 33 additions & 10 deletions scripts/malcolm_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Copyright (c) 2023 Battelle Energy Alliance, LLC. All rights reserved.

import base64
import glob
import os

Expand Down Expand Up @@ -348,25 +349,47 @@ def StartMalcolm(namespace, malcolmPath, configPath):
results_dict['create_namespaced_config_map']['result'] = dict()
for configMapName, configMapFiles in MALCOLM_CONFIGMAPS.items():
try:
configMap = kubeImported.client.V1ConfigMap()
configMap.metadata = kubeImported.client.V1ObjectMeta(name=configMapName)
configMap.data = {}
dataMap = {}
binaryDataMap = {}
for fname in configMapFiles:
if os.path.isfile(fname):
configMap.data[os.path.basename(fname)] = file_contents(fname)
contents = file_contents(
fname,
binary_fallback=True,
)
if hasattr(contents, 'decode'):
binaryDataMap[os.path.basename(fname)] = base64.b64encode(contents).decode('utf-8')
else:
dataMap[os.path.basename(fname)] = contents
elif os.path.isdir(fname):
for subfname in glob.iglob(os.path.join(os.path.join(fname, '**'), '*'), recursive=True):
if os.path.isfile(subfname):
configMap.data[os.path.basename(subfname)] = file_contents(subfname)
contents = file_contents(
subfname,
binary_fallback=True,
)
if hasattr(contents, 'decode'):
binaryDataMap[os.path.basename(subfname)] = base64.b64encode(contents).decode(
'utf-8'
)
else:
dataMap[os.path.basename(subfname)] = contents
results_dict['create_namespaced_config_map']['result'][
configMapName
] = client.create_namespaced_config_map(
namespace=namespace,
body=configMap,
body=kubeImported.client.V1ConfigMap(
metadata=kubeImported.client.V1ObjectMeta(
name=configMapName,
namespace=namespace,
),
data=dataMap if dataMap else {},
binary_data=binaryDataMap if binaryDataMap else {},
),
).metadata
except kubeImported.client.rest.ApiException as x:
if x.status != 409:
if not results_dict['create_namespaced_config_map']['error']:
if 'error' not in results_dict['create_namespaced_config_map']:
results_dict['create_namespaced_config_map']['error'] = dict()
results_dict['create_namespaced_config_map']['error'][
os.path.basename(configMapName)
Expand All @@ -392,7 +415,7 @@ def StartMalcolm(namespace, malcolmPath, configPath):
).metadata
except kubeImported.client.rest.ApiException as x:
if x.status != 409:
if not results_dict['create_namespaced_config_map_from_env_file']['error']:
if 'error' not in results_dict['create_namespaced_config_map_from_env_file']:
results_dict['create_namespaced_config_map_from_env_file']['error'] = dict()
results_dict['create_namespaced_config_map_from_env_file']['error'][
os.path.basename(envFileName)
Expand All @@ -419,14 +442,14 @@ def StartMalcolm(namespace, malcolmPath, configPath):
)
except kubeImported.client.rest.ApiException as x:
if x.status != 409:
if not results_dict['create_from_yaml']['error']:
if 'error' not in results_dict['create_from_yaml']:
results_dict['create_from_yaml']['error'] = dict()
results_dict['create_from_yaml']['error'][os.path.basename(yamlName)] = LoadStrIfJson(str(x))
if not results_dict['create_from_yaml']['error'][os.path.basename(yamlName)]:
results_dict['create_from_yaml']['error'][os.path.basename(yamlName)] = str(x)
except kubeImported.utils.FailToCreateError as fe:
if [exc for exc in fe.api_exceptions if exc.status != 409]:
if not results_dict['create_from_yaml']['error']:
if 'error' not in results_dict['create_from_yaml']:
results_dict['create_from_yaml']['error'] = dict()
results_dict['create_from_yaml']['error'][os.path.basename(yamlName)] = LoadStrIfJson(str(fe))
if not results_dict['create_from_yaml']['error'][os.path.basename(yamlName)]:
Expand Down
19 changes: 16 additions & 3 deletions scripts/malcolm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,23 @@ def touch(filename):

###################################################################################################
# read the contents of a text file
def file_contents(filename, encoding='utf8'):
def file_contents(filename, encoding='utf-8', binary_fallback=False):
if os.path.isfile(filename):
with open(filename, encoding=encoding) as f:
return f.read()
decodeErr = False

try:
with open(filename, 'r', encoding=encoding) as f:
return f.read()
except (UnicodeDecodeError, AttributeError):
if binary_fallback:
decodeErr = True
else:
raise

if decodeErr and binary_fallback:
with open(filename, 'rb') as f:
return f.read()

else:
return None

Expand Down

0 comments on commit d2e1760

Please sign in to comment.