Skip to content

Commit

Permalink
add docker rest api agent
Browse files Browse the repository at this point in the history
docker agent for api-agent #121
* support for node creation, start, stop, restart, deletion

Signed-off-by: Dixing (Dex) Xu <dixingxu@gmail.com>
  • Loading branch information
dexhunter committed Sep 12, 2020
1 parent ae053b6 commit 0d82c0b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/agent/docker-rest-agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Pre-requisite
3 changes: 3 additions & 0 deletions src/agent/docker-rest-agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask
flask-restful
requests
116 changes: 116 additions & 0 deletions src/agent/docker-rest-agent/server.py
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)

0 comments on commit 0d82c0b

Please sign in to comment.