├── .github
└── workflows
│ ├── publish.yml
│ └── test.yml
├── .gitignore
├── Justfile
├── LICENSE
├── README.md
├── build.sh
├── datasette_litestream
├── __init__.py
└── templates
│ └── litestream.html
├── demo
└── metadata.yaml
├── download.sh
├── pyproject.toml
├── pytest.ini
└── tests
└── test_datasette_litestream.py
/.github/workflows/publish.yml:
--------------------------------------------------------------------------------
1 | name: Publish Python Package
2 |
3 | on:
4 | release:
5 | types: [created]
6 |
7 | permissions:
8 | contents: read
9 |
10 | jobs:
11 | test:
12 | if: false
13 | runs-on: ubuntu-latest
14 | strategy:
15 | matrix:
16 | python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
17 | steps:
18 | - uses: actions/checkout@v4
19 | - name: Set up Python ${{ matrix.python-version }}
20 | uses: actions/setup-python@v5
21 | with:
22 | python-version: ${{ matrix.python-version }}
23 | cache: pip
24 | cache-dependency-path: setup.py
25 | - name: Install dependencies
26 | run: |
27 | pip install '.[test]'
28 | - name: Run tests
29 | run: |
30 | python -m pytest
31 | deploy:
32 | runs-on: ubuntu-latest
33 | #needs: [test]
34 | steps:
35 | - uses: actions/checkout@v4
36 | - name: Set up Python
37 | uses: actions/setup-python@v5
38 | with:
39 | python-version: "3.13"
40 | cache: pip
41 | cache-dependency-path: setup.py
42 | - name: Install dependencies
43 | run: |
44 | pip install setuptools wheel twine build
45 | - name: Build wheels
46 | run: |
47 | ./download.sh
48 | ./build.sh
49 | - name: Publish
50 | env:
51 | TWINE_USERNAME: __token__
52 | TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
53 | run: |
54 | python -m build
55 | twine upload dist/*
56 |
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | name: Test
2 |
3 | on: [push, pull_request]
4 |
5 | permissions:
6 | contents: read
7 |
8 | jobs:
9 | test:
10 | runs-on: ubuntu-latest
11 | strategy:
12 | matrix:
13 | python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
14 | steps:
15 | - uses: actions/checkout@v4
16 | - name: Set up Python ${{ matrix.python-version }}
17 | uses: actions/setup-python@v5
18 | with:
19 | python-version: ${{ matrix.python-version }}
20 | cache: pip
21 | cache-dependency-path: setup.py
22 | - id: cache-litestream
23 | uses: actions/cache@v3
24 | with:
25 | path: $HOME/.local/bin/litestream
26 | key: ${{ runner.os }}-primes
27 | - run: mkdir -p $HOME/.local/bin
28 | - if: steps.cache-litestream.outputs.cache-hit != 'true'
29 | run: curl -L https://github.com/benbjohnson/litestream/releases/download/v0.3.11/litestream-v0.3.11-linux-amd64.tar.gz |tar -xz -C $HOME/.local/bin
30 | - run: echo "$HOME/.local/bin" >> $GITHUB_PATH
31 | - name: Install dependencies
32 | run: |
33 | pip install '.[test]'
34 | - name: Run tests
35 | run: |
36 | python -m pytest
37 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .venv
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 | venv
6 | .eggs
7 | .pytest_cache
8 | *.egg-info
9 | .DS_Store
10 | .vscode
11 | dist
12 | build
13 |
14 |
15 | *.db
16 | *.db-shm
17 | *.db-wal
18 | .env
19 |
20 | *-litestream
21 | *-backup
22 | *.whl
23 |
24 | datasette_litestream/bin/*
25 | tmp/
26 |
27 | *.tar.gz
28 |
--------------------------------------------------------------------------------
/Justfile:
--------------------------------------------------------------------------------
1 | dev:
2 | DATASETTE_SECRET=abc123 watchexec --signal SIGKILL --restart --clear -e py,ts,html,js,css,yaml -- \
3 | python3 -m datasette --root demo/*.db --metadata=demo/metadata.yaml -p 8002
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # datasette-litestream
2 |
3 | [](https://pypi.org/project/datasette-litestream/)
4 | [](https://github.com/datasette/datasette-litestream/releases)
5 | [](https://github.com/datasette/datasette-litestream/actions?query=workflow%3ATest)
6 | [](https://github.com/datasette/datasette-litestream/blob/main/LICENSE)
7 |
8 | A Datasette <-> Litestream plugin.
9 |
10 | ## Installation
11 |
12 | The plugin requires a recent alpha version of Datasette 1.0, whcih can be installed with:
13 |
14 | pip install datasette==1.0a6
15 |
16 | Then install this plugin in the same environment as Datasette.
17 |
18 | datasette install datasette-litestream
19 |
20 | ## Usage
21 |
22 | ### Replicate a single database to S3
23 |
24 | To replicate `my_database.db` to S3, use the following configuration in your `metadata.yaml` file:
25 |
26 | ```yaml
27 | databases:
28 | my_database:
29 | plugins:
30 | datasette-litestream:
31 | replicas:
32 | - url: s3://my-bucket/my_database
33 | ```
34 |
35 | Then make sure you export `LITESTREAM_ACCESS_KEY_ID` and `LITESTREAM_SECRET_ACCESS_KEY` with your S3 credentials (or `AWS_ACCESS_KEY_ID` `AWS_SECRET_ACCESS_KEY`), then run with:
36 |
37 | ```
38 | datasette my_database.db -m metadata.yaml
39 | ```
40 |
41 | ### Replicate all databases
42 |
43 | If you have multiple attached databases in Datasette and want to replicate all of them, the top-level `all-replicate` key can be used.
44 |
45 | ```yaml
46 | plugins:
47 | datasette-litestream:
48 | all-replicate:
49 | - s3://my-bucket/$DB_NAME
50 | ```
51 |
52 | When `all-replicate` is used, a new replica URL is generated for each attached database. In this case, if you had a database named `parking_tickets` and another named `city_budget`, then `datasette-litestream` will replicate them to `s3://my-bucket/parking_tickets` and `s3://my-bucket/city_budget`.
53 |
54 | This is done with "variables" that `datasette-litestream` replaces in the `all-replicate` URL. The supported variables are:
55 |
56 | - `$DB_NAME`: The name of the Datasette database to replicate.
57 | - `$DB_DIRECTORY`: The full parent directory that the SQLite database resides.
58 | - `$PWD`: The current working directory of the Datasette process.
59 |
60 | ## Config
61 |
62 | Some configuration in the `metadata.yaml` will be used to auto-generate the [`litestream.yml`](https://litestream.io/reference/config/) file under the hood. You can use this to customize the Litestream replication process.
63 |
64 | ### Top-level
65 |
66 | The following are valid keys that are allowed when specifying top-level plugin configuration:
67 |
68 | - `all-replicate`: A template replica URL used to replicate all attached Datasette databases, see aboce for details.
69 | - `metrics-addr`: Defines the [`addr:` Litestream option](https://litestream.io/reference/config/#metrics), which will expose a Prometheus endpoint at the given URL. Use which caution on public Datasette instances! When defined, the metrics info will appear on the `datasette-litestream` status page.
70 | - `access-key-id`: An alternate way to provide a S3 access key (though the `LITESTREAM_ACCESS_KEY_ID` environment variable is preferred).
71 | - `secret-access-key`: An alternate way to provide a S3 secret key (though the `LITESTREAM_SECRET_ACCESS_KEY` environment variable is preferred).
72 |
73 | None of these keys are required.
74 |
75 | Example:
76 |
77 | ```yaml
78 | plugins:
79 | datasette-litestream:
80 | all-replicate:
81 | - XXX
82 | - YYY
83 | metrics-addr: :5001
84 | access-key-id: $YOUR_KEY
85 | secret-access-key: $YOUR_SECRET
86 | ```
87 |
88 | ### Database-level
89 |
90 | The following options are allowed on database-level plugin configuration.
91 |
92 | - `replicas`
93 | - `monitor-interval`
94 | - `checkpoint-interval`
95 | - `min-checkpoint-page-count`
96 | - `max-checkpoint-page-count`
97 |
98 | ```yaml
99 | databases:
100 | my_database:
101 | plugins:
102 | datasette-litestream:
103 | replicas:
104 | - s3://...
105 | - file://...
106 | monitor-interval: XXX
107 | checkpoint-interval: XXX
108 | min-checkpoint-page-count: XXX
109 | max-checkpoint-page-count: XXX
110 | ```
111 |
112 | See [Litestream Database settings](https://litestream.io/reference/config/#database-settings) for more information.
113 |
114 | ## Development
115 |
116 | To set up this plugin locally, first checkout the code. Then create a new virtual environment:
117 |
118 | cd datasette-litestream
119 | python3 -m venv venv
120 | source venv/bin/activate
121 |
122 | Now install the dependencies and test dependencies:
123 |
124 | pip install -e '.[test]'
125 |
126 | To run the tests:
127 |
128 | pytest
129 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | DATASETTE_LITESTREAM_VERSION="$(python3 -c "import tomllib; print(tomllib.loads(open('pyproject.toml', 'r').read())['project']['version'])")"
4 |
5 | mkdir -p datasette_litestream/bin
6 |
7 | function build_wheel {
8 | cp $1 datasette_litestream/bin/litestream
9 | python3 -m build . --wheel
10 | mv dist/datasette_litestream-*-py3-none-any.whl $2
11 | }
12 |
13 |
14 | build_wheel tmp/litestream-darwin-amd64 "dist/datasette_litestream-$DATASETTE_LITESTREAM_VERSION-py3-none-macosx_10_6_x86_64.whl"
15 | build_wheel tmp/litestream-darwin-arm64 "dist/datasette_litestream-$DATASETTE_LITESTREAM_VERSION-py3-none-macosx_11_0_arm64.whl"
16 | build_wheel tmp/litestream-linux-amd64 "dist/datasette_litestream-$DATASETTE_LITESTREAM_VERSION-py3-none-manylinux1_x86_64.whl"
17 | build_wheel tmp/litestream-linux-arm7 "dist/datasette_litestream-$DATASETTE_LITESTREAM_VERSION-py3-none-linux_armv7l.whl"
18 | build_wheel tmp/litestream-linux-arm64 "dist/datasette_litestream-$DATASETTE_LITESTREAM_VERSION-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl"
19 |
20 | rm datasette_litestream/bin/litestream
21 | python3 -m build . --sdist
22 |
23 |
24 |
--------------------------------------------------------------------------------
/datasette_litestream/__init__.py:
--------------------------------------------------------------------------------
1 | from datasette import hookimpl, Permission, Forbidden
2 | from datasette.utils.asgi import Response
3 | from pathlib import Path
4 | import atexit
5 | import httpx
6 | import json
7 | import os
8 | import shutil
9 | import subprocess
10 | import tempfile
11 | import time
12 | import uuid
13 | from prometheus_client.parser import text_string_to_metric_families
14 |
15 |
16 | class LitestreamProcess:
17 | """ """
18 |
19 | # The underyling subprocess.Popen() that gets kicked off
20 | process = None
21 |
22 | # the litestream.yaml config, as a dict
23 | litestream_config = None
24 |
25 | # Temporary file where the subprocess logs get forwarded too
26 | logfile = None
27 |
28 | # Temporary file where the litestream.yaml gets saved to
29 | configfile = None
30 |
31 | def __init__(self):
32 | self.logfile = tempfile.NamedTemporaryFile(suffix=".log", delete=True)
33 |
34 | def start_replicate(self):
35 | """starts the litestream process with the given config, logging to logfile"""
36 |
37 | litestream_path = resolve_litestream_path()
38 |
39 | self.configfile = tempfile.NamedTemporaryFile(suffix=".yml", delete=False)
40 |
41 | with self.configfile as f:
42 | f.write(bytes(json.dumps(self.litestream_config), "utf-8"))
43 | config_path = Path(f.name)
44 |
45 | self.process = subprocess.Popen(
46 | [litestream_path, "replicate", "-config", str(config_path)],
47 | stderr=self.logfile,
48 | )
49 |
50 | # wait 500ms to see if there are instant errors (typically config typos)
51 | time.sleep(0.5)
52 | status = self.process.poll()
53 | if status is not None:
54 | logs = open(self.logfile.name, "r").read()
55 | raise Exception(
56 | f"datasette-litestream litestream process failed with return code {status}. Logs:"
57 | + logs
58 | )
59 |
60 | # Sometimes Popen doesn't die on exit, so explicitly attempt to kill it on proccess exit
61 | def onexit():
62 | self.process.kill()
63 | Path(self.configfile.name).unlink()
64 |
65 | atexit.register(onexit)
66 |
67 |
68 | # global variable that tracks each datasette-litestream instance. There is usually just 1,
69 | # but in test suites there may be multiple Datasette instances.
70 | # The keys are a UUID generated in the startup hook, values are a LitestreamProcess
71 | processes = {}
72 |
73 | # The uuid generated at startup is stored on the datasette object, stored in this key attr.
74 | # Meant so we can retrieve it in the separate litestream_status route
75 | DATASETTE_LITESTREAM_PROCESS_KEY = "__DATASETTE_LITESTREAM_PROCESS_KEY__"
76 |
77 |
78 | def resolve_litestream_path():
79 | """resolives the full path to a litestream binary. Hopefully is bundled in the installed wheel"""
80 |
81 | # First try to see if litestream was bundled with that package, in a pre-built wheel
82 | wheel_path = Path(__file__).resolve().parent / "bin" / "litestream"
83 | if wheel_path.exists():
84 | return str(wheel_path)
85 |
86 | # Fallback to any litestream binary on the system.
87 | executable_path = shutil.which("litestream")
88 |
89 | if executable_path is None:
90 | raise Exception("litestream not found.")
91 |
92 | return str(executable_path)
93 |
94 |
95 | @hookimpl
96 | def register_permissions(datasette):
97 | return [
98 | Permission(
99 | name="litestream-view-status",
100 | abbr=None,
101 | description="View litestream statistics and status updates.",
102 | takes_database=False,
103 | takes_resource=False,
104 | default=False,
105 | )
106 | ]
107 |
108 |
109 | @hookimpl
110 | def permission_allowed(actor, action):
111 | # TODO only root can see it?
112 | if action == "litestream-view-status" and actor and actor.get("id") == "root":
113 | return True
114 |
115 |
116 | @hookimpl
117 | def menu_links(datasette, actor):
118 | async def inner():
119 | if (
120 | await datasette.permission_allowed(
121 | actor, "litestream-view-status", default=False
122 | )
123 | # TODO why is this needed?
124 | and datasette.plugin_config("datasette-litestream") is not None
125 | ):
126 | return [
127 | {
128 | "href": datasette.urls.path("/-/litestream-status"),
129 | "label": "Litestream Status",
130 | },
131 | ]
132 |
133 | return inner
134 |
135 |
136 | @hookimpl
137 | def startup(datasette):
138 | global processes
139 |
140 | litestream_process = LitestreamProcess()
141 | litestream_process.litestream_config = {"dbs": []}
142 |
143 | plugin_config_top = datasette.plugin_config("datasette-litestream") or {}
144 |
145 | if "access-key-id" in plugin_config_top:
146 | litestream_process.litestream_config["access-key-id"] = plugin_config_top.get(
147 | "access-key-id"
148 | )
149 |
150 | if "secret-access-key" in plugin_config_top:
151 | litestream_process.litestream_config[
152 | "secret-access-key"
153 | ] = plugin_config_top.get("secret-access-key")
154 |
155 | if "metrics-addr" in plugin_config_top:
156 | litestream_process.litestream_config["addr"] = plugin_config_top.get(
157 | "metrics-addr"
158 | )
159 |
160 | all_replicate = plugin_config_top.get("all-replicate")
161 |
162 | for db_name, db in datasette.databases.items():
163 | if db.path is None:
164 | continue
165 |
166 | db_path = Path(db.path)
167 |
168 | plugin_config_db = datasette.plugin_config(
169 | "datasette-litestream", db_name, fallback=False
170 | )
171 |
172 | # skip this DB is "all-replicate" was not defined or no db-level config was given
173 | if plugin_config_db is None and all_replicate is None:
174 | continue
175 |
176 | db_litestream_config = {
177 | "path": str(db_path.resolve()),
178 | }
179 | if plugin_config_db is not None:
180 | # TODO restrict the possible keys here. We don't want to plugins to redefine "replicas" or "path"
181 | db_litestream_config = {**db_litestream_config, **plugin_config_db}
182 |
183 | if all_replicate is not None:
184 | for i, template in enumerate(all_replicate):
185 | url = (
186 | template.replace("$DB_NAME", db_name)
187 | .replace("$DB_DIRECTORY", str(db_path.resolve().parent))
188 | .replace("$PWD", os.getcwd())
189 | )
190 |
191 | if "replicas" in db_litestream_config:
192 | db_litestream_config["replicas"].append(
193 | {"url": url, "name": f"t{i}"}
194 | )
195 | else:
196 | db_litestream_config["replicas"] = [{"url": url, "name": f"t{i}"}]
197 |
198 | litestream_process.litestream_config["dbs"].append(db_litestream_config)
199 |
200 | # don't run litestream if no top-level or db-level datasette-litestream config was given
201 | if not plugin_config_top and len(litestream_process.litestream_config["dbs"]) == 0:
202 | return
203 |
204 | startup_id = str(uuid.uuid4())
205 | processes[startup_id] = litestream_process
206 | setattr(datasette, DATASETTE_LITESTREAM_PROCESS_KEY, startup_id)
207 |
208 | litestream_process.start_replicate()
209 |
210 |
211 | @hookimpl
212 | def register_routes():
213 | return [
214 | (r"^/-/litestream-status$", litestream_status),
215 | ]
216 |
217 |
218 | async def litestream_status(scope, receive, datasette, request):
219 | if not await datasette.permission_allowed(
220 | request.actor, "litestream-view-status", default=False
221 | ):
222 | raise Forbidden("Permission denied for litestream-view-status")
223 |
224 | startup_id = getattr(datasette, DATASETTE_LITESTREAM_PROCESS_KEY, None)
225 |
226 | if startup_id is None:
227 | return Response.html("
Litestream not running
")
228 |
229 | global proccesses
230 | litestream_process = processes.get(startup_id)
231 |
232 | if litestream_process is None:
233 | return Response.html("Litestream not running
")
234 |
235 | replica_operations = {
236 | "bytes": [],
237 | "total": [],
238 | }
239 | metrics_by_db = {}
240 | go_stats = {}
241 |
242 | metrics_enabled = litestream_process.litestream_config.get("addr") is not None
243 |
244 | if metrics_enabled:
245 | # litestream metrics give the absolute path to the database, so create a mapping
246 | # to the datasette db names
247 | db_name_lookup = {}
248 | for db_name, db in datasette.databases.items():
249 | if db.path is None:
250 | continue
251 | db_name_lookup[str(Path(db.path).resolve())] = db_name
252 |
253 | # TODO detect when non-localhost addresses are used
254 | addr = litestream_process.litestream_config.get("addr")
255 | metrics_page = httpx.get(f"http://localhost{addr}/metrics").text
256 |
257 | for family in text_string_to_metric_families(metrics_page):
258 | for sample in family.samples:
259 | # TODO also ???
260 | if sample.name == "litestream_replica_operation_bytes_total":
261 | replica_operations["bytes"].append({
262 | **sample.labels,
263 | "value": sample.value,
264 | })
265 | elif sample.name == "litestream_replica_operation_total":
266 | replica_operations["total"].append({
267 | **sample.labels,
268 | "value": sample.value,
269 | })
270 |
271 | elif (
272 | sample.name.startswith("litestream_")
273 | # litestream_replica_validation_total has `name` and `status` values that I don't understand
274 | and sample.name != "litestream_replica_validation_total"
275 | ):
276 | print(sample.name)
277 | db = db_name_lookup[sample.labels.get("db")]
278 |
279 | if metrics_by_db.get(db) is None:
280 | metrics_by_db[db] = {}
281 |
282 | metrics_by_db[db][sample.name] = sample.value
283 | elif sample.name in ["go_goroutines", "go_threads"]:
284 | go_stats[sample.name] = sample.value
285 |
286 | return Response.html(
287 | await datasette.render_template(
288 | "litestream.html",
289 | context={
290 | "process": {
291 | "pid": litestream_process.process.pid,
292 | "status": "alive"
293 | if litestream_process.process.poll() is None
294 | else "died",
295 | },
296 | "logs": open(litestream_process.logfile.name, "r").read(),
297 | "metrics_enabled": metrics_enabled,
298 | # TODO redact credentials if they are in here :(
299 | "litestream_config": json.dumps(
300 | litestream_process.litestream_config, indent=2
301 | ),
302 | "replica_operations": replica_operations,
303 | "metrics_by_db": metrics_by_db,
304 | "go_stats": go_stats,
305 | },
306 | )
307 | )
308 |
--------------------------------------------------------------------------------
/datasette_litestream/templates/litestream.html:
--------------------------------------------------------------------------------
1 | {% extends "base.html" %}
2 |
3 | {% block title %}Litestream status{% endblock %}
4 |
5 | {% block extra_head %}
6 | {% endblock %}
7 |
8 | {% block content %}
9 | Litestream Status
10 |
11 | PID: {{process.get("pid")}}
12 | status: {{process.get("status")}}
13 |
14 | litestream.yaml
Config
15 |
16 | {{ litestream_config }}
17 |
18 |
19 | Logs
20 |
21 | {{ logs }}
22 |
23 |
24 | {% if not metrics_enabled %}
25 | WARNING: metrics-addr was not defined, so no litestream metrics are available.
26 | {% else %}
27 | Metrics
28 |
29 | Goroutines: {{ go_stats.get("go_goroutines") }}
30 | Go threads: {{ go_stats.get("go_threads") }}
31 |
32 |
33 | {% if replica_operations.get("bytes") | length > 0 %}
34 | Replica operation bytes
35 |
36 |
37 |
38 | Operation |
39 | Replica Type |
40 | Bytes |
41 |
42 |
43 |
44 | {% for row in replica_operations.get("bytes") %}
45 |
46 | {{ row.get("operation") }} |
47 | {{ row.get("replica_type") }} |
48 | {{ row.get("value") }} |
49 |
50 | {% endfor %}
51 |
52 |
53 | {% endif %}
54 |
55 | {% if replica_operations.get("total") | length > 0 %}
56 | Replica total operations
57 |
58 |
59 |
60 | Operation |
61 | Replica Type |
62 | Number of operations |
63 |
64 |
65 |
66 | {% for row in replica_operations.get("total") %}
67 |
68 | {{ row.get("operation") }} |
69 | {{ row.get("replica_type") }} |
70 | {{ row.get("value") }} |
71 |
72 | {% endfor %}
73 |
74 |
75 | {% endif %}
76 |
77 | {% for db, metrics in metrics_by_db.items() %}
78 | {{ db }}
79 |
80 |
81 |
82 | Key |
83 | Value |
84 |
85 |
86 |
87 | {% for key, value in metrics.items() %}
88 |
89 | {{ key }} |
90 | {{ value }} |
91 |
92 | {% endfor %}
93 |
94 | {% endfor %}
95 |
96 | {% endif %}
97 |
98 |
108 | {% endblock %}
109 |
--------------------------------------------------------------------------------
/demo/metadata.yaml:
--------------------------------------------------------------------------------
1 | plugins:
2 | datasette-litestream:
3 | metrics-addr: :9090
4 | all-replicate:
5 | - file://$PWD/$DB-backup
6 |
--------------------------------------------------------------------------------
/download.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -euo pipefail
3 |
4 | curl -LO https://github.com/benbjohnson/litestream/releases/download/v0.3.11/litestream-v0.3.11-darwin-amd64.zip
5 | curl -LO https://github.com/benbjohnson/litestream/releases/download/v0.3.11/litestream-v0.3.11-darwin-arm64.zip
6 | curl -LO https://github.com/benbjohnson/litestream/releases/download/v0.3.11/litestream-v0.3.11-linux-amd64.tar.gz
7 | curl -LO https://github.com/benbjohnson/litestream/releases/download/v0.3.11/litestream-v0.3.11-linux-arm64.tar.gz
8 | curl -LO https://github.com/benbjohnson/litestream/releases/download/v0.3.11/litestream-v0.3.11-linux-arm7.tar.gz
9 |
10 | unzip -j litestream-v0.3.11-darwin-amd64.zip litestream -d tmp
11 | mv tmp/litestream tmp/litestream-darwin-amd64
12 | unzip -j litestream-v0.3.11-darwin-arm64.zip litestream -d tmp
13 | mv tmp/litestream tmp/litestream-darwin-arm64
14 | tar -xvzf litestream-v0.3.11-linux-amd64.tar.gz -C tmp litestream
15 | mv tmp/litestream tmp/litestream-linux-amd64
16 | tar -xvzf litestream-v0.3.11-linux-arm64.tar.gz -C tmp litestream
17 | mv tmp/litestream tmp/litestream-linux-arm64
18 | tar -xvzf litestream-v0.3.11-linux-arm7.tar.gz -C tmp litestream
19 | mv tmp/litestream tmp/litestream-linux-arm7
20 |
21 |
22 | rm litestream-v0.3.11-darwin-amd64.zip
23 | rm litestream-v0.3.11-darwin-arm64.zip
24 | rm litestream-v0.3.11-linux-amd64.tar.gz
25 | rm litestream-v0.3.11-linux-arm64.tar.gz
26 | rm litestream-v0.3.11-linux-arm7.tar.gz
27 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [project]
2 | name = "datasette-litestream"
3 | version = "0.0.1a10"
4 | description = ""
5 | readme = "README.md"
6 | authors = [{name = "Alex Garcia"}]
7 | license = {text = "Apache-2.0"}
8 | classifiers = [
9 | "License :: OSI Approved :: Apache Software License"
10 | ]
11 | dependencies = ["datasette>=1.0a3", "prometheus-client"]
12 |
13 | [project.urls]
14 | Homepage = "https://github.com/datasette/datasette-litestream"
15 | Changelog = "https://github.com/datasette/datasette-litestream/releases"
16 | Issues = "https://github.com/datasette/datasette-litestream/issues"
17 | CI = "https://github.com/datasette/datasette-litestream/actions"
18 |
19 |
20 | [project.entry-points.datasette]
21 | litestream = "datasette_litestream"
22 |
23 | [project.optional-dependencies]
24 | test = ["pytest", "pytest-asyncio"]
25 |
26 | [tool.setuptools.packages.find]
27 | namespaces = false
28 |
29 | [tool.setuptools.package-data]
30 | datasette_litestream = ["bin/*", "templates/*.html"]
31 |
--------------------------------------------------------------------------------
/pytest.ini:
--------------------------------------------------------------------------------
1 | [pytest]
2 | asyncio_mode = strict
3 | asyncio_default_fixture_loop_scope = function
--------------------------------------------------------------------------------
/tests/test_datasette_litestream.py:
--------------------------------------------------------------------------------
1 | from datasette.app import Datasette
2 | import pytest
3 | import sqlite_utils
4 | from pathlib import Path
5 | import time
6 |
7 | actor_root = {"a": {"id": "root"}}
8 |
9 |
10 | @pytest.fixture
11 | def students_db_path(tmpdir):
12 | path = str(tmpdir / "students.db")
13 | db = sqlite_utils.Database(path)
14 | db["students"].insert_all(
15 | [
16 | {"name": "alex", "age": 10},
17 | {"name": "brian", "age": 20},
18 | {"name": "craig", "age": 30, "[weird (column)]": 1},
19 | ]
20 | )
21 | db.execute("create table courses(name text primary key) without rowid")
22 | db["courses"].insert_all(
23 | [
24 | {"name": "MATH 101"},
25 | {"name": "MATH 102"},
26 | ]
27 | )
28 | return path
29 |
30 |
31 | @pytest.mark.asyncio
32 | async def test_plugin_is_installed():
33 | datasette = Datasette(memory=True)
34 | response = await datasette.client.get("/-/plugins.json")
35 | assert response.status_code == 200
36 | installed_plugins = {p["name"] for p in response.json()}
37 | assert "datasette-litestream" in installed_plugins
38 |
39 |
40 | @pytest.mark.asyncio
41 | async def test_no_litestream_config():
42 | datasette = Datasette(memory=True)
43 |
44 | response = await datasette.client.get("/-/litestream-status")
45 | assert response.status_code == 403
46 |
47 | response = await datasette.client.get(
48 | "/-/litestream-status",
49 | cookies={"ds_actor": datasette.sign(actor_root, "actor")},
50 | )
51 | assert response.status_code == 200
52 | assert response.text == "Litestream not running
"
53 |
54 |
55 | @pytest.mark.asyncio
56 | async def test_basic_db_level(students_db_path):
57 | backup_dir = str(Path(students_db_path).parents[0] / "students-backup")
58 |
59 | assert not Path(backup_dir).exists()
60 |
61 | datasette = Datasette(
62 | [students_db_path],
63 | metadata={
64 | "databases": {
65 | "students": {
66 | "plugins": {
67 | "datasette-litestream": {"replicas": [{"path": backup_dir}]}
68 | }
69 | }
70 | }
71 | },
72 | )
73 |
74 | response = await datasette.client.get("/-/litestream-status")
75 | assert response.status_code == 403
76 |
77 | response = await datasette.client.get(
78 | "/-/litestream-status",
79 | cookies={"ds_actor": datasette.sign(actor_root, "actor")},
80 | )
81 | assert response.status_code == 200
82 | assert "Litestream status" in response.text
83 | assert (
84 | "WARNING: metrics-addr was not defined, so no litestream metrics are available."
85 | in response.text
86 | )
87 |
88 | # have to wait a second for litestream to write the first replica
89 | time.sleep(1)
90 | assert Path(backup_dir).exists()
91 | assert (Path(backup_dir) / "generations").exists()
92 |
93 |
94 | @pytest.mark.asyncio
95 | async def test_metrics(students_db_path):
96 | backup_dir = str(Path(students_db_path).parents[0] / "students-backup")
97 |
98 | assert not Path(backup_dir).exists()
99 |
100 | datasette = Datasette(
101 | [students_db_path],
102 | metadata={
103 | "plugins": {"datasette-litestream": {"metrics-addr": ":9998"}},
104 | "databases": {
105 | "students": {
106 | "plugins": {
107 | "datasette-litestream": {"replicas": [{"path": backup_dir}]}
108 | }
109 | }
110 | },
111 | },
112 | )
113 |
114 | response = await datasette.client.get(
115 | "/-/litestream-status",
116 | cookies={"ds_actor": datasette.sign(actor_root, "actor")},
117 | )
118 |
119 | assert response.status_code == 200
120 | assert "Litestream status" in response.text
121 | assert "Metrics
" in response.text
122 |
--------------------------------------------------------------------------------