summaryrefslogtreecommitdiffstats
path: root/update/templates
diff options
context:
space:
mode:
authorxudan <xudan16@huawei.com>2018-07-06 05:16:40 -0400
committerxudan <xudan16@huawei.com>2018-07-06 05:21:42 -0400
commitb3e40f026d655501bfa581452c447784604ecb05 (patch)
tree406f8bfc1abc1b33f98153d03abd34ef7b0e2fe9 /update/templates
parentb1b0ea32d1a296c7d055c5391261dcad6be48c63 (diff)
Move all web portal code to the new repo dovetail-webportal
This is only the first step to simply copy the file here. There still need some more work to make sure all work well. All the changes will be submitted with other patches to make it easily to review. JIRA: DOVETAIL-671 Change-Id: I64d32a9df562184166b6199e2719f298687d1a0a Signed-off-by: xudan <xudan16@huawei.com>
Diffstat (limited to 'update/templates')
-rw-r--r--update/templates/__init__.py0
-rw-r--r--update/templates/backup_mongodb.py45
-rw-r--r--update/templates/changes_in_mongodb.py51
-rw-r--r--update/templates/restore_mongodb.py38
-rwxr-xr-xupdate/templates/rm_images.sh8
-rw-r--r--update/templates/rm_olds.sh15
-rw-r--r--update/templates/update_mongodb.py90
-rw-r--r--update/templates/utils.py48
8 files changed, 295 insertions, 0 deletions
diff --git a/update/templates/__init__.py b/update/templates/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/update/templates/__init__.py
diff --git a/update/templates/backup_mongodb.py b/update/templates/backup_mongodb.py
new file mode 100644
index 0000000..9c24377
--- /dev/null
+++ b/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/update/templates/changes_in_mongodb.py b/update/templates/changes_in_mongodb.py
new file mode 100644
index 0000000..1a4d5a1
--- /dev/null
+++ b/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/update/templates/restore_mongodb.py b/update/templates/restore_mongodb.py
new file mode 100644
index 0000000..c45a0e6
--- /dev/null
+++ b/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/update/templates/rm_images.sh b/update/templates/rm_images.sh
new file mode 100755
index 0000000..6722573
--- /dev/null
+++ b/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/update/templates/rm_olds.sh b/update/templates/rm_olds.sh
new file mode 100644
index 0000000..c6bca18
--- /dev/null
+++ b/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/update/templates/update_mongodb.py b/update/templates/update_mongodb.py
new file mode 100644
index 0000000..f759592
--- /dev/null
+++ b/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/update/templates/utils.py b/update/templates/utils.py
new file mode 100644
index 0000000..4254fee
--- /dev/null
+++ b/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)