-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpublish.py
executable file
·136 lines (109 loc) · 4.46 KB
/
publish.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
from base64 import b64encode
import json
import requests
import sys, getopt
import os
import boto3
import flask
from flask import render_template
fapp = flask.Flask('pub', template_folder='.')
API_BASE_URL = {"develop": "https://q6kkptbenj.execute-api.us-east-1.amazonaws.com/dev/",
"prod": "https://w3qym6pdvb.execute-api.us-east-1.amazonaws.com/"}
LANDING_PAGE = {"develop": 91469,
"prod": 87812}
LICENSE_PAGE = {"develop": 91471,
"prod": 91164}
'''
Publish file to S3 and get version
'''
def publish_app_js(stage):
global API_BASE_URL
# Create an S3 client
s3 = boto3.client('s3')
with fapp.app_context():
tmpl_vars = {'API_BASE_URL': API_BASE_URL[stage]}
rendered_content = render_template('dist/app.js', **tmpl_vars)
rendered_content = rendered_content.encode('ascii', 'ignore')
f = s3.put_object(Body=bytes(rendered_content), Bucket='cdn.neo4jlabs.com', Key='startups-v2/' + stage + '/app.js', ACL='public-read', ContentType='text/javascript')
return f['VersionId']
def get_latest_license(key):
# Create an S3 client
s3 = boto3.client('s3')
bucket = 'neo4j-startup-licenses'
f = s3.head_object(Bucket=bucket, Key=key)
return 'https://s3.amazonaws.com/%s/%s?versionId=%s' % (bucket, key, str(f['VersionId']))
def get_latest_neo4j_inc_license():
return get_latest_license('neo4j-inc-startup-license.pdf')
def get_latest_neo4j_ab_license():
return get_latest_license('neo4j-ab-startup-license.pdf')
'''
Publish file to S3 and get version
'''
def publish_view_license_js(stage):
# Create an S3 client
s3 = boto3.client('s3')
with fapp.app_context():
tmpl_vars = {'API_BASE_URL': API_BASE_URL[stage]}
rendered_content = render_template('dist/license.js', **tmpl_vars)
rendered_content = rendered_content.encode('ascii', 'ignore')
f = s3.put_object(Body=bytes(rendered_content), Bucket='cdn.neo4jlabs.com', Key='startups-v2/' + stage + '/view-license.js', ACL='public-read', ContentType='text/javascript')
return f['VersionId']
'''
Get page content
'''
def get_page_content(filename):
file = open('html/%s' % filename)
return file.read()
'''
Update wordpress page
'''
def update_wordpress_page(pageId, content):
url = 'https://neo4j.com/wp-json/wp/v2/pages/%d' % (pageId)
auth = b64encode('{}:{}'.format(os.getenv('PUBLISH_DOCS_USERNAME'), os.getenv('PUBLISH_DOCS_PASSWORD')).encode()).decode()
headers = {
'Accept': 'application/json',
'Authorization': 'Basic {}'.format(auth),
}
r = requests.get(url, headers=headers)
response = json.loads(r.content)
# build response for update
response['content'] = content
headers['Content-Type'] = 'application/json'
print(url)
pr = requests.post(url, headers=headers, data=json.dumps(response))
print(pr.content)
return pr.content
def main(argv):
stage = 'develop'
try:
opts, args = getopt.getopt(argv,"h",['stage='])
except getopt.GetoptError:
print('publish.py --stage <stage>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('publish.py --stage <stage>')
sys.exit()
elif opt in ("--stage"):
stage = arg
print('Stage is "%s"' % (stage))
if stage != 'develop' and stage != 'prod':
print("Stages 'prod' + 'develop' are only supported stages currently")
sys.exit()
if 'PUBLISH_DOCS_USERNAME' in os.environ and 'PUBLISH_DOCS_PASSWORD' in os.environ:
# publish new JS
appVersionId = publish_app_js(stage)
viewLicenseVersionId = publish_view_license_js(stage)
# publish wordpress page, replacing version of JS with latest published
with fapp.app_context():
tmpl_vars = {'js_location': 'https://cdn.neo4jlabs.com/startups-v2/' + stage + '/app.js', 'js_version': appVersionId, 'neo4j_ab_license_url': get_latest_neo4j_ab_license(), 'neo4j_inc_license_url': get_latest_neo4j_inc_license()}
rendered_content = render_template('html/index.html', **tmpl_vars)
pageContent = update_wordpress_page(LANDING_PAGE[stage], rendered_content)
tmpl_vars = {'js_location': 'https://cdn.neo4jlabs.com/startups-v2/' + stage + '/view-license.js', 'js_version': viewLicenseVersionId}
rendered_content = render_template('html/view-license.html', **tmpl_vars)
pageContent = update_wordpress_page(LICENSE_PAGE[stage], rendered_content)
else:
print("Environment variables for PUBLISH_DOCS_USERNAME and PUBLISH_DOCS_PASSWORD must be set")
sys.exit()
if __name__ == "__main__":
main(sys.argv[1:])