puppetboard-dp 8.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. puppetboard/__init__.py +3 -0
  2. puppetboard/app.py +127 -0
  3. puppetboard/asgi.py +40 -0
  4. puppetboard/core.py +277 -0
  5. puppetboard/data.py +254 -0
  6. puppetboard/default_settings.py +121 -0
  7. puppetboard/docker_settings.py +213 -0
  8. puppetboard/errors.py +52 -0
  9. puppetboard/forms.py +37 -0
  10. puppetboard/gunicorn_conf.py +15 -0
  11. puppetboard/healthcheck.py +22 -0
  12. puppetboard/mcp_server.py +282 -0
  13. puppetboard/ratelimit.py +31 -0
  14. puppetboard/schedulers/classes.py +61 -0
  15. puppetboard/static/api/openapi.json +668 -0
  16. puppetboard/static/css/fonts.css +210 -0
  17. puppetboard/static/css/puppetboard.css +969 -0
  18. puppetboard/static/css/radiator.css +53 -0
  19. puppetboard/static/custom-natural/natural.js +42 -0
  20. puppetboard/static/custom-natural-ip/natural-ip.js +42 -0
  21. puppetboard/static/custom-natural-size/natural-size.js +54 -0
  22. puppetboard/static/custom-natural-time-delta/natural-time-delta.js +85 -0
  23. puppetboard/static/favicon.ico +0 -0
  24. puppetboard/static/favicon.svg +56 -0
  25. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_SAvzAbt.woff2 +0 -0
  26. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_SQvzA.woff2 +0 -0
  27. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_SYvzAbt.woff2 +0 -0
  28. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_ScvzAbt.woff2 +0 -0
  29. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_SgvzAbt.woff2 +0 -0
  30. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_SkvzAbt.woff2 +0 -0
  31. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_SovzAbt.woff2 +0 -0
  32. puppetboard/static/fonts/cousine/d6lIkaiiRdih4SpP_SsvzAbt.woff2 +0 -0
  33. puppetboard/static/fonts/lato/S6u8w4BMUTPHjxsAUi-qJCY.woff2 +0 -0
  34. puppetboard/static/fonts/lato/S6u8w4BMUTPHjxsAXC-q.woff2 +0 -0
  35. puppetboard/static/fonts/lato/S6u9w4BMUTPHh6UVSwaPGR_p.woff2 +0 -0
  36. puppetboard/static/fonts/lato/S6u9w4BMUTPHh6UVSwiPGQ.woff2 +0 -0
  37. puppetboard/static/fonts/lato/S6u_w4BMUTPHjxsI5wq_FQft1dw.woff2 +0 -0
  38. puppetboard/static/fonts/lato/S6u_w4BMUTPHjxsI5wq_Gwft.woff2 +0 -0
  39. puppetboard/static/fonts/lato/S6uyw4BMUTPHjx4wXg.woff2 +0 -0
  40. puppetboard/static/fonts/lato/S6uyw4BMUTPHjxAwXjeu.woff2 +0 -0
  41. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4gaVI.woff2 +0 -0
  42. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4iaVIGxA.woff2 +0 -0
  43. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4jaVIGxA.woff2 +0 -0
  44. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4kaVIGxA.woff2 +0 -0
  45. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4saVIGxA.woff2 +0 -0
  46. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4taVIGxA.woff2 +0 -0
  47. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4uaVIGxA.woff2 +0 -0
  48. puppetboard/static/fonts/open-sans/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0B4vaVIGxA.woff2 +0 -0
  49. puppetboard/static/js/dailychart.js +152 -0
  50. puppetboard/static/js/pretty.js +47 -0
  51. puppetboard/static/js/radiator.js +21 -0
  52. puppetboard/static/js/scroll.top.js +13 -0
  53. puppetboard/static/js/utils.js +92 -0
  54. puppetboard/static/libs/billboard.js/billboard.min.css +9 -0
  55. puppetboard/static/libs/billboard.js/billboard.pkgd.min.js +61 -0
  56. puppetboard/static/libs/datatables.net/dataTables.min.js +4 -0
  57. puppetboard/static/libs/datatables.net-buttons/dataTables.buttons.min.js +4 -0
  58. puppetboard/static/libs/datatables.net-buttons-se/buttons.semanticui.min.css +1 -0
  59. puppetboard/static/libs/datatables.net-buttons-se/buttons.semanticui.min.js +4 -0
  60. puppetboard/static/libs/datatables.net-se/dataTables.semanticui.min.css +9 -0
  61. puppetboard/static/libs/datatables.net-se/dataTables.semanticui.min.js +4 -0
  62. puppetboard/static/libs/fomantic-ui/semantic.min.css +11 -0
  63. puppetboard/static/libs/fomantic-ui/semantic.min.js +11 -0
  64. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-Bold.woff +0 -0
  65. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-Bold.woff2 +0 -0
  66. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-BoldItalic.woff +0 -0
  67. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-BoldItalic.woff2 +0 -0
  68. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-Italic.woff +0 -0
  69. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-Italic.woff2 +0 -0
  70. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-Regular.woff +0 -0
  71. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/Lato-Regular.woff2 +0 -0
  72. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-Bold.woff +0 -0
  73. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-Bold.woff2 +0 -0
  74. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-BoldItalic.woff +0 -0
  75. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-BoldItalic.woff2 +0 -0
  76. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-Italic.woff +0 -0
  77. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-Italic.woff2 +0 -0
  78. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-Regular.woff +0 -0
  79. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/LatoLatin-Regular.woff2 +0 -0
  80. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/brand-icons.woff +0 -0
  81. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/brand-icons.woff2 +0 -0
  82. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/icons.woff +0 -0
  83. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/icons.woff2 +0 -0
  84. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/outline-icons.woff +0 -0
  85. puppetboard/static/libs/fomantic-ui/themes/default/assets/fonts/outline-icons.woff2 +0 -0
  86. puppetboard/static/libs/jquery/jquery.min.js +2 -0
  87. puppetboard/static/libs/jszip/jszip.min.js +13 -0
  88. puppetboard/static/libs/moment.js/moment-with-locales.min.js +217 -0
  89. puppetboard/templates/400.html +5 -0
  90. puppetboard/templates/403.html +5 -0
  91. puppetboard/templates/404.html +5 -0
  92. puppetboard/templates/412.html +5 -0
  93. puppetboard/templates/500.html +10 -0
  94. puppetboard/templates/_facts_sorter.html +38 -0
  95. puppetboard/templates/_macros.html +195 -0
  96. puppetboard/templates/_static_offline.html +15 -0
  97. puppetboard/templates/_static_online.html +16 -0
  98. puppetboard/templates/catalog.html +63 -0
  99. puppetboard/templates/catalog_compare.html +90 -0
  100. puppetboard/templates/catalogs.html +21 -0
  101. puppetboard/templates/catalogs.json.tpl +40 -0
  102. puppetboard/templates/class.html +22 -0
  103. puppetboard/templates/class_resource.json.tpl +16 -0
  104. puppetboard/templates/classes.html +23 -0
  105. puppetboard/templates/classes.json.tpl +23 -0
  106. puppetboard/templates/fact.html +72 -0
  107. puppetboard/templates/facts.html +167 -0
  108. puppetboard/templates/failures.html +45 -0
  109. puppetboard/templates/index.html +151 -0
  110. puppetboard/templates/inventory.html +21 -0
  111. puppetboard/templates/inventory.json.tpl +23 -0
  112. puppetboard/templates/layout.html +121 -0
  113. puppetboard/templates/metric.html +28 -0
  114. puppetboard/templates/metrics.html +14 -0
  115. puppetboard/templates/node.html +94 -0
  116. puppetboard/templates/nodes.html +87 -0
  117. puppetboard/templates/query.html +210 -0
  118. puppetboard/templates/radiator.html +109 -0
  119. puppetboard/templates/report.html +196 -0
  120. puppetboard/templates/reports.html +87 -0
  121. puppetboard/templates/reports.json.tpl +29 -0
  122. puppetboard/utils.py +274 -0
  123. puppetboard/version.py +6 -0
  124. puppetboard/views/api.py +157 -0
  125. puppetboard/views/catalogs.py +160 -0
  126. puppetboard/views/classes.py +282 -0
  127. puppetboard/views/dailychart.py +114 -0
  128. puppetboard/views/facts.py +399 -0
  129. puppetboard/views/failures.py +86 -0
  130. puppetboard/views/index.py +115 -0
  131. puppetboard/views/inventory.py +104 -0
  132. puppetboard/views/metrics.py +85 -0
  133. puppetboard/views/nodes.py +103 -0
  134. puppetboard/views/query.py +167 -0
  135. puppetboard/views/radiator.py +107 -0
  136. puppetboard/views/reports.py +274 -0
  137. puppetboard_dp-8.2.0.dist-info/METADATA +474 -0
  138. puppetboard_dp-8.2.0.dist-info/RECORD +140 -0
  139. puppetboard_dp-8.2.0.dist-info/WHEEL +4 -0
  140. puppetboard_dp-8.2.0.dist-info/licenses/LICENSE +191 -0
