2015-07-22 20:01:49 +03:00
|
|
|
from realms import config, create_app, db, __version__, flask_cli as cli
|
2014-11-11 19:35:09 +02:00
|
|
|
from realms.lib.util import random_string, in_virtualenv, green, yellow, red
|
2014-10-17 00:54:45 +03:00
|
|
|
from subprocess import call, Popen
|
|
|
|
from multiprocessing import cpu_count
|
|
|
|
import click
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
import os
|
2014-10-22 23:38:32 +03:00
|
|
|
import pip
|
2014-10-24 02:58:58 +03:00
|
|
|
import time
|
2014-11-11 22:48:11 +02:00
|
|
|
import subprocess
|
2014-10-17 00:54:45 +03:00
|
|
|
|
2014-10-24 02:58:58 +03:00
|
|
|
# called to discover commands in modules
|
|
|
|
app = create_app()
|
2014-10-22 00:06:27 +03:00
|
|
|
|
2014-10-17 00:54:45 +03:00
|
|
|
def get_user():
|
|
|
|
for name in ('SUDO_USER', 'LOGNAME', 'USER', 'LNAME', 'USERNAME'):
|
|
|
|
user = os.environ.get(name)
|
|
|
|
if user:
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def get_pid():
|
|
|
|
try:
|
|
|
|
with file(config.PIDFILE) as f:
|
2014-10-24 02:58:58 +03:00
|
|
|
return f.read().strip()
|
2014-10-17 00:54:45 +03:00
|
|
|
except IOError:
|
2014-10-24 02:58:58 +03:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def is_running(pid):
|
|
|
|
if not pid:
|
|
|
|
return False
|
|
|
|
|
|
|
|
pid = int(pid)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError:
|
2014-10-17 00:54:45 +03:00
|
|
|
return False
|
|
|
|
|
2014-10-24 02:58:58 +03:00
|
|
|
return True
|
|
|
|
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
def module_exists(module_name):
|
|
|
|
try:
|
|
|
|
__import__(module_name)
|
|
|
|
except ImportError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@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,
|
2014-10-17 06:18:57 +03:00
|
|
|
prompt='Enter wiki data directory.',
|
2014-10-17 00:54:45 +03:00
|
|
|
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?')
|
2014-11-17 19:25:26 +02:00
|
|
|
@click.option('--search-type',
|
|
|
|
default=config.CACHE_TYPE,
|
|
|
|
type=click.Choice(['simple', 'elasticsearch']),
|
|
|
|
prompt='Search type?')
|
2014-10-17 00:54:45 +03:00
|
|
|
@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
|
|
|
|
"""
|
|
|
|
|
2014-10-24 02:58:58 +03:00
|
|
|
try:
|
|
|
|
os.mkdir('/etc/realms-wiki')
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2014-10-17 00:54:45 +03:00
|
|
|
conf = {}
|
|
|
|
|
|
|
|
for k, v in kw.items():
|
|
|
|
conf[k.upper()] = v
|
|
|
|
|
|
|
|
conf_path = config.update(conf)
|
|
|
|
|
|
|
|
if conf['CACHE_TYPE'] == 'redis':
|
|
|
|
ctx.invoke(setup_redis)
|
|
|
|
elif conf['CACHE_TYPE'] == 'memcached':
|
|
|
|
ctx.invoke(setup_memcached)
|
|
|
|
|
2014-11-17 19:25:26 +02:00
|
|
|
if conf['SEARCH_TYPE'] == 'elasticsearch':
|
|
|
|
ctx.invoke(setup_elasticsearch)
|
|
|
|
|
2014-10-17 06:18:57 +03:00
|
|
|
green('Config saved to %s' % conf_path)
|
2014-10-23 01:16:55 +03:00
|
|
|
|
|
|
|
if not conf_path.startswith('/etc/realms-wiki'):
|
2014-11-11 19:35:09 +02:00
|
|
|
yellow('Note: You can move file to /etc/realms-wiki/realms-wiki.json')
|
2014-10-23 01:16:55 +03:00
|
|
|
click.echo()
|
|
|
|
|
2014-10-17 06:18:57 +03:00
|
|
|
yellow('Type "realms-wiki start" to start server')
|
|
|
|
yellow('Type "realms-wiki dev" to start server in development mode')
|
2014-10-23 01:16:55 +03:00
|
|
|
yellow('Full usage: realms-wiki --help')
|
2014-10-17 00:54:45 +03:00
|
|
|
|
2014-10-24 02:58:58 +03:00
|
|
|
|
2014-10-17 00:54:45 +03:00
|
|
|
@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)
|
|
|
|
install_redis()
|
|
|
|
|
|
|
|
|
2014-11-17 19:25:26 +02:00
|
|
|
@click.command()
|
|
|
|
@click.option('--elasticsearch-url',
|
|
|
|
default=getattr(config, 'ELASTICSEARCH_URL', 'http://127.0.0.1:9200'),
|
|
|
|
prompt='Elasticsearch URL')
|
|
|
|
def setup_elasticsearch(**kw):
|
|
|
|
conf = {}
|
|
|
|
|
|
|
|
for k, v in kw.items():
|
|
|
|
conf[k.upper()] = v
|
|
|
|
|
|
|
|
config.update(conf)
|
|
|
|
|
|
|
|
|
2014-10-17 00:54:45 +03:00
|
|
|
def get_prefix():
|
|
|
|
return sys.prefix
|
|
|
|
|
|
|
|
|
2014-10-22 23:38:32 +03:00
|
|
|
@cli.command(name='pip')
|
2014-10-17 00:54:45 +03:00
|
|
|
@click.argument('cmd', nargs=-1)
|
2014-10-22 23:38:32 +03:00
|
|
|
def pip_(cmd):
|
2014-10-17 00:54:45 +03:00
|
|
|
""" Execute pip commands, useful for virtualenvs
|
|
|
|
"""
|
2014-10-22 23:38:32 +03:00
|
|
|
pip.main(cmd)
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def install_redis():
|
2014-10-22 23:38:32 +03:00
|
|
|
pip.main(['install', 'redis'])
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def install_mysql():
|
2014-10-22 23:38:32 +03:00
|
|
|
pip.main(['install', 'MySQL-Python'])
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def install_postgres():
|
2014-10-22 23:38:32 +03:00
|
|
|
pip.main(['install', 'psycopg2'])
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
2014-10-24 02:58:58 +03:00
|
|
|
def install_crate():
|
|
|
|
pip.main(['install', 'crate'])
|
|
|
|
|
|
|
|
|
2014-10-17 00:54:45 +03:00
|
|
|
def install_memcached():
|
2014-10-22 23:38:32 +03:00
|
|
|
pip.main(['install', 'python-memcached'])
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@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('--user',
|
|
|
|
default=get_user(),
|
|
|
|
type=click.STRING,
|
|
|
|
prompt='Run as which user? (it must exist)')
|
|
|
|
@click.option('--port',
|
|
|
|
default=config.PORT,
|
|
|
|
type=click.INT,
|
|
|
|
prompt='What port to listen on?')
|
|
|
|
@click.option('--workers',
|
|
|
|
default=cpu_count() * 2 + 1,
|
|
|
|
type=click.INT,
|
|
|
|
prompt="Number of workers? (defaults to ncpu*2+1)")
|
|
|
|
def setup_upstart(**kwargs):
|
|
|
|
""" Start upstart conf creation wizard
|
|
|
|
"""
|
|
|
|
from realms.lib.util import upstart_script
|
|
|
|
|
|
|
|
if in_virtualenv():
|
|
|
|
app_dir = get_prefix()
|
|
|
|
path = '/'.join(sys.executable.split('/')[:-1])
|
|
|
|
else:
|
|
|
|
# Assumed root install, not sure if this matters?
|
|
|
|
app_dir = '/'
|
|
|
|
path = None
|
|
|
|
|
|
|
|
kwargs.update(dict(app_dir=app_dir, path=path))
|
|
|
|
|
|
|
|
conf_file = '/etc/init/realms-wiki.conf'
|
2014-10-23 01:16:55 +03:00
|
|
|
script = upstart_script(**kwargs)
|
2014-10-17 00:54:45 +03:00
|
|
|
|
2014-10-23 01:16:55 +03:00
|
|
|
try:
|
|
|
|
with open(conf_file, 'w') as f:
|
|
|
|
f.write(script)
|
|
|
|
green('Wrote file to %s' % conf_file)
|
|
|
|
except IOError:
|
|
|
|
with open('/tmp/realms-wiki.conf', 'w') as f:
|
|
|
|
f.write(script)
|
|
|
|
yellow("Wrote file to /tmp/realms-wiki.conf, to install type:")
|
|
|
|
yellow("sudo mv /tmp/realms-wiki.conf /etc/init/realms-wiki.conf")
|
|
|
|
|
|
|
|
click.echo()
|
|
|
|
click.echo("Upstart usage:")
|
|
|
|
green("sudo start realms-wiki")
|
|
|
|
green("sudo stop realms-wiki")
|
|
|
|
green("sudo restart realms-wiki")
|
|
|
|
green("sudo status realms-wiki")
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.argument('json_string')
|
|
|
|
def configure(json_string):
|
2014-10-24 02:58:58 +03:00
|
|
|
""" Set config, expects JSON encoded string
|
2014-10-17 00:54:45 +03:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
config.update(json.loads(json_string))
|
|
|
|
except ValueError, e:
|
2014-10-17 06:18:57 +03:00
|
|
|
red('Config value should be valid JSON')
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
2014-10-24 02:58:58 +03:00
|
|
|
@click.option('--port', default=config.PORT)
|
2014-10-17 00:54:45 +03:00
|
|
|
def dev(port):
|
|
|
|
""" Run development server
|
|
|
|
"""
|
2014-10-17 06:18:57 +03:00
|
|
|
green("Starting development server")
|
2014-10-24 02:58:58 +03:00
|
|
|
|
|
|
|
config_path = config.get_path()
|
|
|
|
if config_path:
|
|
|
|
green("Using config: %s" % config_path)
|
|
|
|
else:
|
|
|
|
yellow("Using default configuration")
|
|
|
|
|
2014-10-22 23:38:32 +03:00
|
|
|
create_app().run(host="0.0.0.0",
|
|
|
|
port=port,
|
|
|
|
debug=True)
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def start_server():
|
2014-10-24 02:58:58 +03:00
|
|
|
if is_running(get_pid()):
|
2014-10-17 06:18:57 +03:00
|
|
|
yellow("Server is already running")
|
2014-10-17 00:54:45 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
flags = '--daemon --pid %s' % config.PIDFILE
|
|
|
|
|
2014-10-17 06:18:57 +03:00
|
|
|
green("Server started. Port: %s" % config.PORT)
|
2014-10-17 00:54:45 +03:00
|
|
|
|
2014-10-24 02:58:58 +03:00
|
|
|
config_path = config.get_path()
|
|
|
|
if config_path:
|
|
|
|
green("Using config: %s" % config_path)
|
|
|
|
else:
|
|
|
|
yellow("Using default configuration")
|
|
|
|
|
2014-10-22 00:06:27 +03:00
|
|
|
Popen("gunicorn 'realms:create_app()' -b 0.0.0.0:%s -k gevent %s" %
|
2014-10-22 23:38:32 +03:00
|
|
|
(config.PORT, flags), shell=True, executable='/bin/bash')
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def stop_server():
|
|
|
|
pid = get_pid()
|
2014-10-24 02:58:58 +03:00
|
|
|
if not is_running(pid):
|
2014-10-17 06:18:57 +03:00
|
|
|
yellow("Server is not running")
|
2014-10-17 00:54:45 +03:00
|
|
|
else:
|
2014-10-17 06:18:57 +03:00
|
|
|
yellow("Shutting down server")
|
2014-10-17 00:54:45 +03:00
|
|
|
call(['kill', pid])
|
2014-10-24 02:58:58 +03:00
|
|
|
while is_running(pid):
|
|
|
|
time.sleep(1)
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def run():
|
|
|
|
""" Run production server (alias for start)
|
|
|
|
"""
|
|
|
|
start_server()
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def start():
|
|
|
|
""" Run server daemon
|
|
|
|
"""
|
|
|
|
start_server()
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def stop():
|
|
|
|
""" Stop server
|
|
|
|
"""
|
|
|
|
stop_server()
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def restart():
|
|
|
|
""" Restart server
|
|
|
|
"""
|
|
|
|
stop_server()
|
|
|
|
start_server()
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def status():
|
|
|
|
""" Get server status
|
|
|
|
"""
|
2014-10-29 22:06:54 +02:00
|
|
|
pid = get_pid()
|
|
|
|
if not is_running(pid):
|
2014-10-17 06:18:57 +03:00
|
|
|
yellow("Server is not running")
|
2014-10-17 00:54:45 +03:00
|
|
|
else:
|
2014-10-17 06:18:57 +03:00
|
|
|
green("Server is running PID: %s" % pid)
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def create_db():
|
|
|
|
""" Creates DB tables
|
|
|
|
"""
|
2014-10-17 06:18:57 +03:00
|
|
|
green("Creating all tables")
|
2014-10-31 00:59:19 +02:00
|
|
|
with app.app_context():
|
2014-11-11 19:35:09 +02:00
|
|
|
green('DB_URI: %s' % app.config.get('DB_URI'))
|
|
|
|
db.metadata.create_all(db.get_engine(app))
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.confirmation_option(help='Are you sure you want to drop the db?')
|
|
|
|
def drop_db():
|
|
|
|
""" Drops DB tables
|
|
|
|
"""
|
2014-10-17 06:18:57 +03:00
|
|
|
yellow("Dropping all tables")
|
2014-10-31 00:59:19 +02:00
|
|
|
with app.app_context():
|
2014-11-11 19:35:09 +02:00
|
|
|
db.metadata.drop_all(db.get_engine(app))
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def test():
|
|
|
|
""" Run tests
|
|
|
|
"""
|
2014-10-22 23:38:32 +03:00
|
|
|
for mod in [('flask.ext.testing', 'Flask-Testing'), ('nose', 'nose'), ('blinker', 'blinker')]:
|
2014-10-17 00:54:45 +03:00
|
|
|
if not module_exists(mod[0]):
|
2014-10-22 23:38:32 +03:00
|
|
|
pip.main(['install', mod[1]])
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
nosetests = get_prefix() + "/bin/nosetests" if in_virtualenv() else "nosetests"
|
|
|
|
|
2014-10-22 23:38:32 +03:00
|
|
|
call([nosetests, 'realms'])
|
2014-10-17 00:54:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def version():
|
|
|
|
""" Output version
|
|
|
|
"""
|
2014-10-22 23:38:32 +03:00
|
|
|
green(__version__)
|
2014-10-17 00:54:45 +03:00
|
|
|
|
2014-10-17 06:18:57 +03:00
|
|
|
|
2014-11-11 22:48:11 +02:00
|
|
|
@cli.command(add_help_option=False)
|
|
|
|
def deploy():
|
|
|
|
""" Deploy to PyPI and docker hub
|
|
|
|
"""
|
|
|
|
call("python setup.py sdist upload", shell=True)
|
|
|
|
call("sudo docker build --no-cache -t realms/realms-wiki %s/docker" % app.config['APP_PATH'], shell=True)
|
|
|
|
id_ = json.loads(Popen("sudo docker inspect realms/realms-wiki".split(), stdout=subprocess.PIPE).communicate()[0])[0]['Id']
|
|
|
|
call("sudo docker tag %s realms/realms-wiki:%s" % (id_, __version__), shell=True)
|
|
|
|
call("sudo docker push realms/realms-wiki", shell=True)
|
|
|
|
|
2014-10-17 00:54:45 +03:00
|
|
|
if __name__ == '__main__':
|
2014-11-17 19:25:26 +02:00
|
|
|
cli()
|