cli and setuptools wip

This commit is contained in:
Matthew Scragg 2014-10-16 22:18:57 -05:00
parent 07852bdd98
commit 5985bff7d6
4 changed files with 46 additions and 37 deletions

1
.gitignore vendored
View file

@ -12,6 +12,7 @@ pidfile
config.py config.py
config.sls config.sls
config.json config.json
realms-wiki.json
realms/static/vendor realms/static/vendor
realms/static/assets/* realms/static/assets/*
/wiki.db /wiki.db

View file

@ -1 +1 @@
0.3.13 0.3.14

View file

@ -3,4 +3,4 @@
from realms.cli import cli from realms.cli import cli
if __name__ == '__main__': if __name__ == '__main__':
cli() cli()

View file

@ -8,6 +8,15 @@ import sys
import os import os
def check_su(f):
if not in_virtualenv() and not is_su():
# This does not account for people the have user level python installs
# that aren't virtual environments! Should be rare I think
red("This command requires root privileges, use sudo or run as root.")
sys.exit()
return f
def get_user(): def get_user():
for name in ('SUDO_USER', 'LOGNAME', 'USER', 'LNAME', 'USERNAME'): for name in ('SUDO_USER', 'LOGNAME', 'USER', 'LNAME', 'USERNAME'):
user = os.environ.get(name) user = os.environ.get(name)
@ -41,7 +50,20 @@ def module_exists(module_name):
return True return True
def green(s):
click.secho(s, fg='green')
def yellow(s):
click.secho(s, fg='yellow')
def red(s):
click.secho(s, fg='red')
@cli.command() @cli.command()
@check_su
@click.option('--site-title', @click.option('--site-title',
default=config.SITE_TITLE, default=config.SITE_TITLE,
prompt='Enter site title.') prompt='Enter site title.')
@ -56,7 +78,7 @@ def module_exists(module_name):
prompt='Enter secret key.') prompt='Enter secret key.')
@click.option('--wiki-path', @click.option('--wiki-path',
default=config.WIKI_PATH, default=config.WIKI_PATH,
prompt='Where do you want to store wiki data?', prompt='Enter wiki data directory.',
help='Wiki Directory (git repo)') help='Wiki Directory (git repo)')
@click.option('--allow-anon', @click.option('--allow-anon',
default=config.ALLOW_ANON, default=config.ALLOW_ANON,
@ -78,12 +100,6 @@ def setup(ctx, **kw):
""" Start setup wizard """ Start setup wizard
""" """
if not in_virtualenv() and not is_su():
# This does not account for people the have user level python installs
# that aren't virtual environments! Should be rare I think
click.secho("Setup requires root privileges, use sudo or run as root")
return
conf = {} conf = {}
for k, v in kw.items(): for k, v in kw.items():
@ -96,9 +112,9 @@ def setup(ctx, **kw):
elif conf['CACHE_TYPE'] == 'memcached': elif conf['CACHE_TYPE'] == 'memcached':
ctx.invoke(setup_memcached) ctx.invoke(setup_memcached)
click.secho('Config saved to %s' % conf_path, fg='green') green('Config saved to %s' % conf_path)
click.secho('Type "realms-wiki start" to start server', fg='yellow') yellow('Type "realms-wiki start" to start server')
click.secho('Type "realms-wiki dev" to start server in development mode', fg='yellow') yellow('Type "realms-wiki dev" to start server in development mode')
@click.command() @click.command()
@ -131,10 +147,6 @@ def get_prefix():
def get_pip(): def get_pip():
""" Get virtualenv path for pip """ Get virtualenv path for pip
""" """
if not in_virtualenv() and not is_su():
click.secho("This command requires root, use sudo or run as root")
return
if in_virtualenv(): if in_virtualenv():
return get_prefix() + '/bin/pip' return get_prefix() + '/bin/pip'
else: else:
@ -142,6 +154,7 @@ def get_pip():
@cli.command() @cli.command()
@check_su
@click.argument('cmd', nargs=-1) @click.argument('cmd', nargs=-1)
def pip(cmd): def pip(cmd):
""" Execute pip commands, useful for virtualenvs """ Execute pip commands, useful for virtualenvs
@ -180,6 +193,7 @@ def setup_memcached(**kw):
@cli.command() @cli.command()
@check_su
@click.option('--user', @click.option('--user',
default=get_user(), default=get_user(),
type=click.STRING, type=click.STRING,
@ -196,13 +210,6 @@ def setup_upstart(**kwargs):
""" Start upstart conf creation wizard """ Start upstart conf creation wizard
""" """
from realms.lib.util import upstart_script from realms.lib.util import upstart_script
import realms
print os.path.dirname(realms.__file__)
if not is_su():
click.secho("Please run this command as root or use sudo", fg='red')
return
if in_virtualenv(): if in_virtualenv():
app_dir = get_prefix() app_dir = get_prefix()
@ -219,10 +226,10 @@ def setup_upstart(**kwargs):
with open('/etc/init/realms-wiki.conf', 'w') as f: with open('/etc/init/realms-wiki.conf', 'w') as f:
f.write(upstart_script(**kwargs)) f.write(upstart_script(**kwargs))
click.secho('Wrote file to %s' % conf_file, fg='yellow') green('Wrote file to %s' % conf_file)
click.echo("Type 'sudo start realms-wiki' to start") green("Type 'sudo start realms-wiki' to start")
click.echo("Type 'sudo stop realms-wiki' to stop") green("Type 'sudo stop realms-wiki' to stop")
click.echo("Type 'sudo restart realms-wiki' to restart") green("Type 'sudo restart realms-wiki' to restart")
@cli.command() @cli.command()
@ -233,7 +240,7 @@ def configure(json_string):
try: try:
config.update(json.loads(json_string)) config.update(json.loads(json_string))
except ValueError, e: except ValueError, e:
click.secho('Config value should be valid JSON', fg='red') red('Config value should be valid JSON')
@cli.command() @cli.command()
@ -241,7 +248,7 @@ def configure(json_string):
def dev(port): def dev(port):
""" Run development server """ Run development server
""" """
click.secho("Starting development server", fg='green') green("Starting development server")
app.run(host="0.0.0.0", app.run(host="0.0.0.0",
port=port, port=port,
debug=True) debug=True)
@ -249,12 +256,12 @@ def dev(port):
def start_server(): def start_server():
if get_pid(): if get_pid():
click.echo("Server is already running") yellow("Server is already running")
return return
flags = '--daemon --pid %s' % config.PIDFILE flags = '--daemon --pid %s' % config.PIDFILE
click.secho("Server started. Port: %s" % config.PORT, fg='green') green("Server started. Port: %s" % config.PORT)
Popen('gunicorn realms:app -b 0.0.0.0:%s -k gevent %s' % Popen('gunicorn realms:app -b 0.0.0.0:%s -k gevent %s' %
(config.PORT, flags), shell=True, executable='/bin/bash') (config.PORT, flags), shell=True, executable='/bin/bash')
@ -263,9 +270,9 @@ def start_server():
def stop_server(): def stop_server():
pid = get_pid() pid = get_pid()
if not pid: if not pid:
click.echo("Server is not running") yellow("Server is not running")
else: else:
click.echo("Shutting down server") yellow("Shutting down server")
call(['kill', pid]) call(['kill', pid])
@ -304,16 +311,16 @@ def status():
""" """
pid = get_pid() pid = get_pid()
if not pid: if not pid:
click.echo("Server is not running") yellow("Server is not running")
else: else:
click.echo("Server is running PID: %s" % pid) green("Server is running PID: %s" % pid)
@cli.command() @cli.command()
def create_db(): def create_db():
""" Creates DB tables """ Creates DB tables
""" """
click.echo("Creating all tables") green("Creating all tables")
db.create_all() db.create_all()
@ -322,7 +329,7 @@ def create_db():
def drop_db(): def drop_db():
""" Drops DB tables """ Drops DB tables
""" """
click.echo("Dropping all tables") yellow("Dropping all tables")
db.drop_all() db.drop_all()
@ -346,5 +353,6 @@ def version():
with open('VERSION') as f: with open('VERSION') as f:
return f.read().strip() return f.read().strip()
if __name__ == '__main__': if __name__ == '__main__':
cli() cli()