|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys, os, glob |
| 4 | +import subprocess |
| 5 | +import argparse |
| 6 | +import datetime |
| 7 | +import helpers |
| 8 | + |
| 9 | + |
| 10 | +def dates(): |
| 11 | + # What day is it? (0=Sun -> 6=Sat) |
| 12 | + dayofweek = datetime.datetime.today().weekday() |
| 13 | + |
| 14 | + # Figure out which week of the month we are in |
| 15 | + weekofmonth = (datetime.datetime.now().day-1)/7+1 |
| 16 | + |
| 17 | + print "Day %s of week %s" % (dayofweek, weekofmonth) |
| 18 | + |
| 19 | + return(dayofweek,weekofmonth) |
| 20 | + |
| 21 | + |
| 22 | +def run_imports(dryrun): |
| 23 | + print "Processing Imports..." |
| 24 | + |
| 25 | + # Find any sha256 files in the import dir |
| 26 | + infiles = glob.glob(helpers.IMPORTDIR + '/*.sha256') |
| 27 | + |
| 28 | + # Extract the dataset timestamp/name from the filename and add to a new list |
| 29 | + # Assumes naming standard sat6_export_YYYYMMDD-HHMM_NAME.sha256 |
| 30 | + # 'sorted' function should result in imports being done in correct order by filename |
| 31 | + tslist = [] |
| 32 | + good_imports = False |
| 33 | + for f in sorted(infiles): |
| 34 | + dstime = f.split('_')[-2] |
| 35 | + dsname = (f.split('_')[-1]).split('.')[-2] |
| 36 | + tslist.append(dstime + '_' + dsname) |
| 37 | + |
| 38 | + if tslist: |
| 39 | + msg = 'Found import datasets on disk...\n' + '\n'.join(tslist) |
| 40 | + else: |
| 41 | + msg = 'No import datasets to process' |
| 42 | + helpers.log_msg(msg, 'INFO') |
| 43 | + print msg |
| 44 | + |
| 45 | + # Now for each import file in the list, run the import script in unattended mode:-) |
| 46 | + if tslist: |
| 47 | + if not dryrun: |
| 48 | + for dataset in tslist: |
| 49 | + rc = subprocess.call(['/usr/local/bin/sat_import', '-u', '-r', '-d', dataset]) |
| 50 | + |
| 51 | + # If the import is successful |
| 52 | + if rc == 0: |
| 53 | + good_imports = True |
| 54 | + |
| 55 | + else: |
| 56 | + msg = "Dry run - not actually performing import" |
| 57 | + helpers.log_msg(msg, 'WARNING') |
| 58 | + |
| 59 | + return good_imports |
| 60 | + |
| 61 | + |
| 62 | +def publish_cv(dryrun): |
| 63 | + print "Running Content View Publish..." |
| 64 | + |
| 65 | + # Set the initial state |
| 66 | + good_publish = False |
| 67 | + |
| 68 | + if not dryrun: |
| 69 | + rc = subprocess.call(['/usr/local/bin/publish_content_views', '-q', '-a']) |
| 70 | + else: |
| 71 | + msg = "Dry run - not actually performing publish" |
| 72 | + helpers.log_msg(msg, 'WARNING') |
| 73 | + rc = subprocess.call(['/usr/local/bin/publish_content_views', '-q', '-a', '-d']) |
| 74 | + |
| 75 | + if rc == 0: |
| 76 | + good_publish = True |
| 77 | + |
| 78 | + return good_publish |
| 79 | + |
| 80 | + |
| 81 | +def promote_cv(dryrun, lifecycle): |
| 82 | + print "Running Content View Promotion to " + lifecycle + "..." |
| 83 | + |
| 84 | + # Set the initial state |
| 85 | + good_promote = False |
| 86 | + |
| 87 | + if not dryrun: |
| 88 | + rc = subprocess.call(['/usr/local/bin/promote_content_views', '-q', '-e', lifecycle]) |
| 89 | + else: |
| 90 | + msg = "Dry run - not actually performing promotion" |
| 91 | + helpers.log_msg(msg, 'WARNING') |
| 92 | + rc = subprocess.call(['/usr/local/bin/promote_content_views', '-q', '-d', '-e', lifecycle]) |
| 93 | + |
| 94 | + if rc == 0: |
| 95 | + good_promote = True |
| 96 | + |
| 97 | + return good_promote |
| 98 | + |
| 99 | + |
| 100 | +def push_puppet(dryrun): |
| 101 | + print "Pushing puppet modules to puppet-forge server..." |
| 102 | + |
| 103 | + # Set the initial state |
| 104 | + good_puppetpush = False |
| 105 | + |
| 106 | + if not dryrun: |
| 107 | + for dataset in tslist: |
| 108 | + rc = subprocess.call(['/usr/local/bin/push-puppetforge', '-r', 'puppet-forge']) |
| 109 | + |
| 110 | + # If the import is successful |
| 111 | + if rc == 0: |
| 112 | + good_puppetpush = True |
| 113 | + |
| 114 | + else: |
| 115 | + msg = "Dry run - not actually performing module push" |
| 116 | + helpers.log_msg(msg, 'WARNING') |
| 117 | + |
| 118 | + return good_puppetpush |
| 119 | + |
| 120 | + |
| 121 | +def clean_cv(dryrun): |
| 122 | + print "Running Content View Cleanup..." |
| 123 | + |
| 124 | + if not dryrun: |
| 125 | + rc = subprocess.call(['/usr/local/bin/clean_content_views', '-a', '-c']) |
| 126 | + else: |
| 127 | + msg = "Dry run - not actually performing cleanup" |
| 128 | + helpers.log_msg(msg, 'WARNING') |
| 129 | + rc = subprocess.call(['/usr/local/bin/clean_content_views', '-a', '-c', '-d']) |
| 130 | + |
| 131 | + |
| 132 | +def main(args): |
| 133 | + |
| 134 | + ### Run import/publish on scheduled day |
| 135 | + |
| 136 | + # Check for sane input |
| 137 | + parser = argparse.ArgumentParser( |
| 138 | + description='Imports, Publishes and Promotes content views.') |
| 139 | + parser.add_argument('-d', '--dryrun', help='Dry Run - Only show what will be done', |
| 140 | + required=False, action="store_true") |
| 141 | + parser.add_argument('-p', '--puppet', help='Include puppet-forge module push', |
| 142 | + required=False, action="store_true") |
| 143 | + |
| 144 | + args = parser.parse_args() |
| 145 | + |
| 146 | + # Set default flags and read in options given to us |
| 147 | + if args.dryrun: |
| 148 | + dryrun = True |
| 149 | + else: |
| 150 | + dryrun = False |
| 151 | + |
| 152 | + run_publish = False |
| 153 | + run_promote = True |
| 154 | + |
| 155 | + # Determine the day of week and week of month for use in our scheduling |
| 156 | + (dayofweek, weekofmonth) = dates() |
| 157 | + |
| 158 | + |
| 159 | + # Run promotion first - this ensures content consistency (QA->Prod, Library->QA) |
| 160 | + if dayofweek == 1: |
| 161 | + if weekofmonth == 4: |
| 162 | + run_promote = promote_cv(dryrun, 'Production') |
| 163 | + |
| 164 | + # Run QA promotion on 2nd and 4th Monday. Conditional on Prod promotion success |
| 165 | + if weekofmonth == 2 or weekofmonth == 4: |
| 166 | + if run_promote: |
| 167 | + run_promote = promote_cv(dryrun, 'Quality') |
| 168 | + |
| 169 | + |
| 170 | + # Every day, check if there are any imports in our input dir and import them. |
| 171 | + # run_publish will be returned as 'True' if any successful imports were performed. |
| 172 | + # If no imports are performed, or they fail, publish can't be triggered. |
| 173 | + run_publish = run_imports(dryrun) |
| 174 | + |
| 175 | + # If the imports succeeded, we can go ahead and publish the new content to Library |
| 176 | + if run_publish: |
| 177 | + publish_cv(dryrun) |
| 178 | + # Push any new puppet-forge modules if we have requested that |
| 179 | + if args.puppet: |
| 180 | + push_puppet(dryrun) |
| 181 | + |
| 182 | + # Run content view cleanup once a month, after we have done all promotions for the month. |
| 183 | + if dayofweek == 4: |
| 184 | + if weekofmonth == 4: |
| 185 | + clean_cv(dryrun) |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == "__main__": |
| 189 | + try: |
| 190 | + main(sys.argv[1:]) |
| 191 | + except KeyboardInterrupt, e: |
| 192 | + print >> sys.stderr, ("\n\nExiting on user cancel.") |
| 193 | + sys.exit(1) |
0 commit comments