forked from RedHatInsights/pendo-config-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
259 lines (229 loc) · 8.48 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
import json
import yaml
import client as client
import time
import sys
import click
import logging
import os
import coloredlogs
from time import perf_counter
DELAY=1
UNMERGED_FILE='holder.yml'
log = logging.getLogger("pendo-config")
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
coloredlogs.install(
fmt='%(name)s %(levelname)s %(message)s',
level_styles={
'info': {'color': 'green'},
'warning': {'color': 'yellow'},
'critical': {'color': 'red'},
},
logger=log,
)
def _pretty_time_delta(seconds):
# https://gist.github.com/thatalextaylor/7408395
seconds = int(seconds)
days, seconds = divmod(seconds, 86400)
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
if days > 0:
return "%dd%dh%dm%ds" % (days, hours, minutes, seconds)
elif hours > 0:
return "%dh%dm%ds" % (hours, minutes, seconds)
elif minutes > 0:
return "%dm%ds" % (minutes, seconds)
else:
return "%ds" % (seconds,)
def beta_pages(path, dry_run):
with open(path, 'r') as data:
yml = yaml.safe_load(data)
for name, group in yml.items():
name = f'_beta {name}'
log.info(f'Creating group: [{name}]')
if not dry_run:
pendo_group = client.create_group_idempotent(name, client.get_color(name, group))
if 'pages' in group:
for page_name, page in group['pages'].items():
beta_pages = {'url_rules': [] }
for i, url in enumerate(page['url_rules']):
page['url_rules'][i] = '//*{}'.format(page['url_rules'][i])
page['url_rules'][i] = page['url_rules'][i].replace('//*/', '//*/beta/')
beta_pages['url_rules'].append(page['url_rules'][i])
page['url_rules'][i] = page['url_rules'][i].replace('//*/beta/', '//*/preview/')
beta_pages['url_rules'].append(page['url_rules'][i])
log.info(f'Creating page: [{page_name}] {beta_pages}')
if not dry_run:
client.create_page_in_group(pendo_group, page_name, beta_pages)
time.sleep(DELAY)
def aggregate_pages(path, dry_run):
with open(path, 'r') as data:
yml = yaml.safe_load(data)
for name, group in yml.items():
name = f'_aggregate {name}'
log.info(f'Creating group: [{name}]')
if not dry_run:
pendo_group = client.create_group_idempotent(name, client.get_color(name, group))
if 'pages' in group:
for page_name, page in group['pages'].items():
aggregate_pages = {'url_rules': [] }
for i, url in enumerate(page['url_rules']):
page['url_rules'][i] = '//*{}'.format(page['url_rules'][i])
aggregate_pages['url_rules'].append(page['url_rules'][i])
page['url_rules'][i] = page['url_rules'][i].replace('//*/', '//*/beta/')
aggregate_pages['url_rules'].append(page['url_rules'][i])
page['url_rules'][i] = page['url_rules'][i].replace('//*/beta/', '//*/preview/')
aggregate_pages['url_rules'].append(page['url_rules'][i])
log.info(f'Creating page: [{page_name}] {aggregate_pages}')
if not dry_run:
client.create_page_in_group(pendo_group, page_name, aggregate_pages)
time.sleep(DELAY)
def preview_pages(path, dry_run):
with open(path, 'r') as data:
yml = yaml.safe_load(data)
for name, group in yml.items():
name = f'_preview {name}'
log.info(f'Creating group: [{name}]')
if not dry_run:
pendo_group = client.create_group_idempotent(name, client.get_color(name, group))
if 'pages' in group:
for page_name, page in group['pages'].items():
preview_pages = {'url_rules': [] }
for i, url in enumerate(page['url_rules']):
page['url_rules'][i] = '//*{}'.format(page['url_rules'][i])
page['url_rules'][i] = page['url_rules'][i].replace('//*/', '//*/preview/')
preview_pages['url_rules'].append(page['url_rules'][i])
log.info(f'Creating page: [{page_name}] {preview_pages}')
if not dry_run:
client.create_page_in_group(pendo_group, page_name, preview_pages)
time.sleep(DELAY)
# Send the data to the pendo
def main_loop(path, dry_run):
with open(path, 'r') as data:
yml = yaml.safe_load(data)
for name, group in yml.items():
log.info(f'Creating group: [{name}]')
if not dry_run:
pendo_group = client.create_group_idempotent(name, client.get_color(name, group))
if 'pages' in group:
for page_name, page in group['pages'].items():
for i, url in enumerate(page['url_rules']):
page['url_rules'][i] = '//*{}'.format(page['url_rules'][i])
log.info(f'Creating page: [{page_name}] {page}')
if not dry_run:
client.create_page_in_group(pendo_group, page_name, page)
time.sleep(DELAY)
if 'features' in group:
scope = False
if '_scope' in group['features']:
scope = group['features']['_scope']
del group['features']['_scope']
for feature_name, feature in group['features'].items():
if scope:
for i, selector in enumerate(feature['selectors']):
txt = feature['selectors'][i]
txt = txt.replace('{}', scope, 1)
feature['selectors'][i] = txt
log.info(f'Creating feature: [{feature_name}] {feature}')
if not dry_run:
client.create_feature_in_group(pendo_group, feature_name, feature)
time.sleep(DELAY)
# Check if app exists in main.yml
def check_app(appName):
log.info('Checking if app exists in main.yml')
with open('./data/main.yml', 'r') as data:
yml = yaml.safe_load(data)
ymlArray = yml["applications"]
if appName in ymlArray:
log.info('App exists')
return True
else:
log.info('App does not exist')
return False
# Build the app using main_loop
def build_app(appName, dry_run):
log.warning(f'Building: {appName}')
location = './data/'
appFile = appName + '.yml'
fullPathname = location + appFile
log.info(f'Using file: {appFile}')
# Aggregate pages
log.info('Creating aggregate pages')
aggregate_pages(fullPathname, dry_run)
# Stable pages and features
log.info('Creating stable pages and features')
main_loop(fullPathname, dry_run)
# Beta pages and features
log.info('Creating beta pages and features')
beta_pages(fullPathname, dry_run)
# Preview pages and features
log.info('Creating preview pages and features')
preview_pages(fullPathname, dry_run)
if not dry_run:
generate_stash(appName)
client.clear_ids_map()
def generate_stash(appName):
location = './stash/'
pathname = appName + '_stash.yml'
fullPathname = location + pathname
with open(fullPathname, 'w') as outfile:
yaml.dump(client.get_ids_map(), outfile)
def clearUnmergedFile():
file = open(UNMERGED_FILE, 'w')
file.truncate()
file.close()
# main
@click.command()
@click.option(
'--apps',
'-a',
type=str,
multiple=True,
default=[],
help='Name of the app(s) you wish to build',
)
@click.option(
'--dry-run',
'-d',
is_flag=True,
help='Used to determine if data should actually be added or removed from Pendo',
)
@click.option(
'--unmerged',
'-u',
is_flag=True,
help='Push all recently merged apps to Pendo (holder.yml)'
)
@click.option(
'--clear',
'-c',
is_flag=True,
help='Clear unmerged app list'
)
def main(apps, dry_run, unmerged, clear):
start = perf_counter()
if clear:
clearUnmergedFile()
return
if unmerged:
with open(UNMERGED_FILE, 'r') as data:
yml = yaml.safe_load(data)
apps = yml
clearUnmergedFile()
if apps:
for app in apps:
log.info(f'App is: {app}')
if check_app(app):
build_app(app, dry_run)
else:
log.info('No app specified, building all apps')
with open('./data/main.yml', 'r') as data:
yml = yaml.safe_load(data)
ymlArray = yml["applications"]
for app in ymlArray:
build_app(app, dry_run)
end = perf_counter()
log.critical(f'Elapsed time: {_pretty_time_delta(end - start)}')
if __name__ == "__main__":
main()