X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=tools%2Fvmware_ovf_upload.py;h=834068de6501dcd717a42391f94141ee4a8b2f01;hb=3388212eac8fa6aff2f5d766e62e33962816501b;hp=4f3f0e7a66e6b3fbd5a7a20a76012bb5d825833d;hpb=a4347784b2c9bbe518efbbcfbafcfcc6a41b5aa3;p=osm%2Fdevops.git diff --git a/tools/vmware_ovf_upload.py b/tools/vmware_ovf_upload.py index 4f3f0e7a..834068de 100755 --- a/tools/vmware_ovf_upload.py +++ b/tools/vmware_ovf_upload.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -## +# # # Copyright 2016-2017 VMware Inc. # This file is part of ETSI OSM # All Rights Reserved. @@ -19,26 +19,25 @@ # # For those usages not covered by the Apache License, Version 2.0 please # contact: osslegalrouting@vmware.com -## +# # - -from xml.etree import ElementTree as XmlElementTree -from pyvcloud.vcd.client import BasicLoginCredentials,Client -from pyvcloud.vcd.vdc import VDC -from pyvcloud.vcd.org import Org -import sys,os import logging +import os +from progressbar import Percentage, Bar, ETA, FileTransferSpeed, ProgressBar +from pyvcloud.vcd.client import BasicLoginCredentials, Client +from pyvcloud.vcd.org import Org +import re import requests +import sys import time -import re -import hashlib -from progressbar import Percentage, Bar, ETA, FileTransferSpeed, ProgressBar - +from xml.etree import ElementTree as XmlElementTree API_VERSION = '5.6' + class vCloudconfig(object): - def __init__(self, host=None, user=None, password=None,orgname=None, logger=None): + + def __init__(self, host=None, user=None, password=None, orgname=None, logger=None): self.url = host self.user = user self.password = password @@ -55,12 +54,12 @@ class vCloudconfig(object): try: self.logger.debug("Logging in to a vcd {} as user {}".format(self.org, - self.user)) + self.user)) client = Client(self.url, verify_ssl_certs=False) client.set_credentials(BasicLoginCredentials(self.user, self.org, self.password)) - except: + except Exception: raise Exception("Can't connect to a vCloud director org: " - "{} as user: {}".format(self.org, self.user)) + "{} as user: {}".format(self.org, self.user)) return client @@ -84,19 +83,19 @@ class vCloudconfig(object): self.logger.debug("get_catalog_id_from_path() client requesting {} ".format(path)) - dirpath, filename = os.path.split(path) - flname, file_extension = os.path.splitext(path) + _, filename = os.path.split(path) + _, file_extension = os.path.splitext(path) if file_extension != '.ovf': self.logger.debug("Wrong file extension {} connector support only OVF container.".format(file_extension)) raise Exception("Wrong container. vCloud director supports only OVF.") self.logger.debug("File name {} Catalog Name {} file path {} ".format(filename, - catalog_name, - path)) + catalog_name, + path)) try: client = self.connect() if not client: - raise Exception("Failed to connect vCD") + raise Exception("Failed to connect vCD") org = Org(client, resource=client.get_org()) catalogs = org.list_catalogs() except Exception as exp: @@ -109,7 +108,7 @@ class vCloudconfig(object): if result is None: raise Exception("Failed to create new catalog {} ".format(catalog_name)) result = self.upload_ovf(org=org, catalog_name=catalog_name, image_name=filename.split(".")[0], - media_file_name=path, description='medial_file_name', progress=progress) + media_file_name=path, description='medial_file_name', progress=progress) if not result: raise Exception("Failed to create vApp template for catalog {} ".format(catalog_name)) return self.get_catalogid(catalog_name, catalogs) @@ -118,18 +117,18 @@ class vCloudconfig(object): # search for existing catalog if we find same name we return ID if catalog['name'] == catalog_name: self.logger.debug("Found existing catalog entry for {} " - "catalog id {}".format(catalog_name, - self.get_catalogid(catalog_name, catalogs))) + "catalog id {}".format(catalog_name, + self.get_catalogid(catalog_name, catalogs))) return self.get_catalogid(catalog_name, catalogs) # if we didn't find existing catalog we create a new one and upload image. self.logger.debug("Creating new catalog entry {} - {}".format(catalog_name, catalog_name)) - result = org.create_catalog(catalog_name, catalog_name) + result = org.create_catalog(catalog_name, catalog_name) if result is None: raise Exception("Failed to create new catalog {} ".format(catalog_name)) result = self.upload_ovf(org=org, catalog_name=catalog_name, image_name=filename.split(".")[0], - media_file_name=path, description='medial_file_name', progress=progress) + media_file_name=path, description='medial_file_name', progress=progress) if not result: raise Exception("Failed create vApp template for catalog {} ".format(catalog_name)) @@ -176,21 +175,23 @@ class vCloudconfig(object): continue catalog_href = "{}/api/catalog/{}/action/upload".format(self.url, catalog['id']) data = """ - {} vApp Template + + {} vApp Template """.format(catalog_name, description) if client: - headers = {'Accept':'application/*+xml;version=' + API_VERSION, - 'x-vcloud-authorization': client._session.headers['x-vcloud-authorization']} + headers = {'Accept': 'application/*+xml;version=' + API_VERSION, + 'x-vcloud-authorization': client._session.headers['x-vcloud-authorization']} headers['Content-Type'] = 'application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml' response = requests.post(url=catalog_href, - headers=headers, - data=data, - verify=False) + headers=headers, + data=data, + verify=False) if response.status_code != 201: self.logger.debug("Failed to create vApp template") - raise Exception("Failed to create vApp template") + raise Exception("Failed to create vApp template") if response.status_code == requests.codes.created: catalogItem = XmlElementTree.fromstring(response.content) @@ -200,45 +201,48 @@ class vCloudconfig(object): template = href response = requests.get(url=href, - headers=headers, - verify=False) + headers=headers, + verify=False) if response.status_code == requests.codes.ok: headers['Content-Type'] = 'Content-Type text/xml' - result = re.search('rel="upload:default"\shref="(.*?\/descriptor.ovf)"',response.content) + result = re.search('rel="upload:default"\shref="(.*?\/descriptor.ovf)"', response.content) if result: transfer_href = result.group(1) response = requests.put(url=transfer_href, headers=headers, - data=open(media_file_name, 'rb'), - verify=False) + data=open(media_file_name, 'rb'), + verify=False) if response.status_code != requests.codes.ok: self.logger.debug( - "Failed create vApp template for catalog name {} and image {}".format(catalog_name, - media_file_name)) + "Failed create vApp template for catalog name {} and image {}".format( + catalog_name, + media_file_name)) return False # TODO fix this with aync block time.sleep(5) - self.logger.debug("vApp template for catalog name {} and image {}".format(catalog_name, media_file_name)) + self.logger.debug("vApp template for catalog name {} and image {}".format( + catalog_name, + media_file_name)) # uploading VMDK file # check status of OVF upload and upload remaining files. response = requests.get(url=template, - headers=headers, + headers=headers, verify=False) if response.status_code == requests.codes.ok: - result = re.search('rel="upload:default"\s*href="(.*?vmdk)"',response.content) + result = re.search('rel="upload:default"\s*href="(.*?vmdk)"', response.content) if result: link_href = result.group(1) # we skip ovf since it already uploaded. if 'ovf' in link_href: continue # The OVF file and VMDK must be in a same directory - head, tail = os.path.split(media_file_name) + head, _ = os.path.split(media_file_name) file_vmdk = head + '/' + link_href.split("/")[-1] if not os.path.isfile(file_vmdk): return False @@ -248,7 +252,7 @@ class vCloudconfig(object): hrefvmdk = link_href if progress: widgets = ['Uploading file: ', Percentage(), ' ', Bar(), ' ', ETA(), ' ', - FileTransferSpeed()] + FileTransferSpeed()] progress_bar = ProgressBar(widgets=widgets, maxval=statinfo.st_size).start() bytes_transferred = 0 @@ -260,9 +264,9 @@ class vCloudconfig(object): bytes_transferred, len(my_bytes) - 1, statinfo.st_size) headers['Content-Length'] = str(len(my_bytes)) response = requests.put(url=hrefvmdk, - headers=headers, - data=my_bytes, - verify=False) + headers=headers, + data=my_bytes, + verify=False) if response.status_code == requests.codes.ok: bytes_transferred += len(my_bytes) if progress: @@ -270,7 +274,7 @@ class vCloudconfig(object): else: self.logger.debug( 'file upload failed with error: [%s] %s' % (response.status_code, - response.content)) + response.content)) f.close() return False @@ -282,18 +286,21 @@ class vCloudconfig(object): else: self.logger.debug("Failed retrieve vApp template for catalog name {} for OVF {}". format(catalog_name, media_file_name)) - return False + return False except Exception as exp: self.logger.debug("Failed while uploading OVF to catalog {} for OVF file {} with Exception {}" - .format(catalog_name,media_file_name, exp)) + .format(catalog_name, media_file_name, exp)) raise Exception( "Failed while uploading OVF to catalog {} for OVF file {} with Exception {}" - .format(catalog_name,media_file_name, exp)) + .format(catalog_name, media_file_name, exp)) self.logger.debug("Failed to retrieve catalog name {} for OVF file {}".format(catalog_name, media_file_name)) - return False + return False + if __name__ == "__main__": + print("This file is deprecated. Please use ovf_uplader_cli instead.") + # vmware vcloud director credentials vcd_hostname = sys.argv[1] vcd_username = sys.argv[2] @@ -302,17 +309,7 @@ if __name__ == "__main__": # OVF image path to be upload to vCD ovf_file_path = sys.argv[5] - # changing virtual system type in ovf file - fh = open(ovf_file_path,'r') - content = fh.read() - content = content.replace('vmx-7','vmx-07') - fh.close() - fh1 = open(ovf_file_path,'w') - fh1.write(content) - fh1.close() - - - logging.basicConfig(filename='ovf_upload.log',level=logging.DEBUG) + logging.basicConfig(filename='ovf_upload.log', level=logging.DEBUG) logger = logging.getLogger(__name__) obj = vCloudconfig(vcd_hostname, vcd_username, vcd_password, orgname, logger) @@ -322,10 +319,11 @@ if __name__ == "__main__": # Get image name from cirros vnfd cirros_yaml = '../descriptor-packages/vnfd/cirros_vnf/src/cirros_vnfd.yaml' - rh = open(cirros_yaml,'r') - match = re.search("image:\s'(.*?)'\n",rh.read()) - if match: catalog = match.group(1) + rh = open(cirros_yaml, 'r') + match = re.search("image:\s'(.*?)'\n", rh.read()) + if match: + catalog = match.group(1) if file_extension == '.ovf': obj.get_catalog_id_from_path(catalog_name=catalog, path=ovf_file_path, - progress=True) + progress=True)