From 58f9109bd2a03e10d51c398fe1b7e020ab0a6252 Mon Sep 17 00:00:00 2001 From: Matthew Scragg Date: Thu, 18 Sep 2014 16:44:01 -0500 Subject: [PATCH] add dockerfile move cli functions in module add configure command --- docker/Dockerfile | 37 ++++++++++ docker/realms-wiki.sh | 7 ++ manage.py | 131 +--------------------------------- realms/cli.py | 144 ++++++++++++++++++++++++++++++++++++++ realms/config/__init__.py | 2 +- setup.py | 2 +- 6 files changed, 192 insertions(+), 131 deletions(-) create mode 100644 docker/Dockerfile create mode 100644 docker/realms-wiki.sh create mode 100644 realms/cli.py diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..7ea6b1b --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,37 @@ +FROM realms/base + +# Packages +RUN add-apt-repository -y ppa:chris-lea/node.js && apt-get update +RUN apt-get install -y python build-essential git libpcre3-dev python-software-properties \ + python-pip python-virtualenv python-dev pkg-config curl libxml2-dev libxslt1-dev zlib1g-dev \ + libffi-dev nodejs libyaml-dev + +# Realms Code +RUN cd /home/deploy && git clone https://github.com/scragg0x/realms-wiki + +# Bower stuff for frontend assets +RUN npm install -g bower +RUN bower --allow-root --config.cwd=/home/deploy/realms-wiki --config.directory=realms/static/vendor --config.interactive=false install + +# Virtualenv building +RUN virtualenv /home/deploy/realms-wiki/.venv +RUN /home/deploy/realms-wiki/.venv/bin/pip install /home/deploy/realms-wiki + +# Link to cli entry point +RUN ln -s /home/deploy/realms-wiki/.venv/bin/realms-wiki /usr/local/bin/realms-wiki && chmod +x /usr/local/bin/realms-wiki + +# Logging +RUN mkdir /var/log/realms-wiki && chown deploy.deploy /var/log/realms-wiki + +# Hand over to deploy user +RUN chown -R deploy.deploy /home/deploy + +# Upstart +RUN mkdir /etc/service/realms-wiki +ADD realms-wiki.sh /etc/service/realms-wiki/run +RUN chmod +x /etc/service/realms-wiki/run + +# Clear some fat +RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +EXPOSE 5000 diff --git a/docker/realms-wiki.sh b/docker/realms-wiki.sh new file mode 100644 index 0000000..cd21fec --- /dev/null +++ b/docker/realms-wiki.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +if [ "${REALMS_WIKI_CONFIG}" != "" ]; then + realms-wiki configure ${REALMS_WIKI_CONFIG} +fi + +exec /sbin/setuser deploy realms-wiki run >>/var/log/realms-wiki/realms-wiki.log 2>&1 \ No newline at end of file diff --git a/manage.py b/manage.py index 453a8d8..a567619 100644 --- a/manage.py +++ b/manage.py @@ -1,131 +1,4 @@ -from gevent import wsgi -from realms import config, app, cli, db -from realms.lib.util import random_string -import click - - -@cli.command() -@click.option('--site-title', - default=config.SITE_TITLE, - prompt='Enter site title.') -@click.option('--base_url', - default=config.BASE_URL, - prompt='Enter base URL.') -@click.option('--port', - default=config.PORT, - prompt='Enter port number.') -@click.option('--secret-key', - default=config.SECRET_KEY if config.SECRET_KEY != "CHANGE_ME" else random_string(64), - prompt='Enter secret key.') -@click.option('--wiki-path', - default=config.WIKI_PATH, - prompt='Where do you want to store wiki data?', - help='Wiki Directory (git repo)') -@click.option('--allow-anon', - default=config.ALLOW_ANON, - is_flag=True, - prompt='Allow anonymous edits?') -@click.option('--registration-enabled', - default=config.REGISTRATION_ENABLED, - is_flag=True, - prompt='Enable registration?') -@click.option('--cache-type', - default=config.CACHE_TYPE, - type=click.Choice([None, 'simple', 'redis', 'memcached']), - prompt='Cache type?') -@click.option('--db-uri', - default=config.DB_URI, - prompt='Database URI, Examples: http://goo.gl/RyW0cl') -@click.pass_context -def setup(ctx, **kw): - """ Start setup wizard - """ - conf = {} - - for k, v in kw.items(): - conf[k.upper()] = v - - config.update(conf) - - if conf['CACHE_TYPE'] == 'redis': - ctx.invoke(setup_redis) - elif conf['CACHE_TYPE'] == 'memcached': - ctx.invoke(setup_memcached) - - click.secho('Config saved to %s/config.json' % config.APP_PATH, fg='green') - click.secho('Type "realms-wiki run" to start server', fg='yellow') - - -@click.command() -@click.option('--cache-redis-host', - default=getattr(config, 'CACHE_REDIS_HOST', "127.0.0.1"), - prompt='Redis host') -@click.option('--cache-redis-port', - default=getattr(config, 'CACHE_REDIS_POST', 6379), - prompt='Redis port') -@click.option('--cache-redis-password', - default=getattr(config, 'CACHE_REDIS_PASSWORD', None), - prompt='Redis password') -@click.option('--cache-redis-db', - default=getattr(config, 'CACHE_REDIS_DB', 0), - prompt='Redis db') -def setup_redis(**kw): - conf = {} - - for k, v in kw.items(): - conf[k.upper()] = v - - config.update(conf) - - -@click.command() -@click.option('--cache-memcached-servers', - default=getattr(config, 'CACHE_MEMCACHED_SERVERS', ["127.0.0.1:11211"]), - type=click.STRING, - prompt='Memcached servers, separate with a space') -def setup_memcached(**kw): - conf = {} - - for k, v in kw.items(): - conf[k.upper()] = v - - config.update(conf) - - -@cli.command() -@click.option('--port', default=5000) -def dev(port): - """ Run development server - """ - click.secho("Starting development server", fg='green') - app.run(host="0.0.0.0", - port=port, - debug=True) - - -@cli.command() -def run(): - """ Run production server - """ - click.secho("Server started. Env: %s Port: %s" % (config.ENV, config.PORT), fg='green') - wsgi.WSGIServer(('', int(config.PORT)), app).serve_forever() - - -@cli.command() -def create_db(): - """ Creates DB tables - """ - click.echo("Creating all tables") - db.create_all() - - -@cli.command() -@click.confirmation_option(help='Are you sure you want to drop the db?') -def drop_db(): - """ Drops DB tables - """ - click.echo("Dropping all tables") - db.drop_all() +from realms.cli import main if __name__ == '__main__': - cli() + main() diff --git a/realms/cli.py b/realms/cli.py new file mode 100644 index 0000000..e3ba12a --- /dev/null +++ b/realms/cli.py @@ -0,0 +1,144 @@ +from gevent import wsgi +from realms import config, app, cli, db +from realms.lib.util import random_string +import click +import json + + +@cli.command() +@click.option('--site-title', + default=config.SITE_TITLE, + prompt='Enter site title.') +@click.option('--base_url', + default=config.BASE_URL, + prompt='Enter base URL.') +@click.option('--port', + default=config.PORT, + prompt='Enter port number.') +@click.option('--secret-key', + default=config.SECRET_KEY if config.SECRET_KEY != "CHANGE_ME" else random_string(64), + prompt='Enter secret key.') +@click.option('--wiki-path', + default=config.WIKI_PATH, + prompt='Where do you want to store wiki data?', + help='Wiki Directory (git repo)') +@click.option('--allow-anon', + default=config.ALLOW_ANON, + is_flag=True, + prompt='Allow anonymous edits?') +@click.option('--registration-enabled', + default=config.REGISTRATION_ENABLED, + is_flag=True, + prompt='Enable registration?') +@click.option('--cache-type', + default=config.CACHE_TYPE, + type=click.Choice([None, 'simple', 'redis', 'memcached']), + prompt='Cache type?') +@click.option('--db-uri', + default=config.DB_URI, + prompt='Database URI, Examples: http://goo.gl/RyW0cl') +@click.pass_context +def setup(ctx, **kw): + """ Start setup wizard + """ + conf = {} + + for k, v in kw.items(): + conf[k.upper()] = v + + config.update(conf) + + if conf['CACHE_TYPE'] == 'redis': + ctx.invoke(setup_redis) + elif conf['CACHE_TYPE'] == 'memcached': + ctx.invoke(setup_memcached) + + click.secho('Config saved to %s/config.json' % config.APP_PATH, fg='green') + click.secho('Type "realms-wiki run" to start server', fg='yellow') + + +@click.command() +@click.option('--cache-redis-host', + default=getattr(config, 'CACHE_REDIS_HOST', "127.0.0.1"), + prompt='Redis host') +@click.option('--cache-redis-port', + default=getattr(config, 'CACHE_REDIS_POST', 6379), + prompt='Redis port') +@click.option('--cache-redis-password', + default=getattr(config, 'CACHE_REDIS_PASSWORD', None), + prompt='Redis password') +@click.option('--cache-redis-db', + default=getattr(config, 'CACHE_REDIS_DB', 0), + prompt='Redis db') +def setup_redis(**kw): + conf = {} + + for k, v in kw.items(): + conf[k.upper()] = v + + config.update(conf) + + +@click.command() +@click.option('--cache-memcached-servers', + default=getattr(config, 'CACHE_MEMCACHED_SERVERS', ["127.0.0.1:11211"]), + type=click.STRING, + prompt='Memcached servers, separate with a space') +def setup_memcached(**kw): + conf = {} + + for k, v in kw.items(): + conf[k.upper()] = v + + config.update(conf) + + +@cli.command() +@click.argument('config_json') +def configure(config_json): + """ Set config.json, expects JSON encoded string + """ + try: + config.update(json.loads(config_json)) + except ValueError, e: + click.secho('Config value should be valid JSON', fg='red') + + +@cli.command() +@click.option('--port', default=5000) +def dev(port): + """ Run development server + """ + click.secho("Starting development server", fg='green') + app.run(host="0.0.0.0", + port=port, + debug=True) + + +@cli.command() +def run(): + """ Run production server + """ + click.secho("Server started. Env: %s Port: %s" % (config.ENV, config.PORT), fg='green') + wsgi.WSGIServer(('', int(config.PORT)), app).serve_forever() + + +@cli.command() +def create_db(): + """ Creates DB tables + """ + click.echo("Creating all tables") + db.create_all() + + +@cli.command() +@click.confirmation_option(help='Are you sure you want to drop the db?') +def drop_db(): + """ Drops DB tables + """ + click.echo("Dropping all tables") + db.drop_all() + + +def main(): + cli() \ No newline at end of file diff --git a/realms/config/__init__.py b/realms/config/__init__.py index 434212f..1c7fdb3 100644 --- a/realms/config/__init__.py +++ b/realms/config/__init__.py @@ -23,7 +23,7 @@ def read(): def save(conf): with open(os.path.join(APP_PATH, 'config.json'), 'w') as f: - f.write(json.dumps(conf, sort_keys=True, indent=4, separators=(',', ': '))) + f.write(json.dumps(conf, sort_keys=True, indent=4, separators=(',', ': ')).strip() + '\n') APP_PATH = os.path.abspath(os.path.dirname(__file__) + "/../..") USER_HOME = os.path.abspath(os.path.expanduser("~")) diff --git a/setup.py b/setup.py index 9f20489..ef539a0 100644 --- a/setup.py +++ b/setup.py @@ -34,5 +34,5 @@ setup(name='realms-wiki', classifiers=CLASSIFIERS, entry_points=''' [console_scripts] - realms-wiki=manage:cli + realms-wiki=realms.cli:main ''') \ No newline at end of file