Mercurial > piecrust2
annotate piecrust/commands/builtin/admin.py @ 597:79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
* Use a `settings` object to configure Flask.
* Accept an `app.cfg` file in the admin folder to configure Flask.
* Get a proper first site name when the cookie isn't set yet.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 20 Jan 2016 21:39:18 -0800 |
parents | b884bef3e611 |
children | effbc78b5528 |
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.") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 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
|
35 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 def checkedRun(self, ctx): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 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
|
38 return self._runFoodTruck(ctx) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 return ctx.args.sub_func(ctx) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 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
|
42 from foodtruck import settings |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
43 settings.FOODTRUCK_CMDLINE_MODE = True |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 from foodtruck.main import run_foodtruck |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 run_foodtruck(debug=ctx.args.debug) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 def _initFoodTruck(self, ctx): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 import getpass |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 import bcrypt |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 secret_key = os.urandom(22) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 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
|
53 admin_password = getpass.getpass("Admin password: ") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 if not admin_password: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 logger.warning("No administration password set!") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 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
|
57 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
|
58 "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
|
59 "`chef admin genpass` command.") |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 else: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 binpw = admin_password.encode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 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
|
63 admin_password = hashpw |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 ft_config = """ |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 security: |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 username: %(username)s |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 # 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
|
69 password: %(password)s |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 """ |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 ft_config = ft_config % { |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 'username': admin_username, |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 'password': admin_password |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 } |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 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
|
76 fp.write(ft_config) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
78 flask_config = """ |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
79 secret_key = '%(secret_key)s' |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
80 """ |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
81 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
|
82 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
|
83 fp.write(flask_config) |
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
588
diff
changeset
|
84 |
588
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 def _generatePassword(self, ctx): |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 import bcrypt |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 binpw = ctx.args.password.encode('utf8') |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 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
|
89 logger.info(hashpw) |
b884bef3e611
admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 |