#!/usr/bin/python ############################################################################## ### ### generate-manifest.py ### ### A Vendor utility to generate a valid heat zip manifest file for the AT&T onboarding. ### ### Usage: ### ### generate-manifest.py [-f|--folder] vendor-heat-directory [-n|--name] manifest-name [-d|--description] manifet-description ### ### For example: ### ### ./generate-manifest.py --folder ./vota --name vOTA --description "HOT template to create vOTA server" ### ### Help: ### The script is doing the following: ### 1) Split the files into different types ### a. .env files ### b. Network files (anything containing the string network) ### c. Volume files (anything containing the string volume) ### d. Top level Heat files ### e. Other types ### 2) Match env files to heat files – looking for same name ignoring suffix and extension ### 3) Match Network childs ### a. Look for Top level heats which name is a substring of the name of the Network heat name. ### 4) Match Volume childs ### a. Look for Top level heats which name is a substring of the name of the Volume heat name. ### 5) Generate the JSON file from the above ### ### ### Author: Avi Ziv ### Version 1.4 for ASDC/AT&T 1.0 ### Date: 13 July 2016 ### ############################################################################## # import os,sys,getopt,json,re import os, sys, getopt, re from collections import OrderedDict from json import JSONEncoder import json VERSION = "1.4" ENV_EXT = ".env" SHELL_EXT = ".sh" YAML_EXT = [".yaml", ".yml"] # VERSION_DELIMITER_PATTERN='_v\d{*}.\d{*}' # VERSION_DELIMITER_PATTERN='_v*.*' #v1.0 VERSION_DELIMITER_PATTERN = '_v\d+.\d+' #07_12_2016 VERSION_DELIMITER_PATTERN2 = '_\d+-\d+-\d+' # types HEAT = "HEAT" HEAT_BASE = "HEAT_BASE" HEAT_NET = "HEAT_NET" HEAT_VOL = "HEAT_VOL" HEAT_ENV = "HEAT_ENV" SHELL = "SHELL" OTHER = "OTHER" globalVolumeVal = "VOLUME" globalNetworkVal = "NETWORK" globalBaseVal = "BASE" def version(): return VERSION def usage(): print ('usage: ' + sys.argv[0] + ' [-f|--folder] vendor-heat-directory [-n|--name] manifest-name [-d|--description] manifet-description' ) def header(): print ("\nASDC Vendor manifest file generator, version " + version() + "\n") def getEnvVariables(value, defaultValue): try: eVal = os.environ[value] return eVal except KeyError: print ("Missing ${" + value + "} envirunment variable. Using default value: " + defaultValue) return defaultValue def getF(listFiles): print ("Analyzing files ...") foundABase = False files = listFiles jsons = {} lOfEnvs = {} lOfVolumes = {} lOfNetworks = {} lOfHeats = {} lOfShels = {} lOfArtifacts = {} for f in files: fullFilename = f[1] fObj = ManifestFileInfo(fullFilename) if fObj.isEnv(): lOfEnvs[fObj.file_name] = fObj elif fObj.isShell(): lOfShels[fObj.file_name] = fObj elif fObj.isVolume(): lOfVolumes[fObj.file_name] = fObj elif fObj.isNetwork(): lOfNetworks[fObj.file_name] = fObj elif (fObj.isYaml() and not fObj.isBase()): lOfHeats[fObj.file_name] = fObj elif fObj.isArtifact(): lOfArtifacts[fObj.file_name] = fObj elif (fObj.isBase() and fObj.isYaml()): foundABase = True lOfHeats[fObj.file_name] = fObj jsons['heats'] = lOfHeats jsons['envs'] = lOfEnvs jsons['shells'] = lOfShels jsons['volumes'] = lOfVolumes jsons['networks'] = lOfNetworks jsons['artifacts'] = lOfArtifacts if not foundABase: print (">>> Warning: No Base was found") return jsons def loadFilesToList(folder): print ("Analyzing files in folder: << " + folder + " >>") files = os.listdir(folder) listOfFiles = [] for f in files: if os.path.isdir(os.path.join(folder, f)): ConsoleLogger.warning("Sub folders are ignored by this script, you may want to remove it before archiving") continue filename, file_extension = os.path.splitext(f) if filename == 'MANIFEST': ConsoleLogger.warning("Your folder already contains a manifest file that will be overridden") continue listOfFiles.append([filename, f]) return listOfFiles def make(files): flist = [] dEnvs = {} dEnvs = files['envs'] dHeats = files['heats'] dNetworks = files['networks'] dVolumes = files['volumes'] dArtifacts = files['artifacts'] dShells = files['shells'] env_items = dEnvs.items() for fileItem in env_items: env_name = fileItem[1].file_name env_base = fileItem[1].base_file_name if env_name in dHeats: dHeats[env_name].add(fileItem[1]) continue if env_name in dNetworks.items(): dNetworks[env_name].add(fileItem[1]) continue if env_name in dVolumes.items(): dVolumes[env_name[0]].add(env_name[1]) continue for fName in dHeats: heat_base = dHeats[fName].base_file_name if env_base in heat_base: dHeats[fName].add(dEnvs[env_name]) break else: for fName in dNetworks: net_base = dNetworks[fName].base_file_name if env_base in net_base: dNetworks[fName].add(dEnvs[env_name]) break else: for fName in dVolumes: vol_base = dVolumes[fName].base_file_name if env_base in vol_base: dVolumes[fName].ad