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 getOne(self, name): """ 查询某name的swarm集群信息 """ if self.isMember(name): return (_ for _ in self._swarms if _.get("name") == name).next() else: logger.warn("get one swarm named %s, but no data" % name)
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 _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 _unpickleActive(self): """ 反序列化信息取出活跃仓库 """ try: with open(self._dir1, "r") as f: data = json.load(f) except Exception, e: logger.warn(e, exc_info=True) res = {}
def _unpickle(self): """ 反序列化信息取出所有数据 """ try: with open(self._dir0, "r") as f: data = json.load(f) except Exception, e: logger.warn(e, exc_info=True) res = []
def getOne(self, name): """ 查询某name的仓库信息 """ if self.isMember(name): return (_ for _ in self._registries if _.get("name") == name).next() else: logger.warn( "no such registry named {}, return an empty dict".format(name)) return {}
def _unpickleActive(self): """ 反序列化信息取出活跃集群 """ if self._ssm == "local": try: with open(self._ssActive, "r") as f: data = json.load(f) except Exception, e: logger.warn(e, exc_info=True) res = {} else: res = data or {}
def _unpickle(self): """ 反序列化信息取出所有数据 """ if self._ssm == "local": try: with open(self._ssSwarms, "r") as f: data = json.load(f) except Exception, e: logger.warn(e, exc_info=True) res = [] else: res = data or []
def _checkSwarmToken(self, leader): """ 根据Leader查询集群令牌 """ try: swarm = requests.get(Splice(netloc=leader, port=self.port, path='/swarm').geturl, timeout=self.timeout, verify=self.verify).json() token = swarm.get('JoinTokens') except Exception, e: logger.warn(e, exc_info=True)
def UploadImage(): editorType = request.args.get("editorType", "wangEditor") logger.debug(request.files) f = request.files.get("WriteBlogImage") or request.files.get( "editormd-image-file") if f and allowed_file(f.filename): filename = secure_filename(gen_filename() + "." + f.filename.split('.')[-1]) #随机命名 filedir = os.path.join(upload_page.root_path, BLOG_UPLOAD_FOLDER) logger.info("get allowed file %s, its name is %s, save in %s" % (f, filename, filedir)) if not os.path.exists(filedir): os.makedirs(filedir) f.save(os.path.join(filedir, filename)) if PLUGINS['UpYunStorage']['enable']: imgUrl = "/interest.blog/blog/" + filename upres = UploadImage2Upyun(os.path.join(filedir, filename), imgUrl) imgUrl = PLUGINS['UpYunStorage']['dn'].strip("/") + imgUrl logger.info( "Blog to Upyun file saved, its url is %s, result is %s" % (imgUrl, upres)) else: imgUrl = request.url_root + BLOG_IMAGE_UPLOAD_DIR + filename logger.info("Blog to local file saved in %s, its url is %s" % (filedir, imgUrl)) if editorType == "wangEditor": res = Response(imgUrl) res.headers["ContentType"] = "text/html" else: res = jsonify(url=imgUrl, message=None, success=1) res.headers["ContentType"] = "application/json" res.headers["Charset"] = "utf-8" logger.debug(res) return res else: result = r"error|未成功获取文件,上传失败" logger.warn(result) if editorType == "wangEditor": res = Response(result) res.headers["ContentType"] = "text/html" else: res = jsonify(message=result, success=0) res.headers["ContentType"] = "application/json" res.headers["Charset"] = "utf-8" logger.debug(res) return res
def POST(self, image, **params): """ Create a service in docker swarm mode cluster with api. 1. get, convert, check params, 2. post data to swarm api. """ res = {"msg": None, "code": 0} logger.info("Create service, the required image is %s" % image) #check leader if not self.leader: res.update(msg="No active swarm", code=-1000) logger.info(res) return res ###get params, in version 0.1.10, optional only is "name, env, mount, publish, replicas", required "image". logger.debug(params) try: name = params.get("name") #str, needn't convert env = self.commaConvert( params.get("env")) if params.get("env") else [] #source data likes "key=value, key2=value2", convert to list or tuple, \n, #`env`'ask likes this, [key=value, key2=value2] or (key3=value3, key4=value4). mount = self.commaConvert( params.get("mount")) if params.get("mount") else [] #source data likes "src:dst:true:mode, src:dst:true|false:mode", example "/data/swarmopsapi-logs:/SwarmOpsApi/logs:true:bind, /data/cmlogs:/data/cmdata/logs:false:bind", convert it, \n, #`mount`'ask likes this, [src:dst:true|false(default false, rw):mode, ]. publish = self.commaConvert( params.get("publish")) if params.get("publish") else [] #source data likes "src:desc:protocol, src2:desc2", such as `env`, convert it, \n #list or tuple, `publish`'ask likes this, [src:desc, src2:desc2] or (). except Exception, e: logger.warn(e, exc_info=True) res.update(msg="parameters error", code=40000) logger.info(res) return res
#product environment start application with `tornado IOLoop` and `gevent server` from main import app, __version__ from utils.public import logger from config import GLOBAL, PRODUCT Host = GLOBAL.get('Host') Port = GLOBAL.get('Port') ProcessName = PRODUCT.get('ProcessName') ProductType = PRODUCT.get('ProductType') try: import setproctitle except ImportError as e: logger.warn( "%s, try to pip install setproctitle, otherwise, you can't use the process to customize the function" % e) else: setproctitle.setproctitle(ProcessName) msg = "The process is %s" % ProcessName print(msg) logger.info(msg) try: msg = "%s has been launched, %s:%d, with %s." % (ProcessName, Host, Port, ProductType) print(msg) logger.info(msg) if ProductType == 'gevent': from gevent.wsgi import WSGIServer http_server = WSGIServer((Host, Port), app)
def PUT(self, serviceFlag, **params): """update a service in docker swarm mode cluster with api. 1. get, convert, check params, 2. post data to swarm api. """ res = {"msg": None, "code": 0} logger.info("Update service flag(id/name) is %s, other params is %s" % (serviceFlag, params)) #check params if not serviceFlag: logger.warn("service id/name is empty") res.update(msg="service id/name is empty", code=50000) logger.info(res) return res #check leader if not self.leader: res.update(msg="No active swarm", code=-1000) logger.info(res) return res #check UpdateServiceType USType = params.get("UpdateServiceType", "api") if USType not in ("ui", "api"): res.update("UpdateServiceType error", code=50001) logger.info(res) return res serviceSourceData = self.GET(service=serviceFlag, core=True).get("data") logger.debug(serviceSourceData) if isinstance(serviceSourceData, (list, tuple)) and len(serviceSourceData) > 0: serviceSourceData = serviceSourceData[0] serviceFlag2ID = serviceSourceData.get("ID") #default service data for api(UI filled). defaultName = serviceSourceData.get("Name") defaultEnv = serviceSourceData.get("Env") defaultMount = serviceSourceData.get("Mounts") defaultPublish = serviceSourceData.get("NetPorts") defaultImage = serviceSourceData.get("Image") defaultReplicas = serviceSourceData.get("Replicas") defaultVersion = serviceSourceData.get("Version") try: if USType == "ui": image = params.get("image") name = params.get("name") env = self.commaConvert( params.get("env")) if params.get("env") else None mount = self.commaConvert( params.get("mount")) if params.get("mount") else None publish = self.commaConvert(params.get( "publish")) if params.get("publish") else None replicas = int(params.get("replicas")) delay = int(params.get("delay", 10)) parallelism = int(params.get("parallelism", 1)) else: image = params.get("image") or defaultImage name = params.get("name") or defaultName env = self.commaConvert( params.get("env")) if params.get("env") else defaultEnv mount = self.commaConvert(params.get( "mount")) if params.get("mount") else defaultMount publish = self.commaConvert( params.get("publish")) if params.get( "publish") else defaultPublish replicas = int(params.get("replicas")) if params.get( "replicas") else defaultReplicas delay = int( params.get("delay")) if params.get("delay") else 10 parallelism = int(params.get("parallelism")) if params.get( "parallelism") else 1 except Exception, e: logger.warn(e, exc_info=True) res.update(msg="parameters error", code=50001) logger.info(res) return res
#source data likes "src:dst:true:mode, src:dst:true|false:mode", example "/data/swarmopsapi-logs:/SwarmOpsApi/logs:true:bind, /data/cmlogs:/data/cmdata/logs:false:bind", convert it, \n, #`mount`'ask likes this, [src:dst:true|false(default false, rw):mode, ]. publish = self.commaConvert( params.get("publish")) if params.get("publish") else [] #source data likes "src:desc:protocol, src2:desc2", such as `env`, convert it, \n #list or tuple, `publish`'ask likes this, [src:desc, src2:desc2] or (). except Exception, e: logger.warn(e, exc_info=True) res.update(msg="parameters error", code=40000) logger.info(res) return res try: replicas = int(params.get("replicas", 1)) #int, number of instances, detault is 1. except ValueError, e: logger.warn(e) res.update(msg="replicas not an integer", code=40001) logger.info(res) return res #check params if not image: logger.warn("image is empty") res.update(msg="image is empty", code=40002) logger.info(res) return res elif not isinstance(env, (str, unicode)) and not isinstance( env, (list, tuple)): logger.warn("env not a list or tuple, type is %s" % type(env)) res.update(msg="env not a list or tuple", code=40003) logger.info(res)