# HG changeset patch # User Ludovic Chabant # Date 1453095881 28800 # Node ID b884bef3e61107f934bcf97ab4fc8a20abc3039f # Parent d4a01a02399843e00e13b531d1aa018a6614c61e admin: New `admin` command to manage FoodTruck-related things. Remove old FoodTruck command and packaging. diff -r d4a01a023998 -r b884bef3e611 foodtruck.py --- a/foodtruck.py Sat Jan 16 14:24:35 2016 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -from foodtruck.main import main - - -if __name__ == '__main__': - main() - diff -r d4a01a023998 -r b884bef3e611 foodtruck/main.py --- a/foodtruck/main.py Sat Jan 16 14:24:35 2016 -0800 +++ b/foodtruck/main.py Sun Jan 17 21:44:41 2016 -0800 @@ -1,48 +1,13 @@ -import sys import logging -import argparse -from .web import app logger = logging.getLogger(__name__) -def main(): - parser = argparse.ArgumentParser( - description="FoodTruck command line utility") - parser.add_argument( - '--debug', - help="Show debug information", - action='store_true') - parser.add_argument( - '--version', - help="Print version and exit", - action='store_true') - - args = parser.parse_args() - if args.version: - try: - from .__version__ import version - except ImportError: - print("Can't find version information.") - args.exit(1) - print("FoodTruck %s" % version) - args.exit(0) - - root_logger = logging.getLogger() - root_logger.setLevel(logging.INFO) - if args.debug: - root_logger.setLevel(logging.DEBUG) - - log_handler = logging.StreamHandler(sys.stdout) - if args.debug: - log_handler.setLevel(logging.DEBUG) - else: - log_handler.setLevel(logging.INFO) - root_logger.addHandler(log_handler) - +def run_foodtruck(debug=False): + from .web import app try: - app.run(debug=args.debug, threaded=True) + app.run(debug=debug, threaded=True) except SystemExit: # This is needed for Werkzeug's code reloader to be able to correctly # shutdown the child process in order to restart it (otherwise, SSE diff -r d4a01a023998 -r b884bef3e611 piecrust/commands/builtin/admin.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/piecrust/commands/builtin/admin.py Sun Jan 17 21:44:41 2016 -0800 @@ -0,0 +1,84 @@ +import os +import logging +from piecrust.commands.base import ChefCommand + + +logger = logging.getLogger(__name__) + + +class AdministrationPanelCommand(ChefCommand): + def __init__(self): + super(AdministrationPanelCommand, self).__init__() + self.name = 'admin' + self.description = "Manages the PieCrust administration panel." + self.requires_website = False + + def setupParser(self, parser, app): + subparsers = parser.add_subparsers() + + p = subparsers.add_parser( + 'init', + help="Creates a new administration panel website.") + p.set_defaults(sub_func=self._initFoodTruck) + + p = subparsers.add_parser( + 'genpass', + help=("Generates the hashed password for use as an " + "admin password")) + p.add_argument('password', help="The password to hash.") + p.set_defaults(sub_func=self._generatePassword) + + p = subparsers.add_parser( + 'run', + help="Runs the administrative panel website.") + p.set_defaults(sub_func=self._runFoodTruck) + + def checkedRun(self, ctx): + if not hasattr(ctx.args, 'sub_func'): + return self._runFoodTruck(ctx) + return ctx.args.sub_func(ctx) + + def _runFoodTruck(self, ctx): + from foodtruck.main import run_foodtruck + run_foodtruck(debug=ctx.args.debug) + + def _initFoodTruck(self, ctx): + import getpass + import bcrypt + + secret_key = os.urandom(22) + admin_username = input("Admin username (admin): ") or 'admin' + admin_password = getpass.getpass("Admin password: ") + if not admin_password: + logger.warning("No administration password set!") + logger.warning("Don't make this instance of FoodTruck public.") + logger.info("You can later set an admin password by editing " + "the `foodtruck.yml` file and using the " + "`chef admin genpass` command.") + else: + binpw = admin_password.encode('utf8') + hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8') + admin_password = hashpw + + ft_config = """ +foodtruck: + secret_key: %(secret_key)s +security: + username: %(username)s + # You can generate another hashed password with `chef admin genpass`. + password: %(password)s +""" + ft_config = ft_config % { + 'secret_key': secret_key, + 'username': admin_username, + 'password': admin_password + } + with open('foodtruck.yml', 'w', encoding='utf8') as fp: + fp.write(ft_config) + + def _generatePassword(self, ctx): + import bcrypt + binpw = ctx.args.password.encode('utf8') + hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8') + logger.info(hashpw) + diff -r d4a01a023998 -r b884bef3e611 piecrust/plugins/builtin.py --- a/piecrust/plugins/builtin.py Sat Jan 16 14:24:35 2016 -0800 +++ b/piecrust/plugins/builtin.py Sun Jan 17 21:44:41 2016 -0800 @@ -1,4 +1,5 @@ from piecrust.commands.base import HelpCommand +from piecrust.commands.builtin.admin import AdministrationPanelCommand from piecrust.commands.builtin.baking import ( BakeCommand, ShowRecordCommand) from piecrust.commands.builtin.info import ( @@ -62,7 +63,8 @@ PluginsCommand(), BakeCommand(), ShowRecordCommand(), - ServeCommand()] + ServeCommand(), + AdministrationPanelCommand()] def getCommandExtensions(self): return [ diff -r d4a01a023998 -r b884bef3e611 setup.py --- a/setup.py Sat Jan 16 14:24:35 2016 -0800 +++ b/setup.py Sun Jan 17 21:44:41 2016 -0800 @@ -199,8 +199,7 @@ 'Programming Language :: Python :: 3' ], entry_points={'console_scripts': [ - 'chef = piecrust.main:main', - 'foodtruck = foodtruck.main:main' + 'chef = piecrust.main:main' ]} )