def do_set_working_mode(): working_mode = request.form['workingmode'] returned_mode = ConfigRepo.set_working_mode(working_mode) if working_mode == 'Create': consortium_name = request.form['consortiumname'] consortium_addr = request.form['consortiumaddr'] msg, node_list = app.component_manager.add_consortium( consortium_name, consortium_addr) if msg: return Response(msg, status=500, mimetype='text/plain') elif working_mode == 'Attach': consortium_name = request.form['consortiumname'] consortium_addr = request.form['consortiumaddr'] orderer_name = request.form['orderername'] orderer_addr = request.form['ordereraddr'] orderer_port = request.form['ordererport'] orderer_cert = request.form['orderercert'] msg, node_list = app.component_manager.attach_orderer( consortium_name, consortium_addr, orderer_name, orderer_addr, orderer_port, orderer_cert) if msg: return Response(msg, status=500, mimetype='text/plain') return Response(returned_mode, status=200, mimetype='text/plain')
def _match_docker_list_(component_dict): component_dict['deployed'] = os.path.exists(ConfigRepo.TARGET_REPO) # Check status error_code, str_out, str_err = ScriptManager._execute_( ['docker', 'ps']) if error_code != 0: return # reset working_mode, _ = ConfigRepo.load_working_conf() for item in component_dict['nodes']: node_type = item['type'] if working_mode == 'AttachOrganizations' and node_type in ( 'consortium', 'orderer'): item['status'] = '------' else: item['status'] = 'inactive' # match lines = str_out.decode().splitlines() offset = lines[0].index('NAMES') for elem in lines[1:]: name = elem[offset:] for item in component_dict['nodes']: if item['addr'] == name or item['owner'] == name: if working_mode == 'AttachOrganizations' and item[ 'type'] in ('consortium', 'orderer'): item['status'] = '------' else: item['status'] = 'active' break
def _copy_private_keys_(cls, component_dict): working_mode, _ = ConfigRepo.load_working_conf() consortium_node, orderer_list, org_list, peer_dict = cls._get_lists_of_nodes_( component_dict) if working_mode == 'CreateNetwork': consortium_addr = read_and_strip(consortium_node['addr']) for node in orderer_list: orderer_addr = node['addr'] target_dir = os.getcwd( ) + os.sep + ConfigRepo.ORDERER_TARGET_REPO + os.sep + consortium_addr + os.sep + 'orderers' + os.sep + orderer_addr for filename in glob(target_dir + os.sep + 'tls' + os.sep + 'keystore/*'): copyfile( filename, target_dir + os.sep + 'tls' + os.sep + 'server.key') for org_node in org_list: org_addr = read_and_strip(org_node['addr']) for node in peer_dict[org_addr]: peer_addr = node['addr'] target_dir = os.getcwd( ) + os.sep + ConfigRepo.PEER_TARGET_REPO + os.sep + org_addr + os.sep + 'peers' + os.sep + peer_addr for filename in glob(target_dir + os.sep + 'tls' + os.sep + 'keystore/*'): copyfile( filename, target_dir + os.sep + 'tls' + os.sep + 'server.key')
def deploy_activate(cls, component_manager): component_dict = component_manager.get_clear_node_list() working_mode, _ = ConfigRepo.load_working_conf() # ---- Don't deploy is one server is up, just activate ---- for node in component_dict['nodes']: if node['status'] == 'active': return "Deployment not allowed if one server is active", None, None if os.path.exists(os.getcwd() + os.sep + ConfigRepo.TARGET_REPO): return "Target directory " + os.getcwd( ) + os.sep + ConfigRepo.TARGET_REPO + " already exists\n remove directory before deploying" # ---- Validation ---- msg = cls._validate_deploy_(component_dict, working_mode) if msg: return msg, None, None # ---- Fabric CA Server Configuration (yaml) ---- cls._generate_fabric_ca_scripts_(component_dict) # ---- Configtx Script Generation ---- cls._generate_configtx_scripts_(component_dict) # ---- Docker Compose Script Generation ---- cls._generate_docker_compose_scripts_(component_dict, working_mode) # ---- Create the tree structure of organizations --- cls._create_organization_tree_structure_(component_dict, working_mode) # ----------------------------------------------- # Build execute list # ----------------------------------------------- return cls._generate_deployment_script_(component_dict, working_mode)
def do_get_working_mode(): working_mode, working_dir = ConfigRepo.load_working_conf() if working_dir: ConfigRepo.set_network(working_dir) return Response(working_mode, status=200, mimetype='text/plain')
except GetoptError: print('usage: python app.py --config <config file>') Install.main() sys.exit(-1) for opt, arg in opts: if opt in ('-c, --config'): config_file = arg if not config_file: print('usage: python app.py --config <config file>') Install.main() sys.exit(-1) app.config.from_json(config_file) # Change base directory to the data directory found in configuration. # All paths will be relative to that ConfigRepo.set_directories(app.config) os.chdir(ConfigRepo.DATA_REPO) # -------------------------------- # Logger Settings # -------------------------------- root = logging.getLogger() root.addHandler(default_handler) log = logging.getLogger('werkzeug') os.makedirs(app.config['LOG_PATH'], exist_ok=True) rotating_handler = RotatingFileHandler(app.config['LOG_PATH'] + os.sep + 'SocialFabric.log', maxBytes=app.config['LOG_MAX_SIZE'], backupCount=app.config['LOG_MAX_FILES']) root.addHandler(rotating_handler) log.addHandler(rotating_handler)
def _create_config_yaml_(cls, component_dict): net_template_processor = NetworkTemplateProcessor() working_mode, _ = ConfigRepo.load_working_conf() consortium_node, orderer_list, org_list, peer_dict = cls._get_lists_of_nodes_( component_dict) local_addr = 'localhost' # FIX_ME if working_mode == 'CreateNetwork': # Consortium bcc_consortium_addr = read_and_strip(consortium_node['addr']) bcc_consortium_port = read_and_strip(consortium_node['ports']) bcc_ca_addr = read_and_strip(consortium_node['owner']) bcc_ca_cert = local_addr + '-' + bcc_consortium_port + '-' + bcc_ca_addr.replace( '.', '-') + '.pem' target_dir = ConfigRepo.ORDERER_TARGET_REPO + os.sep + bcc_consortium_addr + os.sep + 'msp' with open(target_dir + os.sep + 'config.yaml', 'w') as f: f.write( net_template_processor.process( 'config.yaml', BCC_CA_PEM_CERT=bcc_ca_cert)) # Consortium Users user_list = UserManager.find_all_users(bcc_consortium_addr) for user in user_list: target_dir = ConfigRepo.ORDERER_TARGET_REPO + os.sep + bcc_consortium_addr + os.sep + 'users' + os.sep + user + os.sep + 'msp' user_dict = UserManager.get_user_dict(bcc_consortium_addr, user) if user_dict['admin']: with open(target_dir + os.sep + 'config.yaml', 'w') as f: f.write( net_template_processor.process( 'config.yaml', BCC_CA_PEM_CERT=bcc_ca_cert)) # Orderers for node in orderer_list: bcc_orderer_addr = read_and_strip(node['addr']) target_dir = ConfigRepo.ORDERER_TARGET_REPO + os.sep + bcc_consortium_addr + os.sep + 'orderers' + os.sep + bcc_orderer_addr + os.sep + 'msp' with open(target_dir + os.sep + 'config.yaml', 'w') as f: f.write( net_template_processor.process( 'config.yaml', BCC_CA_PEM_CERT=bcc_ca_cert)) # Organizations for org_node in org_list: bcc_org_addr = read_and_strip(org_node['addr']) bcc_org_port = read_and_strip(org_node['ports']) bcc_ca_addr = read_and_strip(org_node['owner']) bcc_ca_cert = local_addr + '-' + bcc_org_port + '-' + bcc_ca_addr.replace( '.', '-') + '.pem' target_dir = ConfigRepo.PEER_TARGET_REPO + os.sep + bcc_org_addr + os.sep + 'msp' with open(target_dir + os.sep + 'config.yaml', 'w') as f: f.write( net_template_processor.process( 'config.yaml', BCC_CA_PEM_CERT=bcc_ca_cert)) # Organization Users user_list = UserManager.find_all_users(bcc_org_addr) for user in user_list: target_dir = ConfigRepo.PEER_TARGET_REPO + os.sep + bcc_org_addr + os.sep + 'users' + os.sep + user + os.sep + 'msp' user_dict = UserManager.get_user_dict(bcc_org_addr, user) if user_dict['admin']: with open(target_dir + os.sep + 'config.yaml', 'w') as f: f.write( net_template_processor.process( 'config.yaml', BCC_CA_PEM_CERT=bcc_ca_cert)) # Organization Peers for node in peer_dict[bcc_org_addr]: bcc_peer_addr = read_and_strip(node['addr']) target_dir = ConfigRepo.PEER_TARGET_REPO + os.sep + bcc_org_addr + os.sep + 'peers' + os.sep + bcc_peer_addr + os.sep + 'msp' with open(target_dir + os.sep + 'config.yaml', 'w') as f: f.write( net_template_processor.process( 'config.yaml', BCC_CA_PEM_CERT=bcc_ca_cert))
def reactivate(cls, component_manager): component_dict = component_manager.get_clear_node_list() working_mode, _ = ConfigRepo.load_working_conf() script_manager = ScriptManager() bcc_fabric_ca_list = cls._get_list_of_fabric_ca_(component_dict) bcc_consortium_node, bcc_orderer_list, bcc_org_list, bcc_peer_dict = cls._get_lists_of_nodes_( component_dict) # ---- Start Fabric CA Services ---- for node in bcc_fabric_ca_list: if node['port']: ca_addr = read_and_strip(node['addr']) script_manager.add( [ 'docker-compose', '-f', ConfigRepo.DOCKER_REPO + os.sep + 'docker-compose.yaml', 'up', '-d', ca_addr ], directory=ConfigRepo.NETWORK_NAME, environment={ 'IMAGE_TAG': cls.IMAGE_TAG, 'COMPOSE_PROJECT_NAME': cls.COMPOSE_PROJECT_NAME }) if working_mode == 'CreateNetwork': # ---- Start Orderer and Nodes Services ---- for node in bcc_orderer_list: name = node['addr'] script_manager.add( [ 'docker-compose', '-f', ConfigRepo.DOCKER_REPO + os.sep + 'docker-compose.yaml', 'up', '-d', name ], directory=ConfigRepo.NETWORK_NAME, environment={ 'IMAGE_TAG': cls.IMAGE_TAG, 'COMPOSE_PROJECT_NAME': cls.COMPOSE_PROJECT_NAME }) # --- start components --- for org_node in bcc_org_list: for node in bcc_peer_dict[read_and_strip(org_node['addr'])]: # FIX_ME CouchDB should be a different node peer_name = node['addr'] couchdb_name = 'couchdb.' + peer_name cli_name = 'cli.' + peer_name script_manager.add( [ 'docker-compose', '-f', ConfigRepo.DOCKER_REPO + os.sep + 'docker-compose.yaml', 'up', '-d', couchdb_name, peer_name, cli_name ], directory=ConfigRepo.NETWORK_NAME, environment={ 'IMAGE_TAG': cls.IMAGE_TAG, 'COMPOSE_PROJECT_NAME': cls.COMPOSE_PROJECT_NAME }) return None, component_dict, script_manager.serialize()