Mercurial > piecrust2
comparison piecrust/commands/builtin/admin.py @ 917:33a89139c284
serve: Add `--admin` option to run the administration panel.
- Removed the `admin run` command.
- Cleaned up middlewares a bit.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 29 Sep 2017 08:42:38 -0700 |
parents | dcdec4b951a1 |
children |
comparison
equal
deleted
inserted
replaced
916:84ce51430346 | 917:33a89139c284 |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import logging | 3 import logging |
4 from piecrust import CACHE_DIR, CONFIG_PATH | 4 from piecrust import CONFIG_PATH |
5 from piecrust.commands.base import ChefCommand | 5 from piecrust.commands.base import ChefCommand |
6 from piecrust.pathutil import SiteNotFoundError | 6 from piecrust.pathutil import SiteNotFoundError |
7 | 7 |
8 | 8 |
9 logger = logging.getLogger(__name__) | 9 logger = logging.getLogger(__name__) |
22 p = subparsers.add_parser( | 22 p = subparsers.add_parser( |
23 'init', | 23 'init', |
24 help="Creates a new administration panel website.") | 24 help="Creates a new administration panel website.") |
25 p.set_defaults(sub_func=self._initAdminSite) | 25 p.set_defaults(sub_func=self._initAdminSite) |
26 | 26 |
27 p = subparsers.add_parser( | |
28 'genpass', | |
29 help=("Generates the hashed password for use as an " | |
30 "admin password")) | |
31 p.add_argument('password', help="The password to hash.") | |
32 p.set_defaults(sub_func=self._generatePassword) | |
33 | |
34 p = subparsers.add_parser( | |
35 'run', | |
36 help="Runs the administrative panel website.") | |
37 p.add_argument( | |
38 '-p', '--port', | |
39 help="The port for the administrative panel website.", | |
40 default=8090) | |
41 p.add_argument( | |
42 '-a', '--address', | |
43 help="The host for the administrative panel website.", | |
44 default='localhost') | |
45 p.add_argument( | |
46 '--no-assets', | |
47 help="Don't process and monitor the asset folder(s).", | |
48 dest='monitor_assets', | |
49 action='store_false') | |
50 p.add_argument( | |
51 '--use-reloader', | |
52 help="Restart the server when PieCrust code changes", | |
53 action='store_true') | |
54 p.add_argument( | |
55 '--use-debugger', | |
56 help="Show the debugger when an error occurs", | |
57 action='store_true') | |
58 p.set_defaults(sub_func=self._runAdminSite) | |
59 | |
60 def checkedRun(self, ctx): | 27 def checkedRun(self, ctx): |
61 if ctx.app.root_dir is None: | 28 if ctx.app.root_dir is None: |
62 raise SiteNotFoundError(theme=ctx.app.theme_site) | 29 raise SiteNotFoundError(theme=ctx.app.theme_site) |
63 | 30 |
64 if not hasattr(ctx.args, 'sub_func'): | 31 if not hasattr(ctx.args, 'sub_func'): |
65 ctx.parser.parse_args(['admin', '--help']) | 32 ctx.parser.parse_args(['admin', '--help']) |
66 return | 33 return |
67 return ctx.args.sub_func(ctx) | 34 return ctx.args.sub_func(ctx) |
68 | |
69 def _runAdminSite(self, ctx): | |
70 # See `_run_sse_check` in `piecrust.serving.wrappers` for an | |
71 # explanation of this check. | |
72 if (ctx.args.monitor_assets and ( | |
73 not (ctx.args.debug or ctx.args.use_reloader) or | |
74 os.environ.get('WERKZEUG_RUN_MAIN') == 'true')): | |
75 from piecrust.serving.procloop import ProcessingLoop | |
76 out_dir = os.path.join( | |
77 ctx.app.root_dir, CACHE_DIR, 'admin', 'server') | |
78 proc_loop = ProcessingLoop(ctx.appfactory, out_dir) | |
79 proc_loop.start() | |
80 | |
81 es = { | |
82 'FOODTRUCK_CMDLINE_MODE': True, | |
83 'FOODTRUCK_ROOT': ctx.app.root_dir, | |
84 'FOODTRUCK_URL_PREFIX': '', | |
85 'SECRET_KEY': os.urandom(22), | |
86 'LOGIN_DISABLED': True} | |
87 if ctx.args.debug or ctx.args.use_debugger: | |
88 es['DEBUG'] = True | |
89 | |
90 run_foodtruck( | |
91 host=ctx.args.address, | |
92 port=ctx.args.port, | |
93 use_reloader=ctx.args.use_reloader, | |
94 extra_settings=es) | |
95 | 35 |
96 def _initAdminSite(self, ctx): | 36 def _initAdminSite(self, ctx): |
97 import io | 37 import io |
98 import getpass | 38 import getpass |
99 from piecrust.admin import bcryptfallback as bcrypt | 39 from piecrust.admin import bcryptfallback as bcrypt |
136 from piecrust.admin import bcryptfallback as bcrypt | 76 from piecrust.admin import bcryptfallback as bcrypt |
137 binpw = ctx.args.password.encode('utf8') | 77 binpw = ctx.args.password.encode('utf8') |
138 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8') | 78 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8') |
139 logger.info(hashpw) | 79 logger.info(hashpw) |
140 | 80 |
141 | |
142 def run_foodtruck(host=None, port=None, use_reloader=False, | |
143 extra_settings=None): | |
144 es = {} | |
145 if extra_settings: | |
146 es.update(extra_settings) | |
147 | |
148 # Disable PIN protection with Werkzeug's debugger. | |
149 os.environ['WERKZEUG_DEBUG_PIN'] = 'off' | |
150 | |
151 try: | |
152 from piecrust.admin.web import create_foodtruck_app | |
153 app = create_foodtruck_app(es) | |
154 app.run(host=host, port=port, use_reloader=use_reloader, | |
155 threaded=True) | |
156 except SystemExit: | |
157 # This is needed for Werkzeug's code reloader to be able to correctly | |
158 # shutdown the child process in order to restart it (otherwise, SSE | |
159 # generators will keep it alive). | |
160 try: | |
161 from . import pubutil | |
162 logger.debug("Shutting down SSE generators from main...") | |
163 pubutil.server_shutdown = True | |
164 except ImportError: | |
165 pass | |
166 raise | |
167 |