Mercurial > piecrust2
annotate piecrust/commands/builtin/admin.py @ 640:59968ee07a07
admin: Don't require `bcrypt` for running FoodTruck with `chef`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 14 Feb 2016 22:06:32 -0800 |
parents | 200c7063affa |
children | 466bbddd121e |
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 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from piecrust.commands.base import ChefCommand |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
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 logger = logging.getLogger(__name__) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
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 class AdministrationPanelCommand(ChefCommand): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 def __init__(self): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 super(AdministrationPanelCommand, self).__init__() |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 self.name = 'admin' |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 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
|
14 self.requires_website = False |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 def setupParser(self, parser, app): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 subparsers = parser.add_subparsers() |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 p = subparsers.add_parser( |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 'init', |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 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
|
22 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
|
23 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 p = subparsers.add_parser( |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 'genpass', |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 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
|
27 "admin password")) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 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
|
29 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
|
30 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 p = subparsers.add_parser( |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 'run', |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 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
|
34 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
|
35 '-p', '--port', |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
36 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
|
37 default=8090) |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
38 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
|
39 '-a', '--address', |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
40 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
|
41 default='localhost') |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 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
|
43 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 def checkedRun(self, ctx): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 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
|
46 return self._runFoodTruck(ctx) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 return ctx.args.sub_func(ctx) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 def _runFoodTruck(self, ctx): |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
50 from foodtruck import settings |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 run_foodtruck( |
200c7063affa
admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents:
611
diff
changeset
|
55 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
|
56 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
|
57 debug=ctx.args.debug) |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 def _initFoodTruck(self, ctx): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 import getpass |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 import bcrypt |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 secret_key = os.urandom(22) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 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
|
65 admin_password = getpass.getpass("Admin password: ") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 if not admin_password: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 logger.warning("No administration password set!") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 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
|
69 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
|
70 "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
|
71 "`chef admin genpass` command.") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 else: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 binpw = admin_password.encode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 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
|
75 admin_password = hashpw |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 ft_config = """ |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 security: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 username: %(username)s |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 # 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
|
81 password: %(password)s |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 """ |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 ft_config = ft_config % { |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 'username': admin_username, |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 'password': admin_password |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 } |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 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
|
88 fp.write(ft_config) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
90 flask_config = """ |
601
effbc78b5528
admin: Better error reporting, general clean-up.
Ludovic Chabant <ludovic@chabant.com>
parents:
597
diff
changeset
|
91 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
|
92 """ |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
93 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
|
94 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
|
95 fp.write(flask_config) |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
96 |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 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
|
98 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
|
99 binpw = ctx.args.password.encode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 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
|
101 logger.info(hashpw) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 |