-
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.
completed and tested major container-level API
- Loading branch information
Showing
22 changed files
with
1,211 additions
and
732 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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from collections import deque | ||
|
||
|
||
class Appliance: | ||
|
||
def __init__(self, id, containers=[], pending=[], **kwargs): | ||
self.__id = id | ||
self.__containers = list(containers) | ||
self.__pending = deque(pending) | ||
|
||
@property | ||
def id(self): | ||
return self.__id | ||
|
||
@property | ||
def containers(self): | ||
return self.__containers | ||
|
||
@property | ||
def pending(self): | ||
return list(self.__pending) | ||
|
||
@containers.setter | ||
def containers(self, contrs): | ||
self.__containers = list(contrs) | ||
|
||
def enqueue_pending_containers(self, contr_id): | ||
self.__pending.append(contr_id) | ||
|
||
def dequeue_pending_container(self): | ||
return self.__pending.popleft() | ||
|
||
def to_render(self): | ||
return dict(id=self.id, | ||
containers=[c if isinstance(c, str) else c.to_render() | ||
for c in self.containers]) | ||
|
||
def to_save(self): | ||
return dict(id=self.id, | ||
containers=[c if isinstance(c, str) else c.id for c in self.containers], | ||
pending=self.pending) |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import json | ||
|
||
from tornado.web import RequestHandler | ||
|
||
from appliance.manager import ApplianceManager | ||
from container.manager import ContainerManager | ||
from util import Loggable | ||
|
||
|
||
class AppliancesHandler(RequestHandler, Loggable): | ||
|
||
def initialize(self, config): | ||
self.__app_mgr = ApplianceManager(config) | ||
self.__contr_mgr = ContainerManager(config) | ||
|
||
async def post(self): | ||
status, app = await self.__app_mgr.create_appliance(json.loads(self.request.body)) | ||
self.set_status(status) | ||
self.write(json.dumps(app.to_render()) if status == 201 else app) | ||
self.finish() | ||
if status == 201: | ||
await self.__app_mgr.process_next_pending_container(app) | ||
|
||
|
||
class ApplianceHandler(RequestHandler, Loggable): | ||
|
||
def initialize(self, config): | ||
self.__app_mgr = ApplianceManager(config) | ||
self.__contr_mgr = ContainerManager(config) | ||
|
||
async def get(self, app_id): | ||
status, resp = await self.__app_mgr.get_appliance(app_id) | ||
self.set_status(status) | ||
self.write(resp) | ||
# self.write(json.dumps(resp.to_render()) if status == 200 else resp) | ||
|
||
async def delete(self, app_id): | ||
status, app, resp = await self.__app_mgr.delete_appliance(app_id) | ||
self.set_status(status) | ||
self.write(resp) | ||
if app: | ||
await self._deprovision_containers(app.containers) | ||
|
||
async def _deprovision_containers(self, contrs): | ||
for c in contrs: | ||
await self.__contr_mgr.deprovision_container(c) |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from util import Singleton, Loggable, MotorClient, SecureAsyncHttpClient | ||
from container.manager import ContainerManager | ||
|
||
class ApplianceManager(Loggable, metaclass=Singleton): | ||
|
||
def __init__(self): | ||
self.__app_col = MotorClient().requester.appliance | ||
self.__contr_mgr = ContainerManager() | ||
self.__http_cli = SecureAsyncHttpClient() | ||
|
||
async def get_appliance(self, app_id, verbose=True): | ||
pass | ||
|
||
|
||
async def create_appliance(self, app): | ||
pass | ||
|
||
async def delete_appliance(self, app_id): | ||
pass | ||
|
||
async def save_appliance(self, app, upsert=True): | ||
await self.__app_col.replace_one(dict(id=app.id), app.to_save(), upsert=upsert) |
This file was deleted.
Oops, something went wrong.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from tornado.ioloop import PeriodicCallback | ||
|
||
from util import Singleton, Loggable, SecureAsyncHttpClient | ||
|
||
|
||
class Host: | ||
|
||
def __init__(self, hostname, attributes={}): | ||
self.__attributes = dict(**attributes, hostname=hostname) | ||
|
||
@property | ||
def hostname(self): | ||
return self.__attributes.get('hostname', None) | ||
|
||
@property | ||
def attributes(self): | ||
return dict(self.__attributes) | ||
|
||
def to_render(self): | ||
return self.attributes | ||
|
||
|
||
class Cluster(Loggable, metaclass=Singleton): | ||
|
||
MONITOR_INTERVAL = 30000 # query cluster info every minute | ||
|
||
def __init__(self, config): | ||
self.__master_url_base = '%s/%s'%(config['dcos']['master_url'], | ||
config['dcos']['mesos_master_route']) | ||
self.__config = config | ||
self.__http_cli = SecureAsyncHttpClient(config) | ||
self.__hosts = [] | ||
|
||
@property | ||
def hosts(self): | ||
return list(self.__hosts) | ||
|
||
def find_host_by_attribute(self, key, val): | ||
return list(filter(lambda h: h.attributes.get(key, None) == val, self.__hosts)) | ||
|
||
async def monitor(self): | ||
async def query_mesos(): | ||
status, body, err = await self.__http_cli.get('%s/slaves'%self.__master_url_base) | ||
if status != 200: | ||
self.logger.debug(err) | ||
return | ||
self.logger.debug('Collect host info') | ||
self.__hosts = [Host(hostname=h['hostname'], attributes=h['attributes']) | ||
for h in body['slaves']] | ||
await query_mesos() | ||
PeriodicCallback(query_mesos, self.MONITOR_INTERVAL).start() | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import json | ||
|
||
from tornado.web import RequestHandler | ||
|
||
from cluster.base import Cluster | ||
from util import Loggable | ||
|
||
|
||
class ClusterInfoHandler(RequestHandler, Loggable): | ||
|
||
def initialize(self, config): | ||
self.__cluster = Cluster(config) | ||
|
||
def get(self): | ||
self.write(json.dumps([h.to_render() for h in self.__cluster.hosts])) |
Oops, something went wrong.