Initial POC commit for RestAPI support

1. Support to run KloudBuster from Rest API;
2. Adding kb_gen_chart.py to "pip install";

Change-Id: I84776fd3c5933b12e77ac1ac811c3b480cdcad29
This commit is contained in:
Yichen Wang
2015-08-18 17:01:07 -07:00
parent 2fc1e6df28
commit 4598308410
6 changed files with 141 additions and 80 deletions

View File

@@ -14,6 +14,7 @@
import os
import sys
import threading
kb_main_path = os.path.split(os.path.abspath(__file__))[0] + "/../../.."
sys.path.append(kb_main_path)
@@ -24,32 +25,23 @@ from kb_config import KBConfig
from pecan import expose
from pecan import response
lock = threading.Lock()
kb_config = KBConfig()
class ConfigController(object):
def __init__(self):
self.kb_config = KBConfig()
self.kb_status = 'READY'
# self.def_config = self.kb_config.config_scale
@expose(generic=True)
def default_config(self):
return "DEFAULT_CONFIG"
# return str(self.def_config)
@expose(generic=True)
def running_config(self):
return str(self.kb_config.config_scale)
@expose(generic=True)
def status(self):
return self.kb_status
@status.when(method='PUT')
def status_PUT(self, **kw):
return str(kw)
return str(kb_config.config_scale)
@running_config.when(method='POST')
def running_config_POST(self, arg):
if not lock.acquire(False):
response.status = 403
response.text = u"An instance of KloudBuster is running, you cannot change"\
"the config until the run is finished!"
return response.text
try:
# Expectation:
# {
@@ -59,16 +51,15 @@ class ConfigController(object):
# 'topo_cfg': {<TOPOLOGY_CONFIGS>}
# 'tenants_cfg': {<TENANT_AND_USER_LISTS_FOR_REUSING>}
# }
user_config = dict(arg)
print user_config
user_config = eval(arg)
# Parsing credentials from application input
cred_config = user_config['credentials']
cred_tested = Credentials(openrc_contents=cred_config['tested_rc'],
cred_tested = Credentials(openrc_contents=cred_config['tested-rc'],
pwd=cred_config['tested-passwd'])
if ('testing-rc' in cred_config and
cred_config['testing-rc'] != cred_config['tested-rc']):
cred_testing = Credentials(openrc_contents=cred_config['testing_rc'],
cred_testing = Credentials(openrc_contents=cred_config['testing-rc'],
pwd=cred_config['testing-passwd'])
else:
# Use the same openrc file for both cases
@@ -81,10 +72,11 @@ class ConfigController(object):
f = open(pubkey_filename, 'w')
f.write(user_config['kb_cfg']['public_key_file'])
f.close()
self.kb_config.config_scale['public_key_file'] = pubkey_filename
kb_config.config_scale['public_key_file'] = pubkey_filename
alt_config = Configuration.from_string(user_config['kb_cfg']).configure()
self.kb_config.config_scale = self.kb_config.config_scale.merge(alt_config)
if user_config['kb_cfg']:
alt_config = Configuration.from_string(user_config['kb_cfg']).configure()
kb_config.config_scale = kb_config.config_scale.merge(alt_config)
# Parsing topology configs from application input
if 'topo_cfg' in user_config:
@@ -100,10 +92,13 @@ class ConfigController(object):
except Exception as e:
response.status = 403
response.text = u"Error while parsing configurations: %s" % (e.message)
lock.release()
return response.text
self.kb_config.init_with_rest_api(cred_tested=cred_tested,
cred_testing=cred_testing,
topo_cfg=topo_cfg,
tenants_list=tenants_list)
return str(self.kb_config.config_scale)
kb_config.init_with_rest_api(cred_tested=cred_tested,
cred_testing=cred_testing,
topo_cfg=topo_cfg,
tenants_list=tenants_list)
lock.release()
return "OK!"

View File

@@ -0,0 +1,64 @@
# Copyright 2015 Cisco Systems, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sys
import traceback
kb_main_path = os.path.split(os.path.abspath(__file__))[0] + "/../../.."
sys.path.append(kb_main_path)
from api_cfg import kb_config as kb_config
from api_cfg import lock as kb_config_lock
from kloudbuster import KloudBuster
from pecan import expose
from pecan import response
class KBController(object):
def __init__(self):
self.kb_status = 'READY'
@expose(generic=True)
def status(self):
return self.kb_status
@expose(generic=True)
def run(self):
if (not kb_config.cred_tested) or (not kb_config.cred_testing):
response.status = 403
response.text = u"Credentials to the cloud are missing."\
"(Forgot to provide the config?)"
return response.text
if not kb_config_lock.acquire(False):
response.status = 403
response.text = u"An instance of KloudBuster is running, you cannot "\
"change the config until the run is finished!"
return response.text
try:
kloudbuster = KloudBuster(
kb_config.cred_tested, kb_config.cred_testing,
kb_config.server_cfg, kb_config.client_cfg,
kb_config.topo_cfg, kb_config.tenants_list)
if kloudbuster.check_and_upload_images():
kloudbuster.run()
except Exception:
response.status = 403
response.text = u"Error while running KloudBuster:\n%s" % traceback.format_exc()
kb_config_lock.release()
return response.text
kb_config_lock.release()
return "OK!"

View File

@@ -12,7 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from config import ConfigController
from api_cfg import ConfigController
from api_kb import KBController
from pecan import abort
from pecan import expose
@@ -21,6 +22,8 @@ class APIController(object):
def _lookup(self, primary_key, *remainder):
if primary_key == "config":
return ConfigController(), remainder
elif primary_key == "kloudbuster":
return KBController(), remainder
else:
abort(404)