1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, # You can obtain one at http://mozilla.org/MPL/2.0/.
9 from concurrent
import futures
10 from pprint
import pprint
15 from mozbuild
.util
import memoize
19 def create_aws_session():
21 This function creates an aws session that is
22 shared between upload and delete both.
25 level
= os
.environ
.get("MOZ_SCM_LEVEL", "1")
27 "1": "gecko-docs.mozilla.org-l1",
28 "2": "gecko-docs.mozilla.org-l2",
29 "3": "gecko-docs.mozilla.org",
31 secrets_url
= "http://taskcluster/secrets/v1/secret/"
32 secrets_url
+= "project/releng/gecko/build/level-{}/gecko-docs-upload".format(level
)
34 # Get the credentials from the TC secrets service. Note that these
35 # differ per SCM level
36 if "TASK_ID" in os
.environ
:
37 print("Using AWS credentials from the secrets service")
38 session
= requests
.Session()
39 res
= session
.get(secrets_url
)
40 res
.raise_for_status()
41 secret
= res
.json()["secret"]
42 session
= boto3
.session
.Session(
43 aws_access_key_id
=secret
["AWS_ACCESS_KEY_ID"],
44 aws_secret_access_key
=secret
["AWS_SECRET_ACCESS_KEY"],
48 print("Trying to use your AWS credentials..")
49 session
= boto3
.session
.Session(region_name
=region
)
51 s3
= session
.client("s3", config
=botocore
.client
.Config(max_pool_connections
=20))
57 def get_s3_keys(s3
, bucket
):
58 kwargs
= {"Bucket": bucket
}
61 response
= s3
.list_objects_v2(**kwargs
)
62 for obj
in response
["Contents"]:
63 all_keys
.append(obj
["Key"])
66 kwargs
["ContinuationToken"] = response
["NextContinuationToken"]
73 def s3_set_redirects(redirects
):
74 s3
, bucket
= create_aws_session()
76 configuration
= {"IndexDocument": {"Suffix": "index.html"}, "RoutingRules": []}
78 for path
, redirect
in redirects
.items():
80 "Condition": {"KeyPrefixEquals": path
},
81 "Redirect": {"ReplaceKeyPrefixWith": redirect
},
83 if os
.environ
.get("MOZ_SCM_LEVEL") == "3":
84 rule
["Redirect"]["HostName"] = "firefox-source-docs.mozilla.org"
86 configuration
["RoutingRules"].append(rule
)
88 s3
.put_bucket_website(
90 WebsiteConfiguration
=configuration
,
94 def s3_delete_missing(files
, key_prefix
=None):
95 """Delete files in the S3 bucket.
97 Delete files on the S3 bucket that doesn't match the files
98 given as the param. If the key_prefix is not specified, missing
99 files that has main/ as a prefix will be removed. Otherwise, it
100 will remove files with the same prefix as key_prefix.
102 s3
, bucket
= create_aws_session()
103 files_on_server
= get_s3_keys(s3
, bucket
)
106 path
for path
in files_on_server
if path
.startswith(key_prefix
)
110 path
for path
in files_on_server
if not path
.startswith("main/")
112 files
= [key_prefix
+ "/" + path
if key_prefix
else path
for path
, f
in files
]
113 files_to_delete
= [path
for path
in files_on_server
if path
not in files
]
116 while files_to_delete
:
117 keys_to_remove
= [{"Key": key
} for key
in files_to_delete
[:query_size
]]
118 response
= s3
.delete_objects(
121 "Objects": keys_to_remove
,
124 pprint(response
, indent
=2)
125 files_to_delete
= files_to_delete
[query_size
:]
128 def s3_upload(files
, key_prefix
=None):
129 """Upload files to an S3 bucket.
131 ``files`` is an iterable of ``(path, BaseFile)`` (typically from a
134 Keys in the bucket correspond to source filenames. If ``key_prefix`` is
135 defined, key names will be ``<key_prefix>/<path>``.
137 s3
, bucket
= create_aws_session()
139 def upload(f
, path
, bucket
, key
, extra_args
):
140 # Need to flush to avoid buffering/interleaving from multiple threads.
141 sys
.stdout
.write("uploading %s to %s\n" % (path
, key
))
143 s3
.upload_fileobj(f
, bucket
, key
, ExtraArgs
=extra_args
)
146 with futures
.ThreadPoolExecutor(20) as e
:
147 for path
, f
in files
:
148 content_type
, content_encoding
= mimetypes
.guess_type(path
)
151 if content_type
.startswith("text/"):
152 content_type
+= '; charset="utf-8"'
153 extra_args
["ContentType"] = content_type
155 extra_args
["ContentEncoding"] = content_encoding
158 key
= "%s/%s" % (key_prefix
, path
)
162 # The file types returned by mozpack behave like file objects. But
163 # they don't accept an argument to read(). So we wrap in a BytesIO.
165 e
.submit(upload
, io
.BytesIO(f
.read()), path
, bucket
, key
, extra_args
)
168 s3_delete_missing(files
, key_prefix
)
169 # Need to do this to catch any exceptions.