From 0cf6b232ac9cf128ee9183a27c08f4f74ab2e2e6 Mon Sep 17 00:00:00 2001 From: grakiss Date: Thu, 28 Sep 2017 03:47:54 -0400 Subject: add api&web services for cvp JIRA: DOVETAIL-512 add api&web services for cvp Change-Id: I9ef9525e980fe61dc3108035ef9a3ff8783b2697 Signed-off-by: grakiss --- cvp/update/templates/__init__.py | 0 cvp/update/templates/backup_mongodb.py | 45 +++++++++++++++ cvp/update/templates/changes_in_mongodb.py | 51 +++++++++++++++++ cvp/update/templates/restore_mongodb.py | 38 +++++++++++++ cvp/update/templates/rm_images.sh | 8 +++ cvp/update/templates/rm_olds.sh | 15 +++++ cvp/update/templates/update_mongodb.py | 90 ++++++++++++++++++++++++++++++ cvp/update/templates/utils.py | 48 ++++++++++++++++ 8 files changed, 295 insertions(+) create mode 100644 cvp/update/templates/__init__.py create mode 100644 cvp/update/templates/backup_mongodb.py create mode 100644 cvp/update/templates/changes_in_mongodb.py create mode 100644 cvp/update/templates/restore_mongodb.py create mode 100755 cvp/update/templates/rm_images.sh create mode 100644 cvp/update/templates/rm_olds.sh create mode 100644 cvp/update/templates/update_mongodb.py create mode 100644 cvp/update/templates/utils.py (limited to 'cvp/update/templates') diff --git a/cvp/update/templates/__init__.py b/cvp/update/templates/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cvp/update/templates/backup_mongodb.py b/cvp/update/templates/backup_mongodb.py new file mode 100644 index 00000000..9c243776 --- /dev/null +++ b/cvp/update/templates/backup_mongodb.py @@ -0,0 +1,45 @@ +############################################################################## +# Copyright (c) 2016 ZTE Corporation +# feng.xiaowei@zte.com.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import argparse +import datetime +import os + +from utils import execute, main, get_abspath + +parser = argparse.ArgumentParser(description='Backup MongoDBs') + +parser.add_argument('-u', '--url', + type=str, + required=False, + default='mongodb://127.0.0.1:27017/', + help='Mongo DB URL for Backups') +parser.add_argument('-o', '--output_dir', + type=str, + required=False, + default='./', + help='Output directory for the backup.') + +parser.add_argument('-d', '--db', + type=str, + required=False, + default='test_results_collection', + help='database for the backup.') + + +def backup(args): + db = args.db + out = get_abspath(args.output_dir) + now = datetime.datetime.now() + out = os.path.join(out, '%s__%s' % (db, now.strftime('%Y_%m_%d_%H%M%S'))) + cmd = ['mongodump', '-o', '%s' % out] + execute(cmd, args) + + +if __name__ == '__main__': + main(backup, parser) diff --git a/cvp/update/templates/changes_in_mongodb.py b/cvp/update/templates/changes_in_mongodb.py new file mode 100644 index 00000000..1a4d5a16 --- /dev/null +++ b/cvp/update/templates/changes_in_mongodb.py @@ -0,0 +1,51 @@ +############################################################################## +# Copyright (c) 2016 ZTE Corporation +# feng.xiaowei@zte.com.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +# 09/06/2016: change for migration after refactoring +# 16/06/2016: Alignment of test name (JIRA: FUNCTEST-304) +############################################################################## +collections_old2New = { + # 'pod': 'pods', + # 'test_projects': 'projects', + # 'test_testcases': 'testcases', + # 'test_results': 'results' +} + +fields_old2New = { + # 'test_results': [({}, {'creation_date': 'start_date'})] +} + +docs_old2New = { + # 'test_results': [ + # ({'criteria': 'failed'}, {'criteria': 'FAILED'}), + # ({'criteria': 'passed'}, {'criteria': 'PASS'}) + # ] + # 'testcases': [ + # ({'name': 'vPing'}, {'name': 'vping_ssh'}), + # ({'name': 'Tempest'}, {'name': 'tempest_smoke_serial'}), + # ({'name': 'Rally'}, {'name': 'rally_sanity'}), + # ({'name': 'ODL'}, {'name': 'odl'}), + # ({'name': 'vIMS'}, {'name': 'vims'}), + # ({'name': 'ONOS'}, {'name': 'onos'}), + # ({'name': 'vPing_userdata'}, {'name': 'vping_userdata'}), + # ({'name': 'ovno'}, {'name': 'ocl'}) + # ], + # 'results': [ + # ({'case_name': 'vPing'}, {'case_name': 'vping_ssh'}), + # ({'case_name': 'Tempest'}, {'case_name': 'tempest_smoke_serial'}), + # ({'case_name': 'Rally'}, {'case_name': 'rally_sanity'}), + # ({'case_name': 'ODL'}, {'case_name': 'odl'}), + # ({'case_name': 'vIMS'}, {'case_name': 'vims'}), + # ({'case_name': 'ONOS'}, {'case_name': 'onos'}), + # ({'case_name': 'vPing_userdata'}, {'case_name': 'vping_userdata'}), + # ({'case_name': 'ovno'}, {'case_name': 'ocl'}) + # ] + 'results': [ + ({'trust_indicator': 0}, + {'trust_indicator': {'current': 0, 'histories': []}}) + ] +} diff --git a/cvp/update/templates/restore_mongodb.py b/cvp/update/templates/restore_mongodb.py new file mode 100644 index 00000000..c45a0e62 --- /dev/null +++ b/cvp/update/templates/restore_mongodb.py @@ -0,0 +1,38 @@ +############################################################################## +# Copyright (c) 2016 ZTE Corporation +# feng.xiaowei@zte.com.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import argparse + +from utils import execute, main, get_abspath + +parser = argparse.ArgumentParser(description='Restore MongoDBs') + +parser.add_argument('-u', '--url', + type=str, + required=False, + default='mongodb://127.0.0.1:27017/', + help='Mongo DB URL for Backup') +parser.add_argument('-i', '--input_dir', + type=str, + required=True, + help='Input directory for the Restore.') +parser.add_argument('-d', '--db', + type=str, + required=False, + default='test_results_collection', + help='database name after the restore.') + + +def restore(args): + input_dir = get_abspath(args.input_dir) + cmd = ['mongorestore', '%s' % input_dir] + execute(cmd, args) + + +if __name__ == '__main__': + main(restore, parser) diff --git a/cvp/update/templates/rm_images.sh b/cvp/update/templates/rm_images.sh new file mode 100755 index 00000000..6722573b --- /dev/null +++ b/cvp/update/templates/rm_images.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +number=`docker images | awk 'NR != 1' | grep testapi | wc -l` +if [ $number -gt 0 ]; then + images=`docker images -a | awk 'NR != 1' | grep testapi | awk '{print $1}'` + echo "begin to rm images $images" + docker images | awk 'NR != 1' | grep testapi | awk '{print $3}' | xargs docker rmi -f &>/dev/null +fi diff --git a/cvp/update/templates/rm_olds.sh b/cvp/update/templates/rm_olds.sh new file mode 100644 index 00000000..c6bca186 --- /dev/null +++ b/cvp/update/templates/rm_olds.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +proc_number=`ps -ef | grep opnfv-testapi | grep -v grep | wc -l` +if [ $proc_number -gt 0 ]; then + procs=`ps -ef | grep opnfv-testapi | grep -v grep` + echo "begin to kill opnfv-testapi $procs" + ps -ef | grep opnfv-testapi | grep -v grep | awk '{print $2}' | xargs kill -kill &>/dev/null +fi + +number=`docker ps -a | awk 'NR != 1' | grep testapi | wc -l` +if [ $number -gt 0 ]; then + containers=number=`docker ps -a | awk 'NR != 1' | grep testapi` + echo "begin to rm containers $containers" + docker ps -a | awk 'NR != 1' | grep testapi | awk '{print $1}' | xargs docker rm -f &>/dev/null +fi diff --git a/cvp/update/templates/update_mongodb.py b/cvp/update/templates/update_mongodb.py new file mode 100644 index 00000000..f7595928 --- /dev/null +++ b/cvp/update/templates/update_mongodb.py @@ -0,0 +1,90 @@ +############################################################################## +# Copyright (c) 2016 ZTE Corporation +# feng.xiaowei@zte.com.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import argparse + +from pymongo import MongoClient + +from changes_in_mongodb import collections_old2New, \ + fields_old2New, docs_old2New +from utils import main, parse_mongodb_url + +parser = argparse.ArgumentParser(description='Update MongoDBs') + +parser.add_argument('-u', '--url', + type=str, + required=False, + default='mongodb://127.0.0.1:27017/', + help='Mongo DB URL for Backups') + +parser.add_argument('-d', '--db', + type=str, + required=False, + default='test_results_collection', + help='database for the update.') + + +def assert_collections(a_dict): + if a_dict is not None: + collections = eval_db('collection_names') + no_collections = [] + for collection in a_dict.keys(): + if collection not in collections: + no_collections.append(collection) + assert len(no_collections) == 0, \ + 'collections {} not exist'.format(no_collections) + + +def rename_collections(a_dict): + if a_dict is not None: + for collection, new_name in a_dict.iteritems(): + eval_collection(collection, 'rename', new_name) + + +def rename_fields(a_dict): + collection_update(a_dict, '$rename') + + +def change_docs(a_dict): + collection_update(a_dict, '$set') + + +def eval_db(method, *args, **kwargs): + exec_db = db.__getattribute__(method) + return exec_db(*args, **kwargs) + + +def eval_collection(collection, method, *args, **kwargs): + exec_collection = db.__getattr__(collection) + return exec_collection.__getattribute__(method)(*args, **kwargs) + + +def collection_update(a_dict, operator): + if a_dict is not None: + for collection, updates in a_dict.iteritems(): + for (query, doc) in updates: + doc_dict = {operator: doc} + eval_collection(collection, 'update', query, + doc_dict, upsert=False, multi=True) + + +def update(args): + parse_mongodb_url(args.url) + client = MongoClient(args.url) + global db + db = client[args.db] + assert_collections(docs_old2New) + assert_collections(fields_old2New) + assert_collections(collections_old2New) + change_docs(docs_old2New) + rename_fields(fields_old2New) + rename_collections(collections_old2New) + + +if __name__ == '__main__': + main(update, parser) diff --git a/cvp/update/templates/utils.py b/cvp/update/templates/utils.py new file mode 100644 index 00000000..4254fee3 --- /dev/null +++ b/cvp/update/templates/utils.py @@ -0,0 +1,48 @@ +############################################################################## +# Copyright (c) 2016 ZTE Corporation +# feng.xiaowei@zte.com.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import os +import urlparse +import subprocess + + +def get_abspath(path): + assert os.path.isdir(path), 'Path %s can\'t be found.' % path + return os.path.abspath(path) + + +def parse_mongodb_url(url): + url = urlparse.urlparse(url) + assert url.scheme == 'mongodb', 'URL must be a MongoDB URL' + return url + + +def url_parse(url): + url = parse_mongodb_url(url) + return url.username, url.password, url.hostname, url.port + + +def execute(cmd, args): + (username, password, hostname, port) = url_parse(args.url) + cmd.extend(['--host', '%s' % hostname, '--port', '%s' % port]) + db = args.db + if db is not None: + cmd.extend(['--db', '%s' % db]) + if username is not None: + cmd.extend(['-u', '%s' % username, '-p', '%s' % password]) + print('execute: %s' % cmd) + execute_output = subprocess.check_output(cmd) + print(execute_output) + + +def main(method, parser): + args = parser.parse_args() + try: + method(args) + except AssertionError as msg: + print(msg) -- cgit 1.2.3-korg