comparison piecrust/commands/builtin/admin.py @ 588:b884bef3e611

admin: New `admin` command to manage FoodTruck-related things. Remove old FoodTruck command and packaging.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 17 Jan 2016 21:44:41 -0800
parents
children 79a31a3c947b
comparison
equal deleted inserted replaced
587:d4a01a023998 588:b884bef3e611
1 import os
2 import logging
3 from piecrust.commands.base import ChefCommand
4
5
6 logger = logging.getLogger(__name__)
7
8
9 class AdministrationPanelCommand(ChefCommand):
10 def __init__(self):
11 super(AdministrationPanelCommand, self).__init__()
12 self.name = 'admin'
13 self.description = "Manages the PieCrust administration panel."
14 self.requires_website = False
15
16 def setupParser(self, parser, app):
17 subparsers = parser.add_subparsers()
18
19 p = subparsers.add_parser(
20 'init',
21 help="Creates a new administration panel website.")
22 p.set_defaults(sub_func=self._initFoodTruck)
23
24 p = subparsers.add_parser(
25 'genpass',
26 help=("Generates the hashed password for use as an "
27 "admin password"))
28 p.add_argument('password', help="The password to hash.")
29 p.set_defaults(sub_func=self._generatePassword)
30
31 p = subparsers.add_parser(
32 'run',
33 help="Runs the administrative panel website.")
34 p.set_defaults(sub_func=self._runFoodTruck)
35
36 def checkedRun(self, ctx):
37 if not hasattr(ctx.args, 'sub_func'):
38 return self._runFoodTruck(ctx)
39 return ctx.args.sub_func(ctx)
40
41 def _runFoodTruck(self, ctx):
42 from foodtruck.main import run_foodtruck
43 run_foodtruck(debug=ctx.args.debug)
44
45 def _initFoodTruck(self, ctx):
46 import getpass
47 import bcrypt
48
49 secret_key = os.urandom(22)
50 admin_username = input("Admin username (admin): ") or 'admin'
51 admin_password = getpass.getpass("Admin password: ")
52 if not admin_password:
53 logger.warning("No administration password set!")
54 logger.warning("Don't make this instance of FoodTruck public.")
55 logger.info("You can later set an admin password by editing "
56 "the `foodtruck.yml` file and using the "
57 "`chef admin genpass` command.")
58 else:
59 binpw = admin_password.encode('utf8')
60 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8')
61 admin_password = hashpw
62
63 ft_config = """
64 foodtruck:
65 secret_key: %(secret_key)s
66 security:
67 username: %(username)s
68 # You can generate another hashed password with `chef admin genpass`.
69 password: %(password)s
70 """
71 ft_config = ft_config % {
72 'secret_key': secret_key,
73 'username': admin_username,
74 'password': admin_password
75 }
76 with open('foodtruck.yml', 'w', encoding='utf8') as fp:
77 fp.write(ft_config)
78
79 def _generatePassword(self, ctx):
80 import bcrypt
81 binpw = ctx.args.password.encode('utf8')
82 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8')
83 logger.info(hashpw)
84