Exemplo n.º 1
0
def __build_ca_service(caID, peerOrg, ca_image, tls, pk_name, port, network):
    try:
        environment = [
            "FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server",
            "FABRIC_CA_SERVER_CA_NAME=ca-" + peerOrg.org_name,
            "FABRIC_CA_SERVER_TLS_ENABLED=" + tls,
            "FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca."
            + peerOrg.domain + "-cert.pem",
            "FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/"
            + str(pk_name)
        ]
        ports = [str(port) + ":" + str(CA_PORT)]
        #command="sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca."+peerOrg.domain+"-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/"+pk_name+" -b admin:adminpw -d'"
        command = "sh -c 'fabric-ca-server start -b admin:adminpw -d'"
        volumes = [
            "./crypto-config/peerOrganizations/" + peerOrg.domain +
            "/ca/:/etc/hyperledger/fabric-ca-server-config"
        ]
        container_name = caID
        networks = [network]
        return DockerService(ports=ports,
                             volumes=volumes,
                             environment=environment,
                             command=command,
                             image=ca_image,
                             container_name=container_name,
                             networks=networks)
    except Exception as e:
        logger.error(e)
        raise Exception("Build ca_service for Docker-compose.yaml failled!")
Exemplo n.º 2
0
def create_configtx_file(config, dir, configtx_filename=CNFIGTX_FN):
    logger.info("Create configtx.yaml...")
    try:
        orderer_default = __create_orderer(config)
        orderer_org = __create_orderer_org(config)
        peers_org = __create_peers_org(config)
        application_default = __create_application(config)
        orderer_default['Organizations'] = orderer_org
        application_default['Organizations'] = peers_org

        orderer_profil = {}
        sample_consortium = {}
        consortiums = {}
        channel_profil = {}
        profiles = {}
        dic = {}
        orderer_profil['Orderer'] = orderer_default
        sample_consortium['Organizations'] = peers_org
        consortiums['SampleConsortium'] = sample_consortium
        orderer_profil['Consortiums'] = consortiums
        channel_profil['Consortium'] = "SampleConsortium"
        channel_profil['Application'] = application_default
        profiles['OrgsChannel'] = channel_profil
        profiles['OrgsOrdererGenesis'] = orderer_profil
        dic['Profiles'] = profiles
        dump_yaml_file(data=dic, file_path=dir + "/" + configtx_filename)
        logger.debug(json.dumps(dic, indent=4))
    except Exception as e:
        logger.error(e)
        raise Exception("Create crypto-config.yaml failled!")
Exemplo n.º 3
0
def create_docker_compose_file(ordererOrgs,
                               peerOrgs,
                               peer_image,
                               orderer_image,
                               ca_image,
                               tls,
                               dir,
                               network="net",
                               filename=DOCKERCOMPOSE_FN):
    try:
        logger.info("Create docker-compose.yaml...")
        services = []
        for peerOrg in peerOrgs:
            caID = "ca_peer" + peerOrg.org_name
            #pk_name=caID.upper()+"_PK"
            cmd = ('cd {0}/crypto-config/peerOrganizations/{1}/ca/ && ls *_sk'.
                   format(dir, peerOrg.domain))
            pk_name = exec_cmd(cmd).replace('\n', '').replace('\r', '')
            services.append(
                __build_ca_service(caID=caID,
                                   peerOrg=peerOrg,
                                   ca_image=ca_image,
                                   tls=tls,
                                   pk_name=pk_name,
                                   port=peerOrg.ca.port,
                                   network=network))
            for peer in peerOrg.peers:
                peerID = peer.hostname
                services.append(
                    __build_peer_service(peerID=peerID,
                                         peerOrg=peerOrg,
                                         peer_image=peer_image,
                                         tls=tls,
                                         port_req=peer.request_port,
                                         port_evt=peer.event_port,
                                         network=network))
        for ordererOrg in ordererOrgs:
            ordererID = ordererOrg.org_name.lower() + "." + ordererOrg.domain
            services.append(
                __build_orderer_service(ordererID=ordererID,
                                        ordererOrg=ordererOrg,
                                        orderer_image=orderer_image,
                                        peerOrgs=peerOrgs,
                                        tls=tls,
                                        port=ordererOrg.port,
                                        network=network))
        dockerfile_dict = {'version': "2"}
        dockerfile_dict['networks'] = {network: None}
        dockerfile_dict['services'] = {
            service.container_name: service.to_dict()
            for service in services
        }
        dump_yaml_file(data=dockerfile_dict, file_path=dir + "/" + filename)
        #logger.debug(json.dumps(dockerfile_dict, indent=4))
        #exec_cmd('cp {0}samples/1.0.4/docker-compose.yaml {1}/{2}'.format(my_path, dir, "docker-compose.yaml"))
        #exec_cmd('cp {0}samples/1.0.4/base.yaml {1}/{2}'.format(my_path, dir, "base.yaml"))

    except Exception as e:
        logger.error(e)
        raise Exception("create_docker_compose_file failled!")
