diff --git a/src/agent/docker-rest-agent/README.md b/src/agent/docker-rest-agent/README.md new file mode 100644 index 000000000..69c9851c9 --- /dev/null +++ b/src/agent/docker-rest-agent/README.md @@ -0,0 +1 @@ +## Pre-requisite \ No newline at end of file diff --git a/src/agent/docker-rest-agent/requirements.txt b/src/agent/docker-rest-agent/requirements.txt new file mode 100644 index 000000000..94f0dc9a6 --- /dev/null +++ b/src/agent/docker-rest-agent/requirements.txt @@ -0,0 +1,3 @@ +Flask +flask-restful +requests \ No newline at end of file diff --git a/src/agent/docker-rest-agent/server.py b/src/agent/docker-rest-agent/server.py new file mode 100644 index 000000000..91bca6dd2 --- /dev/null +++ b/src/agent/docker-rest-agent/server.py @@ -0,0 +1,116 @@ +from flask import Flask, jsonify, request +import docker +import sys +import logging + +app = Flask(__name__) + +client = docker.from_env() +res = {'code': 'OK', 'data': {}, 'msg': ''} + +@app.route('/api/v1/networks', methods=['GET']) +def get_network(): + return jsonify({'networks': client.containers.list()}) + +@app.route('/api/v1/nodes', methods=['POST']) +def create_node(): + msp = request.form.get('msp') + tls = request.form.get('tls') + bootstrap_block = request.form.get('bootstrap_block') + peer_config_file = request.form.get('peer_config_file') + orderer_config_file = request.form.get('orderer_config_file') + img = request.form.get('img', default='yeasy/hyperledger-fabric:2.2.0') + cmd = request.form.get('cmd', default='bash /tmp/init.sh') + name_tag = request.form.get('name', 'cello-hlf-peer') + + env = { + 'HLF_NODE_MSP':msp, + 'HLF_NODE_TLS':tls, + 'HLF_NODE_BOOTSTRAP_BLOCK':bootstrap_block, + 'HLF_NODE_PEER_CONFIG':peer_config_file, + 'HLF_NODE_ORDERER_CONFIG':orderer_config_file, + } + + + try: + # same as `docker run -dit yeasy/hyperledge-fabric:2.2.0 -e VARIABLES`` + container = client.containers.run(img, cmd, detach=True, tty=True, stdin_open=True, name=name_tag, environment=env) + except: + res['code'] = 'Fail' + res['data'] = sys.exc_info()[0] + res['msg'] = 'failed' + logging.debug(res) + raise + res['data']['status'] = 'created' + res['data']['id'] = container.id + res['data']['public-grpc'] = '127.0.0.1:7050' + res['data']['public-raft'] = '127.0.0.1:7052' + return res + +@app.route('/api/v1/nodes/', methods=['GET', 'POST']) +def operate_node(id): + + if request.method == 'POST': + act = request.form.get('action', 'start') + else: + act = request.args.get('action') + container = client.containers.get(id) + + if act == 'start': + try: + container.start() + except: + res['code'] = 'Fail' + res['data'] = sys.exc_info()[0] + res['msg'] = 'start failed' + logging.debug(res) + raise + res['data']['status'] = 'started' + elif act == 'restart': + try: + container.restart() + except: + res['code'] = 'Fail' + res['data'] = sys.exc_info()[0] + res['msg'] = 'restart failed' + logging.debug(res) + raise + res['data']['status'] = 'restarted' + elif act == 'stop': + try: + container.stop() + except: + res['code'] = 'Fail' + res['data'] = sys.exc_info()[0] + res['msg'] = 'stop failed' + logging.debug(res) + raise + res['data']['status'] = 'stopped' + elif act == 'delete': + try: + container.remove() + except: + res['code'] = 'Fail' + res['data'] = sys.exc_info()[0] + res['msg'] = 'delete failed' + logging.debug(res) + raise + res['data']['status'] = 'deleted' + elif act == 'query': + try: + res['data']['status'] = container.status() + except: + res['code'] = 'Fail' + res['data'] = sys.exc_info()[0] + res['msg'] = 'query failed' + logging.debug(res) + raise + else: + res['msg'] = 'undefined action' + + + return res + + +if __name__ == '__main__': + app.run(port=5001, debug=True) \ No newline at end of file