copy_files.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. assert os.path.exists(dir_path)
  44. if os.path.isdir(dir_path):
  45. paths = [os.path.join(dir_path, f) for f in os.listdir(dir_path)]
  46. else:
  47. paths = [dir_path]
  48. return paths
  49. dest_resp_mapping = {
  50. "wheels": "presigned_resp_prod_wheels",
  51. "branch_wheels": "presigned_resp_prod_wheels",
  52. "jars": "presigned_resp_prod_wheels",
  53. "branch_jars": "presigned_resp_prod_wheels",
  54. "logs": "presigned_logs",
  55. }
  56. def upload_paths(paths, resp, destination):
  57. dest_key = dest_resp_mapping[destination]
  58. c = resp.json()[dest_key]
  59. of = OrderedDict(c["fields"])
  60. sha = os.environ["BUILDKITE_COMMIT"]
  61. branch = os.environ["BUILDKITE_BRANCH"]
  62. bk_job_id = os.environ["BUILDKITE_JOB_ID"]
  63. current_os = os.uname().sysname.lower()
  64. for path in paths:
  65. fn = os.path.split(path)[-1]
  66. of["key"] = {
  67. "wheels": f"latest/{fn}",
  68. "branch_wheels": f"{branch}/{sha}/{fn}",
  69. "jars": f"jars/latest/{current_os}/{fn}",
  70. "branch_jars": f"jars/{branch}/{sha}/{current_os}/{fn}",
  71. "logs": f"bazel_events/{branch}/{sha}/{bk_job_id}/{fn}"
  72. }[destination]
  73. of["file"] = open(path, "rb")
  74. r = requests.post(c["url"], files=of)
  75. print(f"Uploaded {path} to {of['key']}", r.status_code)
  76. if __name__ == "__main__":
  77. parser = argparse.ArgumentParser(
  78. description="Helper script to upload files to S3 bucket")
  79. parser.add_argument("--path", type=str, required=False)
  80. parser.add_argument("--destination", type=str)
  81. args = parser.parse_args()
  82. assert args.destination in {
  83. "branch_jars", "branch_wheels", "jars", "logs", "wheels",
  84. "docker_login"
  85. }
  86. assert "BUILDKITE_JOB_ID" in os.environ
  87. assert "BUILDKITE_COMMIT" in os.environ
  88. resp = perform_auth()
  89. if args.destination == "docker_login":
  90. handle_docker_login(resp)
  91. else:
  92. paths = gather_paths(args.path)
  93. print("Planning to upload", paths)
  94. upload_paths(paths, resp, args.destination)