Exemplo n.º 4
0
def generate():
    try:
        data = json.loads(request.data.decode())
        generate_all_material(data)
    except Exception as e:
        logger.error(str(e))
        abort(400)
    return jsonify({'result': "material generated"}), 201
Exemplo n.º 5
0
def __build_peer_service(peerID, peerOrg, peer_image, tls, port_req, port_evt,
                         network):
    try:
        environment = [
            "CORE_PEER_ID=" + peerID,
            "CORE_PEER_ADDRESS=" + peerID + ":" + str(PEER_REQUEST_PORT),
            "CORE_PEER_LOCALMSPID=" + peerOrg.org_name + "MSP",
            "CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock",
            "CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=artifacts_default",
            "CORE_LOGGING_LEVEL=DEBUG",
            "CORE_PEER_GOSSIP_USELEADERELECTION=true",
            "CORE_PEER_GOSSIP_ORGLEADER=false",
            "CORE_PEER_GOSSIP_EXTERNALENDPOINT=" + peerID + ":" +
            str(PEER_REQUEST_PORT),
            "CORE_PEER_GOSSIP_BOOTSTRAP=" + peerID + ":" +
            str(PEER_REQUEST_PORT),
            #"CORE_PEER_GOSSIP_SKIPHANDSHAKE=true",
            "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/crypto/peer/msp",
            "CORE_PEER_TLS_ENABLED=" + tls,
            "CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/crypto/peer/tls/server.key",
            "CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/crypto/peer/tls/server.crt",
            "CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/crypto/peer/tls/ca.crt"
        ]
        ports = [
            str(port_req) + ":" + str(PEER_REQUEST_PORT),
            str(port_evt) + ":" + str(PEER_EVENT_PORT)
        ]
        command = "peer node start"
        volumes = [
            "/var/run/:/host/var/run/",
            "./crypto-config/peerOrganizations/" + peerOrg.domain + "/peers/" +
            peerID + "/msp:/etc/hyperledger/crypto/peer/msp",
            "./crypto-config/peerOrganizations/" + peerOrg.domain + "/peers/" +
            peerID + "/tls:/etc/hyperledger/crypto/peer/tls"
        ]
        container_name = peerID
        networks = [network]
        working_dir = "/opt/gopath/src/github.com/hyperledger/fabric/peer"
        return DockerService(ports=ports,
                             volumes=volumes,
                             environment=environment,
                             command=command,
                             image=peer_image,
                             container_name=container_name,
                             working_dir=working_dir,
                             networks=networks)
    except Exception as e:
        logger.error(e)
        raise Exception("build_peer_service for Docker-compose.yaml failled!")
Exemplo n.º 6
0
def __build_orderer_service(ordererID, ordererOrg, orderer_image, peerOrgs,
                            tls, port, network):
    try:
        environment = [
            "ORDERER_GENERAL_LOGLEVEL=debug",
            "ORDERER_GENERAL_LISTENADDRESS=0.0.0.0",
            "ORDERER_GENERAL_GENESISMETHOD=file",
            "ORDERER_GENERAL_GENESISFILE=/etc/hyperledger/configtx/genesis.block",
            "ORDERER_GENERAL_LOCALMSPID=" + ordererOrg.mspid,
            "ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/crypto/orderer/msp",
            "ORDERER_GENERAL_TLS_ENABLED=" + tls,
            "ORDERER_GENERAL_TLS_PRIVATEKEY=/etc/hyperledger/crypto/orderer/tls/server.key",
            "ORDERER_GENERAL_TLS_CERTIFICATE=/etc/hyperledger/crypto/orderer/tls/server.crt",
            "ORDERER_GENERAL_TLS_ROOTCAS=[/etc/hyperledger/crypto/orderer/tls/ca.crt, /etc/hyperledger/crypto/peerOrg1/tls/ca.crt, /etc/hyperledger/crypto/peerOrg2/tls/ca.crt]"
        ]
        ports = [str(port) + ":" + str(ORDERER_PORT)]
        command = "orderer"
        volumes = [
            "./:/etc/hyperledger/configtx",
            "./crypto-config/ordererOrganizations/" + ordererOrg.domain +
            "/orderers/" + ordererOrg.org_name.lower() + "." +
            ordererOrg.domain + "/:/etc/hyperledger/crypto/orderer",
            "./crypto-config/peerOrganizations/" + peerOrgs[0].domain +
            "/peers/" + peerOrgs[0].peers[0].hostname +
            ":/etc/hyperledger/crypto/peerOrg1",
            "./crypto-config/peerOrganizations/" + peerOrgs[1].domain +
            "/peers/" + peerOrgs[1].peers[0].hostname +
            ":/etc/hyperledger/crypto/peerOrg2"
        ]
        container_name = ordererID
        networks = [network]
        working_dir = "/opt/gopath/src/github.com/hyperledger/fabric/orderers"
        return DockerService(ports=ports,
                             volumes=volumes,
                             environment=environment,
                             command=command,
                             image=orderer_image,
                             container_name=container_name,
                             working_dir=working_dir,
                             networks=networks)
    except Exception as e:
        logger.error(e)
        raise Exception(
            "build_orderer_service for Docker-compose.yaml failled!")
