This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 658262 - Add the update_site.py script, r=peterbe
The script is based on the one from Playdoh.
- Loading branch information
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Usage: update_site.py [options] | ||
Updates a server's sources, vendor libraries, packages CSS/JS | ||
assets, migrates the database, and other nifty deployment tasks. | ||
Options: | ||
-h, --help show this help message and exit | ||
-e ENVIRONMENT, --environment=ENVIRONMENT | ||
Type of environment. One of (prod|dev|stage) Example: | ||
update_site.py -e stage | ||
-v, --verbose Echo actions before taking them. | ||
""" | ||
|
||
import os | ||
import sys | ||
from textwrap import dedent | ||
from optparse import OptionParser | ||
|
||
# Constants | ||
PROJECT = 0 | ||
VENDOR = 1 | ||
|
||
ENV_BRANCH = { | ||
# 'environment': [PROJECT_BRANCH, VENDOR_BRANCH], | ||
'dev': ['develop', 'master'], | ||
'stage': ['develop', 'master'], | ||
'prod': ['master', 'master'], | ||
} | ||
|
||
GIT_PULL = "git pull -q origin %(branch)s" | ||
GIT_SUBMODULE = "git submodule update --init --recursive" | ||
|
||
EXEC = 'exec' | ||
CHDIR = 'chdir' | ||
|
||
|
||
def update_site(env, debug): | ||
"""Run through commands to update this site.""" | ||
error_updating = False | ||
here = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
project_branch = {'branch': ENV_BRANCH[env][PROJECT]} | ||
vendor_branch = {'branch': ENV_BRANCH[env][VENDOR]} | ||
|
||
commands = [ | ||
(CHDIR, here), | ||
(EXEC, GIT_PULL % project_branch), | ||
(EXEC, GIT_SUBMODULE), | ||
] | ||
|
||
commands += [ | ||
(CHDIR, os.path.join(here, 'vendor')), | ||
(EXEC, GIT_PULL % vendor_branch), | ||
(EXEC, GIT_SUBMODULE), | ||
] | ||
|
||
commands += [ | ||
(CHDIR, here), | ||
(EXEC, './vendor/src/schematic/schematic migrations/'), | ||
] | ||
|
||
for cmd, cmd_args in commands: | ||
if CHDIR == cmd: | ||
if debug: | ||
sys.stdout.write("cd %s\n" % cmd_args) | ||
os.chdir(cmd_args) | ||
elif EXEC == cmd: | ||
if debug: | ||
sys.stdout.write("%s\n" % cmd_args) | ||
if not 0 == os.system(cmd_args): | ||
error_updating = True | ||
break | ||
else: | ||
raise Exception("Unknown type of command %s" % cmd) | ||
|
||
if error_updating: | ||
sys.stderr.write("There was an error while updating. Please try again " | ||
"later. Aborting.\n") | ||
|
||
|
||
def main(): | ||
""" Handels command line args. """ | ||
debug = False | ||
usage = dedent("""\ | ||
%prog [options] | ||
Updates a server's sources, vendor libraries, packages CSS/JS | ||
assets, migrates the database, and other nifty deployment tasks. | ||
""".rstrip()) | ||
|
||
options = OptionParser(usage=usage) | ||
e_help = "Type of environment. One of (%s) Example: update_site.py \ | ||
-e stage" % '|'.join(ENV_BRANCH.keys()) | ||
options.add_option("-e", "--environment", help=e_help) | ||
options.add_option("-v", "--verbose", | ||
help="Echo actions before taking them.", | ||
action="store_true", dest="verbose") | ||
(opts, _) = options.parse_args() | ||
|
||
if opts.verbose: | ||
debug = True | ||
if opts.environment in ENV_BRANCH.keys(): | ||
update_site(opts.environment, debug) | ||
else: | ||
sys.stderr.write("Invalid environment!\n") | ||
options.print_help(sys.stderr) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |