forked from hyperledger/cello
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docker agent for api-agent hyperledger#121 * support for node creation, start, stop, restart, deletion Signed-off-by: Dixing (Dex) Xu <dixingxu@gmail.com>
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## Pre-requisite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Flask | ||
flask-restful | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/<id>', 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) |