Created
June 19, 2016 18:16
-
-
Save sawantuday/4058597d4c2fe93360efe675caf8f8ec to your computer and use it in GitHub Desktop.
Docker APIs implemented in python. Very bad code, trust me :(
This file contains hidden or 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
from flask import Flask | |
from flask import Response | |
from flask import request | |
import requests | |
import json | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
return "Hello World!" | |
## containers section | |
@app.route("/container/create", methods=["POST"]) | |
def container_create(): | |
name=request.form['name'] | |
image=request.form['image'] | |
cmd=request.form['cmd'] | |
containerId=None | |
headers={'Content-Type':'application/json'} | |
data={'name':name,'Image':image,'Cmd':cmd,'AttachStdout':True,'AttachStderr':True,"OpenStdin":True} | |
params = {'name':name} | |
url='http://127.0.0.1:2375/containers/create' | |
r=requests.post(url,data=json.dumps(data),headers=headers,params=params) | |
print 'first call %d %s' % (r.status_code, r.text) | |
if r.status_code == 404: | |
## image not found fetch image and then create container | |
params={'fromImage':image} | |
url='http://127.0.0.1:2375/images/create' | |
r2 = requests.post(url, params=params, headers=headers) | |
print 'Pulling image status code %d ' % r2.status_code | |
if r2.status_code != 200: | |
return Response( | |
r.text + 'first if', | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
## here we have image. Create a container out of it | |
data={'name':name,'Image':image,'Cmd':cmd,'AttachStdout':True,'AttachStderr':True,"OpenStdin":True} | |
url='http://127.0.0.1:2375/containers/create' | |
r3=requests.post(url,data=json.dumps(data),headers=headers,params=params) | |
print 'second call %d %s' % (r3.status_code, r3.text) | |
if r3.status_code != 201: | |
return Response( | |
r.text + 'second if', | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
data = r3.json() | |
print "json is %s" % json.dumps(data) | |
containerId = data['Id'] | |
elif r.status_code == 201: | |
data = r.json() | |
print "json is %s" % json.dumps(data) | |
containerId = data['Id'] | |
else: | |
return Response( | |
r.text + 'third if', | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
## here we have created a container. Now it need to be started | |
if containerId is None: | |
return Response( | |
'some random error', | |
status='500', | |
content_type=r.headers["content-type"], | |
) | |
else: | |
## start container | |
url='http://127.0.0.1:2375/containers/%s/start' % containerId | |
r4=requests.post(url) | |
return Response( | |
r4.text + 'container ID ' + containerId, | |
status=r4.status_code, | |
content_type='application/json', | |
) | |
@app.route("/container/start", methods=['POST']) | |
def container_start(): | |
containerId = request.form['id'] | |
# headers={'Content-Type':'application/json'} | |
params={'t':5} | |
url='http://127.0.0.1:2375/containers/%s/start' % containerId | |
r=requests.post(url, params=params) | |
return Response( | |
r.text, | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
@app.route('/container/stop', methods=['POST']) | |
def container_stop(): | |
containerId = request.form['id'] | |
# headers={'Content-Type':'application/json'} | |
params={'t':5} | |
url='http://127.0.0.1:2375/containers/%s/stop' % containerId | |
r=requests.post(url, params=params) | |
return Response( | |
r.text, | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
@app.route('/container/restart', methods=['POST']) | |
def container_restart(): | |
containerId = request.form['id'] | |
# headers={'Content-Type':'application/json'} | |
params={'t':5} | |
url='http://127.0.0.1:2375/containers/%s/restart' % containerId | |
r=requests.post(url, params=params) | |
return Response( | |
r.text, | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
@app.route('/container/info', methods=['POST']) | |
def container_info(): | |
containerId = request.form['id'] | |
url='http://127.0.0.1:2375/containers/%s/json' % containerId | |
r=requests.get(url) | |
return Response( | |
r.text, | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
@app.route('/container/logs', methods=['POST']) | |
def container_logs(): | |
containerId = request.form['id'] | |
#since=request.form['since'] | |
#limit=request.form['limit'] | |
url='http://127.0.0.1:2375/containers/%s/logs?stderr=1&stdout=1×tamps=1&tail=100' % containerId | |
r=requests.get(url) | |
return Response( | |
r.text, | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
## image section | |
@app.route('/image/create', methods=['POST']) | |
def image_create(): | |
fromImage=request.form['fromImage'] | |
headers={'Content-Type':'application/json'} | |
params={'fromImage':fromImage} | |
url='http://127.0.0.1:2375/images/create' | |
r=requests.post(url, params=params, headers=headers) | |
return Response( | |
r.text, | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
@app.route('/image/info', methods=['POST']) | |
def image_info(): | |
imageName=request.form['imageName'] | |
url='http://127.0.0.1:2375/images/%s/json' % imageName | |
r=requests.get(url) | |
return Response( | |
r.text, | |
status=r.status_code, | |
content_type=r.headers["content-type"], | |
) | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment