Mercurial > piecrust2
annotate piecrust/commands/builtin/admin.py @ 653:466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
* This makes the compiled assets available for the site preview.
* Remove unneeded `--foodtruck` option to `bake`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 18 Feb 2016 20:54:37 -0800 |
parents | 59968ee07a07 |
children | dba53f0f7671 |
rev | line source |
---|---|
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import logging |
653
466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
3 from piecrust import CACHE_DIR |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 from piecrust.commands.base import ChefCommand |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 logger = logging.getLogger(__name__) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 class AdministrationPanelCommand(ChefCommand): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 def __init__(self): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 super(AdministrationPanelCommand, self).__init__() |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 self.name = 'admin' |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 self.description = "Manages the PieCrust administration panel." |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 self.requires_website = False |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 def setupParser(self, parser, app): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 subparsers = parser.add_subparsers() |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 p = subparsers.add_parser( |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 'init', |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 help="Creates a new administration panel website.") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 p.set_defaults(sub_func=self._initFoodTruck) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 p = subparsers.add_parser( |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 'genpass', |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 help=("Generates the hashed password for use as an " |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 "admin password")) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 p.add_argument('password', help="The password to hash.") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 p.set_defaults(sub_func=self._generatePassword) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 p = subparsers.add_parser( |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 'run', |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 help="Runs the administrative panel website.") |
619
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
35 p.add_argument( |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
36 '-p', '--port', |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
37 help="The port for the administrative panel website.", |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
38 default=8090) |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
39 p.add_argument( |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
40 '-a', '--address', |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
41 help="The host for the administrative panel website.", |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
42 default='localhost') |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 p.set_defaults(sub_func=self._runFoodTruck) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 def checkedRun(self, ctx): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 if not hasattr(ctx.args, 'sub_func'): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 return self._runFoodTruck(ctx) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 return ctx.args.sub_func(ctx) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 def _runFoodTruck(self, ctx): |
653
466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
51 from piecrust.processing.pipeline import ProcessorPipeline |
466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
52 out_dir = os.path.join( |
466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
53 ctx.app.root_dir, CACHE_DIR, 'foodtruck', 'server') |
466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
54 proc = ProcessorPipeline(ctx.app, out_dir) |
466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
55 proc.run() |
466bbddd121e
admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
56 |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
57 from foodtruck import settings |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
58 settings.FOODTRUCK_CMDLINE_MODE = True |
611
906cc2520773
admin: Use the app directory, not the cwd, in case of `--root`.
Ludovic Chabant <ludovic@chabant.com>
parents:
601
diff
changeset
|
59 settings.FOODTRUCK_ROOT = ctx.app.root_dir |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 from foodtruck.main import run_foodtruck |
619
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
61 run_foodtruck( |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
62 host=ctx.args.address, |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
63 port=ctx.args.port, |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
64 debug=ctx.args.debug) |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 def _initFoodTruck(self, ctx): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 import getpass |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 import bcrypt |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 secret_key = os.urandom(22) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 admin_username = input("Admin username (admin): ") or 'admin' |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 admin_password = getpass.getpass("Admin password: ") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 if not admin_password: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 logger.warning("No administration password set!") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 logger.warning("Don't make this instance of FoodTruck public.") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 logger.info("You can later set an admin password by editing " |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 "the `foodtruck.yml` file and using the " |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 "`chef admin genpass` command.") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 else: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 binpw = admin_password.encode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 admin_password = hashpw |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 ft_config = """ |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 security: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 username: %(username)s |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 # You can generate another hashed password with `chef admin genpass`. |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 password: %(password)s |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 """ |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 ft_config = ft_config % { |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 'username': admin_username, |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
92 'password': admin_password |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 } |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 with open('foodtruck.yml', 'w', encoding='utf8') as fp: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 fp.write(ft_config) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
97 flask_config = """ |
601
effbc78b5528
admin: Better error reporting, general clean-up.
Ludovic Chabant <ludovic@chabant.com>
parents:
597
diff
changeset
|
98 SECRET_KEY = %(secret_key)s |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
99 """ |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
100 flask_config = flask_config % {'secret_key': secret_key} |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
101 with open('app.cfg', 'w', encoding='utf8') as fp: |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
102 fp.write(flask_config) |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
103 |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 def _generatePassword(self, ctx): |
640
59968ee07a07
admin: Don't require `bcrypt` for running FoodTruck with `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
619
diff
changeset
|
105 from foodtruck import bcryptfallback as bcrypt |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 binpw = ctx.args.password.encode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 logger.info(hashpw) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 |