forked from sympy/sympy_gamma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.py
executable file
·103 lines (91 loc) · 4.04 KB
/
deploy.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
#!/usr/bin/env python
import os
import sys
import argparse
import subprocess
TAG_COMMAND = 'git describe --exact-match HEAD --tags'
DEPLOY_COMMAND = ('python2 $SDK_LOCATION/appcfg.py '
'--oauth2_refresh_token=$OAUTH_REFRESH_TOKEN update .')
ROLLBACK_COMMAND = ('python2 $SDK_LOCATION/appcfg.py '
'--oauth2_refresh_token=$OAUTH_REFRESH_TOKEN rollback .')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate an App Engine app.yaml and optionally deploy this application.')
parser.add_argument('--generate-only',
help="Only generate app.yaml. Do not deploy.",
action='store_true')
group = parser.add_mutually_exclusive_group()
group.add_argument('--generate-test',
help='Generate app.yaml for the test application.',
nargs='?',
type=int,
default=-1,
const=-1)
group.add_argument('--generate-production',
help='Generate app.yaml for the production application.',
nargs='?',
type=int,
default=-1,
const=-1)
args = parser.parse_args()
deploy_app = not args.generate_only
config_type = 'test'
if (args.generate_production > 0 or
(os.environ.get('TRAVIS_PULL_REQUEST') == 'false' and
args.generate_test < 0)):
config_type = 'production'
application = version = None
if config_type == 'production':
print "Generating production configuration."
application = 'sympy-gamma-hrd'
# On main branch. Get the tag corresponding to the current commit;
# if the tag does not exist, do not deploy.
git_process = subprocess.Popen(TAG_COMMAND,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdin, stdout = git_process.communicate()
if not stdin and args.generate_production <= 0:
print "ERROR: Could not determine version to deploy."
print "Either tag the current commit as 'version-VERSION' or provide " + \
"--generate-production VERSION."
sys.exit(1)
if args.generate_production > 0:
version = args.generate_production
else:
try:
version = int(stdin[8:])
except ValueError:
print "ERROR: Could not determine version number from tag", stdin
sys.exit(1)
else:
print "Generating test configuration."
application = 'sympy-gamma-tests'
if args.generate_test > 0:
# User provided test version
version = args.generate_test
elif 'TRAVIS_PULL_REQUEST' in os.environ:
# Get PR number from Travis
version = int(os.environ.get('TRAVIS_PULL_REQUEST'))
else:
print "ERROR: Must provide --generate-test VERSION if not running under Travis."
sys.exit(1)
print "Generating configuration for version", version
with open('app.yaml.template') as f:
template = f.read()
configuration = template.format(application=application, version=version)
with open('app.yaml', 'w') as f:
f.write(configuration)
print "Generated configuration."
if deploy_app:
if os.environ.get('OAUTH_REFRESH_TOKEN'):
print "Deploying..."
return_code = subprocess.call(DEPLOY_COMMAND, shell=True)
if return_code == 0:
print "Deployed application."
if config_type == 'test':
print "Deployed to https://%s-dot-sympy-gamma-tests.appspot.com/" % version
else:
print "Could not deploy application. Running appcfg rollback..."
subprocess.call(ROLLBACK_COMMAND, shell=True)
else:
print "No credentials found for deployment."