-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
177 lines (143 loc) · 6.22 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
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
175
176
177
"""
Script to deploy the IDP-Z3 project, to be used whenever there is a new version
ready.
To run it:
* commit your last changes in the repository
* `poetry env use 3.11 & poetry update`
* start the web and client servers
* `poetry run python3.11 deploy.py`
It does the following things:
* add a tag in git;
* update version in pyproject.toml;
* deploy idp-engine package to pypi;
* deploy folint package to pypi;
* deploy IDP-Z3 to GAE;
* add version in versions.json.
Before actually deploying, it runs the tests to make sure that there are no
errors, and regenerates the '/IDP-Z3/idp_web_server/static' folder.
Authors: Pierre Carbonnelle, Simon Vandevelde
"""
from distutils.dir_util import copy_tree
import json
import re
import requests
import subprocess
import sys
def run(c, **kwargs):
return subprocess.run(c.split(' '), **kwargs)
def get(c, **kwargs):
return run(c, capture_output=True, **kwargs).stdout
def require_clean_work_tree(cwd):
assert 0 == run('git diff-files --quiet --ignore-submodules --', cwd=cwd).returncode, \
"Cannot deploy: you have unstaged changes."
assert 0 == run('git diff-index --cached --quiet HEAD --ignore-submodules --', cwd=cwd).returncode, \
"Cannot deploy: your index contains uncommitted changes."
run('git pull', cwd=cwd) # may fail if there are commits
def query_user(query, default="y", get=False):
if get is True:
return input(query)
if default == "y":
return input(query) in "Yy"
else:
return input(query) in "Nn"
def running(url):
try:
response = requests.get(url)
return response.status_code == 200
except requests.exceptions.RequestException as e:
return False
if not query_user("Did you do a 'poetry update'? (Y/n)"):
sys.exit()
# run Cypress tests
if not running("http://localhost:5000"):
query_user("Please start web server.")
if not running("http://localhost:4201"):
query_user("Please start web client.")
run("npx cypress open")
run('git pull origin', check=True)
run('python3 test.py generate')
if query_user("Run benchmark? (Y/n)"):
run('python3 test.py benchmark')
update_statics = query_user("Update the '/IDP-Z3/idp_web_server/static' folder? (Y/n) ")
if update_statics:
# Generate static and commit.
run('npm run-script build', cwd='idp_web_client', check=True)
print("Copying to static folder ...")
copy_tree('idp_web_client/dist/', './idp_web_server/static')
# Create new version tag.
new_tag = query_user("Create new tag? (Y/n) ")
if new_tag:
# Verify we are on main branch.
branch = get('git rev-parse --abbrev-ref HEAD')
assert branch == b'main\n', \
"Cannot deploy: IDP-Z3 not in main branch !"
# Find old tag.
current_tag = (get("git describe --tags --abbrev=0")
.decode("utf-8").strip()) # Strip newline
major, minor, patch = current_tag.split('.')
release_type = query_user("(M)ajor, (m)inor or (p)atch release? ",
get=True)
# Compute new tag
if release_type == "M":
tag_version = f"{int(major)+1}.0.0"
elif release_type == "m":
tag_version = f"{major}.{int(minor)+1}.0"
elif release_type == "p":
tag_version = f"{major}.{minor}.{int(patch)+1}"
else:
raise IOError("Incorrect release type")
assert query_user(f"Release {tag_version} ? (Y/N)")
# We also need to modify the pyproject.toml.
with open("./pyproject.toml", "r") as fp:
pyproject = fp.read()
pyproject = re.sub(r'version = ".*"',
f'version = "{tag_version}"',
pyproject)
with open("./pyproject.toml", "w") as fp:
fp.write(pyproject)
_ = query_user("Update version (and contributors) in CHANGELOG. Ready ?(Y/N)")
_ = query_user("Update version in IDP-Z3.py. Ready ?(Y/N)")
_ = query_user("Update folint and IDP-Z3 version in /folint/setup.py. Ready ?(Y/N)")
# Add and commit.
run("git add -A")
run("git commit")
run("git push origin main")
if new_tag:
# create tag
run(f"git tag {tag_version}")
# Push tags.
run(f"git push origin {tag_version}")
# Publish new version of IDP-Z3 on Pypi.
run("poetry install")
run("poetry build")
print("Publishing to pypi (Username: krr)")
run("poetry publish") # use `poetry config http-basic.pypi <username> <password>` to set up
run("rm -rf ./dist")
# publish new version of folint on Pypi
run("python3.11 setup.py sdist bdist_wheel", cwd="folint")
run("poetry run python3.11 -m twine upload dist/*", cwd="folint")
# if input("Deploy on Heroku ?") in "Yy":
# run("git push heroku main")
if new_tag or query_user("Deploy to Google App Engine? (Y/n) "):
print("Deploying to GAE")
# Push to google repository
# run("git push google main")
# run("git push google main", cwd='idp_web_client')
# deploy to GAE
run(f"gcloud app deploy {'' if new_tag else '--no-promote'}")
# update versions.list at https://gist.github.com/IDP-Z3/5d82c61fa39e8aa23da1642a2e2b420a
versions = get("gcloud app versions list --sort-by=~LAST_DEPLOYED").decode("utf-8")
id = versions.splitlines()[1].split(" ")[1]
print("latest version : ", id)
with open("../5d82c61fa39e8aa23da1642a2e2b420a/versions.json") as json_file:
data = json.load(json_file)
data['IDP-Z3 latest'] = f"{id}-dot-interactive-consultant.ew.r.appspot.com"
if new_tag:
data[f'IDP-Z3 {tag_version}'] = data['IDP-Z3 latest']
with open("../5d82c61fa39e8aa23da1642a2e2b420a/versions.json", "w") as outfile:
json.dump(data, outfile, indent=4)
run("git add versions.json" , cwd="../5d82c61fa39e8aa23da1642a2e2b420a")
run('git commit -m "latest"', cwd="../5d82c61fa39e8aa23da1642a2e2b420a")
run("git push origin master", cwd="../5d82c61fa39e8aa23da1642a2e2b420a")
# open browser on GAE
run(f"browse https://{id}-dot-interactive-consultant.ew.r.appspot.com/")