annotate piecrust/admin/views/publish.py @ 1185:24413a2963b9

admin: Add hidden system information on publish page for troubleshooting.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 01 Oct 2020 10:58:33 -0700
parents 0d699f04968c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1185
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
1 import os
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
2 import sys
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import copy
1185
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
4 import pprint
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import logging
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from flask import request, g, url_for, render_template, Response
1151
0d699f04968c cm: Update dependencies and fix imports of Flask plugins.
Ludovic Chabant <ludovic@chabant.com>
parents: 968
diff changeset
7 from flask_login import login_required
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 629
diff changeset
8 from ..blueprint import foodtruck_bp
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 from ..pubutil import PublishLogReader
968
20f49786937c admin: Fix bug on the publish view.
Ludovic Chabant <ludovic@chabant.com>
parents: 961
diff changeset
10 from ..views import with_menu_context
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 logger = logging.getLogger(__name__)
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 629
diff changeset
16 @foodtruck_bp.route('/publish', methods=['GET', 'POST'])
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 @login_required
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 def publish():
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 if request.method == 'POST':
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 target = request.form.get('target')
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 if not target:
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 raise Exception("No target specified.")
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
610
efc1dc916e7c admin: Configuration changes.
Ludovic Chabant <ludovic@chabant.com>
parents: 602
diff changeset
24 g.site.publish(target)
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
610
efc1dc916e7c admin: Configuration changes.
Ludovic Chabant <ludovic@chabant.com>
parents: 602
diff changeset
26 site = g.site
efc1dc916e7c admin: Configuration changes.
Ludovic Chabant <ludovic@chabant.com>
parents: 602
diff changeset
27 pub_cfg = copy.deepcopy(site.piecrust_app.config.get('publish', {}))
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 if not pub_cfg:
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
29 data = {'error': "There are no publish targets defined in your "
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 "configuration file."}
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 return render_template('error.html', **data)
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
952
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
33 try:
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
34 with open(site.publish_log_file, 'r') as fp:
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
35 last_pub_log = fp.read()
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
36 except OSError:
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
37 last_pub_log = None
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
38
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 data = {}
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 629
diff changeset
40 data['url_run'] = url_for('.publish')
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
41 data['site_title'] = site.piecrust_app.config.get('site/title',
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
42 "Unnamed Website")
1185
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
43 data['sysinfo'] = str({
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
44 'sys.base_exec_prefix': sys.base_exec_prefix,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
45 'sys.base_prefix': sys.base_prefix,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
46 'sys.exec_prefix': sys.exec_prefix,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
47 'sys.executable': sys.executable,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
48 'sys.path': sys.path,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
49 'sys.platform': sys.platform,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
50 'sys.prefix': sys.prefix,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
51 'PYTHONHOME': os.getenv('PYTHONHOME'),
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
52 'PYTHONPATH': os.getenv('PYTHONPATH')
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
53 })
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 data['targets'] = []
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 for tn in sorted(pub_cfg.keys()):
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 tc = pub_cfg[tn]
629
40e897e2f11e admin: Make the publish UI handle new kinds of target configurations.
Ludovic Chabant <ludovic@chabant.com>
parents: 613
diff changeset
57 desc = None
40e897e2f11e admin: Make the publish UI handle new kinds of target configurations.
Ludovic Chabant <ludovic@chabant.com>
parents: 613
diff changeset
58 if isinstance(tc, dict):
40e897e2f11e admin: Make the publish UI handle new kinds of target configurations.
Ludovic Chabant <ludovic@chabant.com>
parents: 613
diff changeset
59 desc = tc.get('description')
1185
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
60
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
61 tc = tc.copy()
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
62 tc.pop('description')
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
63 tc = pprint.pformat(tc, indent=4)
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
64
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 data['targets'].append({
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 'name': tn,
1185
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
67 'description': desc,
24413a2963b9 admin: Add hidden system information on publish page for troubleshooting.
Ludovic Chabant <ludovic@chabant.com>
parents: 1151
diff changeset
68 'config': tc
812
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
69 })
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70
952
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
71 data['last_log'] = last_pub_log
94fd4f07da83 admin: Fix more URL prefix issues, improve publishing.
Ludovic Chabant <ludovic@chabant.com>
parents: 920
diff changeset
72
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 with_menu_context(data)
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 return render_template('publish.html', **data)
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 629
diff changeset
78 @foodtruck_bp.route('/publish-log')
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 @login_required
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 def stream_publish_log():
613
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents: 610
diff changeset
81 pid_path = g.site.publish_pid_file
e2e955a3bb25 publish: Add publish command.
Ludovic Chabant <ludovic@chabant.com>
parents: 610
diff changeset
82 log_path = g.site.publish_log_file
602
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 rdr = PublishLogReader(pid_path, log_path)
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 response = Response(rdr.run(), mimetype='text/event-stream')
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 response.headers['Cache-Control'] = 'no-cache'
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 return response
c6bc0ef03f82 admin: Better UI for publishing websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88