copy_files.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import argparse
  2. import os
  3. from collections import OrderedDict
  4. import sys
  5. import time
  6. import subprocess
  7. from typing import List
  8. from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
  9. import requests
  10. def retry(f):
  11. def inner():
  12. resp = None
  13. for _ in range(5):
  14. resp = f()
  15. print("Getting Presigned URL, status_code", resp.status_code)
  16. if resp.status_code >= 500:
  17. print("errored, retrying...")
  18. print(resp.text)
  19. time.sleep(5)
  20. else:
  21. return resp
  22. if resp is None or resp.status_code >= 500:
  23. print("still errorred after many retries")
  24. sys.exit(1)
  25. return inner
  26. @retry
  27. def perform_auth():
  28. auth = BotoAWSRequestsAuth(
  29. aws_host="vop4ss7n22.execute-api.us-west-2.amazonaws.com",
  30. aws_region="us-west-2",
  31. aws_service="execute-api",
  32. )
  33. resp = requests.get(
  34. "https://vop4ss7n22.execute-api.us-west-2.amazonaws.com/endpoint/",
  35. auth=auth,
  36. params={"job_id": os.environ["BUILDKITE_JOB_ID"]})
  37. return resp
  38. def handle_docker_login(resp):
  39. pwd = resp.json()["docker_password"]
  40. subprocess.call(
  41. ["docker", "login", "--username", "raytravisbot", "--password", pwd])
  42. def gather_paths(dir_path) -> List[str]:
  43. dir_path = dir_path.replace("/", os.path.sep)
  44. assert os.path.exists(dir_path)
  45. if os.path.isdir(dir_path):
  46. paths = [os.path.join(dir_path, f) for f in os.listdir(dir_path)]
  47. else:
  48. paths = [dir_path]
  49. return paths
  50. dest_resp_mapping = {
  51. "wheels": "presigned_resp_prod_wheels",
  52. "branch_wheels": "presigned_resp_prod_wheels",
  53. "jars": "presigned_resp_prod_wheels",
  54. "branch_jars": "presigned_resp_prod_wheels",
  55. "logs": "presigned_logs",
  56. }
  57. def upload_paths(paths, resp, destination):
  58. dest_key = dest_resp_mapping[destination]
  59. c = resp.json()[dest_key]
  60. of = OrderedDict(c["fields"])
  61. sha = os.environ["BUILDKITE_COMMIT"]
  62. branch = os.environ["BUILDKITE_BRANCH"]
  63. bk_job_id = os.environ["BUILDKITE_JOB_ID"]
  64. current_os = sys.platform
  65. for path in paths:
  66. fn = os.path.split(path)[-1]
  67. of["key"] = {
  68. "wheels": f"latest/{fn}",
  69. "branch_wheels": f"{branch}/{sha}/{fn}",
  70. "jars": f"jars/latest/{current_os}/{fn}",
  71. "branch_jars": f"jars/{branch}/{sha}/{current_os}/{fn}",
  72. "logs": f"bazel_events/{branch}/{sha}/{bk_job_id}/{fn}"
  73. }[destination]
  74. of["file"] = open(path, "rb")
  75. r = requests.post(c["url"], files=of)
  76. print(f"Uploaded {path} to {of['key']}", r.status_code)
  77. if __name__ == "__main__":
  78. parser = argparse.ArgumentParser(
  79. description="Helper script to upload files to S3 bucket")
  80. parser.add_argument("--path", type=str, required=False)
  81. parser.add_argument("--destination", type=str)
  82. args = parser.parse_args()
  83. assert args.destination in {
  84. "branch_jars", "branch_wheels", "jars", "logs", "wheels",
  85. "docker_login"
  86. }
  87. assert "BUILDKITE_JOB_ID" in os.environ
  88. assert "BUILDKITE_COMMIT" in os.environ
  89. resp = perform_auth()
  90. if args.destination == "docker_login":
  91. handle_docker_login(resp)
  92. else:
  93. paths = gather_paths(args.path)
  94. print("Planning to upload", paths)
  95. upload_paths(paths, resp, args.destination)