@@ -0,0 +1,3 @@
1
+ #
2
+ # Puppetboard
3
+ #
puppetboard/app.py ADDED
@@ -0,0 +1,127 @@
1
+ from __future__ import absolute_import
2
+ from __future__ import unicode_literals
3
+
4
+ import logging
5
+ import os
6
+ import sys
7
+ from datetime import datetime
8
+
9
+ from flask import render_template, Response
10
+
11
+ # these imports are required by Flask - DO NOT remove them although they look unused
12
+ # noinspection PyUnresolvedReferences
13
+ import puppetboard.views.catalogs # noqa: F401
14
+ # noinspection PyUnresolvedReferences
15
+ import puppetboard.views.classes # noqa: F401
16
+ # noinspection PyUnresolvedReferences
17
+ import puppetboard.views.dailychart # noqa: F401
18
+ # noinspection PyUnresolvedReferences
19
+ import puppetboard.views.facts # noqa: F401
20
+ # noinspection PyUnresolvedReferences
21
+ import puppetboard.views.index # noqa: F401
22
+ # noinspection PyUnresolvedReferences
23
+ import puppetboard.views.inventory # noqa: F401
24
+ # noinspection PyUnresolvedReferences
25
+ import puppetboard.views.metrics # noqa: F401
26
+ # noinspection PyUnresolvedReferences
27
+ import puppetboard.views.nodes # noqa: F401
28
+ # noinspection PyUnresolvedReferences
29
+ import puppetboard.views.query # noqa: F401
30
+ # noinspection PyUnresolvedReferences
31
+ import puppetboard.views.radiator # noqa: F401
32
+ # noinspection PyUnresolvedReferences
33
+ import puppetboard.views.reports # noqa: F401
34
+ # noinspection PyUnresolvedReferences
35
+ import puppetboard.views.failures # noqa: F401
36
+ # noinspection PyUnresolvedReferences
37
+ import puppetboard.views.api # noqa: F401
38
+ import puppetboard.errors # noqa: F401
39
+
40
+ from puppetboard.core import get_app, get_puppetdb, get_scheduler
41
+ from puppetboard.version import __version__
42
+ from puppetboard.utils import is_a_test, check_db_version, check_secret_key
43
+
44
+ app = get_app()
45
+ puppetdb = get_puppetdb()
46
+ get_scheduler()
47
+ running_as = os.path.basename(sys.argv[0])
48
+ if not is_a_test():
49
+ check_db_version(puppetdb)
50
+ check_secret_key(app.config.get('SECRET_KEY'))
51
+
52
+ logging.basicConfig(level=app.config['LOGLEVEL'].upper())
53
+ log = logging.getLogger(__name__)
54
+
55
+ menu_entries = [
56
+ ('index', 'Overview'),
57
+ ('failures', 'Failures'),
58
+ ('nodes', 'Nodes'),
59
+ ('facts', 'Facts'),
60
+ ('reports', 'Reports'),
61
+ ('metrics', 'Metrics'),
62
+ ('inventory', 'Inventory'),
63
+ ('catalogs', 'Catalogs'),
64
+ ('classes', 'Classes'),
65
+ ('radiator', 'Radiator'),
66
+ ('query', 'Query'),
67
+ ]
68
+
69
+ if not app.config.get('ENABLE_QUERY'):
70
+ menu_entries.remove(('query', 'Query'))
71
+
72
+ if not app.config.get('ENABLE_CATALOG'):
73
+ menu_entries.remove(('catalogs', 'Catalogs'))
74
+
75
+ if not app.config.get('ENABLE_CLASS'):
76
+ menu_entries.remove(('classes', 'Classes'))
77
+
78
+ app.jinja_env.globals.update(menu_entries=menu_entries)
79
+
80
+
81
+ @app.context_processor
82
+ def utility_processor():
83
+ def now(format='%m/%d/%Y %H:%M:%S'):
84
+ """returns the formated datetime"""
85
+ return datetime.now().strftime(format)
86
+
87
+ def version():
88
+ return __version__
89
+
90
+ def fact_os_detection(os_facts):
91
+ os_name = ""
92
+ os_family = os_facts['family']
93
+
94
+ try:
95
+ if os_family == "windows":
96
+ os_name = os_facts["windows"]["product_name"]
97
+ elif os_family == "Darwin":
98
+ os_name = os_facts["macosx"]["product"]
99
+ else:
100
+ os_name = os_facts["distro"]["description"]
101
+ except KeyError:
102
+ pass
103
+
104
+ return os_name
105
+
106
+ return dict(
107
+ now=now,
108
+ version=version,
109
+ fact_os_detection=fact_os_detection,
110
+ )
111
+
112
+
113
+ @app.route('/offline/<path:filename>')
114
+ def offline_static(filename):
115
+ mimetype = 'text/html'
116
+ if filename.endswith('.css'):
117
+ mimetype = 'text/css'
118
+ elif filename.endswith('.js'):
119
+ mimetype = 'text/javascript'
120
+
121
+ return Response(response=render_template('static/%s' % filename),
122
+ status=200, mimetype=mimetype)
123
+
124
+
125
+ @app.route('/status')
126
+ def health_status():
127
+ return 'OK'
puppetboard/asgi.py ADDED
@@ -0,0 +1,40 @@
1
+ """ASGI entry point: the Flask web UI and API plus the MCP server on /mcp.
2
+
3
+ gunicorn -k asgi puppetboard.asgi:app
4
+
5
+ The Flask app keeps working on its own as WSGI (puppetboard.app:app).
6
+ """
7
+ from a2wsgi import WSGIMiddleware
8
+
9
+ from puppetboard.app import app as flask_app
10
+
11
+ wsgi = WSGIMiddleware(flask_app)
12
+ mcp = None
13
+ if flask_app.config['ENABLE_MCP']:
14
+ from puppetboard.mcp_server import McpEndpoint
15
+ mcp = McpEndpoint(flask_app.config)
16
+
17
+
18
+ async def app(scope, receive, send):
19
+ if scope['type'] == 'lifespan':
20
+ if mcp:
21
+ await mcp(scope, receive, send)
22
+ else:
23
+ await no_lifespan(receive, send)
24
+ return
25
+ root = scope.get('root_path', '')
26
+ path = scope['path'][len(root):] if root and scope['path'].startswith(root) else scope['path']
27
+ if mcp and scope['type'] == 'http' and path in ('/mcp', '/mcp/'):
28
+ await mcp({**scope, 'path': '/mcp', 'raw_path': b'/mcp', 'root_path': ''}, receive, send)
29
+ else:
30
+ await wsgi(scope, receive, send)
31
+
32
+
33
+ async def no_lifespan(receive, send):
34
+ while True:
35
+ message = await receive()
36
+ if message['type'] == 'lifespan.startup':
37
+ await send({'type': 'lifespan.startup.complete'})
38
+ elif message['type'] == 'lifespan.shutdown':
39
+ await send({'type': 'lifespan.shutdown.complete'})
40
+ return
puppetboard/core.py ADDED
@@ -0,0 +1,277 @@
1
+ import logging
2
+ import re
3
+ import socket
4
+ import time
5
+ from importlib.metadata import version
6
+
7
+ from flask import Flask
8
+ from markupsafe import escape
9
+ from flask_caching import Cache
10
+ from flask_apscheduler import APScheduler
11
+ from werkzeug.middleware.proxy_fix import ProxyFix
12
+ from pypuppetdb import connect
13
+
14
+ from puppetboard.utils import (get_or_abort, jsonprint,
15
+ url_for_field, quote_columns_data)
16
+ from puppetboard.version import __version__ as own_version
17
+
18
+ REPORTS_COLUMNS = [
19
+ {'attr': 'end', 'filter': 'end_time',
20
+ 'name': 'End time', 'type': 'datetime'},
21
+ {'attr': 'status', 'name': 'Status', 'type': 'status'},
22
+ {'attr': 'certname', 'name': 'Certname', 'type': 'node'},
23
+ {'attr': 'version', 'filter': 'configuration_version',
24
+ 'name': 'Configuration version'},
25
+ {'attr': 'agent_version', 'filter': 'puppet_version',
26
+ 'name': 'Agent version'},
27
+ ]
28
+
29
+ CATALOGS_COLUMNS = [
30
+ {'attr': 'certname', 'name': 'Certname', 'type': 'node'},
31
+ {'attr': 'catalog_timestamp', 'name': 'Compile Time'},
32
+ {'attr': 'form', 'name': 'Compare'},
33
+ ]
34
+
35
+ APP = None
36
+ PUPPETDB = None
37
+ CACHE = None
38
+ SCHEDULER = None
39
+
40
+
41
+ def get_app():
42
+ global APP
43
+
44
+ if APP is None:
45
+ app = Flask(__name__)
46
+ app.config.from_object('puppetboard.default_settings')
47
+ app.config.from_envvar('PUPPETBOARD_SETTINGS', silent=True)
48
+ app.secret_key = app.config['SECRET_KEY']
49
+
50
+ logging.basicConfig(level=app.config['LOGLEVEL'].upper())
51
+
52
+ # The *.json.tpl templates build HTML for DataTables cells: escape them too
53
+ app.jinja_env.autoescape = lambda name: name is None or name.endswith(
54
+ ('.html', '.htm', '.xml', '.xhtml', '.svg', '.tpl'))
55
+ app.jinja_env.trim_blocks = True
56
+ app.jinja_env.lstrip_blocks = True
57
+ app.jinja_env.filters['jsonprint'] = jsonprint
58
+ app.jinja_env.globals['url_for_field'] = url_for_field
59
+ app.jinja_env.globals['quote_columns_data'] = quote_columns_data
60
+ app.jinja_env.add_extension('jinja2.ext.do')
61
+ # Trust one level of proxy headers (e.g. X-Forwarded-Proto from Traefik)
62
+ # so Flask generates correct https:// URLs and doesn't issue http:// redirects
63
+ app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
64
+ APP = app
65
+
66
+ return APP
67
+
68
+
69
+ def get_puppetdb():
70
+ global PUPPETDB
71
+
72
+ if PUPPETDB is None:
73
+ app = get_app()
74
+ puppetdb = connect(host=app.config['PUPPETDB_HOST'],
75
+ port=app.config['PUPPETDB_PORT'],
76
+ ssl_verify=app.config['PUPPETDB_SSL_VERIFY'],
77
+ ssl_key=app.config['PUPPETDB_KEY'],
78
+ ssl_cert=app.config['PUPPETDB_CERT'],
79
+ timeout=app.config['PUPPETDB_TIMEOUT'],
80
+ protocol=app.config['PUPPETDB_PROTO'], )
81
+
82
+ requests_version = version("requests")
83
+ user_agent_header = {
84
+ "user-agent": f"puppetboard/{own_version} (r/{requests_version})",
85
+ }
86
+ puppetdb.session.headers = {**puppetdb.session.headers, **user_agent_header}
87
+
88
+ PUPPETDB = puppetdb
89
+
90
+ return PUPPETDB
91
+
92
+
93
+ def get_cache():
94
+ global CACHE
95
+
96
+ if CACHE is None:
97
+ app = get_app()
98
+ cache = Cache()
99
+ cache.init_app(app)
100
+
101
+ CACHE = cache
102
+
103
+ return CACHE
104
+
105
+
106
+ def get_scheduler():
107
+ global SCHEDULER
108
+
109
+ if SCHEDULER is None:
110
+ app = get_app()
111
+ if not app.config['SCHEDULER_ENABLED']:
112
+ return SCHEDULER
113
+
114
+ try:
115
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
116
+ sock.bind(('127.0.0.1', app.config['SCHEDULER_LOCK_BIND_PORT']))
117
+ except socket.error:
118
+ print('scheduler already running: socket.error')
119
+ return SCHEDULER
120
+ else:
121
+ print('scheduler enabled')
122
+ scheduler = APScheduler()
123
+ scheduler.init_app(app)
124
+ scheduler.start()
125
+
126
+ SCHEDULER = scheduler
127
+
128
+ return SCHEDULER
129
+
130
+
131
+ # Every page lists the environments; they change rarely, so ask PuppetDB at
132
+ # most once a minute per process instead of on every request.
133
+ ENVIRONMENTS_TTL = 60
134
+ _environments_cache: dict = {}
135
+
136
+
137
+ def environment_names() -> list:
138
+ cached = _environments_cache.get('names')
139
+ if cached and time.monotonic() - cached[0] < ENVIRONMENTS_TTL:
140
+ return cached[1]
141
+ names = sorted(env['name'] for env in get_or_abort(get_puppetdb().environments))
142
+ _environments_cache['names'] = (time.monotonic(), names)
143
+ return names
144
+
145
+
146
+ def environments() -> dict:
147
+ envs = {}
148
+ envs_from_db = environment_names()
149
+
150
+ # Adding all environments
151
+ envs['All Environments'] = {
152
+ 'url': url_for_field('env', '*'),
153
+ 'icon': 'server',
154
+ 'divider': True
155
+ }
156
+
157
+ # Adding favorite envs
158
+ favorite_envs = [
159
+ env
160
+ for env in get_app().config.get('FAVORITE_ENVS')
161
+ if env in envs_from_db
162
+ ]
163
+
164
+ divider = True
165
+ for env in favorite_envs:
166
+ envs[env] = {
167
+ 'url': url_for_field('env', env),
168
+ 'icon': 'star',
169
+ 'divider': divider
170
+ }
171
+ # Only the first envs from this group should have a divider
172
+ divider = False
173
+
174
+ # Adding other envs
175
+ divider = True
176
+ for env in envs_from_db:
177
+ if env in favorite_envs:
178
+ continue
179
+
180
+ envs[env] = {
181
+ 'url': url_for_field('env', env),
182
+ 'icon': '',
183
+ 'divider': divider
184
+ }
185
+ # Only the first envs from this group should have a divider
186
+ divider = False
187
+
188
+ return envs
189
+
190
+
191
+ # as documented in
192
+ # https://flask.palletsprojects.com/en/2.0.x/patterns/streaming/#streaming-from-templates
193
+ def stream_template(template_name, **context):
194
+ app = get_app()
195
+ app.update_template_context(context)
196
+ t = app.jinja_env.get_template(template_name)
197
+ rv = t.stream(context)
198
+ # about 100 template events per chunk (~10 KB) instead of 5 (~120 bytes):
199
+ # thousands of tiny writes were the main cost of big streamed pages
200
+ rv.enable_buffering(100)
201
+ return rv
202
+
203
+
204
+ def get_raw_error(source: str, message: str) -> str:
205
+ """Report log entry as HTML; the node-controlled text is escaped."""
206
+ # prefix with source, if it's not trivial
207
+ if source != 'Puppet':
208
+ message = source + "\n\n" + message
209
+ message = str(escape(message))
210
+
211
+ if '\n' in message:
212
+ message = f"<pre>{message}</pre>"
213
+
214
+ return message
215
+
216
+
217
+ def get_friendly_error(source: str, message: str, certname: str) -> str:
218
+ # NOTE: the order of the below operations matters in some cases!
219
+
220
+ # prefix with source, if it's not trivial
221
+ if source != 'Puppet':
222
+ message = source + "\n\n" + message
223
+
224
+ # shorten the file paths
225
+ code_prefix_to_remove = get_app().config['CODE_PREFIX_TO_REMOVE']
226
+ message = re.sub(f'file: {code_prefix_to_remove}', 'file: …', message)
227
+
228
+ # remove some unuseful parts
229
+ too_long_prefix = "Could not retrieve catalog from remote server: " \
230
+ "Error 500 on SERVER: " \
231
+ "Server Error: "
232
+ message = re.sub(f'^{too_long_prefix}', '', message)
233
+
234
+ message = re.sub(r"(Evaluation Error: Error while evaluating a )",
235
+ r"Error while evaluating a ", message)
236
+
237
+ # remove redundant certnames
238
+ # certnames contain dots and come from the report, so match them literally
239
+ redundant_certname = re.escape(f" on node {certname}")
240
+ message = re.sub(f'{redundant_certname}$', '', message)
241
+
242
+ redundant_certname = re.escape(f" for {certname}")
243
+ message = re.sub(f'{redundant_certname} ', ' ', message)
244
+
245
+ # add extra line breaks for readability
246
+ message = re.sub(r"(Error while evaluating a .*?),",
247
+ r"\1:\n\n", message)
248
+
249
+ message = re.sub(r"( returned \d+:) ",
250
+ r"\1\n\n", message)
251
+
252
+ # reformat and rephrase ending expression that says where in the code is the error
253
+ # NOTE: this has to be done AFTER removing " on node ..."
254
+ # but BEFORE replacing spaces with &nbsp;
255
+ message = re.sub(r"(\S)\s+\(file: ([0-9a-zA-Z/_\-.…]+, line: \d+, column: \d+)\)\s*$",
256
+ r"\1\n\n…in \2.", message)
257
+
258
+ message = re.sub(r"(\S)\s+\(file: ([0-9a-zA-Z/_\-.…]+, line: \d+)\)\s*$",
259
+ r"\1\n\n…in \2.", message)
260
+
261
+ return message
262
+
263
+
264
+ def to_html(message: str) -> str:
265
+ """Plain text (e.g. from get_friendly_error) as HTML; the text is escaped."""
266
+ message = str(escape(message))
267
+
268
+ # replace \n with <br/> to not have to use <pre> which breaks wrapping
269
+ message = re.sub(r"\n", "<br/>", message)
270
+
271
+ # prevent line breaking inside expressions that provide code location
272
+ message = re.sub(r"\(file: (.*?), line: (.*?), column: (.*?)\)",
273
+ r"(file:&nbsp;\1,&nbsp;line:&nbsp;\2,&nbsp;column:&nbsp;\3)", message)
274
+ message = re.sub(r"\(file: (.*?), line: (.*?)\)",
275
+ r"(file:&nbsp;\1,&nbsp;line:&nbsp;\2)", message)
276
+
277
+ return message