def _checkSwarmLeader(self, swarm): """ 查询swarm集群Leader """ logger.info("check swarm %s leader, the request swarm manager is %s" % (swarm.get("name"), swarm.get("manager"))) if swarm: try: url = Splice(netloc=swarm.get("manager")[0], port=self.port, path='/nodes').geturl data = requests.get(url, timeout=self.timeout, verify=self.verify).json() if "message" in data: raise TypeError( "The response that first get leader is error, data is {}" .format(data)) except Exception, e: logger.warn(e, exc_info=True) try: url = Splice(netloc=swarm.get("manager")[-1], port=self.port, path='/nodes').geturl data = requests.get(url, timeout=self.timeout, verify=self.verify).json() except Exception, e: logger.error(e, exc_info=True) data = None
def _checkServiceTaskNode(self, leader, serviceId): """ 查询某service(ID)的实例节点 """ url = Splice(netloc=leader, port=self.port, path='/tasks').geturl logger.info("Get service %s task, that url is %s" % (serviceId, url)) data = requests.get(url, params={ "filters": json.dumps( {'desired-state': { 'running': True }}) }).json() #data = requests.get(url).json() nodes = [ _['NodeID'] for _ in data if _['Status']['State'] == 'running' and _['ServiceID'] == serviceId ] ips = [] for node in nodes: nodeinfo = self._checkSwarmNode(leader, node) ip = nodeinfo.get('ManagerStatus', {}).get( 'Addr', '').split(':')[0] or nodeinfo['Spec'].get( 'Labels', {}).get('ipaddr') ips.append(ip) return {"ips": ips, "nodes": nodes}
def _checkSwarmHealth(self, leader): """ 根据Leader查询某swarm集群是否健康 """ state = [] mnum = 0 logger.info( "To determine whether the cluster is healthy, starting, swarm leader is %s" % leader) try: nodes = requests.get(Splice(netloc=leader, port=self.port, path='/nodes').geturl, timeout=self.timeout, verify=self.verify).json() logger.debug("check swarm health, swarm nodes length is %d" % len(nodes)) for node in nodes: if node['Spec'].get('Role') == 'manager': mnum += 1 isHealth = True if node['Status'][ 'State'] == 'ready' and node['Spec'].get( 'Availability') == 'active' and node.get( 'ManagerStatus', {}).get( 'Reachability') == 'reachable' else False if isHealth: state.append(isHealth) except Exception, e: logger.warn(e, exc_info=True) return "ERROR"
def _get_imageId(self, ImageName, tag="latest"): """ 查询某个镜像tag的imageId/digest """ ReqUrl = self._baseUrl + "/repositories/{}/tags/{}".format( ImageName, tag ) if self.version == 1 else self._baseUrl + "/{}/manifests/{}".format( ImageName, tag) logger.info("_get_imageId for url {}".format(ReqUrl)) try: if self.version == 1: r = requests.get(ReqUrl, timeout=self.timeout, verify=self.verify) else: r = requests.head( ReqUrl, timeout=self.timeout, verify=self.verify, allow_redirects=True, headers={ "Content-Type": "application/vnd.docker.distribution.manifest.v2+json" }) except Exception, e: logger.error(e, exc_info=True) return False
def __init__(self, port=2375, timeout=2, ActiveSwarm=None): self.port = port self.timeout = timeout self.verify = False self.swarm = ActiveSwarm self.leader = self._checkSwarmLeader(self.swarm) if self.swarm != {} else None logger.info("Network Api Init, ActiveSwarm is %s, the leader is %s" %(self.swarm, self.leader))
def get(self): """ 查询私有仓 """ query = request.args.get("q") ImageName = request.args.get("ImageName") ImageId = request.args.get("ImageId") tag = request.args.get("tag") if g.auth: res = {"msg": None, "data": None} if query == "url": res.update(data=g.registry.url) elif query == "status": res.update(data=g.registry.status) elif query == "version": res.update(data=g.registry.version) elif query == "all_repository": res.update(data=g.registry._list_all_repository) elif query == "all_tag": res.update(data=g.registry._list_repository_tag(ImageName)) elif query == "all_imageId_ancestry": res.update(data=g.registry._list_imageId_ancestry(ImageId)) elif query == "imageId_info": res.update(data=g.registry._get_imageId_info(ImageId)) logger.info(res) return res else: return abort(403)
def DELETE(self, name): """ 删除当前存储中的私有仓 """ res = {"msg": None, "code": 0, "success": False} logger.info("the name that will delete is %s" % name) if name in ("member", "active", "all"): res.update(msg="name reserved for the system key words", code=10005) elif self.isActive(name): res.update(msg="not allowed to delete the active cluster", code=10006) elif self.isMember(name): registry = self.getOne(name) logger.info("Will delete registry is %s" % registry) self._registries.remove(registry) if self.isMember(name): logger.info("Delete fail") res.update(success=False) else: logger.info("Delete successfully, pickle current registries") self._pickle(self._registries) res.update(success=True) else: res.update(msg="This registry does not exist", code=10007) logger.info(res) return res
def __init__(self, timeout=2, verify=False, ActiveRegistry={}): self.timeout = timeout self.verify = verify self._addr = ActiveRegistry.get("addr") self._ver = ActiveRegistry.get("version") self._auth = ActiveRegistry.get("auth") logger.info("Registry API Init, registry is {}".format(self._addr))
def GET(self, get, **kwargs): res = {"msg": None, "code": 0} checkState = kwargs.get("checkState", False) UpdateManager = kwargs.get("UpdateManager", False) logger.info("get %s request, the query params is %s" % (get, kwargs)) if not isinstance(get, (str, unicode)) or not get: res.update(msg="GET: query params type error or none", code=-1010) else: get = get.lower() if get == "all": res.update(data=self.getSwarm(checkState, UpdateManager)) elif get == "active": res.update(data=self.getActive) elif get == "leader": res.update(data=self._checkSwarmLeader(self.getActive)) elif get == "member": res.update(data=self.getMember) elif get == "method": res.update(data=self.getMethod) else: if self.isMember(get): res.update(data=self.getOne(get)) else: res.update(msg="No such swarm", code=-1011) logger.info(res) return res
def _search_all_repository(self, url, version=1, q=""): """ 搜索私有仓所有镜像 """ if url: ReqUrl = url.strip( "/") + "/v1/search" if version == 1 else url.strip( "/") + "/v2/_catalog" logger.info("_search_all_repository for url {}".format(ReqUrl)) try: Images = requests.get(ReqUrl, timeout=self.timeout, verify=self.verify, params={ "q": q }).json() except Exception, e: logger.error(e, exc_info=True) else: if version == 1: return Images["results"] else: return [{ "name": _, "description": None } for _ in Images["repositories"] if q in _]
def POST(self, swarmName, swarmIp): """ add a swarm cluster into current, check, pickle. """ res = {"msg": None, "code": 0} swarmIp = swarmIp.strip() swarmName = swarmName.strip() logger.debug( "post a swarm cluster, name is %s, ip is %s, check ip is %s" % (swarmName, swarmIp, ip_check(swarmIp))) if not swarmName or not swarmIp or not ip_check(swarmIp): res.update(msg="POST: data params error", code=-1020) elif self.isMember(swarmName): res.update(msg="POST: swarm cluster already exists", code=-1021) else: #access node ip's info, and get all remote managers url = Splice(netloc=swarmIp, port=self.port, path='/info').geturl swarm = dict(name=swarmName) logger.info( "init a swarm cluter named %s, will get swarm ip info, that url is %s" % (swarmName, url)) try: nodeinfo = requests.get(url, timeout=self.timeout, verify=self.verify).json() logger.debug("get swarm ip info, response is %s" % nodeinfo) swarm["manager"] = [ nodes["Addr"].split(":")[0] for nodes in nodeinfo["Swarm"]["RemoteManagers"] ] except Exception, e: logger.error(e, exc_info=True) res.update(msg="POST: access the node ip has exception", code=-1022) else:
def RollingUpgrade(self, serviceFlag, tag): """ 服务滚动升级 """ res = {"msg": None, "code": 0} logger.info("Update service flag(id/name) is %s, tag is %s" % (serviceFlag, tag)) #check params if not serviceFlag: logger.warn("service id/name is empty") res.update(msg="service id/name is empty", code=60000) logger.info(res) return res if not tag: res.update(msg="tag error", code=60001) logger.info(res) return res #check leader if not self.leader: res.update(msg="No active swarm", code=-1000) logger.info(res) return res data = self.GET(service=serviceFlag, core=True, core_convert=False).get("data")[0] Image = "{}:{}".format(data.get("Image").split(":")[0], tag) res.update(self.PUT(serviceFlag, image=Image)) logger.info(res) return res
def DELETE(self, name): """ 删除当前存储中的群集 """ res = {"msg": None, "code": 0, "success": False} logger.info("the name that will delete is %s" % name) if name in ("leader", "active", "all"): res.update(msg="DELETE: name reserved for the system key words", code=-1031) elif self.isActive(name): res.update(msg="DELETE: not allowed to delete the active cluster", code=-1032) elif self.isMember(name): swarm = self.getOne(name) logger.info("Will delete swarm cluster is %s" % swarm) self._swarms.remove(swarm) if self.isMember(name): logger.info("Delete fail") res.update(success=False) else: logger.info("Delete successfully, pickle current swarm") self._pickle(self._swarms) res.update(success=True) else: res.update(msg="DELETE: this swarm cluster does not exist", code=-1030) logger.info(res) return res
def before_request(): g.requestId = gen_requestId() g.sessionId = request.cookies.get("sessionId", "") g.username = request.cookies.get("username", "") g.expires = request.cookies.get("time", "") g.signin = isLogged_in('.'.join([g.username, g.expires, g.sessionId])) logger.info("Start Once Access, and this requestId is %s, isLogged_in:%s" % (g.requestId, g.signin))
def post(self): """ 创建网络 """ ip = request.form.get("ip") role = request.form.get("role", "Worker") logger.info(request.form) return g.node.POST(ip=ip, role=role)
def post(self): """ 创建网络 """ ip = request.form.get("ip") role = request.form.get("role", "Worker") logger.info(request.form) if g.auth: return g.node.POST(ip=ip, role=role) else: return abort(403)
def POST(self, name, addr, version=1, auth=None): """ 创建 """ res = {"msg": None, "code": 0} try: version = int(version) except Exception, e: logger.error(e, exc_info=True) res.update(msg="params error", code=-10002) logger.info(res) return res
def _delete_repository(self, ImageName): """ 删除一个镜像 """ ReqUrl = self._baseUrl + "/repositories/{}/".format(ImageName) logger.info("_delete_repository for url {}".format(ReqUrl)) try: delete_repo_result = requests.delete(ReqUrl, timeout=self.timeout, verify=self.verify).json() except Exception, e: logger.error(e, exc_info=True) return False
def _list_imageId_ancestry(self, ImageId): """ 列出某个镜像所有父镜像 """ ReqUrl = self._baseUrl + "/images/{}/ancestry".format(ImageId) logger.info("_list_imageId_ancestry for url {}".format(ReqUrl)) try: ImageIds = requests.get(ReqUrl, timeout=self.timeout, verify=self.verify).json() except Exception, e: logger.error(e, exc_info=True) return False
def __init__(self, port=2375, timeout=2, ActiveSwarm={}): self.port = port self.verify = False self.timeout = timeout self.swarm = ActiveSwarm self.leader = self._checkSwarmLeader( self.swarm) if self.swarm != {} else None self.commaConvert = lambda string: [ l for l in re.split(comma_Pat, string) if l ] logger.info("Service Api Init, ActiveSwarm is %s, the leader is %s" % (self.swarm, self.leader))
def add_header(response): response.headers["X-SaintIC-Request-Id"] = g.requestId logger.info({ "AccessLog": True, "status_code": response.status_code, "method": request.method, "ip": request.headers.get('X-Real-Ip', request.remote_addr), "url": request.url, "referer": request.headers.get('Referer'), "agent": request.headers.get("User-Agent"), "requestId": g.requestId }) return response
def delete(self): """ 删除镜像<标签> """ ImageName = request.form.get("ImageName") ImageTag = request.form.get("ImageTag") logger.info("api registry delete, ImageName:{}, ImageTag:{}".format( ImageName, ImageTag)) if ImageTag: return g.registry.delete_an_image_tag(ImageName=ImageName, tag=ImageTag) else: return g.registry.delete_an_image(ImageName=ImageName)
def __init__(self, timeout=2, Registry={}): self.timeout = timeout self.verify = False #Base Registry info self._url = Registry["RegistryAddr"] self._ver = Registry["RegistryVersion"] self._auth = Registry["RegistryAuthentication"] #Instantiation the registry self._baseUrl = SpliceURL.Modify(self._url, path="/v1").geturl if int( self._ver) == 1 else SpliceURL.Modify(self._url, path="/v2").geturl logger.info( "Registry API Init, registry is {}, status is {}, _baseUrl is {}". format(self._url, self.status, self._baseUrl))
def before_request(): g.auth = True g.requestId = gen_requestId() g.registries = registries g.registry = ApiRegistryManager(ActiveRegistry=g.registries.getActive) g.sysInfo = { "Version": __version__, "Author": __author__, "Email": __email__, "Doc": __doc__ } logger.info("Start Once Access, and this requestId is {}".format( g.requestId))
def DELETE(self, serviceFlag): #delete a service res = {"msg": None, "code": 0} logger.info(serviceFlag) if not self.leader: res.update(msg="No active swarm", code=-1000) logger.info(res) return res if not serviceFlag: res.update(msg="no service id or name<%s>" % serviceFlag, code=50100) logger.info(res) return res logger.info("delete service, check parameters pass") try: SwarmEngineServiceDeleteUrl = Splice(netloc=self.leader, port=self.port, path="/services/%s" % serviceFlag).geturl SwarmEngineServiceDeleteRes = requests.delete( SwarmEngineServiceDeleteUrl, timeout=self.timeout, verify=self.verify) SwarmEngineServiceDeleteCode = SwarmEngineServiceDeleteRes.status_code except Exception, e: logger.error(e, exc_info=True) res.update(success=False, code=50200, msg="delete service<%s> fail" % serviceFlag)
def getSwarm(self, checkState=False, UpdateManager=False): """ 查询存储中所有Swarm集群信息(并检查健康状态) """ logger.info( "get all swarm and check state(%s) for all swarm cluster with UpdateManager(%s), start" % (checkState, UpdateManager)) swarms = [] for swarm in self._swarms: if checkState == True: swarm.update(state=self._checkSwarmHealth( self._checkSwarmLeader(swarm))) elif "state" in swarm: swarm.pop("state") if UpdateManager == True: logger.info("Update manager in getSwarm, start") try: manager = self._checkSwarmManager( self._checkSwarmLeader(swarm)) except Exception, e: logger.error(e, exc_info=True) logger.info("Update manager in getSwarm, end, fail") else: if manager: swarm.update(manager=manager) logger.info( "Update manager in getSwarm, end, successfully, manager is %s" % manager) swarms.append(swarm)
def PUT(self, node_id, node_role, node_labels): """ 更新节点 """ res = {"msg": None, "code": 0, "success": False} if not self.leader: res.update(msg="No Active Swarm", code=-1005) logger.info(res) return res if not node_role in ("Manager", "Worker"): res.update(msg="role error", code=-1006) logger.info(res) return res if isinstance(node_labels, (str, unicode)): labels = string2dict(node_labels) elif isinstance(node_labels, dict): labels = node_labels else: res.update(msg="node_labels error", code=-1007) logger.info(res) return res res.update(success=self._UpdateNode(leader=self.leader, node_id=node_id, node_role=node_role, labels=labels)) logger.info(res) return res
def DELETE(self, ip, force): """ 节点离开集群 """ res = {"msg": None, "code": 0, "success": False} force = True if force in ("true", "True", True) else False if not self.leader: res.update(msg="No Active Swarm", code=-1008) logger.info(res) return res res.update(success=self._LeaveSwarm(node_ip=ip.strip(), force=force)) logger.info(res) return res
def before_request(): g.startTime = time.time() g.requestId = gen_requestId() g.sessionId = request.cookies.get("sessionId", "") g.username = request.cookies.get("username", "") g.expires = request.cookies.get("time", "") g.auth = isLogged_in('.'.join([ g.username, g.expires, g.sessionId ])) g.swarm = swarm g.service = ServiceManager(ActiveSwarm=g.swarm.getActive) g.node = NodeManager(ActiveSwarm=g.swarm.getActive) g.network = NetworkManager(ActiveSwarm=g.swarm.getActive) g.registry = RegistryManager(Registry=REGISTRY) g.sysInfo = {"Version": __version__, "Author": __author__, "Email": __email__, "Doc": __doc__} logger.info("Start Once Access, and this requestId is %s, auth(%s)" %(g.requestId, g.auth)) app.logger.info(app.url_map)
def add_header(response): response.headers["X-SaintIC-Request-Id"] = g.requestId response.headers["Access-Control-Allow-Origin"] = "*" logger.info({ "AccessLog": True, "status_code": response.status_code, "method": request.method, "ip": request.headers.get('X-Real-Ip', request.remote_addr), "url": request.url, "referer": request.headers.get('Referer'), "agent": request.headers.get("User-Agent"), "requestId": g.requestId, "OneTimeInterval": "%0.2fs" %float(time.time() - g.startTime) }) return response