add dockerfile

move cli functions in module
add configure command
This commit is contained in:
Matthew Scragg 2014-09-18 16:44:01 -05:00
parent d94855e3c5
commit 58f9109bd2
6 changed files with 192 additions and 131 deletions

37
docker/Dockerfile Normal file
View file

@ -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

7
docker/realms-wiki.sh Normal file
View file

@ -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

131
manage.py
View file

@ -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()

144
realms/cli.py Normal file
View file

@ -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()

View file

@ -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("~"))

View file

@ -34,5 +34,5 @@ setup(name='realms-wiki',
classifiers=CLASSIFIERS,
entry_points='''
[console_scripts]
realms-wiki=manage:cli
realms-wiki=realms.cli:main
''')