forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·174 lines (134 loc) · 5.59 KB
/
Copy pathdeploy.py
File metadata and controls
executable file
·174 lines (134 loc) · 5.59 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
"""
Script responsible for deploying both snowplow-tracker-core and snowplow-tracker
Need to be executed in Travis CI environment by tag (core/x.y.z OR x.y.z)
Publish core locally, build tracker using locally publiched core and then
publish altogether
"""
from contextlib import contextmanager
import os
import sys
import subprocess
# Initial setup
if 'TRAVIS_TAG' in os.environ:
TRAVIS_TAG = os.environ.get('TRAVIS_TAG')
else:
sys.exit("Environment variable TRAVIS_TAG is unavailable")
if 'TRAVIS_BUILD_DIR' in os.environ:
TRAVIS_BUILD_DIR = os.environ.get('TRAVIS_BUILD_DIR')
else:
sys.exit("Environment variable TRAVIS_BUILD_DIR is unavailable")
if 'NPM_AUTH_TOKEN' in os.environ:
NPM_AUTH_TOKEN = os.environ.get('NPM_AUTH_TOKEN')
else:
sys.exit("Environment variable NPM_AUTH_TOKEN is unavailable")
if TRAVIS_TAG.startswith('core/'):
PROJECT = 'core'
VERSION = TRAVIS_TAG[5:]
else:
PROJECT = 'tracker'
VERSION = TRAVIS_TAG
if 'AWS_ACCESS_KEY' in os.environ:
AWS_ACCESS_KEY = os.environ.get('AWS_ACCESS_KEY')
else:
sys.exit("Environment variable AWS_ACCESS_KEY is unavailable (required for tracker publishing)")
if 'AWS_SECRET_KEY' in os.environ:
AWS_SECRET_KEY = os.environ.get('AWS_SECRET_KEY')
else:
sys.exit("Environment variable AWS_SECRET_KEY is unavailable (required for tracker publishing)")
if 'AWS_S3_BUCKET' in os.environ:
AWS_S3_BUCKET = os.environ.get('AWS_S3_BUCKET')
else:
sys.exit("Environment variable AWS_S3_BUCKET is unavailable (required for tracker publishing)")
if 'AWS_REGION' in os.environ:
AWS_REGION = os.environ.get('AWS_REGION')
else:
sys.exit("Environment variable AWS_REGION is unavailable (required for tracker publishing)")
if 'AWS_CF_DISTRIBUTION' in os.environ:
AWS_CF_DISTRIBUTION = os.environ.get('AWS_CF_DISTRIBUTION')
else:
sys.exit("Environment variable AWS_CF_DISTRIBUTION is unavailable (required for tracker publishing)")
# Helper functions
def output_if_error(sbt_output):
"""Callback to print stderr and fail deploy if exit status not successful"""
(stdout, stderr) = sbt_output.communicate()
if sbt_output.returncode != 0:
print("Process has been failed.\n" + stdout)
sys.exit(stderr)
def execute(command, callback=output_if_error):
"""Execute shell command with optional callback"""
formatted_command = " ".join(command) if (type(command) == list) else command
print("Executing [{0}]".format(formatted_command))
output = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if hasattr(callback, '__call__'):
return callback(output)
else:
return output
def check_version():
"""Fail deploy if tag version doesn't match SBT version"""
if PROJECT == 'core':
get_version = """var fs=require('fs'); fs.readFile('./core/package.json', 'utf8', function(e,d) { console.log(JSON.parse(d)['version']) });"""
elif PROJECT == 'tracker':
get_version = """var fs=require('fs'); fs.readFile('./package.json', 'utf8', function(e,d) { console.log(JSON.parse(d)['version']) });"""
else:
sys.exit("Unknown project " + str(PROJECT))
node_output = execute(['node', '-e', get_version], None)
print(node_output.stderr.read())
for line in node_output.stdout.read().split("\n"):
print(line)
if line:
if line != VERSION:
sys.exit("Version extracted from TRAVIS_TAG [{0}] doesn't conform declared in package.json [{1}]".format(VERSION, line))
else:
return
sys.exit("Cannot find version in core output:\n" + str(node_output))
@contextmanager
def npm_credentials():
"""Context manager allowing to use different credentials and delete them after use"""
npmrc = os.path.expanduser("~/.npmrc")
if os.path.isfile(npmrc):
os.remove(npmrc)
print("WARNING! ~/.npmrc already exists. It should be deleted after each use")
print("Overrinding existing ~/.npmrc")
else:
print("Creating ~/.npmrc")
with open(npmrc, 'a') as f:
f.write("registry=http://registry.npmjs.org/\n//registry.npmjs.org/:_authToken=" + NPM_AUTH_TOKEN)
yield
print("Deleting ~/.npmrc")
os.remove(npmrc)
@contextmanager
def aws_credentials():
"""Context manager allowing to use different credentials and delete them after use"""
awsjson = os.path.join(TRAVIS_BUILD_DIR, "aws.json")
if os.path.isfile(awsjson):
sys.exit("aws.json already exists. It should be deleted after each use")
else:
print("Creating aws.json")
with open(awsjson, 'a') as f:
f.write(
'{"key":"%(key)s","secret":"%(secret)s","bucket":"%(bucket)s","region":"%(region)s", "distribution":"%(distribution)s"}' % \
{'key': AWS_ACCESS_KEY, 'secret': AWS_SECRET_KEY, 'bucket': AWS_S3_BUCKET, 'distribution': AWS_CF_DISTRIBUTION, 'region': AWS_REGION}
)
yield
print("Deleting aws.json")
os.remove(awsjson)
def publish_core():
os.chdir(os.path.join(TRAVIS_BUILD_DIR, "core"))
with npm_credentials():
execute(['npm', 'publish'])
def publish_tracker():
os.chdir(TRAVIS_BUILD_DIR)
with aws_credentials():
execute(['grunt', 'publish'])
def publish():
if PROJECT == 'core':
publish_core()
elif PROJECT == 'tracker':
publish_tracker()
else:
sys.exit("Unknown project " + str(PROJECT))
if __name__ == "__main__":
# Publish locally all dependencies
check_version()
publish()