def buildSysConfig(serverSection=False, host_key_detect='host_key_detect', file='/data_local/Python/system.ini', identifierPath='/data_local/Python/', wildcard='server__*__*.info', DEFAULT_APPS = {}, CORE_APPS = {}, UI_APPS = {}, INCLUDED_APPS = {}, GLOBAL_CORE_APPS = {}, host_delimiter = ':', net_support_sections = '' ): # Initial Vars/Methods All set attribute # FIXME - Ugly but works... foundsection = False popup = False ConfigParser.getKey = getKey ConfigParser.getListOfKeys = getListOfKeys ConfigParser.host_key_detect = host_key_detect ConfigParser.getSectionApps = getSectionApps ConfigParser.getAppsServers = getAppsServers ConfigParser.getUniqeApps = getUniqeApps ConfigParser.appNotInServers = appNotInServers ConfigParser.DynamicOption = DynamicOption ConfigParser.getAppsByClusterUpper = getAppsByClusterUpper ConfigParser.getAppsByCluster = getAppsByCluster ConfigParser.getAppsBy = getAppsBy ConfigParser.getUnionApps = getUnionApps ConfigParser.filter_by_index = filter_by_index ConfigParser.CORE_APPS = copy.deepcopy(CORE_APPS) ConfigParser.INCLUDED_APPS = INCLUDED_APPS ConfigParser.DEFAULT_APPS = DEFAULT_APPS ConfigParser.UI_APPS = UI_APPS ConfigParser.GLOBAL_CORE_APPS = GLOBAL_CORE_APPS ConfigParser.getInterfaceByIP = getInterfaceByIP ConfigParser.getNetworkByIP = getNetworkByIP ConfigParser.AppVip = AppVip ConfigParser.AppVips = AppVips ConfigParser.getID = getID ConfigParser.host_delimiter = host_delimiter sysConfig = ConfigParser() # Get ini parser objext returnFiles = sysConfig.read(file) if returnFiles.__len__() == 0: raise Exception, 'ConfigParser could not open: \"%s\"' % file # Select server from section by file or force_server_id option under RUNNER section else popup to the user elif not os.path.isfile(file): raise Exception, 'No ini file where found: \"%s\"' % file serverID = glob.glob(os.path.join(identifierPath,wildcard)) old_serverID = serverID sysConfig.server_sections = [s for s in sysConfig.sections() if sysConfig.has_option(s, 'type') and \ sysConfig.get(s, 'type') == 'server' and sysConfig.has_option(s, host_key_detect) ] # 1st condition check if force_server_id is enabled if sysConfig.has_option('RUNNER', 'force_server_id') and sysConfig.get('RUNNER', 'force_server_id'): serverID = sysConfig.get('RUNNER', 'force_server_id') #FIXME - Amir,set the checking if file exist to function if serverID.find(sysConfig.host_delimiter) > -1: #serverID = [x.strip(' ') for x in serverID.split(host_delimiter)] serverSection,serverID = serverID.split(sysConfig.host_delimiter) if not serverID.startswith( sysConfig.get(serverSection, host_key_detect)): serverID = sysConfig.get(serverSection, host_key_detect) + serverID elif serverID.find(sysConfig.host_delimiter) == -1: for section in sysConfig.sections(): if serverID == section: serverSection = serverID foundsection = True if not serverID.startswith( sysConfig.get(serverSection, host_key_detect)) and not foundsection: serverID = sysConfig.get(serverSection, host_key_detect) + serverID # 2nd condition check if file server__*__.info exsit elif serverID.__len__() > 1: raise Exception, 'Found more then one serverID file: %s' % ', '.join(serverID) elif serverID.__len__() == 0: if not serverSection: sys.stdout.write("\n\n[Runner] What is the section name from system.ini file?: ") sys.stdout.flush() serverSection = raw_input() sys.stdout.write("\n\n[Runner] What is \"%s\" server index from system.ini file?: %s" % (serverSection, sysConfig.get(serverSection, host_key_detect))) sys.stdout.flush() serverID = raw_input() if not serverID.startswith( sysConfig.get(serverSection, host_key_detect)): serverID = sysConfig.get(serverSection, host_key_detect) + serverID popup = True elif serverID.__len__() == 1: serverID = os.path.basename(serverID[0]) if serverID[6:].startswith('__'): serverSection = serverID.split('__')[1] serverID = serverID.split('__')[2].split('.')[0] elif serverID[6:].startswith('_'): serverID = serverID.split('_')[1].split('.')[0] else: e = '%s: %s\n' % (sys.exc_info()[0], sys.exc_info()[1]) raise Exception, '%s BadFileName Failed to get server Type or serverID file' % e # 3rd condition check what is the section if didn't get one yet if not serverSection: sys.stdout.write("\n\nWhat is the section name from system.ini file? ") sys.stdout.flush() serverSection = raw_input() if sysConfig.has_option(serverSection, 'type') and sysConfig.get(serverSection, 'type') == 'server': popup = True else: raise Exception, 'Section name must be one of the following\n%s' % sysConfig.server_sections # If popup exist, save the file to HD under /data_local/Python if popup: if old_serverID: os.remove(old_serverID[0]) f = open(os.path.join(identifierPath, 'server__'+ serverSection + '__'+ serverID + '.info'), 'w') f.close() sysConfig.set('RUNNER','Section',serverSection) sysConfig.Section = serverSection # Setting network dict to all network sections: key=net section name, value=ipcalc obj of the nework sysConfig.network_sections = networkSectionList(sysConfig) sysConfig.set('RUNNER', 'network_sections', sysConfig.network_sections) # Build servers Dictionary including replacing ips to full name and implement range options. for section in sysConfig.server_sections: host_key = sysConfig.get(section, host_key_detect) id_list = [] host_id = '' counter = True if sysConfig.has_option(section, 'range') and sysConfig.getint(section, 'range') > 1: for item in sysConfig.items(section): if item[0].startswith(host_key) and item[0][-1].isdigit(): host_id = re.findall('\d+',item[0]) if host_key in id_list: counter = False else: id_list.append(host_key) elif item[0].startswith(host_key) and item[0].endswith('a')and item[0][-2].isdigit(): host_id = re.findall('\d+a',item[0]) id_key = re.search('(\d+)',host_id[0]).group(0) if id_key in id_list: counter = False else: id_list.append(id_key) # Building range server list for this section if host_id: if not counter: raise Exception, 'Range is already builded for server(%s) from Section %s cant build again for %s' % (servername,section,host_key+host_id[0]) else: rebuildRange(sysConfig, section, host_key,host_id) servername = host_key+host_id[0] # update servers ips tp full name and converst servers option to dict serversDictBuild(sysConfig, section, host_key) for section in net_support_sections: if section in sysConfig.sections(): setSectionSupportNetwork(sysConfig,section) # build auto requirement for applications according to GLOBAL_CORE_APPS global_required_apps_builder(sysConfig) # build new options for serverID IPs/hostname/otherhostname/vips/{{ID}} buildSectionInfo(sysConfig, serverSection, serverID) include_apps_builder(sysConfig) # Debug print: if sysConfig.has_option('RUNNER', 'debug'): sections = sysConfig.get('RUNNER', 'debug') if not sections: sections = (serverSection,) else: sections = [s.strip().upper() for s in sections.split(',')] print 'buildSectionInfo DEBUG:' for section in sections: debug = 'DEBUG: Section - \"%s\"' % section print debug print '-' * len(debug) try: for item in sysConfig.items(section): print '%s = %s' % (item[0], item[1]) except: print 'DEBUG Error: Cant find \"%s\" section in ini file.' % section continue print '#' * 10 + ' END Section - %s ' %section + '#' * 10 + '\n' return sysConfig, sysConfig.getKey, sysConfig.getListOfKeys