def HandleSwarm(request): response = '{"master":[{"ip": "153.92.34.6", "port":"2375", "name":"master1"}],"node":[{"ip": "153.92.35.46", "port":"2375", "name":"node1"}]}' print 'here' print 'Raw Data: "%s"' % request.body jsonrequest = json.loads(request.body) print jsonrequest token = '' swarmmaster=0 #check master connectivity for master in jsonrequest["master"]: print master["ip"] sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex((master["ip"], int(master["port"]))) if result == 0: print "Port is open" else: print "Port is not open" return JsonResponse({'status': 'failed', 'content': 'Please check the IP and port('+master["ip"] + ":" + master["port"]+')'}) #check nodes connectivity for node in jsonrequest["node"]: print node["ip"] sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex((node["ip"], int(node["port"]))) if result == 0: print "Port is open" else: print "Port is not open" return JsonResponse({'status': 'failed', 'content': 'Please check the IP and port('+node["ip"] + ":" + node["port"]+')'}) #create master node for master in jsonrequest["master"]: ip = master["ip"] port = master["port"] name = master["name"] url = 'http://'+master["ip"] + ':' + master["port"] resp = generalfunctions.funcPullImage(url, 'swarm', '1.1.3') if resp["status"] == 'success': data={ "AttachStdin": False, "AttachStdout": True, "AttachStderr": True, "Tty": False, "OpenStdin": False, "StdinOnce": False, "Cmd": ["create"], "Image": "swarm:1.1.3" } masterContainer = generalfunctions.funcCreateContainer(url, name, data) if masterContainer["status"] == 'success': print 'container created successfully' masterContainerId = masterContainer["id"] print 'master id:' + masterContainerId startCont = generalfunctions.funcStartContainer(url, masterContainerId) if startCont["status"] == 'success': print 'container started successfully' attachCont = generalfunctions.funcAttachContainer(url, masterContainerId) print attachCont["content"] token = attachCont["content"][8:] print token #insert the master into database cursor = connections['default'].cursor() sql="INSERT into swarm(name, master, master_port) VALUES('"+name+"','"+ip+"','"+port+"')" cursor.execute(sql) swarmmaster=cursor.lastrowid #set master to Y in nodes table sql="UPDATE nodes set ismaster='Y', swarmmaster=0, swarmcontainerid='"+masterContainerId+"',swarmnodename='"+name+"' where ip='"+ip+"' and port='"+port+"'" cursor.execute(sql) else: print 'starting the container failed' else: print 'creating the container failed' else: print 'pulling the image failed' #create slave nodes for node in jsonrequest["node"]: ip = node["ip"] port = node["port"] name = node["name"] url = 'http://'+ ip + ':' + port resp = generalfunctions.funcPullImage(url, 'swarm', '1.1.3') if resp["status"] == 'success': data={ "AttachStdin": False, "AttachStdout": True, "AttachStderr": True, "Tty": False, "OpenStdin": False, "StdinOnce": False, "Cmd": ["join", "--addr="+ip+":"+port, "token://"+token], "Image": "swarm:1.1.3" } nodeContainer = generalfunctions.funcCreateContainer(url, name, data) if nodeContainer["status"] == 'success': nodeContainerId = nodeContainer["id"] startCont = generalfunctions.funcStartContainer(url, nodeContainerId) if startCont["status"] == 'success': print "starting the node success" + ip #set master to Y in nodes table sql="UPDATE nodes set ismaster='N',swarmmaster="+str(swarmmaster)+",swarmnodename='"+name+"',swarmcontainerid='"+nodeContainerId+"' where ip='"+ip+"' and port='"+port+"'" cursor.execute(sql) else: print 'starting the container failed' + ip return JsonResponse({"status": "failed"}) else: print 'creating the container failed' + ip return JsonResponse({"status": "failed"}) else: print 'pulling the image failed' + ip return JsonResponse({"status": "failed"}) #create swarm master manager for master in jsonrequest["master"]: ip = master["ip"] port = master["port"] name = master["name"] + 'manage' url = 'http://'+master["ip"] + ':' + master["port"] resp = generalfunctions.funcPullImage(url, 'swarm', '1.1.3') if resp["status"] == 'success': data={ "PortBindings": { "2375/tcp": [{ "HostPort": "5001" }] }, "AttachStdin": False, "AttachStdout": True, "AttachStderr": True, "Tty": False, "OpenStdin": False, "StdinOnce": False, "Cmd": ["manage", "token://"+token], "Image": "swarm:1.1.3" } masterManagerContainer = generalfunctions.funcCreateContainer(url, name, data) if masterManagerContainer["status"] == 'success': masterManagerContainerId = masterManagerContainer["id"] startCont = generalfunctions.funcStartContainer(url, masterManagerContainerId) if startCont["status"] == 'success': print 'master manager started' sql="INSERT into nodes(ip, port, name, ismaster, swarmmaster, swarmnodename, swarmcontainerid) values('"+ip+"','5001', '"+name+"', 'N', 0, '"+name+"', '"+masterManagerContainerId+"')" cursor.execute(sql) else: print 'starting the container failed' return JsonResponse({"status": "failed"}) else: print 'creating the container failed' return JsonResponse({"status": "failed"}) else: print 'pulling the image failed' + ip return JsonResponse({"status": "failed"}) return JsonResponse({"status": "success", "content": "Swarm created successfully"})
def AddSwarmNode(request): swarmid = request.GET.get('id') ip = request.GET.get('ip') port = request.GET.get('port') name = request.GET.get('name') cursor = connections['default'].cursor() sql = "SELECT * FROM swarm where Id=" + swarmid print sql cursor.execute(sql) results = namedtuplefetchall(cursor) for row in results: id = row.Id masterip = row.master masterport = row.master_port name = row.name token = '' url = 'http://' + masterip + ':' + masterport print sql sql = "SELECT * FROM nodes where ip='" + str(masterip)+"' and port='"+str(masterport)+"'" print sql cursor.execute(sql) masterResults = namedtuplefetchall(cursor) for rowmaster in masterResults: containerid = rowmaster.swarmcontainerid attachCont = generalfunctions.funcAttachContainer(url, containerid) print attachCont["content"] token = attachCont["content"][8:] print token url = 'http://' + ip + ':' + port resp = generalfunctions.funcPullImage(url, 'swarm', '1.1.3') if resp["status"] == 'success': data = { "AttachStdin": False, "AttachStdout": True, "AttachStderr": True, "Tty": False, "OpenStdin": False, "StdinOnce": False, "Cmd": ["join", "--addr=" + ip + ":" + port, "token://" + token], "Image": "swarm:1.1.3" } nodeContainer = generalfunctions.funcCreateContainer(url, name, data) if nodeContainer["status"] == 'success': nodeContainerId = nodeContainer["id"] startCont = generalfunctions.funcStartContainer(url, nodeContainerId) if startCont["status"] == 'success': print "starting the node success" + ip # set master to Y in nodes table sql = "UPDATE nodes set ismaster='N',swarmmaster=" + str(id) + ",swarmnodename='" + name + "',swarmcontainerid='" + nodeContainerId + "' where ip='" + ip + "' and port='" + port + "'" cursor.execute(sql) else: print 'starting the container failed' + ip return JsonResponse({"status": "failed"}) else: print 'creating the container failed' + ip return JsonResponse({"status": "failed"}) else: print 'pulling the image failed' + ip return JsonResponse({"status": "failed"}) return JsonResponse({"status": "success", "content": "Node added to Swarm successfully"})
def AttachContainer(request): id = request.GET.get('id') docker_server_url = request.session['currentip'] r = generalfunctions.funcAttachContainer(docker_server_url, id) return JsonResponse(r)