Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ischneider
Created May 20, 2015 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ischneider/1e42bc923eab9c7a691f to your computer and use it in GitHub Desktop.
Save ischneider/1e42bc923eab9c7a691f to your computer and use it in GitHub Desktop.
build and run geoserver w/ different data directories
import os.path
import argparse
import os
import sys
import subprocess
MAVEN = os.getenv('MAVEN', 'mvn')
GEOSERVER_HOME = os.getenv('GEOSERVER_HOME', None)
GEOTOOLS_HOME = os.getenv('GEOTOOLS_HOME', None)
DATA_DIR_HOME = os.getenv('DATA_DIR_HOME', None)
GEOSERVER_BUILD_DIR = os.getenv('GEOSERVER_BUILD_DIR', None)
if not all([GEOSERVER_HOME, DATA_DIR_HOME, GEOSERVER_BUILD_DIR]):
print 'define env vars'
sys.exit(1)
def print_dir(directory, exit=False):
for d in sorted(os.listdir(directory)):
print d
if exit:
sys.exit(0)
def check_file(path, msg, directory):
if not os.path.exists(path):
print msg, "'%s'" % path, 'does not exist'
print 'possible choices are:'
print
print_dir(directory)
sys.exit(1)
def check_output(cmd, reason=None):
try:
return subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).strip()
except subprocess.CalledProcessError, ex:
if reason:
print 'command failed when', reason
print 'command:', ex.cmd
print '-- start output:'
print ex.output
print '-- end output'
sys.exit(1)
else:
raise ex
format_profiles = lambda args: ('-P%s' % args.profiles) if args.profiles else ''
chdir = lambda *paths: os.chdir(os.path.join(*paths))
def system(template, report_fail=True, **kwargs):
cmd = template % kwargs
print cmd
retval = os.system(cmd)
if report_fail and retval != 0:
print 'failure running', cmd
sys.exit(1)
def build_geoserver(args):
if args.list:
print_dir(GEOSERVER_BUILD_DIR, exit=True)
build_name = args.build_name
if build_name is None:
build_name = check_output('git rev-parse --abbrev-ref HEAD', 'checking for branch name')
if build_name == 'HEAD' and not args.build_name:
print 'you may be in detached HEAD state'
print 'please provide a name for the build'
sys.exit(1)
dest = os.path.join(GEOSERVER_BUILD_DIR, build_name)
if os.path.exists(dest) and not args.overwrite:
print 'build exists, provide -o or --overwrite option'
sys.exit(1)
profiles = format_profiles(args)
if not args.war_only:
chdir(GEOSERVER_HOME, 'src')
system('mvn -T 2C -nsu -o -DskipTests %(profiles)s clean install', profiles=profiles)
chdir(GEOSERVER_HOME, 'src', 'web', 'app')
system('mvn -nsu -o %(profiles)s clean war:exploded', profiles=profiles)
src = os.path.join(GEOSERVER_HOME, 'src', 'web', 'app', 'target', 'geoserver', '')
if not os.path.exists(dest):
os.makedirs(dest)
system('rsync -a --delete %(src)s %(dest)s', src=src, dest=dest)
def jetty_runner(args, datadir):
print args
def webapp_runner(args, datadir):
build_path = args.build_name
if not os.path.exists(build_path):
build_path = os.path.join(GEOSERVER_BUILD_DIR, build_path)
check_file(build_path, 'geoserver build', GEOSERVER_BUILD_DIR)
dirname = os.path.dirname(__file__)
JAVA_ENV = 'GEOSERVER_DATA_DIR="%s"' % datadir
JAVA_OPTS = '-XX:MaxPermSize=256m'
system('%(java_env)s java %(java_opts)s -jar %(jar_file)s --path /geoserver %(build)s',
report_fail=False,
java_env=JAVA_ENV,
java_opts=JAVA_OPTS,
jar_file=os.path.join(dirname, 'webapp-runner.jar'),
build=build_path
)
def maven_jetty(args, datadir):
MAVEN_OPTS = '-XX:MaxPermSize=256m ' \
'-DGEOSERVER_DATA_DIR="%s"' % datadir
CMD_OPTS = ''
if args.debug:
MAVEN_OPTS += ' -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8700,server=y,suspend=y'
if args.port:
CMD_OPTS += ' -Djetty.port=%s -Djetty.stopPort=%s' % (args.port, int(args.port) + 1000)
profiles = format_profiles(args)
chdir(GEOSERVER_HOME, 'src', 'web', 'app')
system('MAVEN_OPTS="%(env)s" %(mvn)s jetty:run ' \
'-DskipTests=true -e -nsu -o %(opts)s ' \
'%(profiles)s',
report_fail=False,
env=MAVEN_OPTS,
mvn=MAVEN,
opts=CMD_OPTS,
profiles=profiles
)
def run_geoserver(args):
datadir = args.datadir
if not os.path.exists(datadir):
datadir = os.path.join(DATA_DIR_HOME, datadir)
check_file(datadir, 'datadir', DATA_DIR_HOME)
if args.build_name:
if args.build:
build_geoserver(args)
webapp_runner(args, datadir)
else:
maven_jetty(args, datadir)
def data_dir(args):
if args.list:
print_dir(DATA_DIR_HOME, exit=True)
if not args.datadir:
print 'must provide a datadir destination name'
sys.exit(1)
src = args.src
if not os.path.exists(src):
src = os.path.join(GEOSERVER_HOME, 'data', args.src, '')
check_file(src, 'source data directory', os.path.join(GEOSERVER_HOME, 'data'))
dest = os.path.join(DATA_DIR_HOME, args.datadir, '')
print 'creating/resetting %s' % os.path.abspath(dest)
os.system('rsync -a --delete %s %s' % (src, dest))
parser = argparse.ArgumentParser(prog='geoserver', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
subparsers = parser.add_subparsers(help='sub-command help')
common_build_parser = argparse.ArgumentParser(add_help=False)
common_build_parser.add_argument('-n', '--build-name', help='build name, defaults to branch')
common_build_parser.add_argument('-P', '--profiles', help='what profiles(modules) to enable')
common_build_parser.add_argument('-w', '--war-only', help='only assemble exploded war', action='store_true')
run_parser = subparsers.add_parser('run', help='run geoserver', parents=[common_build_parser])
run_parser.add_argument('datadir', help='geoserver data dir')
run_parser.add_argument('-b', '--build', help='if set, build first', action='store_true')
run_parser.add_argument('-p', '--port', help='what port to run on')
run_parser.add_argument('-d', '--debug', help='enable debugging', action='store_true')
run_parser.set_defaults(func=run_geoserver)
datadir_parser = subparsers.add_parser('datadir', help='new clean datadir')
datadir_parser.add_argument('datadir', help='destination data dir name', nargs='?')
datadir_parser.add_argument('-l', '--list', help='if set, list directories and exit', action='store_true')
datadir_parser.add_argument('-s', '--src', help='data dir to copy', default='minimal')
datadir_parser.set_defaults(func=data_dir)
build_parser = subparsers.add_parser('build', help='build geoserver exploded war', parents=[common_build_parser])
build_parser.add_argument('-l', '--list', help='if set, list builds and exit', action='store_true')
build_parser.add_argument('-o', '--overwrite', help='allow overwriting existing build', action='store_true')
build_parser.set_defaults(func=build_geoserver)
args = sys.argv
if len(args) == 1:
args = sys.argv + ['--help',]
args = parser.parse_args(args[1:])
args.func(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment