cli adjustments
This commit is contained in:
		
							parent
							
								
									03b030e67c
								
							
						
					
					
						commit
						d2171f9498
					
				
					 6 changed files with 86 additions and 50 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
#!/usr/bin/env python
 | 
			
		||||
import pip
 | 
			
		||||
from realms.cli import cli
 | 
			
		||||
 | 
			
		||||
from realms.commands import cli
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    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 subprocess import call, Popen
 | 
			
		||||
from multiprocessing import cpu_count
 | 
			
		||||
| 
						 | 
				
			
			@ -7,29 +7,10 @@ import json
 | 
			
		|||
import sys
 | 
			
		||||
import os
 | 
			
		||||
import pip
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def print_version(ctx, param, value):
 | 
			
		||||
    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_)
 | 
			
		||||
# called to discover commands in modules
 | 
			
		||||
app = create_app()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_user():
 | 
			
		||||
| 
						 | 
				
			
			@ -42,11 +23,24 @@ def get_user():
 | 
			
		|||
def get_pid():
 | 
			
		||||
    try:
 | 
			
		||||
        with file(config.PIDFILE) as f:
 | 
			
		||||
            pid = f.read().strip()
 | 
			
		||||
            return pid if pid and int(pid) > 0 and not call(['kill', '-s', '0', pid]) else False
 | 
			
		||||
            return f.read().strip()
 | 
			
		||||
    except IOError:
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def is_running(pid):
 | 
			
		||||
    if not pid:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    pid = int(pid)
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        os.kill(pid, 0)
 | 
			
		||||
    except OSError:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    return True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def module_exists(module_name):
 | 
			
		||||
    try:
 | 
			
		||||
| 
						 | 
				
			
			@ -94,6 +88,11 @@ def setup(ctx, **kw):
 | 
			
		|||
    """ Start setup wizard
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        os.mkdir('/etc/realms-wiki')
 | 
			
		||||
    except OSError:
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    conf = {}
 | 
			
		||||
 | 
			
		||||
    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('Full usage: realms-wiki --help')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@click.command()
 | 
			
		||||
@click.option('--cache-redis-host',
 | 
			
		||||
              default=getattr(config, 'CACHE_REDIS_HOST', "127.0.0.1"),
 | 
			
		||||
| 
						 | 
				
			
			@ -163,6 +163,10 @@ def install_postgres():
 | 
			
		|||
    pip.main(['install', 'psycopg2'])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def install_crate():
 | 
			
		||||
    pip.main(['install', 'crate'])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def install_memcached():
 | 
			
		||||
    pip.main(['install', 'python-memcached'])
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -233,7 +237,7 @@ def setup_upstart(**kwargs):
 | 
			
		|||
@cli.command()
 | 
			
		||||
@click.argument('json_string')
 | 
			
		||||
def configure(json_string):
 | 
			
		||||
    """ Set config.json, expects JSON encoded string
 | 
			
		||||
    """ Set config, expects JSON encoded string
 | 
			
		||||
    """
 | 
			
		||||
    try:
 | 
			
		||||
        config.update(json.loads(json_string))
 | 
			
		||||
| 
						 | 
				
			
			@ -242,18 +246,25 @@ def configure(json_string):
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
@cli.command()
 | 
			
		||||
@click.option('--port', default=5000)
 | 
			
		||||
@click.option('--port', default=config.PORT)
 | 
			
		||||
def dev(port):
 | 
			
		||||
    """ Run 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",
 | 
			
		||||
                     port=port,
 | 
			
		||||
                     debug=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def start_server():
 | 
			
		||||
    if get_pid():
 | 
			
		||||
    if is_running(get_pid()):
 | 
			
		||||
        yellow("Server is already running")
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -261,17 +272,25 @@ def start_server():
 | 
			
		|||
 | 
			
		||||
    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" %
 | 
			
		||||
          (config.PORT, flags), shell=True, executable='/bin/bash')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def stop_server():
 | 
			
		||||
    pid = get_pid()
 | 
			
		||||
    if not pid:
 | 
			
		||||
    if not is_running(pid):
 | 
			
		||||
        yellow("Server is not running")
 | 
			
		||||
    else:
 | 
			
		||||
        yellow("Shutting down server")
 | 
			
		||||
        call(['kill', pid])
 | 
			
		||||
        while is_running(pid):
 | 
			
		||||
            time.sleep(1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@cli.command()
 | 
			
		||||
| 
						 | 
				
			
			@ -307,7 +326,7 @@ def restart():
 | 
			
		|||
def status():
 | 
			
		||||
    """ Get server status
 | 
			
		||||
    """
 | 
			
		||||
    pid = get_pid()
 | 
			
		||||
    pid = is_running(get_pid())
 | 
			
		||||
    if not pid:
 | 
			
		||||
        yellow("Server is not running")
 | 
			
		||||
    else:
 | 
			
		||||
| 
						 | 
				
			
			@ -16,15 +16,9 @@ def read():
 | 
			
		|||
        if k.startswith('REALMS_'):
 | 
			
		||||
            conf[k[7:]] = v
 | 
			
		||||
 | 
			
		||||
    for loc in os.curdir, os.path.expanduser("~"), "/etc/realms-wiki", os.environ.get("REALMS_WIKI_CONF"):
 | 
			
		||||
        try:
 | 
			
		||||
            if not loc:
 | 
			
		||||
                continue
 | 
			
		||||
            with open(os.path.join(loc, "realms-wiki.json")) as f:
 | 
			
		||||
    loc = get_path()
 | 
			
		||||
    with open(loc) as f:
 | 
			
		||||
        conf.update(json.load(f))
 | 
			
		||||
            break
 | 
			
		||||
        except IOError:
 | 
			
		||||
            pass
 | 
			
		||||
 | 
			
		||||
    for k in ['APP_PATH', 'USER_HOME']:
 | 
			
		||||
        if k in conf:
 | 
			
		||||
| 
						 | 
				
			
			@ -34,13 +28,35 @@ def read():
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def save(conf):
 | 
			
		||||
    for loc in "/etc/realms-wiki", os.path.expanduser("~"), os.curdir:
 | 
			
		||||
        try:
 | 
			
		||||
            with open(os.path.join(loc, 'realms-wiki.json'), 'w') as f:
 | 
			
		||||
    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 os.path.join(loc, 'realms-wiki.json')
 | 
			
		||||
        except IOError:
 | 
			
		||||
            pass
 | 
			
		||||
    return loc
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_path(check_write=False):
 | 
			
		||||
    """Find config path
 | 
			
		||||
    """
 | 
			
		||||
    for loc in "/etc/realms-wiki", os.path.expanduser("~"), os.curdir:
 | 
			
		||||
        if not loc:
 | 
			
		||||
            continue
 | 
			
		||||
        path = os.path.join(loc, "realms-wiki.json")
 | 
			
		||||
        if os.path.isfile(path):
 | 
			
		||||
            # file exists
 | 
			
		||||
            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__) + "/../..")
 | 
			
		||||
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 = 'postgresql://scott:tiger@localhost/mydatabase'
 | 
			
		||||
# DB_URI = 'oracle://scott:tiger@127.0.0.1:1521/sidname'
 | 
			
		||||
# DB_URI = 'crate://'
 | 
			
		||||
 | 
			
		||||
CACHE_TYPE = 'simple'
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,7 +4,7 @@ from realms.modules.auth.models import User
 | 
			
		|||
from realms.lib.util import green, red, yellow
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@click.group()
 | 
			
		||||
@click.group(short_help="Auth Module")
 | 
			
		||||
def cli():
 | 
			
		||||
    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={
 | 
			
		||||
          'console_scripts': [
 | 
			
		||||
              'realms-wiki = realms.cli:cli'
 | 
			
		||||
              'realms-wiki = realms.commands:cli'
 | 
			
		||||
          ]},
 | 
			
		||||
      author='Matthew Scragg',
 | 
			
		||||
      author_email='scragg@gmail.com',
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue