g3w-admin-plugins 0.0.1__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.
- g3w_admin_plugins-0.0.1.dist-info/METADATA +99 -0
- g3w_admin_plugins-0.0.1.dist-info/RECORD +19 -0
- g3w_admin_plugins-0.0.1.dist-info/WHEEL +5 -0
- g3w_admin_plugins-0.0.1.dist-info/top_level.txt +1 -0
- g3w_plugins/README.md +94 -0
- g3w_plugins/__init__.py +0 -0
- g3w_plugins/apps.py +39 -0
- g3w_plugins/jobs.py +253 -0
- g3w_plugins/management/README.md +25 -0
- g3w_plugins/management/__init__.py +0 -0
- g3w_plugins/management/commands/__init__.py +0 -0
- g3w_plugins/management/commands/plugins.py +33 -0
- g3w_plugins/manager.py +281 -0
- g3w_plugins/reload.py +185 -0
- g3w_plugins/sitetrees.py +20 -0
- g3w_plugins/templates/plugins/index.html +333 -0
- g3w_plugins/urls.py +11 -0
- g3w_plugins/views.py +241 -0
- g3w_plugins/windows_locks.py +100 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: g3w-admin-plugins
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: On-demand manager for public G3W-ADMIN plugins
|
|
5
|
+
Project-URL: Homepage, https://github.com/raruto/g3w-admin-plugins
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/raruto/g3w-admin-plugins/issues
|
|
7
|
+
Classifier: Environment :: Web Environment
|
|
8
|
+
Classifier: Framework :: Django
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
Requires-Dist: Django>=4.2
|
|
14
|
+
Requires-Dist: packaging>=21
|
|
15
|
+
|
|
16
|
+
# G3W-ADMIN-PLUGINS
|
|
17
|
+
|
|
18
|
+
[](https://pypi.org/project/g3w-admin-plugins/)
|
|
19
|
+
|
|
20
|
+
Manage public G3W-ADMIN plugins and bundled modules from a superuser page or the
|
|
21
|
+
command line. External packages are installed only when enabled. The PyPI badge
|
|
22
|
+
will show a version after the first publication.
|
|
23
|
+
|
|
24
|
+
> Installing plugins and their dependencies executes third-party code. Restrict
|
|
25
|
+
> access to trusted superusers, review packages and back up the state directory
|
|
26
|
+
> and database before changing plugins.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Install this repository in the Python environment used by G3W-ADMIN:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
python -m pip install /path/to/g3w-admin-plugins
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Set `G3W_PLUGINS_DIR` to a persistent, writable directory shared by the web and
|
|
37
|
+
consumer processes. In `base/settings/local_settings.py`, immediately after
|
|
38
|
+
defining `G3WADMIN_LOCAL_MORE_APPS` and before plugin-dependent settings, add:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from g3w_plugins.manager import configure
|
|
42
|
+
configure(G3WADMIN_LOCAL_MORE_APPS)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Restart G3W-ADMIN and register the menu:
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
python manage.py sitetree_resync_apps g3w_plugins
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Open **Plugin** in the sidebar as a superuser. The URL is `/admin/plugins/` when
|
|
52
|
+
a frontend is enabled, otherwise `/plugins/`; include any configured language prefix.
|
|
53
|
+
|
|
54
|
+
### Docker
|
|
55
|
+
|
|
56
|
+
Published G3W Docker settings and entrypoint do not install or configure this
|
|
57
|
+
manager automatically. Install it in both web and consumer containers and
|
|
58
|
+
configure it as above. In Docker, the default state directory is
|
|
59
|
+
`/shared-volume/.g3w_plugins`.
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
python manage.py plugins list
|
|
65
|
+
python manage.py plugins enable g3w-admin-panoramax
|
|
66
|
+
python manage.py plugins disable g3w-admin-panoramax
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
| Topic | Behaviour |
|
|
70
|
+
|---|---|
|
|
71
|
+
| Catalog | Seven explicitly listed PyPI plugins; new packages require a catalog change. |
|
|
72
|
+
| Bundled modules | `editing`, `filemanager`, `qplotly` and `qtimeseries` are controlled here but updated with G3W-SUITE, not PyPI. Disabling them retains their data. If `qes` is installed, disable it before disabling `editing`. |
|
|
73
|
+
| Existing configuration | Unmanaged apps remain unchanged. Once managed, their saved desired state overrides the original setting. |
|
|
74
|
+
| Preparation | Enable, disable and update run migrations, collect static files and resynchronize menus before saving the desired state. Plugin-specific settings, external services and version compatibility remain your responsibility. |
|
|
75
|
+
| Restart | CLI changes require restarting web and consumer processes. The UI can request automatic reload on supported Docker deployments; otherwise restart manually. |
|
|
76
|
+
| Updates | The UI checks PyPI and offers newer stable versions for enabled external plugins. A displayed release may still be incompatible; checks and installation require network access. |
|
|
77
|
+
| Disable | Does not uninstall packages, reverse migrations or delete data. |
|
|
78
|
+
|
|
79
|
+
If portal is the configured frontend, change `FRONTEND` before disabling it.
|
|
80
|
+
Published Docker settings include portal by default. After upgrading from the
|
|
81
|
+
former `plugins` app, restart web and consumer processes and run
|
|
82
|
+
`python manage.py sitetree_resync_apps g3w_plugins` to refresh the sidebar.
|
|
83
|
+
|
|
84
|
+
For the state lifecycle, recovery, reload restrictions and deployment details, see
|
|
85
|
+
[plugin internals](g3w_plugins/README.md). For CLI output and behavior, see
|
|
86
|
+
[management commands](g3w_plugins/management/README.md).
|
|
87
|
+
|
|
88
|
+
## Development and releases
|
|
89
|
+
|
|
90
|
+
See [BDD tests](g3w_plugins/tests/README.md) for local, browser and Docker runs.
|
|
91
|
+
CI reuses the shared G3W plugin checks and runs Docker scenarios against the
|
|
92
|
+
published development image and upstream Docker settings.
|
|
93
|
+
|
|
94
|
+
After a successful push, CD uses the shared release actions to publish to PyPI and
|
|
95
|
+
create a GitHub release. Configure PyPI Trusted Publishing for
|
|
96
|
+
`raruto/g3w-admin-plugins` and `.github/workflows/cd.yml`, and allow GitHub
|
|
97
|
+
Actions to write repository contents. The CD workflow must exist on the default
|
|
98
|
+
branch. The shared release action publishes from any pushed branch and starts at
|
|
99
|
+
`v0.0.1` if there are no version tags; package versions are derived from Git tags.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
g3w_plugins/README.md,sha256=Ccei9gsJEJOb4MXR4k5lxrFvadV5ntogvsW_JmgI848,5398
|
|
2
|
+
g3w_plugins/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
g3w_plugins/apps.py,sha256=gDusWJXSdUo7f0xgmJciEjtfs5tNzieAwUsBtjr9OAU,1443
|
|
4
|
+
g3w_plugins/jobs.py,sha256=1DFn_MpJjyX29Z8aw6KoDh-cF8e9NOlgzzZJjLyyBeg,9524
|
|
5
|
+
g3w_plugins/manager.py,sha256=kwbnLSBL4ffsNwkVLbeYmFoxtqMBMG4g-l1jglmPuCY,12322
|
|
6
|
+
g3w_plugins/reload.py,sha256=m84kCbUQh8hB4GPq3PYLovAVmZiHXjkeHvxV69WWupI,7476
|
|
7
|
+
g3w_plugins/sitetrees.py,sha256=SIZcnK-wA5Riut1NToRXtPosd4iBf-9_C-wsYLn4cRM,622
|
|
8
|
+
g3w_plugins/urls.py,sha256=oNEf9utRItnnLXMK9zfeiiM0YegYCrwlkquU3Dg30h8,288
|
|
9
|
+
g3w_plugins/views.py,sha256=w_xs4gaBjJ0HcEuBea_slvbF4r3bqEArJjapQUef05Q,11007
|
|
10
|
+
g3w_plugins/windows_locks.py,sha256=yeiNzYWqqjJmRZjb3B6_OTNPCWCkERUUmeJtG1r54TM,3752
|
|
11
|
+
g3w_plugins/management/README.md,sha256=5lKv3p5vo8_rEE1pY-7nk3EyK7-Y-1lkrL4vzrTaPS8,1437
|
|
12
|
+
g3w_plugins/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
g3w_plugins/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
g3w_plugins/management/commands/plugins.py,sha256=LR0mJxnoAZpBadnv7lmXUN0aBFPdQRvSztax57yEnx8,1426
|
|
15
|
+
g3w_plugins/templates/plugins/index.html,sha256=gbUAQPagOm3pfzwsPKW-ot-1nZkdbg0ODkT-4zJFPwQ,19392
|
|
16
|
+
g3w_admin_plugins-0.0.1.dist-info/METADATA,sha256=UgD5DfLuhdOWslmAmUBUkRg8Tftr41uiQJr8OdobwcA,4649
|
|
17
|
+
g3w_admin_plugins-0.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
18
|
+
g3w_admin_plugins-0.0.1.dist-info/top_level.txt,sha256=hUFWntA7EXYVMesliQ_Yay1MMPqCibDufGNMYPaT2UE,12
|
|
19
|
+
g3w_admin_plugins-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
g3w_plugins
|
g3w_plugins/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Plugin internals
|
|
2
|
+
|
|
3
|
+
This Django app maintains a desired plugin configuration separately from the configuration
|
|
4
|
+
loaded by each running process. See the [main README](../README.md) for installation and use.
|
|
5
|
+
|
|
6
|
+
## Modules
|
|
7
|
+
|
|
8
|
+
| Path | Responsibility |
|
|
9
|
+
|---|---|
|
|
10
|
+
| [`manager.py`](manager.py) | Catalog, persistent state, locking, package installation and `configure()` |
|
|
11
|
+
| [`jobs.py`](jobs.py) | Background preparation, progress and interrupted-job detection |
|
|
12
|
+
| [`reload.py`](reload.py) | Candidate preparation, web reload and consumer heartbeat |
|
|
13
|
+
| [`windows_locks.py`](windows_locks.py) | Windows locks and handle inheritance for child processes |
|
|
14
|
+
| [`apps.py`](apps.py), [`sitetrees.py`](sitetrees.py) | App startup, superuser-only menu and consumer watcher |
|
|
15
|
+
| [`urls.py`](urls.py), [`views.py`](views.py) | Superuser page, actions, status and PyPI version checks |
|
|
16
|
+
| [`templates/plugins/index.html`](templates/plugins/index.html) | Page, status polling and browser-side controls |
|
|
17
|
+
| [`management/`](management/README.md) | Synchronous Django management command |
|
|
18
|
+
| [`tests/`](tests/README.md) | BDD features and Python steps |
|
|
19
|
+
|
|
20
|
+
## Change lifecycle
|
|
21
|
+
|
|
22
|
+
```mermaid
|
|
23
|
+
flowchart TD
|
|
24
|
+
A[Superuser action] --> B[Background job or synchronous fallback]
|
|
25
|
+
B --> C[Lock and validate]
|
|
26
|
+
C --> D[Install selected PyPI packages if needed]
|
|
27
|
+
D --> E[Write candidate state]
|
|
28
|
+
E --> F[migrate, collectstatic, sitetree_resync_apps]
|
|
29
|
+
F --> G[Publish state atomically]
|
|
30
|
+
G --> H{Web job with automatic reload?}
|
|
31
|
+
H -->|Yes| I[Request web and optional consumer reload]
|
|
32
|
+
H -->|No| J[Manual restart]
|
|
33
|
+
K[Management command] --> C
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The CLI follows the same preparation path synchronously and always requires a manual
|
|
37
|
+
restart. In the web UI, status distinguishes the desired state from the state loaded by
|
|
38
|
+
the responding worker. With Huey enabled, confirmation also requires a fresh consumer
|
|
39
|
+
heartbeat for that state. It does not confirm that every web worker has restarted or
|
|
40
|
+
that individual Huey tasks have run.
|
|
41
|
+
|
|
42
|
+
`configure()` runs during Django settings initialization, before plugin-specific
|
|
43
|
+
settings. It loads the published state, or the temporary candidate when invoked by a
|
|
44
|
+
preparation subprocess, and updates `G3WADMIN_LOCAL_MORE_APPS`. Apps without a saved
|
|
45
|
+
decision retain their original configuration.
|
|
46
|
+
|
|
47
|
+
## Persistent state
|
|
48
|
+
|
|
49
|
+
All paths below are relative to `G3W_PLUGINS_DIR` (default
|
|
50
|
+
`/shared-volume/.g3w_plugins` in Docker):
|
|
51
|
+
|
|
52
|
+
| Path | Purpose |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `state.json` | Desired enabled/disabled decisions and selected package directory |
|
|
55
|
+
| `site-<uuid>/` | Isolated installation of selected PyPI plugins and dependencies |
|
|
56
|
+
| `.state-*` | Temporary candidate used during preparation |
|
|
57
|
+
| `job.json` | Latest background operation, phase and result |
|
|
58
|
+
| `consumer-heartbeat.json`, `reload-request.json` | Consumer reload coordination |
|
|
59
|
+
| `.lock`, `.job.lock`, `.job-status.lock` | POSIX process locks; Windows uses named locks instead |
|
|
60
|
+
|
|
61
|
+
State publication uses an atomic replacement after successful preparation. Failed or
|
|
62
|
+
interrupted preparation can still have changed the database, static files or menus;
|
|
63
|
+
check logs and state before retrying. Old package directories remain available to
|
|
64
|
+
running workers. Back up the state directory and database before changes, and remove old
|
|
65
|
+
directories only after stopping all processes that use them.
|
|
66
|
+
|
|
67
|
+
Installation constrains dependencies already in the G3W environment; conflicts fail
|
|
68
|
+
rather than upgrade that environment. An update installs the requested newer stable
|
|
69
|
+
release and pins other managed plugins to their installed versions. PyPI checks are
|
|
70
|
+
cached for five minutes per web worker; a displayed release can still be incompatible.
|
|
71
|
+
The installed version shown is from the current worker and changes after reload.
|
|
72
|
+
|
|
73
|
+
## Deployment constraints
|
|
74
|
+
|
|
75
|
+
- Protect the state directory and package sources from untrusted writes. Enabling a
|
|
76
|
+
catalogued package also installs its dependencies and executes third-party code.
|
|
77
|
+
- Web and consumer processes need the same state and installed packages, Python
|
|
78
|
+
platform and compatible G3W image. A dependency conflict after an image change
|
|
79
|
+
stops startup. To rebuild without starting Django, run
|
|
80
|
+
`python -c "from g3w_plugins.manager import change; change('g3w-admin-panoramax', True)"`
|
|
81
|
+
in the new runtime, naming an enabled external plugin and setting `G3W_PLUGINS_DIR`.
|
|
82
|
+
- GUI preparation runs in a separate process on POSIX and Windows; one GUI change
|
|
83
|
+
runs at a time. Windows processes using the same state directory must share one
|
|
84
|
+
host and resolved path, and must not share it with Linux. Stop all web and CLI
|
|
85
|
+
processes once when updating to this locking scheme.
|
|
86
|
+
- Automatic reload requires Linux pidfds, Gunicorn or Granian, one web service
|
|
87
|
+
instance and at most one Huey consumer. Reload an existing web process once
|
|
88
|
+
after updating the manager. If Huey is enabled, restart the consumer once to
|
|
89
|
+
start its watcher and use the current heartbeat format.
|
|
90
|
+
- If reload fails, retry from the page or restart web and consumer manually.
|
|
91
|
+
Huey waits for running tasks before restarting. The page can follow a changed
|
|
92
|
+
manager URL if the frontend changes.
|
|
93
|
+
- Confirmed success messages disappear on a browser-tab refresh; pending operations
|
|
94
|
+
and errors remain visible. Without JavaScript, refresh to check progress.
|
g3w_plugins/__init__.py
ADDED
|
File without changes
|
g3w_plugins/apps.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from django.apps import AppConfig
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def filter_plugin_menu(tree_items, context):
|
|
8
|
+
request = context.get("request") if context else None
|
|
9
|
+
user = getattr(request, "user", None)
|
|
10
|
+
if user and user.is_active and user.is_superuser:
|
|
11
|
+
return tree_items
|
|
12
|
+
return [item for item in tree_items if item.url != "g3w-plugins"]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class PluginsConfig(AppConfig):
|
|
16
|
+
name = "g3w_plugins"
|
|
17
|
+
verbose_name = "G3W Plugins"
|
|
18
|
+
|
|
19
|
+
def ready(self):
|
|
20
|
+
from sitetree import sitetreeapp
|
|
21
|
+
|
|
22
|
+
previous = sitetreeapp._ITEMS_PROCESSOR
|
|
23
|
+
if not getattr(previous, "_g3w_plugins_filter", False):
|
|
24
|
+
previous_args = sitetreeapp._ITEMS_PROCESSOR_ARGS_LEN
|
|
25
|
+
|
|
26
|
+
def filter_items(tree_items, tree_sender, context):
|
|
27
|
+
if previous:
|
|
28
|
+
if previous_args == 2:
|
|
29
|
+
tree_items = previous(tree_items=tree_items, tree_sender=tree_sender)
|
|
30
|
+
else:
|
|
31
|
+
tree_items = previous(tree_items=tree_items, tree_sender=tree_sender, context=context)
|
|
32
|
+
return filter_plugin_menu(tree_items, context)
|
|
33
|
+
|
|
34
|
+
filter_items._g3w_plugins_filter = True
|
|
35
|
+
sitetreeapp.register_items_hook(filter_items)
|
|
36
|
+
|
|
37
|
+
if os.environ.get("G3WSUITE_CONSUMER") == "0" and "run_huey" in sys.argv:
|
|
38
|
+
from .reload import start_consumer_watcher
|
|
39
|
+
start_consumer_watcher()
|
g3w_plugins/jobs.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""Run plugin changes in background processes and report their progress."""
|
|
2
|
+
|
|
3
|
+
import contextlib
|
|
4
|
+
import json
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
import time
|
|
11
|
+
|
|
12
|
+
from django.conf import settings
|
|
13
|
+
|
|
14
|
+
from .manager import CATALOG, MANAGED, directory, validate_change
|
|
15
|
+
from .reload import available as reload_available, fingerprint, open_master, request_reload, write_json
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def available():
|
|
22
|
+
return os.name in ("posix", "nt")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def lock_job(root):
|
|
26
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
27
|
+
if os.name == "nt":
|
|
28
|
+
from .windows_locks import Lock
|
|
29
|
+
return Lock(root, "job", False)
|
|
30
|
+
import fcntl
|
|
31
|
+
|
|
32
|
+
lock = (root / ".job.lock").open("a+b")
|
|
33
|
+
try:
|
|
34
|
+
fcntl.flock(lock.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
35
|
+
except BaseException:
|
|
36
|
+
lock.close()
|
|
37
|
+
raise
|
|
38
|
+
return lock
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def acquire_job(root):
|
|
42
|
+
deadline = time.monotonic() + 0.5
|
|
43
|
+
while True:
|
|
44
|
+
try:
|
|
45
|
+
return lock_job(root)
|
|
46
|
+
except BlockingIOError:
|
|
47
|
+
if time.monotonic() >= deadline:
|
|
48
|
+
raise
|
|
49
|
+
time.sleep(0.05)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@contextlib.contextmanager
|
|
53
|
+
def locked_status(root):
|
|
54
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
55
|
+
if os.name == "nt":
|
|
56
|
+
from .windows_locks import Lock
|
|
57
|
+
with Lock(root, "job-status", True):
|
|
58
|
+
yield
|
|
59
|
+
else:
|
|
60
|
+
import fcntl
|
|
61
|
+
with (root / ".job-status.lock").open("a+b") as lock:
|
|
62
|
+
fcntl.flock(lock.fileno(), fcntl.LOCK_EX)
|
|
63
|
+
try:
|
|
64
|
+
yield
|
|
65
|
+
finally:
|
|
66
|
+
fcntl.flock(lock.fileno(), fcntl.LOCK_UN)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def read_job(root):
|
|
70
|
+
with locked_status(root):
|
|
71
|
+
path = root / "job.json"
|
|
72
|
+
if not path.exists():
|
|
73
|
+
return None
|
|
74
|
+
with path.open(encoding="utf-8") as file:
|
|
75
|
+
return json.load(file)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def write_job(root, job):
|
|
79
|
+
with locked_status(root):
|
|
80
|
+
write_json(root / "job.json", job)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def status():
|
|
84
|
+
if not available():
|
|
85
|
+
return None
|
|
86
|
+
root = directory()
|
|
87
|
+
try:
|
|
88
|
+
lock = lock_job(root)
|
|
89
|
+
except BlockingIOError:
|
|
90
|
+
job = read_job(root)
|
|
91
|
+
if job and job["status"] == "running":
|
|
92
|
+
return job
|
|
93
|
+
return {"status": "running"}
|
|
94
|
+
with lock:
|
|
95
|
+
job = read_job(root)
|
|
96
|
+
if job is None:
|
|
97
|
+
return None
|
|
98
|
+
if job["status"] == "running":
|
|
99
|
+
job["status"] = "failed"
|
|
100
|
+
job["error"] = (
|
|
101
|
+
"The operation was interrupted. The configuration, database or static files "
|
|
102
|
+
"may already have changed; check the server logs and plugin state before retrying."
|
|
103
|
+
)
|
|
104
|
+
return job
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def start(name, action, update_version=None):
|
|
108
|
+
"""Start one background job, passing its lock to the child process."""
|
|
109
|
+
if not available():
|
|
110
|
+
raise RuntimeError("Background preparation is unavailable on this platform")
|
|
111
|
+
if name not in MANAGED or action not in ("enable", "disable", "update"):
|
|
112
|
+
raise ValueError("Invalid plugin or action")
|
|
113
|
+
if action == "update" and name not in CATALOG:
|
|
114
|
+
raise ValueError("Bundled modules are updated with G3W-SUITE, not PyPI")
|
|
115
|
+
if action == "update" and not update_version:
|
|
116
|
+
raise ValueError("An update version is required")
|
|
117
|
+
validate_change(name, action != "disable")
|
|
118
|
+
root = directory()
|
|
119
|
+
try:
|
|
120
|
+
lock = acquire_job(root)
|
|
121
|
+
except BlockingIOError as error:
|
|
122
|
+
raise RuntimeError("A plugin preparation is already running") from error
|
|
123
|
+
with lock:
|
|
124
|
+
job = {"status": "running", "plugin": name, "action": action, "phase": "starting"}
|
|
125
|
+
write_job(root, job)
|
|
126
|
+
launched = False
|
|
127
|
+
reload_fd = None
|
|
128
|
+
try:
|
|
129
|
+
env = dict(os.environ, DJANGO_SETTINGS_MODULE=settings.SETTINGS_MODULE)
|
|
130
|
+
package_root = str(Path(__file__).resolve().parent.parent)
|
|
131
|
+
env["PYTHONPATH"] = os.pathsep.join(
|
|
132
|
+
[package_root] + ([env["PYTHONPATH"]] if env.get("PYTHONPATH") else [])
|
|
133
|
+
)
|
|
134
|
+
for key in ("G3W_PLUGINS_AUTO_RELOAD", "G3W_PLUGINS_RELOAD_FD",
|
|
135
|
+
"G3W_PLUGINS_RELOAD_ERROR", "G3W_PLUGINS_JOB_FD"):
|
|
136
|
+
env.pop(key, None)
|
|
137
|
+
if reload_available():
|
|
138
|
+
env["G3W_PLUGINS_AUTO_RELOAD"] = "1"
|
|
139
|
+
try:
|
|
140
|
+
reload_fd = open_master()
|
|
141
|
+
env["G3W_PLUGINS_RELOAD_FD"] = str(reload_fd)
|
|
142
|
+
except (OSError, RuntimeError) as error:
|
|
143
|
+
logger.warning("Cannot prepare automatic reload: %s", error)
|
|
144
|
+
env["G3W_PLUGINS_RELOAD_ERROR"] = str(error)
|
|
145
|
+
command = [sys.executable, "-m", "g3w_plugins.jobs", action, name]
|
|
146
|
+
if action == "update":
|
|
147
|
+
command.append(update_version)
|
|
148
|
+
if os.name == "nt":
|
|
149
|
+
from .windows_locks import inherited
|
|
150
|
+
env["G3W_PLUGINS_JOB_HANDLE"] = str(lock.handle)
|
|
151
|
+
try:
|
|
152
|
+
with inherited([lock.handle]) as options:
|
|
153
|
+
subprocess.Popen(command, env=env, cwd=Path(settings.BASE_DIR).parent,
|
|
154
|
+
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
|
|
155
|
+
start_new_session=True, **options)
|
|
156
|
+
launched = True
|
|
157
|
+
finally:
|
|
158
|
+
if launched:
|
|
159
|
+
lock.detach()
|
|
160
|
+
else:
|
|
161
|
+
env["G3W_PLUGINS_JOB_FD"] = str(lock.fileno())
|
|
162
|
+
subprocess.Popen(command, env=env, cwd=Path(settings.BASE_DIR).parent,
|
|
163
|
+
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
|
|
164
|
+
start_new_session=True,
|
|
165
|
+
pass_fds=(lock.fileno(),) + ((reload_fd,) if reload_fd is not None else ()))
|
|
166
|
+
except OSError as error:
|
|
167
|
+
if launched:
|
|
168
|
+
raise RuntimeError(
|
|
169
|
+
f"Preparation started, but launch cleanup failed: {error}. Check its status."
|
|
170
|
+
) from error
|
|
171
|
+
job.update(status="failed", error=f"Could not start preparation: {error}")
|
|
172
|
+
write_job(root, job)
|
|
173
|
+
raise RuntimeError(job["error"]) from error
|
|
174
|
+
finally:
|
|
175
|
+
if reload_fd is not None:
|
|
176
|
+
os.close(reload_fd)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def retry_reload():
|
|
180
|
+
root = directory()
|
|
181
|
+
try:
|
|
182
|
+
lock = acquire_job(root)
|
|
183
|
+
except BlockingIOError as error:
|
|
184
|
+
raise RuntimeError("Wait for plugin preparation to finish before reloading") from error
|
|
185
|
+
with lock:
|
|
186
|
+
target = request_reload()
|
|
187
|
+
job = read_job(root)
|
|
188
|
+
if job and job["status"] in ("succeeded", "reload_failed"):
|
|
189
|
+
job.update(status="succeeded", reload="requested",
|
|
190
|
+
target=target, reload_requested_at=time.time())
|
|
191
|
+
job.pop("error", None)
|
|
192
|
+
write_job(root, job)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def run(action, name, update_version=None):
|
|
196
|
+
job = {"status": "running", "plugin": name, "action": action, "phase": "starting"}
|
|
197
|
+
|
|
198
|
+
def progress(phase):
|
|
199
|
+
job["phase"] = phase
|
|
200
|
+
write_job(directory(), job)
|
|
201
|
+
|
|
202
|
+
try:
|
|
203
|
+
import django
|
|
204
|
+
|
|
205
|
+
django.setup()
|
|
206
|
+
from .manager import change
|
|
207
|
+
from .reload import prepare
|
|
208
|
+
|
|
209
|
+
if action not in ("enable", "disable", "update") or (action == "update" and not update_version):
|
|
210
|
+
raise ValueError("Invalid plugin action or update version")
|
|
211
|
+
options = {}
|
|
212
|
+
if action == "update":
|
|
213
|
+
options = {"update_version": update_version,
|
|
214
|
+
"unmanaged_enabled": CATALOG[name] in settings.G3WADMIN_LOCAL_MORE_APPS}
|
|
215
|
+
state = change(name, action in ("enable", "update"),
|
|
216
|
+
prepare=lambda candidate, lock: prepare(candidate, lock, progress=progress),
|
|
217
|
+
progress=progress, **options)
|
|
218
|
+
except Exception as error:
|
|
219
|
+
logger.exception("Plugin preparation failed")
|
|
220
|
+
job.update(status="failed", error=f"{type(error).__name__}: {error}")
|
|
221
|
+
result = 1
|
|
222
|
+
else:
|
|
223
|
+
job["target"] = fingerprint(state)
|
|
224
|
+
if os.environ.get("G3W_PLUGINS_AUTO_RELOAD") == "1":
|
|
225
|
+
progress("reload")
|
|
226
|
+
try:
|
|
227
|
+
error = os.environ.get("G3W_PLUGINS_RELOAD_ERROR")
|
|
228
|
+
if error:
|
|
229
|
+
raise RuntimeError(error)
|
|
230
|
+
request_reload(pidfd=int(os.environ["G3W_PLUGINS_RELOAD_FD"]),
|
|
231
|
+
expected_state=job["target"])
|
|
232
|
+
except Exception as error:
|
|
233
|
+
logger.exception("Automatic plugin reload failed")
|
|
234
|
+
job.update(status="reload_failed", error=f"{type(error).__name__}: {error}")
|
|
235
|
+
result = 1
|
|
236
|
+
else:
|
|
237
|
+
job.update(status="succeeded", reload="requested", reload_requested_at=time.time())
|
|
238
|
+
result = 0
|
|
239
|
+
else:
|
|
240
|
+
job.update(status="succeeded", reload="manual")
|
|
241
|
+
result = 0
|
|
242
|
+
write_job(directory(), job)
|
|
243
|
+
return result
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
if __name__ == "__main__":
|
|
247
|
+
try:
|
|
248
|
+
result = run(*sys.argv[1:])
|
|
249
|
+
finally:
|
|
250
|
+
if os.name == "nt" and os.environ.get("G3W_PLUGINS_JOB_HANDLE"):
|
|
251
|
+
from .windows_locks import close
|
|
252
|
+
close(int(os.environ["G3W_PLUGINS_JOB_HANDLE"]), release=True)
|
|
253
|
+
sys.exit(result)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Management command
|
|
2
|
+
|
|
3
|
+
[`commands/plugins.py`](commands/plugins.py) registers `manage.py plugins` in a
|
|
4
|
+
configured G3W-ADMIN installation. Run it from the G3W-ADMIN directory with the
|
|
5
|
+
same persistent `G3W_PLUGINS_DIR` used by the web and consumer processes.
|
|
6
|
+
|
|
7
|
+
| Command | Effect |
|
|
8
|
+
|---|---|
|
|
9
|
+
| `python manage.py plugins list` | Show running and desired state for each managed plugin |
|
|
10
|
+
| `python manage.py plugins enable g3w-admin-panoramax` | Install if needed, prepare and save the desired state |
|
|
11
|
+
| `python manage.py plugins disable g3w-admin-panoramax` | Prepare and save the disabled state |
|
|
12
|
+
|
|
13
|
+
`running` reflects the configuration loaded by this command's Django process.
|
|
14
|
+
`after_restart` is the saved decision; `None` means the plugin has not been
|
|
15
|
+
explicitly managed. Package and bundled-module names are listed by the command.
|
|
16
|
+
|
|
17
|
+
Enable and disable are synchronous: they run migrations, collect static files and
|
|
18
|
+
resynchronize menus against a candidate configuration before publishing it. Restart
|
|
19
|
+
the web and consumer processes to load the saved state. There is no CLI `update`
|
|
20
|
+
action; the web UI offers updates for enabled PyPI plugins.
|
|
21
|
+
|
|
22
|
+
Errors are reported as Django command errors. Preparation is not a database
|
|
23
|
+
transaction: failures can leave database, static or menu changes even when the
|
|
24
|
+
desired state was not saved. See [plugin internals](../README.md) for locking,
|
|
25
|
+
recovery and package-directory retention.
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from django.core.management.base import BaseCommand, CommandError
|
|
3
|
+
|
|
4
|
+
from g3w_plugins.manager import MANAGED, change, read_state
|
|
5
|
+
from g3w_plugins.reload import prepare
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Command(BaseCommand):
|
|
9
|
+
help = "List, enable or disable G3W modules and plugins"
|
|
10
|
+
|
|
11
|
+
def add_arguments(self, parser):
|
|
12
|
+
parser.add_argument("action", choices=("list", "enable", "disable"))
|
|
13
|
+
parser.add_argument("plugin", nargs="?", choices=MANAGED)
|
|
14
|
+
|
|
15
|
+
def handle(self, *args, **options):
|
|
16
|
+
action = options["action"]
|
|
17
|
+
plugin = options["plugin"]
|
|
18
|
+
if action == "list":
|
|
19
|
+
if plugin:
|
|
20
|
+
raise CommandError("Do not specify a plugin with list")
|
|
21
|
+
state = read_state()["plugins"]
|
|
22
|
+
for package, app in MANAGED.items():
|
|
23
|
+
running = app in settings.G3WADMIN_LOCAL_MORE_APPS
|
|
24
|
+
desired = state.get(package)
|
|
25
|
+
self.stdout.write(f"{package}: running={running}, after_restart={desired}")
|
|
26
|
+
return
|
|
27
|
+
if not plugin:
|
|
28
|
+
raise CommandError("Specify a plugin")
|
|
29
|
+
try:
|
|
30
|
+
change(plugin, action == "enable", prepare=prepare)
|
|
31
|
+
except (ValueError, RuntimeError, OSError) as error:
|
|
32
|
+
raise CommandError(str(error)) from error
|
|
33
|
+
self.stdout.write("Saved and prepared. Restart G3W-SUITE and its consumer to apply the change.")
|