cli adjustments
This commit is contained in:
parent
03b030e67c
commit
d2171f9498
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import pip
|
|
||||||
from realms.cli import cli
|
from realms.commands import cli
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print
|
print
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from realms import config, create_app, db, cli as cli_, __version__
|
from realms import config, create_app, db, __version__, cli
|
||||||
from realms.lib.util import is_su, random_string, in_virtualenv, green, yellow, red
|
from realms.lib.util import is_su, random_string, in_virtualenv, green, yellow, red
|
||||||
from subprocess import call, Popen
|
from subprocess import call, Popen
|
||||||
from multiprocessing import cpu_count
|
from multiprocessing import cpu_count
|
||||||
|
@ -7,29 +7,10 @@ import json
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import pip
|
import pip
|
||||||
|
import time
|
||||||
|
|
||||||
|
# called to discover commands in modules
|
||||||
def print_version(ctx, param, value):
|
app = create_app()
|
||||||
if not value or ctx.resilient_parsing:
|
|
||||||
return
|
|
||||||
green(__version__)
|
|
||||||
ctx.exit()
|
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
|
||||||
@click.option('--version', is_flag=True, callback=print_version,
|
|
||||||
expose_value=False, is_eager=True)
|
|
||||||
@click.pass_context
|
|
||||||
def cli(ctx):
|
|
||||||
# This could probably done better
|
|
||||||
if ctx.invoked_subcommand in ['setup', 'pip']:
|
|
||||||
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()
|
|
||||||
|
|
||||||
cli.add_command(cli_)
|
|
||||||
|
|
||||||
|
|
||||||
def get_user():
|
def get_user():
|
||||||
|
@ -42,11 +23,24 @@ def get_user():
|
||||||
def get_pid():
|
def get_pid():
|
||||||
try:
|
try:
|
||||||
with file(config.PIDFILE) as f:
|
with file(config.PIDFILE) as f:
|
||||||
pid = f.read().strip()
|
return f.read().strip()
|
||||||
return pid if pid and int(pid) > 0 and not call(['kill', '-s', '0', pid]) else False
|
|
||||||
except IOError:
|
except IOError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def is_running(pid):
|
||||||
|
if not pid:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
pid = int(pid)
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.kill(pid, 0)
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def module_exists(module_name):
|
def module_exists(module_name):
|
||||||
try:
|
try:
|
||||||
|
@ -94,6 +88,11 @@ def setup(ctx, **kw):
|
||||||
""" Start setup wizard
|
""" Start setup wizard
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.mkdir('/etc/realms-wiki')
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
conf = {}
|
conf = {}
|
||||||
|
|
||||||
for k, v in kw.items():
|
for k, v in kw.items():
|
||||||
|
@ -116,6 +115,7 @@ def setup(ctx, **kw):
|
||||||
yellow('Type "realms-wiki dev" to start server in development mode')
|
yellow('Type "realms-wiki dev" to start server in development mode')
|
||||||
yellow('Full usage: realms-wiki --help')
|
yellow('Full usage: realms-wiki --help')
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option('--cache-redis-host',
|
@click.option('--cache-redis-host',
|
||||||
default=getattr(config, 'CACHE_REDIS_HOST', "127.0.0.1"),
|
default=getattr(config, 'CACHE_REDIS_HOST', "127.0.0.1"),
|
||||||
|
@ -163,6 +163,10 @@ def install_postgres():
|
||||||
pip.main(['install', 'psycopg2'])
|
pip.main(['install', 'psycopg2'])
|
||||||
|
|
||||||
|
|
||||||
|
def install_crate():
|
||||||
|
pip.main(['install', 'crate'])
|
||||||
|
|
||||||
|
|
||||||
def install_memcached():
|
def install_memcached():
|
||||||
pip.main(['install', 'python-memcached'])
|
pip.main(['install', 'python-memcached'])
|
||||||
|
|
||||||
|
@ -233,7 +237,7 @@ def setup_upstart(**kwargs):
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument('json_string')
|
@click.argument('json_string')
|
||||||
def configure(json_string):
|
def configure(json_string):
|
||||||
""" Set config.json, expects JSON encoded string
|
""" Set config, expects JSON encoded string
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
config.update(json.loads(json_string))
|
config.update(json.loads(json_string))
|
||||||
|
@ -242,18 +246,25 @@ def configure(json_string):
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option('--port', default=5000)
|
@click.option('--port', default=config.PORT)
|
||||||
def dev(port):
|
def dev(port):
|
||||||
""" Run development server
|
""" Run development server
|
||||||
"""
|
"""
|
||||||
green("Starting development server")
|
green("Starting development server")
|
||||||
|
|
||||||
|
config_path = config.get_path()
|
||||||
|
if config_path:
|
||||||
|
green("Using config: %s" % config_path)
|
||||||
|
else:
|
||||||
|
yellow("Using default configuration")
|
||||||
|
|
||||||
create_app().run(host="0.0.0.0",
|
create_app().run(host="0.0.0.0",
|
||||||
port=port,
|
port=port,
|
||||||
debug=True)
|
debug=True)
|
||||||
|
|
||||||
|
|
||||||
def start_server():
|
def start_server():
|
||||||
if get_pid():
|
if is_running(get_pid()):
|
||||||
yellow("Server is already running")
|
yellow("Server is already running")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -261,17 +272,25 @@ def start_server():
|
||||||
|
|
||||||
green("Server started. Port: %s" % config.PORT)
|
green("Server started. Port: %s" % config.PORT)
|
||||||
|
|
||||||
|
config_path = config.get_path()
|
||||||
|
if config_path:
|
||||||
|
green("Using config: %s" % config_path)
|
||||||
|
else:
|
||||||
|
yellow("Using default configuration")
|
||||||
|
|
||||||
Popen("gunicorn 'realms:create_app()' -b 0.0.0.0:%s -k gevent %s" %
|
Popen("gunicorn 'realms:create_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')
|
||||||
|
|
||||||
|
|
||||||
def stop_server():
|
def stop_server():
|
||||||
pid = get_pid()
|
pid = get_pid()
|
||||||
if not pid:
|
if not is_running(pid):
|
||||||
yellow("Server is not running")
|
yellow("Server is not running")
|
||||||
else:
|
else:
|
||||||
yellow("Shutting down server")
|
yellow("Shutting down server")
|
||||||
call(['kill', pid])
|
call(['kill', pid])
|
||||||
|
while is_running(pid):
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
@ -307,7 +326,7 @@ def restart():
|
||||||
def status():
|
def status():
|
||||||
""" Get server status
|
""" Get server status
|
||||||
"""
|
"""
|
||||||
pid = get_pid()
|
pid = is_running(get_pid())
|
||||||
if not pid:
|
if not pid:
|
||||||
yellow("Server is not running")
|
yellow("Server is not running")
|
||||||
else:
|
else:
|
|
@ -16,15 +16,9 @@ def read():
|
||||||
if k.startswith('REALMS_'):
|
if k.startswith('REALMS_'):
|
||||||
conf[k[7:]] = v
|
conf[k[7:]] = v
|
||||||
|
|
||||||
for loc in os.curdir, os.path.expanduser("~"), "/etc/realms-wiki", os.environ.get("REALMS_WIKI_CONF"):
|
loc = get_path()
|
||||||
try:
|
with open(loc) as f:
|
||||||
if not loc:
|
conf.update(json.load(f))
|
||||||
continue
|
|
||||||
with open(os.path.join(loc, "realms-wiki.json")) as f:
|
|
||||||
conf.update(json.load(f))
|
|
||||||
break
|
|
||||||
except IOError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
for k in ['APP_PATH', 'USER_HOME']:
|
for k in ['APP_PATH', 'USER_HOME']:
|
||||||
if k in conf:
|
if k in conf:
|
||||||
|
@ -34,13 +28,35 @@ def read():
|
||||||
|
|
||||||
|
|
||||||
def save(conf):
|
def save(conf):
|
||||||
|
loc = get_path(check_write=True)
|
||||||
|
with open(loc, 'w') as f:
|
||||||
|
f.write(json.dumps(conf, sort_keys=True, indent=4, separators=(',', ': ')).strip() + '\n')
|
||||||
|
return loc
|
||||||
|
|
||||||
|
|
||||||
|
def get_path(check_write=False):
|
||||||
|
"""Find config path
|
||||||
|
"""
|
||||||
for loc in "/etc/realms-wiki", os.path.expanduser("~"), os.curdir:
|
for loc in "/etc/realms-wiki", os.path.expanduser("~"), os.curdir:
|
||||||
try:
|
if not loc:
|
||||||
with open(os.path.join(loc, 'realms-wiki.json'), 'w') as f:
|
continue
|
||||||
f.write(json.dumps(conf, sort_keys=True, indent=4, separators=(',', ': ')).strip() + '\n')
|
path = os.path.join(loc, "realms-wiki.json")
|
||||||
return os.path.join(loc, 'realms-wiki.json')
|
if os.path.isfile(path):
|
||||||
except IOError:
|
# file exists
|
||||||
pass
|
if not check_write:
|
||||||
|
# Don't care if I can write
|
||||||
|
return path
|
||||||
|
|
||||||
|
if os.access(path, os.W_OK):
|
||||||
|
# Has write access, ok!
|
||||||
|
return path
|
||||||
|
elif os.path.isdir(loc) and check_write:
|
||||||
|
# dir exists file doesn't
|
||||||
|
if os.access(loc, os.W_OK):
|
||||||
|
# can write file
|
||||||
|
return path
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
APP_PATH = os.path.abspath(os.path.dirname(__file__) + "/../..")
|
APP_PATH = os.path.abspath(os.path.dirname(__file__) + "/../..")
|
||||||
USER_HOME = os.path.abspath(os.path.expanduser("~"))
|
USER_HOME = os.path.abspath(os.path.expanduser("~"))
|
||||||
|
@ -63,6 +79,7 @@ DB_URI = 'sqlite:////tmp/wiki.db'
|
||||||
# DB_URI = 'mysql://scott:tiger@localhost/mydatabase'
|
# DB_URI = 'mysql://scott:tiger@localhost/mydatabase'
|
||||||
# DB_URI = 'postgresql://scott:tiger@localhost/mydatabase'
|
# DB_URI = 'postgresql://scott:tiger@localhost/mydatabase'
|
||||||
# DB_URI = 'oracle://scott:tiger@127.0.0.1:1521/sidname'
|
# DB_URI = 'oracle://scott:tiger@127.0.0.1:1521/sidname'
|
||||||
|
# DB_URI = 'crate://'
|
||||||
|
|
||||||
CACHE_TYPE = 'simple'
|
CACHE_TYPE = 'simple'
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ from realms.modules.auth.models import User
|
||||||
from realms.lib.util import green, red, yellow
|
from realms.lib.util import green, red, yellow
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group(short_help="Auth Module")
|
||||||
def cli():
|
def cli():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
__version__ = '0.3.26'
|
__version__ = '0.3.31'
|
2
setup.py
2
setup.py
|
@ -47,7 +47,7 @@ setup(name='realms-wiki',
|
||||||
],
|
],
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
'realms-wiki = realms.cli:cli'
|
'realms-wiki = realms.commands:cli'
|
||||||
]},
|
]},
|
||||||
author='Matthew Scragg',
|
author='Matthew Scragg',
|
||||||
author_email='scragg@gmail.com',
|
author_email='scragg@gmail.com',
|
||||||
|
|
Loading…
Reference in a new issue