mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Added api to execute commands on node
This commit is contained in:
parent
c86f7ddb0e
commit
293587701d
@ -1,14 +1,13 @@
|
|||||||
import os
|
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import kubernetes.client
|
import kubernetes
|
||||||
|
# import kubernetes.client
|
||||||
from kubernetes.client.rest import ApiException
|
from kubernetes.client.rest import ApiException
|
||||||
|
from kubernetes.stream import stream
|
||||||
|
|
||||||
|
|
||||||
MINIKUBE_URI = 'https://127.0.0.1:8443'
|
|
||||||
MINIKUBE_HOME = os.environ.get('MINIKUBE_HOME', os.environ['HOME'])
|
|
||||||
# TODO: create a default pod spec of BDB
|
# TODO: create a default pod spec of BDB
|
||||||
BDB_POD_SPEC = {}
|
BDB_POD_SPEC = {}
|
||||||
|
|
||||||
@ -16,11 +15,11 @@ BDB_POD_SPEC = {}
|
|||||||
class Node():
|
class Node():
|
||||||
|
|
||||||
def __init__(self, namespace='itest-setup', name=None, spec=BDB_POD_SPEC):
|
def __init__(self, namespace='itest-setup', name=None, spec=BDB_POD_SPEC):
|
||||||
|
kubernetes.config.load_kube_config()
|
||||||
config = kubernetes.client.Configuration()
|
config = kubernetes.client.Configuration()
|
||||||
config.host = MINIKUBE_URI
|
config.assert_hostname = False
|
||||||
config.key_file = os.path.join(MINIKUBE_HOME, '.minikube/client.key')
|
kubernetes.client.Configuration.set_default(config)
|
||||||
config.cert_file = os.path.join(MINIKUBE_HOME, '.minikube/client.crt')
|
|
||||||
config.verify_ssl = False
|
|
||||||
self.api_instance = kubernetes.client.CoreV1Api(kubernetes.client.ApiClient(config))
|
self.api_instance = kubernetes.client.CoreV1Api(kubernetes.client.ApiClient(config))
|
||||||
self.namespace = namespace
|
self.namespace = namespace
|
||||||
self.name = name or uuid.uuid4().hex
|
self.name = name or uuid.uuid4().hex
|
||||||
@ -29,7 +28,10 @@ class Node():
|
|||||||
pod.version = 'v1'
|
pod.version = 'v1'
|
||||||
pod.kind = 'Pod'
|
pod.kind = 'Pod'
|
||||||
pod.metadata = {"name": self.name}
|
pod.metadata = {"name": self.name}
|
||||||
pod.spec = {"containers": [{"name": "myapp-container",
|
pod.spec = {"containers": [{"name": "tendermint",
|
||||||
|
"image": "busybox",
|
||||||
|
"command": ["sh", "-c", "echo Hello Kubernetes! && sleep 3600"]},
|
||||||
|
{"name": "bigchaindb",
|
||||||
"image": "busybox",
|
"image": "busybox",
|
||||||
"command": ["sh", "-c", "echo Hello Kubernetes! && sleep 3600"]}]}
|
"command": ["sh", "-c", "echo Hello Kubernetes! && sleep 3600"]}]}
|
||||||
self.pod = pod
|
self.pod = pod
|
||||||
@ -48,6 +50,7 @@ class Node():
|
|||||||
|
|
||||||
def stop(self, return_after_stopping=True):
|
def stop(self, return_after_stopping=True):
|
||||||
""" Stop node."""
|
""" Stop node."""
|
||||||
|
if self.is_running:
|
||||||
body = kubernetes.client.V1DeleteOptions()
|
body = kubernetes.client.V1DeleteOptions()
|
||||||
body.api_version = 'v1'
|
body.api_version = 'v1'
|
||||||
body.grace_period_seconds = 0
|
body.grace_period_seconds = 0
|
||||||
@ -78,6 +81,19 @@ class Node():
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def _exec_command(self, container, command):
|
||||||
|
try:
|
||||||
|
exec_command = ['/bin/sh', '-c', command]
|
||||||
|
resp = stream(self.api_instance.connect_get_namespaced_pod_exec,
|
||||||
|
self.name,
|
||||||
|
self.namespace,
|
||||||
|
container=container,
|
||||||
|
command=exec_command,
|
||||||
|
stderr=True, stdin=False, stdout=True, tty=False)
|
||||||
|
return resp
|
||||||
|
except ApiException as e:
|
||||||
|
print("Exception when executing command: %s\n" % e)
|
||||||
|
|
||||||
def _create_namespace(self, namespace):
|
def _create_namespace(self, namespace):
|
||||||
namespace_spec = kubernetes.client.V1Namespace()
|
namespace_spec = kubernetes.client.V1Namespace()
|
||||||
namespace_spec.api_version = 'v1'
|
namespace_spec.api_version = 'v1'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user