You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

70 lines
2.2 KiB

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import json
  4. import os
  5. from flask import Flask
  6. from flask import request
  7. from flask import jsonify
  8. from aliyunsdkcore.acs_exception.exceptions import ClientException
  9. from aliyunsdkcore.acs_exception.exceptions import ServerException
  10. from aliyunsdkcore.client import AcsClient
  11. from aliyunsdkcr.request.v20160607 import GetImageLayerRequest,GetRepoTagsRequest
  12. # 示例执行异常时建议升级aliyun-python-sdk-core到最新版本
  13. # 设置Client
  14. AK = os.getenv("ak")
  15. SK = os.getenv("secret")
  16. class AliyunCr():
  17. def __init__(self,endpoint="cn-shanghai"):
  18. self.apiClient = AcsClient(AK, SK, endpoint)
  19. def get_repo_tags(self,repo_namespace="ly_release",repo_name="",simple=False):
  20. # 构造请求
  21. request = GetRepoTagsRequest.GetRepoTagsRequest()
  22. # 设置参数
  23. request.set_RepoNamespace(repo_namespace)
  24. request.set_RepoName(repo_name)
  25. # request.set_Tag("tag")
  26. # 根据文档获取资源所在区域对应的RegionId
  27. # 请求地址格式为cr.{regionId}.aliyuncs.com
  28. # request.set_endpoint("cr.cn-shanghai.aliyuncs.com")
  29. # 发起请求
  30. try:
  31. response = self.apiClient.do_action_with_exception(request)
  32. response = json.loads(response)
  33. if not simple:
  34. return response["data"]["tags"]
  35. tags = ""
  36. for i in response["data"]["tags"]:
  37. tags += i["tag"]+"\n"
  38. return tags
  39. except ServerException as e:
  40. print(e)
  41. except ClientException as e:
  42. print(e)
  43. app = Flask(__name__)
  44. @app.route('/image')
  45. def hello_world():
  46. print(request.args)
  47. endpoint = request.args.get('endpoint', 'cn-shanghai')
  48. repo_namespace = request.args.get('repo_namespace', 'ly_release')
  49. repo_name = request.args.get('repo_name', '')
  50. simple = request.args.get('simple', False) #精简显示
  51. if simple:
  52. simple = True
  53. if not repo_name:
  54. return '范例/inage?endpoint=cn-shanghai&repo_namespace=ops&repo_name=cmdb&simple=1'
  55. ali = AliyunCr(endpoint)
  56. data = ali.get_repo_tags(repo_namespace,repo_name,simple)
  57. if not simple:
  58. return jsonify(data)
  59. return data