Beispiel #1
0
    def generator_networks(self, generator_queues=None):
        log.title('generator networks')

        settings, orderers, peers, kafka, zookeeper = self.transform_model()
        tools = GenFabricNetworkTools(settings['output'])

        if generator_queues is None:
            self.__copy_network_files(settings['output'])

        if generator_queues is None or 'crypto' in generator_queues:
            result = tools.gen_crypto_config(orderers, peers)
        if generator_queues is None or 'configtx' in generator_queues:
            result = tools.gen_configtx(orderers, peers, kafka, settings['orderer_type'])
        if generator_queues is None or 'shell' in generator_queues:
            result = tools.gen_generate_shell(peers)
        if generator_queues is None or 'fabric' in generator_queues:
            result = tools.gen_fabric_compose(orderers, peers, zookeeper, kafka)
        if generator_queues is None or 'zookeeper_kafka' in generator_queues:
            result = tools.gen_zookeeper_kafka(zookeeper, kafka)
        if generator_queues is None or 'template' in generator_queues:
            result = tools.gen_fabric_template(orderers, peers, zookeeper, kafka)
        if generator_queues is None or 'couchdb' in generator_queues:
            result = tools.gen_couchdb(orderers, peers, zookeeper, kafka)
        if generator_queues is None or 'monitor' in generator_queues:
            result = tools.gen_monitor(orderers, peers, zookeeper, kafka)
        if generator_queues is None or 'properties' in generator_queues:
            result = tools.gen_properties(orderers, peers)

        # print(result)
        log.done('generator networks')
    def generator(output, template_file, args):
        log.line('template engine generator file')

        result = TemplateEngine.render(template_file, args)

        TemplateEngine.writer(output, template_file, result)

        log.done("template engine generator file")
        return result
    def render(template_file, args):
        log.info("generator config template file: %s" % template_file)
        log.debug("generator args: %s" % args)

        # 获取模板
        template = TemplateEngine.__env.get_template(template_file)
        # 渲染模板
        result = template.render(args)

        return result
    def gen_configtx(self, orderers, peers, kafka, orderer_type):
        log.line('start generator configtx config')

        orderers, peers, kafka, _ = self.__default_args(orderers, peers, kafka)
        log.debug('default orderers: %s' % orderers)
        log.debug('default peers: %s' % peers)
        log.debug('default kafka: %s' % kafka)

        args = dict(orderers=orderers, peers=peers, orderer_type=orderer_type, kafka_count=kafka['count'])
        result = TemplateEngine.generator(self.output_directory, "fabric-configs/configtx.yaml", args)

        log.done('generator configtx config')
        return result
    def writer(output, template_file, data):
        log.debug("generator config to location: %s" % output)

        file_path = output + "/" + template_file
        realpath = os.path.realpath(file_path)
        dirname = os.path.dirname(realpath)
        log.info('generator file to absolute path: %s' % realpath)

        if not os.path.exists(dirname):
            os.makedirs(dirname)

        with open(file_path, 'w', encoding=u'utf-8') as file:
            file.seek(0)
            file.truncate()  # 清空文件

            file.write(data)

            file.flush()
            file.close()
    def gen_crypto_config(self, orderers, peers, orderer_type='kafka'):
        log.line('start generator crypto config')

        orderers, peers, *_ = self.__default_args(orderers, peers)
        log.debug('default orderers: %s' % orderers)
        log.debug('default peers: %s' % peers)

        args = dict(orderers=orderers, peers=peers, orderer_type=orderer_type)
        result = TemplateEngine.generator(self.output_directory, "fabric-configs/crypto-config.yaml", args)

        log.done('generator crypto config')
        return result
    def gen_zookeeper_kafka(self, zookeeper, kafka):
        log.line('start generator fabric zookeeper-kafka file')

        _, _, kafka, zookeeper = self.__default_args(None, None, kafka, zookeeper)
        log.debug('default kafka: %s' % kafka)
        log.debug('default zookeeper: %s' % zookeeper)

        args = dict(zookeeper_count=zookeeper['count'], kafka_count=kafka['count'])
        result = TemplateEngine.generator(self.output_directory, "docker-compose-zookeeper-kafka.yaml", args)

        log.done('generator fabric zookeeper-kafka file')
        return result
    def gen_properties(self, orderers, peers):
        log.line('start generator properties file')

        orderers, peers, *_ = self.__default_args(orderers, peers)
        log.debug('default orderers: %s' % orderers)
        log.debug('default peers: %s' % peers)

        args = dict(orderers=orderers, peers=peers)
        result = TemplateEngine.generator(self.output_directory, "properties/fabric-chaincode.produce.properties", args)
        result = TemplateEngine.generator(self.output_directory, "properties/fabric-chaincode.produce-safe.properties", args)

        log.done('generator properties file')
        return result
