From 24595390fde6736753946648ee083c178b60759c Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Fri, 30 Sep 2016 17:49:57 +0200 Subject: [PATCH] Support of image name and checksum when creating and using an image Change-Id: I1c9f2adba4d77df7fe6e390401c42919d1700c30 Signed-off-by: garciadeblas --- .gitignore-common | 50 ++++++++++++++++++++++++++++++++ database_utils/migrate_vim_db.sh | 18 +++++++++++- httpserver.py | 22 ++++++++++++++ openvimd.py | 4 +-- 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 .gitignore-common diff --git a/.gitignore-common b/.gitignore-common new file mode 100644 index 0000000..3f06bdf --- /dev/null +++ b/.gitignore-common @@ -0,0 +1,50 @@ +## +# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. +# This file is part of openmano +# 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact with: nfvlabs@tid.es +## + +# This is a template with common files to be igonored, after clone make a copy to .gitignore +# cp .gitignore-common .gitignore + +*.pyc + +#auto-ignore +.gitignore + +#logs of openvim +logs + +#pycharm +.idea + +#eclipse +.project +.pydevproject +.settings + +#local stuff files that end in ".local" or folders called "local" +*.local +test/*.local +database_utils/*.local +scripts/*.local +local +test/local +database_utils/local +scripts/local + diff --git a/database_utils/migrate_vim_db.sh b/database_utils/migrate_vim_db.sh index 816cee7..1d379ac 100755 --- a/database_utils/migrate_vim_db.sh +++ b/database_utils/migrate_vim_db.sh @@ -161,6 +161,7 @@ DATABASE_TARGET_VER_NUM=0 [ $OPENVIM_VER_NUM -ge 4001 ] && DATABASE_TARGET_VER_NUM=5 #0.4.1 => 5 [ $OPENVIM_VER_NUM -ge 4002 ] && DATABASE_TARGET_VER_NUM=6 #0.4.2 => 6 [ $OPENVIM_VER_NUM -ge 4005 ] && DATABASE_TARGET_VER_NUM=7 #0.4.5 => 7 +[ $OPENVIM_VER_NUM -ge 4010 ] && DATABASE_TARGET_VER_NUM=8 #0.4.10 => 8 #TODO ... put next versions here @@ -407,13 +408,14 @@ function downgrade_from_6(){ echo "ALTER TABLE resources_port CHANGE COLUMN source_name source_name VARCHAR(24) NULL DEFAULT NULL, CHANGE COLUMN switch_port switch_port VARCHAR(24) NULL DEFAULT NULL, CHANGE COLUMN switch_dpid switch_dpid VARCHAR(23) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int='6';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } + function upgrade_to_7(){ echo " upgrade database from version 0.6 to version 0.7" echo " add 'bind_net','bind_type','cidr','enable_dhcp' to 'nets'" echo "ALTER TABLE nets ADD COLUMN cidr VARCHAR(64) NULL DEFAULT NULL AFTER bind, ADD COLUMN enable_dhcp ENUM('true','false') NOT NULL DEFAULT 'false' after cidr, ADD COLUMN dhcp_first_ip VARCHAR(64) NULL DEFAULT NULL AFTER enable_dhcp, ADD COLUMN dhcp_last_ip VARCHAR(64) NULL DEFAULT NULL AFTER dhcp_first_ip;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE nets CHANGE COLUMN bind provider VARCHAR(36) NULL DEFAULT NULL AFTER vlan;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "ALTER TABLE nets ADD COLUMN bind_net VARCHAR(36) NULL DEFAULT NULL COMMENT 'To connect with other net' AFTER provider, ADD COLUMN bind_type VARCHAR(36) NULL DEFAULT NULL COMMENT 'VLAN: to insert/remove' after bind_net;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 - echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (7, '0.7', '0.4.4', 'Add bind_net to net table', '2016-02-12');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 + echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (7, '0.7', '0.4.5', 'Add bind_net to net table', '2016-02-12');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } function downgrade_from_7(){ echo " downgrade database from version 0.7 to version 0.6" @@ -422,6 +424,20 @@ function downgrade_from_7(){ echo "ALTER TABLE nets DROP COLUMN cidr, DROP COLUMN enable_dhcp, DROP COLUMN bind_net, DROP COLUMN bind_type, DROP COLUMN dhcp_first_ip, DROP COLUMN dhcp_last_ip;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '7';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } + +function upgrade_to_8(){ + echo " upgrade database from version 0.7 to version 0.8" + echo " add column 'checksum' to 'images'" + echo "ALTER TABLE images ADD COLUMN checksum VARCHAR(32) NULL AFTER name;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 + echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (8, '0.8', '0.4.10', 'add column checksum to images', '2016-09-30');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 +} +function downgrade_from_8(){ + echo " downgrade database from version 0.8 to version 0.7" + echo " remove column 'checksum' from 'images'" + echo "ALTER TABLE images DROP COLUMN checksum;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 + echo "DELETE FROM schema_version WHERE version_int = '8';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 +} + #TODO ... put funtions here diff --git a/httpserver.py b/httpserver.py index 86f96a6..88b1f0a 100644 --- a/httpserver.py +++ b/httpserver.py @@ -34,6 +34,8 @@ import yaml import json import threading import datetime +import hashlib +import os import RADclass from jsonschema import validate as js_v, exceptions as js_e import host_thread as ht @@ -61,6 +63,12 @@ HTTP_Conflict = 409 HTTP_Service_Unavailable = 503 HTTP_Internal_Server_Error= 500 +def md5(fname): + hash_md5 = hashlib.md5() + with open(fname, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_md5.update(chunk) + return hash_md5.hexdigest() def check_extended(extended, allow_net_attach=False): '''Makes and extra checking of extended input that cannot be done using jsonschema @@ -1077,6 +1085,20 @@ def http_post_images(tenant_id): metadata_dict = http_content['image'].pop('metadata', None) if metadata_dict is not None: http_content['image']['metadata'] = json.dumps(metadata_dict) + #calculate checksum + host_test_mode = True if config_dic['mode']=='test' or config_dic['mode']=="OF only" else False + try: + image_file = http_content['image'].get('path',None) + if os.path.exists(image_file): + http_content['image']['checksum'] = md5(image_file) + else: + if not host_test_mode: + content = "Image file not found" + print "http_post_images error: %d %s" % (HTTP_Bad_Request, content) + bottle.abort(HTTP_Bad_Request, content) + except Exception as e: + print "ERROR. Unexpected exception: %s" % (str(e)) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) #insert in data base result, content = my.db.new_image(http_content['image'], tenant_id) if result >= 0: diff --git a/openvimd.py b/openvimd.py index 2e22121..9134076 100755 --- a/openvimd.py +++ b/openvimd.py @@ -30,9 +30,9 @@ and host controllers __author__="Alfonso Tierno" __date__ ="$10-jul-2014 12:07:15$" -__version__="0.4.9-r5002" +__version__="0.4.10-r503" version_date="Sep 2016" -database_version="0.7" #expected database schema version +database_version="0.8" #expected database schema version import httpserver import auxiliary_functions as af -- 2.17.1