forked from gchq/Kai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Gh 07 ui develop
- Loading branch information
Showing
22 changed files
with
1,053 additions
and
515 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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
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
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
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
143 changes: 98 additions & 45 deletions
143
infrastructure/lib/rest-api/lambdas/get_graph_request.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,100 @@ | ||
import os | ||
import boto3 | ||
from graph import Graph | ||
import json | ||
from user import User | ||
|
||
graph = Graph() | ||
user = User() | ||
|
||
def handler(event, context): | ||
""" | ||
Main entrypoint for the HTTP GET lambda functions. This function | ||
serves both GET handlers so returns all graphs if no graphName | ||
is specified in the path parameters | ||
""" | ||
path_params = event["pathParameters"] | ||
return_all = False | ||
graph_name = None | ||
if path_params is None or path_params["graphName"] is None: | ||
return_all = True | ||
else: | ||
graph_name = path_params["graphName"] | ||
|
||
requesting_user = user.get_requesting_cognito_user(event) | ||
|
||
if return_all: | ||
return { | ||
"statusCode": 200, | ||
"body": json.dumps(graph.get_all_graphs(requesting_user)) | ||
} | ||
else: | ||
if not user.is_authorized(requesting_user, graph_name): | ||
return { | ||
"statusCode": 403, | ||
"body": "User: {} is not authorized to retrieve graph: {}".format(requesting_user, graph_name) | ||
} | ||
import subprocess | ||
import logging | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.INFO) | ||
|
||
standard_kubeconfig="/tmp/kubeconfig" | ||
|
||
|
||
class CommandHelper: | ||
@staticmethod | ||
def run_command(cmd, release_name): | ||
succeeded=False | ||
try: | ||
return { | ||
"statusCode": 200, | ||
"body": json.dumps(graph.get_graph(graph.format_graph_name(graph_name))) | ||
} | ||
except Exception as e: | ||
return { | ||
"statusCode": 404, | ||
"body": graph_name + " was not found" | ||
} | ||
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True, cwd="/tmp") | ||
succeeded=True | ||
except subprocess.CalledProcessError as err: | ||
logger.error("Error during excution of command: %s against release name: %s", cmd, release_name) | ||
logger.error(err.output) | ||
|
||
return succeeded | ||
|
||
|
||
class KubeConfigurator: | ||
def run_once(f): | ||
def wrapper(*args, **kwargs): | ||
if not wrapper.has_run: | ||
wrapper.has_run = True | ||
return f(*args, **kwargs) | ||
wrapper.has_run = False | ||
return wrapper | ||
|
||
@run_once | ||
def update_kube_config(self, cluster_name, kubeconfig=standard_kubeconfig): | ||
logger.info("Configuring kubectl for cluster: %s", cluster_name); | ||
subprocess.check_call([ 'aws', 'eks', 'update-kubeconfig', | ||
'--name', cluster_name, | ||
'--kubeconfig', kubeconfig | ||
]) | ||
|
||
|
||
class HelmClient: | ||
__HELM_CMD="helm" | ||
|
||
def __init__(self, cluster_name, kubeconfig=standard_kubeconfig): | ||
KubeConfigurator().update_kube_config(cluster_name=cluster_name, kubeconfig=kubeconfig) | ||
self.kubeconfig = kubeconfig | ||
|
||
def __run(self, instruction, release_name, values=None, chart=None, repo=None): | ||
""" | ||
Runs a Helm command and returns True if it succeeds and False if it fails | ||
""" | ||
cmd = [ self.__HELM_CMD, instruction, release_name ] | ||
if chart is not None: | ||
cmd.append(chart) | ||
if repo is not None: | ||
cmd.extend(["--repo", repo]) | ||
if values is not None: | ||
cmd.extend(["--values", values]) | ||
cmd.extend(["--kubeconfig", self.kubeconfig]) | ||
|
||
return CommandHelper.run_command(cmd, release_name) | ||
|
||
def install_chart(self, release_name, values=None, chart="gaffer", repo="https://gchq.github.io/gaffer-docker"): | ||
""" | ||
Installs a Helm chart and returns True if it Succeeds and False if it fails | ||
""" | ||
return self.__run(instruction="install", release_name=release_name, values=values, chart=chart, repo=repo) | ||
|
||
def uninstall_chart(self, release_name): | ||
""" | ||
Uninstalls a Helm release and returns True if it Succeeds and False if it fails | ||
""" | ||
return self.__run(instruction="uninstall", release_name=release_name) | ||
|
||
|
||
class KubernetesClient: | ||
__KUBECTL_CMD="kubectl" | ||
|
||
def __init__(self, cluster_name, kubeconfig=standard_kubeconfig): | ||
KubeConfigurator().update_kube_config(cluster_name=cluster_name, kubeconfig=kubeconfig) | ||
self.kubeconfig = kubeconfig | ||
|
||
def delete_volumes(self, release_name): | ||
""" | ||
Deletes the Persistent Volume Claims associated to a release_name | ||
""" | ||
# HDFS Datanodes & Namenode | ||
self.__delete_volumes(release_name=release_name, selectors=["app.kubernetes.io/instance={}".format(release_name)]) | ||
|
||
# Zookeeper | ||
self.__delete_volumes(release_name=release_name, selectors=["release={}".format(release_name)]) | ||
|
||
def __delete_volumes(self, release_name, selectors): | ||
cmd = [ self.__KUBECTL_CMD, "delete", "pvc", "--kubeconfig", self.kubeconfig ] | ||
for selector in selectors: | ||
cmd.append("--selector") | ||
cmd.append(selector) | ||
|
||
CommandHelper.run_command(cmd, release_name) |
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
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
Oops, something went wrong.