Beispiel #9
0
    def __reader_setting_config(self):
        log.line('reader settings config')

        config = 'settings.yaml'
        if not os.path.exists(config):
            log.error('%s is not found!' % config)
            raise IOError('%s is not found!' % config)

        with open(config, 'r', encoding=u'utf-8') as file:
            data = file.read()

        settings = yaml.load(data)['settings']
        log.debug('settings config data: %s' % settings)

        log.done('reader settings config')
        return settings
Beispiel #10
0
    def clean_output(self):
        settings, *_ = self.transform_model()

        output = settings['output']
        if output is None or len(output) < 1:
            raise IOError('output dir is undefined.')

        log.title('clean networks directory')
        log.info('clean networks directory: "%s"' % output)

        if not os.path.exists(output):
            log.info('[%s] networks directory is not exists' % output)
        else:
            shutil.rmtree(output)

        log.done('clean networks directory')
    def gen_generate_shell(self, peers):
        log.line('start generator shell script')

        _, peers, *_ = self.__default_args(None, peers)
        log.debug('default peers: %s' % peers)

        result = TemplateEngine.generator(self.output_directory, "fabric-configs/generate.sh", dict(peers=peers))

        log.done('generator shell script')
        return result
Beispiel #12
0
    def __copy_network_files(self, output='networks'):
        if output is None or len(output) < 1:
            raise IOError('output dir is undefined.')

        log.line('copy networks file')
        log.info('copy network files to "%s"' % output)

        if os.path.exists(output):
            shutil.rmtree(output)
        else:
            # os.makedirs(output, exist_ok=True)
            pass

        shutil.copytree('templates', output)
        shutil.rmtree('%s/commons-macro' % output)

        log.done('copy networks file')
Beispiel #13
0
    def transform_model(self):
        settings = self.__reader_setting_config()

        log.line('transfor settings to model')
        orderers, peers = [], []
        for orderer in settings['orderers']:
            name, domain, count, hostnames = None, None, None, None

            if 'name' in orderer:
                name = orderer['name']

            if 'domain' in orderer:
                domain = orderer['domain']

            if 'count' in orderer:
                count = orderer['count']

            if 'hostnames' in orderer:
                hostnames = orderer['hostnames']

            orderers.append(OrdererOrg(name, domain, hostnames, count))

        for peer in settings['peers']:
            name = domain = hostnames = template_count = user_count = None

            if 'name' in peer:
                name = peer['name']

            if 'domain' in peer:
                domain = peer['domain']

            if 'hostnames' in peer:
                hostnames = peer['hostnames']

            if 'template_count' in peer:
                template_count = peer['template_count']
            if 'user_count' in peer:
                user_count = peer['user_count']

            peers.append(PeerOrg(name, domain, hostnames, template_count, user_count))

        kafka = settings['kafka']
        zookeeper = settings['zookeeper']

        log.debug('orderers: %s' % str(orderers))
        log.debug('peers: %s' % str(peers))
        log.debug('kafka: %s' % str(kafka))
        log.debug('zookeeper: %s' % str(zookeeper))

        log.done('transfor settings to model')
        return settings, orderers, peers, kafka, zookeeper
