Last active
December 15, 2015 16:39
-
-
Save owad/5291122 to your computer and use it in GitHub Desktop.
Simple script which will update_indexes on your GAE app if there are any NoIndexErrors in your logs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from datetime import datetime | |
PATTERNS = [' - kind', | |
' -', | |
' properties:', | |
' direction:'] | |
LOG_FILE_NAME = 'gae.log' | |
INDEX_FILE = 'index.yaml' | |
NUM_DAYS = 1 | |
if len(sys.argv) > 1: | |
NUM_DAYS = sys.argv[1] | |
def get_app_id_and_version(): | |
f = open('app.yaml', 'r') | |
app_id = None | |
ver = None | |
row = f.readline() | |
while row: | |
if row.startswith('application:'): | |
app_id = row.split(':')[1].strip() | |
if row.startswith('version:'): | |
ver = row.split(':')[1].strip() | |
row = f.readline() | |
if not app_id: | |
raise Exception('Application not defined in your app.yaml file') | |
if not ver: | |
raise Exception('Version not defined in your app.yaml file') | |
return (app_id, ver) | |
def build_indexes(log_file=LOG_FILE_NAME): | |
f = open(log_file, 'r') | |
data = f.readlines() | |
data = [d.strip('\t:') for d in data] | |
lines = ['indexes:', '\n'] | |
for a in data: | |
for i, p in enumerate(PATTERNS): | |
if a.startswith(p): | |
if i == 0: | |
lines.append('\n') | |
lines.append(a[1:]) | |
if len(lines) == 2: | |
return None | |
return lines | |
def move_original_indexes(): | |
os.system('mv %s %s_old' % (INDEX_FILE, INDEX_FILE)) | |
def write_lines(lines): | |
f = open(INDEX_FILE, 'w') | |
f.writelines(lines) | |
f.close() | |
def refresh_logs(): | |
print 'Downloading errored indexes...' | |
app_id, ver = get_app_id_and_version() | |
os.system('appcfg.py request_logs -A %s -V %s --oauth2 --severity=3 %s --num_days=%s' % (app_id, ver, LOG_FILE_NAME, NUM_DAYS)) | |
def update_indexes(): | |
os.system('appcfg.py update_indexes .') | |
built_file = 'index_update_%s.yaml' % datetime.now().strftime('%Y-%m-%d_%H-%M-%S') | |
os.system('mv index.yaml %s' % built_file) | |
print 'Built index moved to %s' % built_file | |
os.system('mv %s_old %s' % (INDEX_FILE, INDEX_FILE)) | |
def run(): | |
refresh_logs() | |
index_lines = build_indexes() | |
if index_lines: | |
move_original_indexes() | |
write_lines(index_lines) | |
update_indexes() | |
else: | |
print 'No indexes to build' | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment