annotate piecrust/commands/builtin/admin.py @ 601:effbc78b5528

admin: Better error reporting, general clean-up.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 24 Jan 2016 10:42:33 -0800
parents 79a31a3c947b
children 906cc2520773
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
601
effbc78b5528 admin: Better error reporting, general clean-up.
Ludovic Chabant <ludovic@chabant.com>
parents: 597
diff changeset
44 settings.FOODTRUCK_ROOT = os.getcwd()
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 from foodtruck.main import run_foodtruck
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 run_foodtruck(debug=ctx.args.debug)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 def _initFoodTruck(self, ctx):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 import getpass
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 import bcrypt
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 secret_key = os.urandom(22)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 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
54 admin_password = getpass.getpass("Admin password: ")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 if not admin_password:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 logger.warning("No administration password set!")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 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
58 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
59 "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
60 "`chef admin genpass` command.")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 else:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 binpw = admin_password.encode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 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
64 admin_password = hashpw
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 ft_config = """
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 security:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 username: %(username)s
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 # 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
70 password: %(password)s
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 """
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 ft_config = ft_config % {
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 'username': admin_username,
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 'password': admin_password
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 }
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 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
77 fp.write(ft_config)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78
597
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 588
diff changeset
79 flask_config = """
601
effbc78b5528 admin: Better error reporting, general clean-up.
Ludovic Chabant <ludovic@chabant.com>
parents: 597
diff changeset
80 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
81 """
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 588
diff changeset
82 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
83 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
84 fp.write(flask_config)
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 588
diff changeset
85
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 def _generatePassword(self, ctx):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 import bcrypt
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 binpw = ctx.args.password.encode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 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
90 logger.info(hashpw)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91