annotate piecrust/commands/builtin/admin.py @ 868:8d25f76fce98

bake: Add ability to specify pipelines to exclude during the bake.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 12 Jun 2017 22:22:19 -0700
parents 4850f8c21b6e
children dcdec4b951a1
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
656
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
2 import os.path
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
653
466bbddd121e admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 640
diff changeset
4 from piecrust import CACHE_DIR
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 from piecrust.commands.base import ChefCommand
842
a85d08ffe1f6 admin: Fix crash when running `admin run` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
6 from piecrust.pathutil import SiteNotFoundError
588
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 logger = logging.getLogger(__name__)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 class AdministrationPanelCommand(ChefCommand):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 def __init__(self):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 super(AdministrationPanelCommand, self).__init__()
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 self.name = 'admin'
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 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
17 self.requires_website = False
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 def setupParser(self, parser, app):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 subparsers = parser.add_subparsers()
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 p = subparsers.add_parser(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
23 'init',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
24 help="Creates a new administration panel website.")
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 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
26
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 p = subparsers.add_parser(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
28 'genpass',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
29 help=("Generates the hashed password for use as an "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
30 "admin password"))
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 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
32 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
33
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 p = subparsers.add_parser(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
35 'run',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
36 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
37 p.add_argument(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
38 '-p', '--port',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
39 help="The port for the administrative panel website.",
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
40 default=8090)
619
200c7063affa admin: Change the default admin server port to 8090, add `--port` option.
Ludovic Chabant <ludovic@chabant.com>
parents: 611
diff changeset
41 p.add_argument(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
42 '-a', '--address',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
43 help="The host for the administrative panel website.",
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
44 default='localhost')
656
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
45 p.add_argument(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
46 '--no-assets',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
47 help="Don't process and monitor the asset folder(s).",
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
48 dest='monitor_assets',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
49 action='store_false')
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 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
51
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def checkedRun(self, ctx):
762
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
53 if ctx.app.root_dir is None:
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
54 raise SiteNotFoundError(theme=ctx.app.theme_site)
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
55
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 if not hasattr(ctx.args, 'sub_func'):
762
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
57 ctx.parser.parse_args(['admin', '--help'])
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
58 return
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 return ctx.args.sub_func(ctx)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 def _runFoodTruck(self, ctx):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
62 # See `_run_sse_check` in `piecrust.serving.wrappers` for an
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
63 # explanation of this check.
656
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
64 if (ctx.args.monitor_assets and (
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
65 not ctx.args.debug or
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
66 os.environ.get('WERKZEUG_RUN_MAIN') == 'true')):
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
67 from piecrust.serving.procloop import ProcessingLoop
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
68 out_dir = os.path.join(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
69 ctx.app.root_dir, CACHE_DIR, 'foodtruck', 'server')
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
70 proc_loop = ProcessingLoop(ctx.appfactory, out_dir)
656
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
71 proc_loop.start()
653
466bbddd121e admin: Run the asset pipeline before showing the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 640
diff changeset
72
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents: 762
diff changeset
73 es = {
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
74 'FOODTRUCK_CMDLINE_MODE': True,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
75 'FOODTRUCK_ROOT': ctx.app.root_dir}
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents: 762
diff changeset
76 from piecrust.admin.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
77 run_foodtruck(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
78 host=ctx.args.address,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
79 port=ctx.args.port,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
80 debug=ctx.args.debug,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
81 extra_settings=es)
588
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 def _initFoodTruck(self, ctx):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 import getpass
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents: 762
diff changeset
85 from piecrust.admin import bcryptfallback as bcrypt
588
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 secret_key = os.urandom(22)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 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
89 admin_password = getpass.getpass("Admin password: ")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 if not admin_password:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 logger.warning("No administration password set!")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 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
93 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
94 "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
95 "`chef admin genpass` command.")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 else:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 binpw = admin_password.encode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 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
99 admin_password = hashpw
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 ft_config = """
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 security:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 username: %(username)s
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 # 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
105 password: %(password)s
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 """
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 ft_config = ft_config % {
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
108 'username': admin_username,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
109 'password': admin_password
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
110 }
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 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
112 fp.write(ft_config)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113
597
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 588
diff changeset
114 flask_config = """
601
effbc78b5528 admin: Better error reporting, general clean-up.
Ludovic Chabant <ludovic@chabant.com>
parents: 597
diff changeset
115 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
116 """
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 588
diff changeset
117 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
118 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
119 fp.write(flask_config)
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 588
diff changeset
120
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 def _generatePassword(self, ctx):
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents: 762
diff changeset
122 from piecrust.admin import bcryptfallback as bcrypt
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 binpw = ctx.args.password.encode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 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
125 logger.info(hashpw)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126