Exemplo n.º 7
0
def create_crypto_config_file(config,
                              dir,
                              cryptog_config_filename=CRYPTOCONFIG_FN):
    logger.info("Create crypto-config.yaml...")
    try:
        dic = {}
        orderer = {}
        ca = {}
        specs = []
        peers = []
        orderers = []
        orderer['Domain'] = config.orderer_org.domain
        orderer['Name'] = "orderer"  #config.orderer_org.org_name
        ca['Country'] = config.cert.country
        ca['Province'] = config.cert.province
        ca['Locality'] = config.cert.locality
        for o in config.orderer_org.orderers:
            specs.append({'Hostname': o.name})
        orderer['Specs'] = specs
        orderer['CA'] = ca
        orderers.append(orderer)
        for peer_org in config.peer_orgs:
            peer = {}
            users = {}
            template = {}
            users['Count'] = peer_org.nb_users
            template['Count'] = peer_org.nb_peers
            peer['Domain'] = peer_org.domain
            peer['Name'] = peer_org.org_name
            peer['CA'] = ca
            peer['Users'] = users
            peer['Template'] = template
            peers.append(peer)
        dic['OrdererOrgs'] = orderers
        dic['PeerOrgs'] = peers
        dump_yaml_file(data=dic, file_path=dir + "/" + cryptog_config_filename)
        logger.debug(json.dumps(dic, indent=4))
        return cryptog_config_filename
    except Exception as e:
        logger.error(e)
        raise Exception("Create crypto-config.yaml failled!")
def create_config_file(config, filename):
    logger.info("Create explorer config file...")
    try:
        dic = {}
        dic['host'] = EXPLORER_HOST
        dic['port'] = EXPLORER_PORT
        dic['channel'] = config.channel.name
        dic['keyValueStore'] = EXPLORER_LOCAL + "fabric-client-kvs"
        dic['eventWaitTime'] = "30000"
        mysql = {}
        mysql['host'] = EXPLORER_MSQL_HOST
        mysql['port'] = EXPLORER_MSQL_PORT
        mysql['database'] = EXPLORER_MSQL_DB
        mysql['username'] = EXPLORER_MSQL_LOGIN
        mysql['password'] = EXPLORER_MSQL_PWD
        dic['mysql'] = mysql
        for o in config.peer_orgs:
            org = {}
            org['name'] = "peer" + o.org_name
            org['mspid'] = o.mspid
            org['ca'] = o.ca.url
            for p in o.peers:
                peer = {}
                peer['request'] = p.request_url
                peer['events'] = p.event_url
                peer['server-hostname'] = p.hostname
                peer[
                    'tls_cacerts'] = EXPLORER_LOCAL + o.domain + "/peer/" + p.name + "/ca.crt"
                org[p.name] = peer
            admin = {}
            admin['key'] = EXPLORER_LOCAL + o.domain + "/admin/keystore"
            admin['cert'] = EXPLORER_LOCAL + o.domain + "/admin/signcerts"
            org['admin'] = admin
            dic[o.org_name] = org
        with open(filename, 'w') as f:
            json.dump(dic, f, ensure_ascii=False, indent=4)
        logger.debug(json.dumps(dic, indent=4))
    except Exception as e:
        logger.error(e)
        raise Exception("Create explorer config file failled!")
Exemplo n.º 9
0
def create_connection_file(config, filename):
    for peer_org in config.peer_orgs:
        logger.info("Create composer config file...")
        try:
            dic ={}
            dic['name']    = 'profil'+peer_org.org_name
            dic['type']    = 'hlfv1'
            dic['mspID']   = peer_org.mspid
            dic['channel'] = config.channel.name            
            dic['timeout'] = 300      
            peers=[]
            for p in peer_org.peers:
                peer = {}
                peer['requestURL']=p.request_url 
                peer['eventURL']=p.event_url 
                peer['cert']=COMPOSER_LOCAL+peer_org.domain+"/peer/"+p.name+"/ca.crt"
                peer['hostnameOverride']=p.hostname          
                peers.append(peer)
            orderers=[]  
            dic['peers']=peers              
            for o in config.orderer_org.orderers:
                orderer = {}
                orderer['url']=o.url 
                orderer['cert']=COMPOSER_LOCAL+config.orderer_org.domain+"/orderer/"+o.name+"/ca.crt"
                orderer['hostnameOverride']=o.hostname     
                orderers.append(orderer)
            dic['orderers']=orderers   
            ca = {}
            ca['url']=peer_org.ca.url 
            ca['name']="ca-"+peer_org.org_name 
            ca['cert']=COMPOSER_LOCAL+peer_org.domain+"/ca/ca.crt"
            ca['hostnameOverride']="ca."+peer_org.domain          
            dic['ca']=ca   
            with open(filename, 'w') as f:
                json.dump(dic, f, ensure_ascii=False, indent=4)
            logger.debug(json.dumps(dic, indent=4))
        except Exception as e:
            logger.error(e)
            raise Exception("Create composer config file failled!")