-
Notifications
You must be signed in to change notification settings - Fork 33
/
auto_content.py
225 lines (170 loc) · 6.91 KB
/
auto_content.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
#!/usr/bin/env python
"""Perform import/publish/promote/cleanup on scheduled days."""
import sys, os, glob
import subprocess
import argparse
import datetime
import helpers
def dates():
"""Return a tuple of day in week and in month, and
Returns
-------
int
day of week (0=Mon, 6=Sun)
int
week of month
dict
dictionary mapping 'human readable' days to their numeric value
"""
# What day is it? (0=Mon -> 6=Sun)
dayofweek = datetime.datetime.today().weekday()
# Figure out which week of the month we are in
weekofmonth = (datetime.datetime.now().day-1)/7+1
print "Day %s of week %s" % (dayofweek, weekofmonth)
# Define dictionary mapping 'human readable' days to their numeric value
days = {'Mon': 0, 'Tue': 1, 'Wed': 2, 'Thu': 3, 'Fri': 4, 'Sat': 5, 'Sun': 6}
return (dayofweek, weekofmonth, days)
def run_imports(dryrun, dayofweek, days):
"""Run imports of satellite content."""
# If we are on an internet connected satellite, there will never be anything to import
# In this case, we'll run the publish on Tuesday
if helpers.DISCONNECTED == False:
print "Internet connected, nothing to import"
if dayofweek == days['Tue']:
good_imports = True
else:
good_imports = False
return good_imports
print "Processing Imports..."
# Find any sha256 files in the import dir
infiles = glob.glob(helpers.IMPORTDIR + '/*.sha256')
# Extract the dataset timestamp/name from the filename and add to a new list
# Assumes naming standard sat6_export_YYYYMMDD-HHMM_NAME.sha256
# 'sorted' function should result in imports being done in correct order by filename
tslist = []
good_imports = False
for f in sorted(infiles):
dstime = f.split('_')[-2]
dsname = (f.split('_')[-1]).split('.')[-2]
tslist.append(dstime + '_' + dsname)
if tslist:
msg = 'Found import datasets on disk...\n' + '\n'.join(tslist)
else:
msg = 'No import datasets to process'
helpers.log_msg(msg, 'INFO')
print msg
# Now for each import file in the list, run the import script in unattended mode:-)
if tslist:
if not dryrun:
for dataset in tslist:
rc = subprocess.call(['/usr/local/bin/sat_import', '-u', '-r', '-d', dataset])
# If the import is successful
if rc == 0:
good_imports = True
else:
msg = "Dry run - not actually performing import"
helpers.log_msg(msg, 'WARNING')
return good_imports
def publish_cv(dryrun):
"""Run the script to publish content views."""
print "Running Content View Publish..."
# Set the initial state
good_publish = False
if not dryrun:
rc = subprocess.call(['/usr/local/bin/publish_content_views', '-q', '-a', '-m'])
else:
msg = "Dry run - not actually performing publish"
helpers.log_msg(msg, 'WARNING')
rc = subprocess.call(['/usr/local/bin/publish_content_views', '-q', '-a', '-m', '-d'])
if rc == 0:
good_publish = True
return good_publish
def promote_cv(dryrun, lifecycle):
"""Run script to promote content view to specified lifecycle."""
print "Running Content View Promotion to " + lifecycle + "..."
# Set the initial state
good_promote = False
if not dryrun:
rc = subprocess.call(['/usr/local/bin/promote_content_views', '-q', '-m', '-e', lifecycle])
else:
msg = "Dry run - not actually performing promotion"
helpers.log_msg(msg, 'WARNING')
rc = subprocess.call(['/usr/local/bin/promote_content_views', '-q', '-d', '-m', '-e', lifecycle])
if rc == 0:
good_promote = True
return good_promote
def push_puppet(dryrun):
"""Perform a push of puppet modules.
This uses the DEFAULT puppet-forge-server defined in config.yml
"""
print "Pushing puppet modules to puppet-forge server..."
# Set the initial state
good_puppetpush = False
if not dryrun:
rc = subprocess.call(['/usr/local/bin/push_puppetforge', '-r', 'puppet-forge'])
# If the import is successful
if rc == 0:
good_puppetpush = True
else:
msg = "Dry run - not actually performing module push"
helpers.log_msg(msg, 'WARNING')
return good_puppetpush
def clean_cv(dryrun):
"""Run the script to clean content views."""
print "Running Content View Cleanup..."
if not dryrun:
rc = subprocess.call(['/usr/local/bin/clean_content_views', '-a', '-c'])
else:
msg = "Dry run - not actually performing cleanup"
helpers.log_msg(msg, 'WARNING')
rc = subprocess.call(['/usr/local/bin/clean_content_views', '-a', '-c', '-d'])
def main(args):
"""Perform import/publish/promote/cleanup on scheduled days."""
# Check for sane input
parser = argparse.ArgumentParser(
description='Imports, Publishes and Promotes content views.')
parser.add_argument('-d', '--dryrun', help='Dry Run - Only show what will be done',
required=False, action="store_true")
parser.add_argument('-p', '--puppet', help='Include puppet-forge module push',
required=False, action="store_true")
args = parser.parse_args()
# Set default flags and read in options given to us
if args.dryrun:
dryrun = True
else:
dryrun = False
run_publish = False
run_promote = True
# Determine the day of week and week of month for use in our scheduling
(dayofweek, weekofmonth, days) = dates()
# MONDAYS
# Run promotion first - this ensures content consistency (QA->Prod, Library->QA)
if dayofweek == days['Mon']:
if weekofmonth == 4:
run_promote = promote_cv(dryrun, 'Production')
# Run QA promotion on 2nd and 4th Monday. Conditional on Prod promotion success
if weekofmonth == 2 or weekofmonth == 4:
if run_promote:
run_promote = promote_cv(dryrun, 'Quality')
# EVERY DAY
# Check if there are any imports in our input dir and import them.
# run_publish will be returned as 'True' if any successful imports were performed.
# If no imports are performed, or they fail, publish can't be triggered.
run_publish = run_imports(dryrun, dayofweek, days)
# If the imports succeeded, we can go ahead and publish the new content to Library
if run_publish:
publish_cv(dryrun)
# Push any new puppet-forge modules if we have requested that
if args.puppet:
push_puppet(dryrun)
# THURSDAYS
# Run content view cleanup once a month, after we have done all promotions for the month.
if dayofweek == days['Thu']:
if weekofmonth == 4:
clean_cv(dryrun)
if __name__ == "__main__":
try:
main(sys.argv[1:])
except KeyboardInterrupt, e:
print >> sys.stderr, ("\n\nExiting on user cancel.")
sys.exit(1)