def truncatlogs(): try: dockerUtil = DockerUtil() message = dockerUtil.truncatDockerLogFile(request.values.get("id")) except Exception, e: message = "error truncat logs, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def uptime(): try: hostUtil = HostUtil() message = hostUtil.get_uptime_stat() except Exception, e: message = "error get uptime, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def inspect(): try: dockerUtil = DockerUtil() containerInfo = dockerUtil.getDockerInspect(request.args.get("id")) except Exception, e: message = "error get container info, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def stats(): try: dockerUtil = DockerUtil() stats = dockerUtil.getDockerStats(request.values.get("ids")) except Exception, e: message = "error stats container, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def getUname(): try: hostUtil = HostUtil() message = hostUtil.uname() except Exception, e: message = "error get uname, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def dockerVolumnRmDangling(): try: dockerUtil = DockerUtil() message = dockerUtil.rmDockerDanglingVolumn() except Exception, e: message = "error rm dangling volumn, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def dockerInfo(): try: dockerUtil = DockerUtil() message = dockerUtil.getDockerInfo() except Exception, e: message = "error get docker info, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def getProcessList(): try: hostUtil = HostUtil() message = hostUtil.processList() except Exception, e: message = "error get sys process list, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def memInfo(): try: hostUtil = HostUtil() message = hostUtil.get_memory_stat() except Exception, e: message = "error get mem info, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def diskPathDu(): try: hostUtil = HostUtil() message = hostUtil.get_path_du(request.args.get("path")) except Exception, e: message = "error get disk device stat, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def diskUsage(): try: hostUtil = HostUtil() message = hostUtil.disk_usage(request.args.get("mountpoint")) except Exception, e: message = "error get disk device stat, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def diskPartitions(): try: hostUtil = HostUtil() message = hostUtil.disk_partitions() except Exception, e: message = "error get disk partitions, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def loadavg(): try: hostUtil = HostUtil() message = hostUtil.get_load_stat() except Exception, e: message = "error get cpu loadavg, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def networkInfo(ifname): try: hostUtil = HostUtil() message = hostUtil.networkinfo(ifname) except Exception, e: message = "error get network %s info, Ex:%s" % (ifname, str(e)) return Response.make_json_response(message, success=False, code=-1)
def networkList(): try: hostUtil = HostUtil() message = hostUtil.network_list() except Exception, e: message = "error get network list, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def imageList(): try: dockerUtil = DockerUtil() message = dockerUtil.getDockerImages() except Exception, e: message = "error get docker images list, Ex:%s" % (str(e)) return Response.make_json_response(message, success=False, code=-1)
def execCommand(): try: hostUtil = HostUtil() message = hostUtil.exec_command(request.values.get("cmd")) except Exception, e: message = "error get sys process list, cmd(%s) Ex:%s" % ( request.values.get("cmd"), str(e)) return Response.make_json_response(message, success=False, code=-1)
def list(): try: dockerUtil = DockerUtil() resContainerList = dockerUtil.getDockerPs( filters=request.args.getlist("filter")) except Exception, e: message = "error list container, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
def exec_command(): try: dockerUtil = DockerUtil() message = dockerUtil.getDockerExec(request.values.get("id"), request.values.get("cmd")) except Exception, e: message = "exec cmd `%s` error, ex:%s" % (request.values.get("cmd"), str(e)) return Response.make_json_response(message, success=False, code=-1)
def logs(): try: dockerUtil = DockerUtil() num = request.args.get("num") if num: num = int(num) logs = dockerUtil.getDockerLogs(request.values.get("id"), num) except Exception, e: message = "error get logs, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1)
from app.response import Response from app.host import host @host.route('/docker/version', methods=['GET']) @session_require def dockerVersion(): try: dockerUtil = DockerUtil() message = dockerUtil.getDockerVerion() except Exception, e: message = "error get docker version, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1) return Response.make_json_response(message) @host.route('/docker/info', methods=['GET']) @session_require def dockerInfo(): try: dockerUtil = DockerUtil() message = dockerUtil.getDockerInfo() except Exception, e: message = "error get docker info, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1) return Response.make_json_response(message)
def decorator(*args, **kwargs): # 现在是模拟登录,获取用户名,项目开发中获取session # username = request.args.get('username') # username = '******' # user = getattr(g, 'username', None) # if user is None : # print 'username is None' # else : # print g.username try: # 1.获取参数 timestamp = request.args.get('timestamp') access_id = request.args.get('access_id') sign = request.args.get('sign') # 2.验证timestamp的有效性 now = int(time.time()) ltime = abs(now - int(timestamp)) if ltime > 60 * 10: raise CustomAuthException('Timestamp timeout.') # 3.验证认证是否正确 octopusConfig = OctopusConfig() apiOptions = octopusConfig.getApiOptions() _access_key = apiOptions.get("access_key") _access_id = apiOptions.get("access_id") _access_ips = apiOptions.get("access_ips") # print "remote addr %s" % request.remote_addr currentIp = ipaddr.IPv4Network(request.remote_addr) if not access_id or not _access_id or not _access_key or not _access_ips or access_id != _access_id: raise CustomAuthException( 'Authentication failed, access_id invalid.') ary_access_ip = _access_ips.split(";") ipAllow = False for tmp_ip in ary_access_ip: if currentIp and currentIp in ipaddr.IPNetwork(tmp_ip): ipAllow = True break if not ipAllow: raise CustomAuthException( 'Authentication failed, not allowed to access from ip %s,allow ip: %s.' % (request.remote_addr, _access_ips)) util = EncryptUtil() mysign = util.getSign(timestamp, access_id, _access_key) mysign = mysign.lower() sign = sign.lower() if cmp(mysign, sign) != 0: raise CustomAuthException( 'Authentication failed, sign invalid.') # 4.exec business g.username = access_id return func(*args, **kwargs) # 判断用户名存在且用户名是什么的时候直接那个视图函数 # if username and username == 'admin': # g.username = username # args = (username,) + args # return func(*args, **kwargs) # else: # #如果没有就重定向到登录页面 # return redirect("login") except CustomAuthException, e: message = "error auth: %s" % str(e) return Response.make_json_response(message, success=False, code=-2)
from app.container import container @container.route('/list', methods=['GET']) @session_require def list(): try: dockerUtil = DockerUtil() resContainerList = dockerUtil.getDockerPs( filters=request.args.getlist("filter")) except Exception, e: message = "error list container, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1) return Response.make_json_response(resContainerList) @container.route('/info', methods=['GET']) @session_require def inspect(): try: dockerUtil = DockerUtil() containerInfo = dockerUtil.getDockerInspect(request.args.get("id")) except Exception, e: message = "error get container info, Ex:%s" % str(e) return Response.make_json_response(message, success=False, code=-1) return Response.make_json_response(containerInfo)