forked from mozilla/activity-stream
/
fabfile.py
executable file
·127 lines (98 loc) · 3.73 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import time
import hashlib
import json
from operator import itemgetter
from distutils.util import strtobool
import boto
from fabric.api import local, env
from boto.s3.key import Key
from pathlib import Path
env.bucket_name = "moz-activity-streams"
S3 = boto.connect_s3()
def to_bool(value):
if not isinstance(value, bool):
return strtobool(value)
else:
return value
def get_s3_headers():
return {
'Cache-Control': 'public, max-age={}'.format(24*60*60),
'Content-Disposition': 'inline',
}
def package(signing_key, signing_password):
local("npm install")
local("npm run package")
local("./node_modules/jpm/bin/jpm sign --api-key {} --api-secret {}"
.format(signing_key, signing_password))
local("mv activity_streams_experiment-*.xpi dist/")
local("mv \@activity-streams-*.update.rdf dist/update.rdf")
local("rm dist/activity-streams-*.xpi")
def get_packages():
dist_dir = Path("./dist")
paths = [(p, p.stat().st_mtime)
for p in dist_dir.rglob("*.xpi")
if p.is_file() and p.stat().st_size]
return paths
def get_latest_package_path():
paths = get_packages()
latest = max(paths, key=itemgetter(1))[0]
return latest
def upload_to_s3(bucket_name, file_path=None):
if file_path is None:
file_path = get_latest_package_path()
dir_path = file_path.as_posix()
bucket = S3.get_bucket(bucket_name)
k = bucket.get_key(dir_path)
if k is not None:
# file exists on S3
md5_hash = hashlib.md5(file_path.open("rb").read()).hexdigest()
if md5_hash == k.etag[1:-1]:
# skip if it's the same file
print "skipping upload for {}".format(dir_path)
latest = bucket.get_key("dist/activity-streams-latest.xpi")
update_manifest = bucket.get_key("dist/update.rdf")
return (k, latest, update_manifest)
print "uploading {}".format(dir_path)
headers = get_s3_headers()
headers["Content-Type"] = "application/x-xpinstall"
k = Key(bucket)
k.name = dir_path
k.set_contents_from_filename(dir_path, headers=headers)
k.set_acl("public-read")
k.copy(bucket_name, "dist/activity-streams-latest.xpi")
# copy latest key
latest = bucket.get_key("dist/activity-streams-latest.xpi")
latest.set_acl("public-read")
# upload update RDF
headers = get_s3_headers()
headers["Content-Type"] = "application/xml"
update_manifest = Key(bucket)
update_manifest.name = "dist/update.rdf"
update_manifest.set_contents_from_filename(
"./dist/update.rdf", headers=headers)
update_manifest.set_acl("public-read")
return (k, latest, update_manifest)
def deploy(run_package=True, signing_key=None, signing_password=None):
if not (signing_key is not None and signing_password is not None):
with open(".amo_config.json", "r") as f:
amo_config = json.load(f)
signing_key = amo_config["api-key"]
signing_password = amo_config["api-secret"]
start = time.time()
run_package = to_bool(run_package)
end_signing = None
if run_package:
package(signing_key, signing_password)
end_signing = time.time()
latest = get_latest_package_path()
key, latest, update_manifest = upload_to_s3(env.bucket_name, latest)
end = time.time()
time_taken = int(end-start)
print "\n===== summary ======"
print "signing time: {} secs".format(int(end_signing - start))
print "time taken: {} secs".format(time_taken)
print "S3 URLs:\n{}\n{}\n{}".format(
key.generate_url(expires_in=0, query_auth=False),
latest.generate_url(expires_in=0, query_auth=False),
update_manifest.generate_url(expires_in=0, query_auth=False),
)