54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
|
|
from flask import Flask, jsonify
|
|
import docker
|
|
import socket
|
|
app = Flask(__name__)
|
|
client = docker.from_env()
|
|
d=0
|
|
for elem in client.images.list():
|
|
for var in elem.tags:
|
|
if var=="app:proxy":
|
|
d=1
|
|
if d==0:
|
|
client.images.build(path="./", tag="app:proxy", network_mode = 'host')
|
|
|
|
@app.route('/', methods = ['GET'])
|
|
def getMessages():
|
|
return jsonify(
|
|
message="Hello world!!!",
|
|
username="Driss"
|
|
)
|
|
|
|
|
|
@app.route('/port', methods = ['GET'])
|
|
def getports():
|
|
sock = socket.socket()
|
|
sock.bind(('',0))
|
|
port= sock.getsockname()[1]
|
|
sock.close()
|
|
client = docker.from_env()
|
|
cont=client.containers.run(image = "app:proxy", ports={5000:port},name='proxynew'+str(port), command="python3 ./microservice.py", detach = True )
|
|
return jsonify(
|
|
message="hello",
|
|
numport= port
|
|
)
|
|
|
|
@app.route('/newContainer', methods = ['GET'])
|
|
def createContainer():
|
|
client = docker.from_env()
|
|
cont = client.containers.run(image = "app:proxy", ports={5000:50001}, name = 'proxy', command="python3 ./microservice.py", detach = True)
|
|
return "Success"
|
|
|
|
@app.route('/delContainer', methods = ['GET'])
|
|
def deleteContainer():
|
|
client = docker.from_env()
|
|
for c in client.containers.list():
|
|
c.stop()
|
|
client.containers.prune()
|
|
return "Success"
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0",port=50000)
|
|
|
|
|