Last active
December 21, 2015 14:19
-
-
Save fredrikbonander/6318840 to your computer and use it in GitHub Desktop.
Deploy script appengine with modules
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import re | |
import argparse | |
import os | |
import zipfile | |
import yaml | |
from subprocess import call | |
__author__ = 'broken' | |
DEFAULT_APP_ROOT = 'app/' | |
DEPLOY_YAML_FILES = [] | |
APP_YAML = None | |
APPLICATION_NAME = None | |
ORG_APPLICATION_NAME = None | |
def store_app_config(file_name, yaml_data): | |
global APP_YAML | |
global ORG_APPLICATION_NAME | |
APP_YAML = (file_name, yaml_data) | |
ORG_APPLICATION_NAME = yaml_data['application'] | |
if not APPLICATION_NAME: | |
global APPLICATION_NAME | |
APPLICATION_NAME = yaml_data['application'] | |
def load_yaml_files(app_root): | |
filenames = [] | |
(_, _, filenames) = os.walk(app_root).next() | |
for file_name in filenames: | |
if file_name.endswith('.yaml') and not file_name == 'app_config.yaml': | |
with open('%s%s' % (app_root, file_name), 'r') as f: | |
yaml_data = yaml.load(f) | |
if 'application' in yaml_data: | |
DEPLOY_YAML_FILES.append((file_name, yaml_data)) | |
if file_name == 'app.yaml': | |
store_app_config(file_name, yaml_data) | |
def update_application_in_yaml_files(app_root, application_name): | |
for file_name, yaml_data in DEPLOY_YAML_FILES: | |
with open('%s%s' % (app_root, file_name), 'w') as open_f: | |
yaml_data['application'] = application_name | |
open_f.write(yaml.dump(yaml_data, default_flow_style=False)) | |
def run(app_root='.'): | |
print 'Locating all yaml files' | |
load_yaml_files(app_root) | |
print 'Set application to: %s' % APP_YAML[1]['application'] | |
update_application_in_yaml_files(app_root, APPLICATION_NAME) | |
print 'Deploying %s' % APP_YAML[1]['application'] | |
call(['appcfg.py', 'update'] + [file_name for file_name, yaml_data in DEPLOY_YAML_FILES if file_name != 'dispatch.yaml']) | |
print 'Updating dispatch' | |
call(['appcfg.py', 'update_dispatch', app_root]) | |
print 'Updating indexes' | |
call(['appcfg.py', 'update_indexes', app_root]) | |
print 'Clean up' | |
update_application_in_yaml_files(app_root, ORG_APPLICATION_NAME) | |
parser = argparse.ArgumentParser(description='Deploy script for appengine modules.') | |
parser.add_argument('-a', '--app', action='store', type=str, help='App root', default=DEFAULT_APP_ROOT) | |
parser.add_argument('-n', '--name', action='store', type=str, help='Application name') | |
args = parser.parse_args() | |
if args.name: | |
global APPLICATION_NAME | |
APPLICATION_NAME = args.name | |
run(app_root=args.app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment