Skip to content

Commit

Permalink
fixed issues with logger, DC/OS authentication, cluster info updates …
Browse files Browse the repository at this point in the history
…and appliance deletion
  • Loading branch information
dcvan24 committed Mar 19, 2018
1 parent 4a8fbcb commit 34fa0f7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions appliance/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def post(self):
if status == 201:
self.__app_monitor.spawn(app)


class ApplianceHandler(RequestHandler, Loggable):

def initialize(self, config):
Expand Down
4 changes: 3 additions & 1 deletion appliance/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ApplianceManager(Loggable, metaclass=Singleton):
def __init__(self, config):
self.__config = config
self.__app_col = MotorClient().requester.appliance
self.__contr_col = MotorClient().requester.container
self.__contr_mgr = ContainerManager(config)
self.__http_cli = SecureAsyncHttpClient(config)

Expand Down Expand Up @@ -72,7 +73,7 @@ async def save_appliance(self, app, upsert=True):
await self.__app_col.replace_one(dict(id=app.id), app.to_save(), upsert=upsert)
return 200, "Appliance '%s' has been saved"%app, None

async def restore_appliances(self):
async def restore_appliance(self, app_id):
raise NotImplemented

async def _get_appliance_from_db(self, app_id):
Expand All @@ -83,6 +84,7 @@ async def _get_appliance_from_db(self, app_id):

async def _delete_appliance_from_db(self, app_id):
await self.__app_col.delete_one(dict(id=app_id))
await self.__contr_col.delete_many(dict(appliance=app_id))
return 200, "Appliance '%s' has been deleted"%app_id, None

async def _deprovision_group(self, app_id):
Expand Down
22 changes: 14 additions & 8 deletions cluster/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from tornado.ioloop import PeriodicCallback

from util import Singleton, Loggable, SecureAsyncHttpClient
Expand Down Expand Up @@ -28,23 +29,28 @@ def __init__(self, config):
self.__config = config
self.__http_cli = SecureAsyncHttpClient(config)
self.__hosts = []
self.__attributes_map = defaultdict(set)

@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))
return list(self.__attributes_map.get((key, val), []))

async def monitor(self):
async def query_mesos():
status, body, err = await self.__http_cli.get('%s/slaves'%self.__config.url.mesos_master)
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']]
status, body, err = await self.__http_cli.get('%s/slaves'%self.__config.url.mesos_master)
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']]
for h in self.__hosts:
for kv_pair in h.attributes.items():
self.__attributes_map[kv_pair].add(h)

await query_mesos()
PeriodicCallback(query_mesos, self.MONITOR_INTERVAL).start()

Expand Down
15 changes: 10 additions & 5 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import motor
import logging
import subprocess

from collections import namedtuple
from tornado.httpclient import AsyncHTTPClient, HTTPError
Expand Down Expand Up @@ -43,7 +44,7 @@ def logger(self):
'::%(lineno)s\t%(message)s')
logger = logging.getLogger(self.__class__.__name__)
logger.setLevel(logging.INFO)
if not logger.hasHandlers():
if not logger.handlers:
stream_hdlr = logging.StreamHandler(sys.stdout)
stream_hdlr.setFormatter(fmt)
file_hdlr = logging.FileHandler('pivot.log')
Expand All @@ -60,7 +61,7 @@ def __init__(self, config):
self.__cli = AsyncHTTPClient()
self.__headers = {
'Content-Type': 'application/json',
'Authorization': 'token=%s'%self._read_auth_token()
'Authorization': 'token=%s'%self._get_auth_token()
}

async def get(self, url, **headers):
Expand Down Expand Up @@ -90,6 +91,10 @@ async def _fetch(self, url, method, body, **headers):
except HTTPError as e:
return e.response.code, None, error(e.response.body.decode('utf-8'))

def _read_auth_token(self):
with open(self.__config.dcos.token_file_path) as f:
return f.readline().strip('\n')
def _get_auth_token(self):
try:
out = subprocess.check_output('dcos config show core.dcos_acs_token', shell=True)
return out.decode('utf-8').strip('\n')
except subprocess.CalledProcessError as e:
self.logger.error(e)
raise Exception('DC/OS is not properly set up and authenticated')

0 comments on commit 34fa0f7

Please sign in to comment.