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 14, 2023
1 parent a376bb5 commit cceb085
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
51 changes: 13 additions & 38 deletions scripts/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import time

from malcolm_common import (
MalcolmTmpPath,
AskForPassword,
AskForString,
BoundPath,
ChooseOne,
DetermineYamlFileFormat,
DisplayMessage,
DisplayProgramBox,
GetUidGidFromComposeFile,
Expand All @@ -35,6 +35,9 @@
MainDialog,
MalcolmAuthFilesExist,
MalcolmPath,
MalcolmTmpPath,
OrchestrationFramework,
OrchestrationFrameworksSupported,
PLATFORM_WINDOWS,
posInt,
ScriptPath,
Expand All @@ -56,10 +59,14 @@
str2bool,
pushd,
)
import malcolm_kubernetes

from malcolm_kubernetes import (
PrintPodStatus,
PrintNodeStatus,
)

from base64 import b64encode
from collections import defaultdict, namedtuple
from enum import Flag, auto
from subprocess import PIPE, STDOUT, DEVNULL, Popen, TimeoutExpired
from urllib.parse import urlparse

Expand Down Expand Up @@ -93,16 +100,6 @@ def __exit__(self, *args):
yamlImported = None


###################################################################################################
class OrchestrationFramework(Flag):
UNKNOWN = auto()
DOCKER_COMPOSE = auto()
KUBERNETES = auto()


OrchestrationFrameworksSupported = OrchestrationFramework.DOCKER_COMPOSE | OrchestrationFramework.KUBERNETES


###################################################################################################
try:
from colorama import init as ColoramaInit, Fore, Back, Style
Expand All @@ -113,28 +110,6 @@ class OrchestrationFramework(Flag):
coloramaImported = False


###################################################################################################
# determine if a YAML file looks like a docker-compose.yml file or a kubeconfig file
def determineYamlFileFormat(inputFileName):
global yamlImported

result = OrchestrationFramework.UNKNOWN
try:
with open(inputFileName, 'r') as cf:
orchestrationYaml = yamlImported.safe_load(cf)

if isinstance(orchestrationYaml, dict):
if any(key in orchestrationYaml for key in ('apiVersion', 'clusters', 'contexts', 'kind')):
result = OrchestrationFramework.KUBERNETES
elif 'services' in orchestrationYaml:
result = OrchestrationFramework.DOCKER_COMPOSE

except Exception as e:
eprint(f'Error deciphering {args.composeFile}: {e}')

return result


###################################################################################################
def checkEnvFilesExist():
global args
Expand Down Expand Up @@ -381,9 +356,9 @@ def status():

elif orchMode is OrchestrationFramework.KUBERNETES:
try:
malcolm_kubernetes.PrintNodeStatus()
PrintNodeStatus()
print()
malcolm_kubernetes.PrintPodStatus(namespace=args.namespace)
PrintPodStatus(namespace=args.namespace)
print()
except Exception as e:
eprint(f'Error getting {args.namespace} status: {e}')
Expand Down Expand Up @@ -1858,7 +1833,7 @@ def main():
if not yamlImported:
exit(2)

if not ((orchMode := determineYamlFileFormat(args.composeFile)) and (orchMode in OrchestrationFrameworksSupported)):
if not ((orchMode := DetermineYamlFileFormat(args.composeFile)) and (orchMode in OrchestrationFrameworksSupported)):
raise Exception(f'{args.composeFile} must be a docker-compose or kubeconfig YAML file')

with pushd(MalcolmPath):
Expand Down
33 changes: 32 additions & 1 deletion scripts/malcolm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from malcolm_utils import eprint, str2bool, run_process, deep_get

from collections import defaultdict, namedtuple
from enum import IntFlag, auto
from enum import Flag, IntFlag, auto

try:
from pwd import getpwuid
Expand Down Expand Up @@ -81,6 +81,15 @@ class UserInterfaceMode(IntFlag):
HOMEBREW_INSTALL_URLS = defaultdict(lambda: 'https://brew.sh/')


class OrchestrationFramework(Flag):
UNKNOWN = auto()
DOCKER_COMPOSE = auto()
KUBERNETES = auto()


OrchestrationFrameworksSupported = OrchestrationFramework.DOCKER_COMPOSE | OrchestrationFramework.KUBERNETES


##################################################################################################
def ReplaceBindMountLocation(line, location, linePrefix):
if os.path.isdir(location):
Expand Down Expand Up @@ -585,6 +594,28 @@ def MalcolmAuthFilesExist(configDir=None):
)


###################################################################################################
# determine if a YAML file looks like a docker-compose.yml file or a kubeconfig file
def DetermineYamlFileFormat(inputFileName):
result = OrchestrationFramework.UNKNOWN

if yamlImported := YAMLDynamic():
try:
with open(inputFileName, 'r') as cf:
orchestrationYaml = yamlImported.safe_load(cf)

if isinstance(orchestrationYaml, dict):
if any(key in orchestrationYaml for key in ('apiVersion', 'clusters', 'contexts', 'kind')):
result = OrchestrationFramework.KUBERNETES
elif 'services' in orchestrationYaml:
result = OrchestrationFramework.DOCKER_COMPOSE

except Exception as e:
eprint(f'Error deciphering {inputFileName}: {e}')

return result


###################################################################################################
# download to file
def DownloadToFile(url, local_filename, debug=False):
Expand Down
8 changes: 6 additions & 2 deletions scripts/malcolm_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

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

MALCOLM_IMAGE_PREFIX = 'ghcr.io/idaholab/malcolm/'

from concurrent.futures import ThreadPoolExecutor, as_completed

from malcolm_common import (
KubernetesDynamic,
)
from malcolm_utils import (
eprint,
tablify,
)


###################################################################################################
MALCOLM_IMAGE_PREFIX = 'ghcr.io/idaholab/malcolm/'


###################################################################################################
def _nanocore_to_millicore(n):
n = int(n[:-1])
return str(round(n / 1000000, 2)) + 'm'
Expand Down

0 comments on commit cceb085

Please sign in to comment.