Beispiel #14
0
def main(argv):
    log.debug('argv: %s' % argv)

    help_usage = '''
    USAGE: python generator_tools.py [OPTIONS] -hcxsfztdmp COMMANDS gen

    OPTIONS: 
      -h,--help         use the help manual.
      -c,--configtx     generator fabric network core config file, create configtx.yaml
      -x,--crypto       generator fabric channel & block core config file, create crypto-config.yaml
      -f,--fabric       generator fabric orderer/peer service, create docker-compose-fabric.yaml
      -s,--shell        generator fabric shell script, create generate.sh
      -z,--zk           generator fabric zookeeper/kafka service, create docker-compose-zookeeper-kafka.yaml
      -t,--template     generator fabric ca service and fabric network config, create docker-compose-fabric-template.yaml
      -d,--couchdb      generator couchdb service, create docker-compose-couchdb.yaml
      -m,--monitor      generator prom monitor service network, create docker-compose-fabric-monitor.yaml
      -p,--properties   generator connect fabric network properties config, create fabric-chaincode.*.properties
    
    COMMANDS:
      gen         generator fabric network config files
      regen       regenerator fabric network config files
      clean       clean fabric networks files
      help        use the help manual.
        
    EXAMPLES: 
      python generator_tools.py -h
      python generator_tools.py help
    
      python generator_tools.py gen
      python generator_tools.py clean
      
      python generator_tools.py -c gen
      python generator_tools.py -cx gen
      python generator_tools.py -cx clean gen
      
      python generator_tools.py -hcxsfztdmp gen
      python generator_tools.py -hcxsfztdmp clean gen
    '''

    # default generator all networks
    tools = GeneratorTools()
    if len(argv) < 1:
        tools.generator_networks()
        sys.exit()

    try:
        long_opts = ["help", "crypto", "configtx", "shell", "fabric", "zk", "template", "couchdb", "monitor", "properties"]
        opts, args = getopt.getopt(argv, "hcxsfztdmp", long_opts)
        log.debug('opts: %s, args: %s' % (opts, args))
    except getopt.GetoptError:
        print(help_usage)
        sys.exit(2)

    generator_queues = []
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            print(help_usage)
            sys.exit()
        elif opt in ("-c", "--crypto"):
            generator_queues.append('crypto')
        elif opt in ("-x", "--configtx"):
            generator_queues.append('configtx')
        elif opt in ("-s", "--shell"):
            generator_queues.append('shell')
        elif opt in ("-f", "--fabric"):
            generator_queues.append('fabric')
        elif opt in ("-z", "--zk"):
            generator_queues.append('zookeeper_kafka')
        elif opt in ("-t", "--template"):
            generator_queues.append('template')
        elif opt in ("-d", "--couchdb"):
            generator_queues.append('couchdb')
        elif opt in ("-m", "--monitor"):
            generator_queues.append('monitor')
        elif opt in ("-p", "--properties"):
            generator_queues.append('properties')

    if len(generator_queues) == 0:
        generator_queues = None

    for arg in args:
        if arg == 'clean':
            tools.clean_output()
        elif arg == 'gen' or arg == 'generator':
            tools.generator_networks(generator_queues)
        elif arg == 'regen':
            tools.clean_output()
            tools.generator_networks(generator_queues)
        else:
            print(help_usage)
            sys.exit()

    if len(args) < 0:
        print(help_usage)
        sys.exit()
    def gen_fabric_compose(self, orderers, peers, zookeeper, kafka):
        log.line('start generator fabric compose file')

        orderers, peers, kafka, zookeeper = self.__default_args(orderers, peers, kafka, zookeeper)
        log.debug('default orderers: %s' % orderers)
        log.debug('default peers: %s' % peers)
        log.debug('default kafka: %s' % kafka)
        log.debug('default zookeeper: %s' % zookeeper)

        args = dict(orderers=orderers, peers=peers, zookeeper_count=zookeeper['count'], kafka_count=kafka['count'])
        result = TemplateEngine.generator(self.output_directory, "docker-compose-fabric.yaml", args)

        log.done('generator fabric compose file')
        return result
    def gen_monitor(self, orderers, peers, zookeeper, kafka):
        log.line('start generator monitor file')

        orderers, peers, kafka, zookeeper = self.__default_args(orderers, peers, kafka, zookeeper)
        log.debug('default orderers: %s' % orderers)
        log.debug('default peers: %s' % peers)
        log.debug('default kafka: %s' % kafka)
        log.debug('default zookeeper: %s' % zookeeper)

        args = dict(zookeeper=zookeeper, kafka=kafka, orderers=orderers, peers=peers)
        result = TemplateEngine.generator(self.output_directory, "docker-compose-fabric-monitor.yaml", args)

        log.done('generator monitor file')
        return result