Skip to content

Commit

Permalink
Merge branch 'master' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Tengler committed Mar 12, 2018
2 parents 177d3dd + b805cb5 commit 5e9d5ae
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
30 changes: 22 additions & 8 deletions kqueen/blueprints/api/views.py
Expand Up @@ -71,9 +71,16 @@ def index():
class ListClusters(ListView):
object_class = Cluster

async def _update_cluster(self, cluster):
cluster.get_state()
return True
async def _update_clusters(self, clusters, loop):
futures = [
loop.run_in_executor(
None,
cluster.get_state
)
for cluster in clusters
]
for result in await asyncio.gather(*futures):
pass

def get_content(self, *args, **kwargs):
clusters = self.obj
Expand All @@ -88,7 +95,7 @@ def get_content(self, *args, **kwargs):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# run coroutines and close loop
loop.run_until_complete(asyncio.gather(*[self._update_cluster(c) for c in clusters]))
loop.run_until_complete(self._update_clusters(clusters, loop))
loop.close()
except Exception as e:
logger.exception('Asyncio loop is NOT available, fallback to simple looping: ')
Expand Down Expand Up @@ -210,9 +217,16 @@ def cluster_resize(pk):
class ListProvisioners(ListView):
object_class = Provisioner

async def _update_provisioner(self, provisioner):
provisioner.engine_status()
return True
async def _update_provisioners(self, provisioners, loop):
futures = [
loop.run_in_executor(
None,
provisioner.engine_status
)
for provisioner in provisioners
]
for result in await asyncio.gather(*futures):
pass

def get_content(self, *args, **kwargs):
provisioners = self.obj
Expand All @@ -227,7 +241,7 @@ def get_content(self, *args, **kwargs):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# run coroutines and close loop
loop.run_until_complete(asyncio.gather(*[self._update_provisioner(p) for p in provisioners]))
loop.run_until_complete(self._update_provisioners(provisioners, loop))
loop.close()
except Exception as e:
logger.exception('Asyncio loop is NOT available, fallback to simple looping: ')
Expand Down
2 changes: 1 addition & 1 deletion kqueen/config/demo.py
Expand Up @@ -3,7 +3,7 @@

class Config(BaseConfig):
DEBUG = False
LOG_LEVEL = 'INFO'
LOG_LEVEL = 'DEBUG'
LOG_CONFIG = 'kqueen/utils/logger_config.yml'

KQUEEN_HOST = '0.0.0.0'
Expand Down
2 changes: 1 addition & 1 deletion kqueen/config/dev.py
Expand Up @@ -3,7 +3,7 @@

class Config(BaseConfig):
DEBUG = True
LOG_LEVEL = 'DEBUG'
LOG_LEVEL = 'INFO'
LOG_CONFIG = 'kqueen/utils/logger_config.yml'

# App secret
Expand Down
2 changes: 1 addition & 1 deletion kqueen/config/prod.py
Expand Up @@ -3,7 +3,7 @@

class Config(BaseConfig):
DEBUG = False
LOG_LEVEL = 'INFO'
LOG_LEVEL = 'DEBUG'
LOG_CONFIG = 'kqueen/utils/logger_config.yml'

KQUEEN_HOST = '0.0.0.0'
11 changes: 2 additions & 9 deletions kqueen/engines/gce.py
Expand Up @@ -46,13 +46,6 @@ class GceEngine(BaseEngine):
'token_uri'
]
}
},
'project': {
'type': 'text',
'label': 'Project ID',
'validators': {
'required': True,
}
}
},
'cluster': {
Expand Down Expand Up @@ -116,7 +109,7 @@ def __init__(self, cluster, **kwargs):
super(GceEngine, self).__init__(cluster, **kwargs)
# Client initialization
self.service_account_info = kwargs.get('service_account_info', {})
self.project = kwargs.get('project', '')
self.project = self.service_account_info.get('project_id', '')
self.zone = kwargs.get('zone', '-')
self.cluster_id = 'a' + self.cluster.id.replace('-', '')
self.cluster_config = {
Expand Down Expand Up @@ -323,7 +316,7 @@ def cluster_list(self):
@classmethod
def engine_status(cls, **kwargs):
service_account_info = kwargs.get('service_account_info', {})
project = kwargs.get('project', '')
project = service_account_info.get('project_id', '')
project_zone = kwargs.get('zone', '-')
credentials = service_account.Credentials.from_service_account_info(service_account_info)
client = googleapiclient.discovery.build('container', 'v1', credentials=credentials)
Expand Down
37 changes: 30 additions & 7 deletions kqueen/models.py
Expand Up @@ -82,7 +82,7 @@ def delete(self):
deprov_status, deprov_msg = self.engine.deprovision()

if deprov_status:
super(Cluster, self).delete()
super().delete()
else:
raise Exception('Unable to deprovision cluster: {}'.format(deprov_msg))

Expand All @@ -94,6 +94,19 @@ def get_kubeconfig(self):
self.save()
return kubeconfig

def save(self, **kwargs):
# while used in async method, app context is not available by default and needs to be imported
from flask import current_app as app
from kqueen.server import create_app
try:
if not app.testing:
app = create_app()
except RuntimeError:
app = create_app()

with app.app_context():
return super().save(**kwargs)

def status(self):
"""Return information about Kubernetes cluster"""
try:
Expand Down Expand Up @@ -346,14 +359,24 @@ def engine_status(self, save=True):
state = engine_class.engine_status(**self.parameters)
if save:
self.state = state
self.save()
self.save(check_status=False)
return state

def save(self, check_status=True):
if check_status:
self.state = self.engine_status(save=False)
self.verbose_name = getattr(self.get_engine_cls(), 'verbose_name', self.engine)
return super(Provisioner, self).save()
def save(self, check_status=True, **kwargs):
# while used in async method, app context is not available by default and needs to be imported
from flask import current_app as app
from kqueen.server import create_app
try:
if not app.testing:
app = create_app()
except RuntimeError:
app = create_app()

with app.app_context():
if check_status:
self.state = self.engine_status(save=False)
self.verbose_name = getattr(self.get_engine_cls(), 'verbose_name', self.engine)
return super().save(**kwargs)


#
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -39,6 +39,7 @@
'flask-swagger-ui',
'gunicorn',
'kubernetes',
'oauth2client==3.0.0',
'pycrypto',
'prometheus_client',
'python-etcd',
Expand Down

0 comments on commit 5e9d5ae

Please sign in to comment.