gitlab_api.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import os
  2. import re
  3. import argparse
  4. import tempfile
  5. import tarfile
  6. import zipfile
  7. from functools import wraps
  8. import gitlab
  9. class Gitlab(object):
  10. JOB_NAME_PATTERN = re.compile(r"(\w+)(\s+(\d+)/(\d+))?")
  11. DOWNLOAD_ERROR_MAX_RETRIES = 3
  12. def __init__(self, project_id=None):
  13. config_data_from_env = os.getenv("PYTHON_GITLAB_CONFIG")
  14. if config_data_from_env:
  15. # prefer to load config from env variable
  16. with tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
  17. temp_file.write(config_data_from_env)
  18. config_files = [temp_file.name]
  19. else:
  20. # otherwise try to use config file at local filesystem
  21. config_files = None
  22. gitlab_id = os.getenv("LOCAL_GITLAB_HTTPS_HOST") # if None, will use the default gitlab server
  23. self.gitlab_inst = gitlab.Gitlab.from_config(gitlab_id=gitlab_id, config_files=config_files)
  24. self.gitlab_inst.auth()
  25. if project_id:
  26. self.project = self.gitlab_inst.projects.get(project_id)
  27. else:
  28. self.project = None
  29. def get_project_id(self, name, namespace=None):
  30. """
  31. search project ID by name
  32. :param name: project name
  33. :param namespace: namespace to match when we have multiple project with same name
  34. :return: project ID
  35. """
  36. projects = self.gitlab_inst.projects.list(search=name)
  37. for project in projects:
  38. if namespace is None:
  39. if len(projects) == 1:
  40. project_id = project.id
  41. break
  42. if project.namespace["path"] == namespace:
  43. project_id = project.id
  44. break
  45. else:
  46. raise ValueError("Can't find project")
  47. return project_id
  48. def download_artifacts(self, job_id, destination):
  49. """
  50. download full job artifacts and extract to destination.
  51. :param job_id: Gitlab CI job ID
  52. :param destination: extract artifacts to path.
  53. """
  54. job = self.project.jobs.get(job_id)
  55. with tempfile.NamedTemporaryFile(delete=False) as temp_file:
  56. job.artifacts(streamed=True, action=temp_file.write)
  57. with zipfile.ZipFile(temp_file.name, "r") as archive_file:
  58. archive_file.extractall(destination)
  59. def retry_download(func):
  60. """
  61. This wrapper will only catch IOError and retry the whole function.
  62. So only use it with download functions, read() inside and atomic
  63. functions
  64. """
  65. @wraps(func)
  66. def wrapper(self, *args, **kwargs):
  67. retried = 0
  68. while True:
  69. try:
  70. res = func(self, *args, **kwargs)
  71. except (IOError, EOFError) as e:
  72. retried += 1
  73. if retried > self.DOWNLOAD_ERROR_MAX_RETRIES:
  74. raise e # get out of the loop
  75. else:
  76. print('Retried for the {} time'.format(retried))
  77. continue
  78. else:
  79. break
  80. return res
  81. return wrapper
  82. def download_artifact(self, job_id, artifact_path, destination=None):
  83. """
  84. download specific path of job artifacts and extract to destination.
  85. :param job_id: Gitlab CI job ID
  86. :param artifact_path: list of path in artifacts (relative path to artifact root path)
  87. :param destination: destination of artifact. Do not save to file if destination is None
  88. :return: A list of artifact file raw data.
  89. """
  90. job = self.project.jobs.get(job_id)
  91. raw_data_list = []
  92. for a_path in artifact_path:
  93. try:
  94. data = job.artifact(a_path)
  95. except gitlab.GitlabGetError as e:
  96. print("Failed to download '{}' from job {}".format(a_path, job_id))
  97. raise e
  98. raw_data_list.append(data)
  99. if destination:
  100. file_path = os.path.join(destination, a_path)
  101. try:
  102. os.makedirs(os.path.dirname(file_path))
  103. except OSError:
  104. # already exists
  105. pass
  106. with open(file_path, "wb") as f:
  107. f.write(data)
  108. return raw_data_list
  109. def find_job_id(self, job_name, pipeline_id=None, job_status="success"):
  110. """
  111. Get Job ID from job name of specific pipeline
  112. :param job_name: job name
  113. :param pipeline_id: If None, will get pipeline id from CI pre-defined variable.
  114. :param job_status: status of job. One pipeline could have multiple jobs with same name after retry.
  115. job_status is used to filter these jobs.
  116. :return: a list of job IDs (parallel job will generate multiple jobs)
  117. """
  118. job_id_list = []
  119. if pipeline_id is None:
  120. pipeline_id = os.getenv("CI_PIPELINE_ID")
  121. pipeline = self.project.pipelines.get(pipeline_id)
  122. jobs = pipeline.jobs.list(all=True)
  123. for job in jobs:
  124. match = self.JOB_NAME_PATTERN.match(job.name)
  125. if match:
  126. if match.group(1) == job_name and job.status == job_status:
  127. job_id_list.append({"id": job.id, "parallel_num": match.group(3)})
  128. return job_id_list
  129. @retry_download
  130. def download_archive(self, ref, destination, project_id=None):
  131. """
  132. Download archive of certain commit of a repository and extract to destination path
  133. :param ref: commit or branch name
  134. :param destination: destination path of extracted archive file
  135. :param project_id: download project of current instance if project_id is None
  136. :return: root path name of archive file
  137. """
  138. if project_id is None:
  139. project = self.project
  140. else:
  141. project = self.gitlab_inst.projects.get(project_id)
  142. with tempfile.NamedTemporaryFile(delete=False) as temp_file:
  143. try:
  144. project.repository_archive(sha=ref, streamed=True, action=temp_file.write)
  145. except gitlab.GitlabGetError as e:
  146. print("Failed to archive from project {}".format(project_id))
  147. raise e
  148. print("archive size: {:.03f}MB".format(float(os.path.getsize(temp_file.name)) / (1024 * 1024)))
  149. with tarfile.open(temp_file.name, "r") as archive_file:
  150. root_name = archive_file.getnames()[0]
  151. archive_file.extractall(destination)
  152. return os.path.join(os.path.realpath(destination), root_name)
  153. if __name__ == '__main__':
  154. parser = argparse.ArgumentParser()
  155. parser.add_argument("action")
  156. parser.add_argument("project_id", type=int)
  157. parser.add_argument("--pipeline_id", "-i", type=int, default=None)
  158. parser.add_argument("--ref", "-r", default="master")
  159. parser.add_argument("--job_id", "-j", type=int, default=None)
  160. parser.add_argument("--job_name", "-n", default=None)
  161. parser.add_argument("--project_name", "-m", default=None)
  162. parser.add_argument("--destination", "-d", default=None)
  163. parser.add_argument("--artifact_path", "-a", nargs="*", default=None)
  164. args = parser.parse_args()
  165. gitlab_inst = Gitlab(args.project_id)
  166. if args.action == "download_artifacts":
  167. gitlab_inst.download_artifacts(args.job_id, args.destination)
  168. if args.action == "download_artifact":
  169. gitlab_inst.download_artifact(args.job_id, args.artifact_path, args.destination)
  170. elif args.action == "find_job_id":
  171. job_ids = gitlab_inst.find_job_id(args.job_name, args.pipeline_id)
  172. print(";".join([",".join([str(j["id"]), j["parallel_num"]]) for j in job_ids]))
  173. elif args.action == "download_archive":
  174. gitlab_inst.download_archive(args.ref, args.destination)
  175. elif args.action == "get_project_id":
  176. ret = gitlab_inst.get_project_id(args.project_name)
  177. print("project id: {}".format(ret))