Skip to content

Commit

Permalink
feat: add endpoint to run containers out of Containernet
Browse files Browse the repository at this point in the history
  • Loading branch information
EsauM10 committed Jul 6, 2023
1 parent 8a34cc9 commit 630842f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
2 changes: 2 additions & 0 deletions clusternet/apis/worker/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from clusternet.apis.worker.controllers.remove_link import RemoveLinkController
from clusternet.apis.worker.controllers.run_command import RunCommandOnHostController
from clusternet.apis.worker.controllers.run_pingall import RunPingallController
from clusternet.apis.worker.controllers.run_service import RunServiceController
from clusternet.apis.worker.controllers.start_docker import StartDockerController
from clusternet.apis.worker.controllers.start_worker import StartWorkerController
from clusternet.apis.worker.controllers.stop_docker import StopDockerController
from clusternet.apis.worker.controllers.stop_worker import StopWorkerController
from clusternet.apis.worker.controllers.update_cpu import UpdateCPUController
from clusternet.apis.worker.controllers.update_memory import UpdateMemoryController

27 changes: 27 additions & 0 deletions clusternet/apis/worker/controllers/run_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from clusternet.apis.presentation.exceptions import BadRequestException
from clusternet.apis.presentation.helpers import (
bad_request, created, error, internal_server_error, validate_required_params
)
from clusternet.apis.presentation.protocols import Controller, HttpRequest, HttpResponse
from clusternet.apis.worker.helpers import get_hostname, run_container


class RunServiceController(Controller):
def __init__(self) -> None:
pass

def handle(self, request: HttpRequest) -> HttpResponse:
required_params = ['name', 'image']
try:
hostname = get_hostname()
validate_required_params(request, required_params)

name = str(request.body.pop('name'))
image = str(request.body.pop('image'))
run_container(name, image, **request.body)

return created({'content': f'[{hostname}]: service {name} created'})
except BadRequestException as ex:
return bad_request(error(f'{ex}'))
except Exception as ex:
return internal_server_error(error(f'{ex}'))
8 changes: 6 additions & 2 deletions clusternet/apis/worker/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import List
from typing import Any, List
import docker
import socket

from mininet.log import info
from clusternet.apis.worker.data import WorkerInstance

client = docker.from_env()

def get_hostname() -> str:
return socket.gethostname()
Expand All @@ -21,4 +22,7 @@ def clean_containers_with_prefix(prefix: str) -> List[str]:
container.remove(force=True) # type: ignore
removed_containers.append(name)

return removed_containers
return removed_containers

def run_container(name: str, image: str, **params: Any):
client.containers.run(image, name=name, remove=True, detach=True, **params)
14 changes: 12 additions & 2 deletions clusternet/client/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
import httpx

from clusternet.client.container import RemoteContainer
Expand All @@ -17,7 +18,7 @@ def add_controller(self, name: str, ip: str, port: int):
print(f'{response.json()["content"]}')


def add_docker(self, name: str, **params) -> RemoteContainer:
def add_docker(self, name: str, **params: Any) -> RemoteContainer:
data = {'name': name, **params}
response = httpx.post(url=f'{self.url}/containers', json=data, timeout=None)

Expand All @@ -28,7 +29,7 @@ def add_docker(self, name: str, **params) -> RemoteContainer:
return RemoteContainer(name, self.url)


def add_link(self, node1: str, node2: str, **params):
def add_link(self, node1: str, node2: str, **params: Any):
data = {'node1': node1, 'node2': node2, **params}
response = httpx.post(url=f'{self.url}/links', json=data, timeout=None)

Expand Down Expand Up @@ -97,6 +98,15 @@ def run_pingall(self):
print(f'{response.json()["content"]}')


def run_service(self, name: str, image: str, **params: Any):
data = {'name': name, 'image': image, **params}
response = httpx.post(url=f'{self.url}/services', json=data, timeout=None)

if(response.is_error):
raise Exception(response.json()['error'])
print(f'{response.json()["content"]}')


def start(self):
response = httpx.get(url=f'{self.url}/start', timeout=None)

Expand Down
11 changes: 9 additions & 2 deletions clusternet/server/worker_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from clusternet.apis.worker.controllers import (
AddController, AddDockerController, AddLinkController, AddSwitchController,
CleanContainersController, ConfigDefaultController, GetDockerIPController,
RemoveDockerController, RemoveLinkController, RunCommandOnHostController, RunPingallController,
StartDockerController, StopDockerController, StartWorkerController, StopWorkerController,
RemoveDockerController, RemoveLinkController, RunCommandOnHostController,
RunServiceController, RunPingallController, StartDockerController,
StopDockerController, StartWorkerController, StopWorkerController,
UpdateCPUController, UpdateMemoryController
)
from clusternet.apis.presentation.helpers import parse_request
Expand Down Expand Up @@ -112,6 +113,12 @@ def run_pingall():
return make_response(controller, request)


@server.route('/services', methods=['POST'])
def run_service():
controller = RunServiceController()
return make_response(controller, request)


@server.route('/start', methods=['GET'])
def start():
controller = StartWorkerController()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='clusternet',
version='0.6.2',
version='0.7.2',
description='Distributed Software Defined Network Emulation',
long_description='Distributed Software Defined Network Emulation',
keywords=['networking', 'emulator', 'containernet', 'mininet', 'OpenFlow', 'SDN', 'fog'],
Expand Down

0 comments on commit 630842f

Please sign in to comment.