Example #1
0
File: User.py Project: cffex/Cersy
    def generateParamClass(self):
        conn = RestCMDBConnector("192.168.1.3", "8080")
        conn.connector()
        
        self.param_classes[self.title] = {}
        self.param_classes[self.title][self.title_group] = {}

        #原有user的属性,attributes为list,保存了同一个CI TYPE:OS_USER的多个CI
        for index in range(0, len(self.attributes)):
            each = self.attributes[index]
            name = each['name']
            self.param_classes[self.title][self.title_group][name] = {}
            export_list = []
            
            for key, value in each.items():            
                if key.startswith('export'):
                    export_list.append(re.sub('export_', '', key) + '=' + value)
                    continue
                self.param_classes[self.title][self.title_group][name][key] = {}
                self.param_classes[self.title][self.title_group][name][key] = value
            
            if len(export_list) != 0 :
                self.param_classes[self.title][self.title_group][name]['profile'] = export_list
             
            #生成users中需要建立信任关系的属性,因为可能会存在多个用户,所以使用列表形式
            vtime = self.parameters[index]["TIME"]
            #获取该HOST_CI和OS_USER的type fid。这里没有初始化为CI,所以访问的时候直接使用字典形式访问
            userCIType = conn.getCIType("OS_USER", timestamp = vtime)
            hostCIType = conn.getCIType("HOST_OS", timestamp = vtime)

            if len(userCIType) != 1 or len(hostCIType) != 1:
                self.log.output('ERROR', '[%s]: 获取的CI TYPE[HOST_OS|OS_USER],无法唯一确定CI TYPE' % 
                                self.__class__.__name__)
                sys.exit(-1)

            targetUserCIList = conn.getCIByRelations(sourcefid = self.parameters[index]["FAMILY_ID"],
                                targettype_fid = userCIType[0]["FAMILY_ID"] )
            #此用户没有信任关系需要建立
            if len(targetUserCIList) == 0:
                continue

            for eachUserCI in targetUserCIList :
                #获取该用户属于的主机,targetHost列表长度应为1
                targetHost = conn.getCIByRelations(targetfid = eachUserCI.parameters["FAMILY_ID"], 
                                sourcetype_fid = hostCIType[0]["FAMILY_ID"], location="source")

                if len(targetHost) != 1:
                    self.log.output('ERROR', '[%s]: 该用户所属的主机不唯一,异常并退出' %  
                                self.__class__.__name__)
                    sys.exit(-1)

                targetHostName = conn.getCI(ciFid = targetHost[0].parameters["FAMILY_ID"], 
                                            timestamp = vtime).parameters["NAME"]
                
                temp_name = eachUserCI.parameters['NAME']
                temp_key = eachUserCI.attributes["KEY"]
                temp_type = eachUserCI.attributes["PROTOCOL_TYPE"]
                temp_title = targetHostName + "@" + temp_name
                
                self.param_classes[self.title][self.title_group][name][self.title_ssh] = {}
                self.param_classes[self.title][self.title_group][name][self.title_ssh][temp_title] = {}
                self.param_classes[self.title][self.title_group][name][self.title_ssh][temp_title]["key"] = {}
                self.param_classes[self.title][self.title_group][name][self.title_ssh][temp_title]["type"] = {}
                self.param_classes[self.title][self.title_group][name][self.title_ssh][temp_title]["key"] = temp_key
                self.param_classes[self.title][self.title_group][name][self.title_ssh][temp_title]["type"] = temp_type
        
        conn.close()
        return self.param_classes
Example #2
0
File: Main.py Project: cffex/Cersy
    hash = HashGenerator()
    log = SysLog()
    
    #hostname = 'slave1'
    hostname = raw_input("Please input hostname: ")
    print "Begin to Generate YAML file, Please wait for a second..."
    ci_host = connector.getCI(ciName = hostname, ciType = "HOST_OS")

    #依据该字段进行查找其实不是非常的必要,只需要将于HOST_OS相关的所有ci找出来就可以了,但是使用该字段后,方便测试
    relationList = ['OSHASUSER',
                     'OSHASKERNEL', 'OSHASUSERGLOBAL',  'OSHASLOG', 'OSHASSERVICE',
                     'OSHASPACKAGE', 'OSHASSECURITY', 'OSHASTIMESYNCCLIENT', 'OSHASGROUP',
                     'OSHASAUTHORIZATION',
                    ]

    #这里认为根据主机找出的ci必须是唯一的,如果不唯一的,那么则报错
    for re in relationList :
        #获取与该os ic具有re关系的所有ci(list)
        CIList = connector.getCIByRelations(sourcefid = ci_host.parameters['FAMILY_ID'], relation = re, 
                                            timestamp = ci_host.parameters['TIME'])
        for index in range(0, len(CIList)):
            factory.CIInitialization(CIList[index])
    
    factory.factoryTransformation()
    hash.generateYAML(hostname, factory.modelList)
    log.output('INFO', 'Generate ENC Finished')
    connector.close()
    
    print "File generates successfully, Please check it..."