Compare commits
15 commits
8f60cfe836
...
efbfc3f4a0
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | efbfc3f4a0 | ||
Antoine Martin | d942dc1098 | ||
Antoine Martin | bc32f8a565 | ||
Antoine Martin | 6c4a496ede | ||
Antoine Martin | 264b7baccd | ||
Antoine Martin | 776876233f | ||
Bruno BELANYI | 3fedfa3342 | ||
Antoine Martin | 378823a1fb | ||
Antoine Martin | 56f82c6467 | ||
Antoine Martin | 3a93d9f994 | ||
Antoine Martin | da20e2c9ac | ||
Antoine Martin | 29c0a4abdf | ||
Antoine Martin | 1506ce1dac | ||
Antoine Martin | fe52787f4d | ||
Antoine Martin | e68fcf3e52 |
19
.github/workflows/cachix.yaml
vendored
Normal file
19
.github/workflows/cachix.yaml
vendored
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
name: "Cachix build"
|
||||||
|
on: [push]
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2.3.4
|
||||||
|
- uses: cachix/install-nix-action@v13
|
||||||
|
with:
|
||||||
|
install_url: https://nixos-nix-install-tests.cachix.org/serve/lb41az54kzk6j12p81br4bczary7m145/install
|
||||||
|
install_options: '--tarball-url-prefix https://nixos-nix-install-tests.cachix.org/serve'
|
||||||
|
extra_nix_config: |
|
||||||
|
experimental-features = nix-command flakes
|
||||||
|
- uses: cachix/cachix-action@v8
|
||||||
|
with:
|
||||||
|
name: alarsyo
|
||||||
|
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
|
||||||
|
- run: |
|
||||||
|
nix build --verbose
|
12
.github/workflows/ci.yaml
vendored
12
.github/workflows/ci.yaml
vendored
|
@ -10,11 +10,11 @@ jobs:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Install nightly toolchain
|
- name: Install stable toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
profile: minimal
|
profile: minimal
|
||||||
toolchain: nightly
|
toolchain: stable
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
- name: Run cargo check
|
- name: Run cargo check
|
||||||
|
@ -29,11 +29,11 @@ jobs:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Install nightly toolchain
|
- name: Install stable toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
profile: minimal
|
profile: minimal
|
||||||
toolchain: nightly
|
toolchain: stable
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
- name: Run cargo test
|
- name: Run cargo test
|
||||||
|
@ -48,11 +48,11 @@ jobs:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Install nightly toolchain
|
- name: Install stable toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
profile: minimal
|
profile: minimal
|
||||||
toolchain: nightly
|
toolchain: stable
|
||||||
override: true
|
override: true
|
||||||
components: rustfmt, clippy
|
components: rustfmt, clippy
|
||||||
|
|
||||||
|
|
1153
Cargo.lock
generated
1153
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "lohr"
|
name = "lohr"
|
||||||
version = "0.3.1"
|
version = "0.4.0"
|
||||||
authors = ["Antoine Martin <antoine@alarsyo.net>"]
|
authors = ["Antoine Martin <antoine@alarsyo.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "Apache-2.0 OR MIT"
|
license = "Apache-2.0 OR MIT"
|
||||||
|
@ -23,7 +23,7 @@ serde_yaml = "0.8.17"
|
||||||
sha2 = "0.9.3"
|
sha2 = "0.9.3"
|
||||||
|
|
||||||
[dependencies.rocket]
|
[dependencies.rocket]
|
||||||
version = "0.4.7"
|
version = "0.5.0-dev"
|
||||||
# don't need private-cookies
|
# don't need private-cookies
|
||||||
default-features = false
|
default-features = false
|
||||||
|
|
||||||
|
@ -31,3 +31,6 @@ default-features = false
|
||||||
version = "2.33.3"
|
version = "2.33.3"
|
||||||
# no need for suggestions or color with only one argument
|
# no need for suggestions or color with only one argument
|
||||||
default-features = false
|
default-features = false
|
||||||
|
|
||||||
|
[patch.crates-io]
|
||||||
|
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "8d4d01106e2e10b08100805d40bfa19a7357e900" }
|
||||||
|
|
138
README.md
Normal file
138
README.md
Normal file
|
@ -0,0 +1,138 @@
|
||||||
|
# lohr
|
||||||
|
|
||||||
|
`lohr` is a Git mirroring tool.
|
||||||
|
|
||||||
|
I created it to solve a simple problem I had: I host my own git server at
|
||||||
|
<https://git.alarsyo.net>, but want to mirror my public projects to GitHub /
|
||||||
|
GitLab, for backup and visibility purposes.
|
||||||
|
|
||||||
|
GitLab has a mirroring setting, but it doesn't allow for multiple mirrors, as
|
||||||
|
far as I know. I also wanted my instance to be the single source of truth.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
Gitea is setup to send webhooks to my `lohr` server on every push update. When
|
||||||
|
`lohr` receives a push, it clones the concerned repository, or updates it if
|
||||||
|
already cloned. Then it pushes the update to **all remotes listed** in the
|
||||||
|
[.lohr](.lohr) file at the repo root.
|
||||||
|
|
||||||
|
### Destructive
|
||||||
|
|
||||||
|
This is a very destructive process: anything removed from the single source of
|
||||||
|
truth is effectively removed from any mirror as well.
|
||||||
|
|
||||||
|
## Installing
|
||||||
|
|
||||||
|
`lohr` is [published on crates.io](https://crates.io/crates/lohr), so you can
|
||||||
|
install it with `cargo install`:
|
||||||
|
|
||||||
|
$ cargo install lohr
|
||||||
|
|
||||||
|
Note: currently this method won't get you the latest version of `lohr`, as it
|
||||||
|
depends on Rocket v0.5.0, which isn't released yet. Updated versions of `lohr`
|
||||||
|
will be published on crates.io as soon as Rocket v0.5.0 releases.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
### Quickstart
|
||||||
|
|
||||||
|
Setting up `lohr` should be quite simple:
|
||||||
|
|
||||||
|
1. Create a `Rocket.toml` file and [add your
|
||||||
|
configuration](https://rocket.rs/v0.4/guide/configuration/).
|
||||||
|
|
||||||
|
2. Export a secret variable:
|
||||||
|
|
||||||
|
$ export LOHR_SECRET=42 # please don't use this secret
|
||||||
|
|
||||||
|
3. Run `lohr`:
|
||||||
|
|
||||||
|
$ cargo run # or `cargo run --release` for production usage
|
||||||
|
|
||||||
|
4. Configure your favorite git server to send a webhook to `lohr`'s address on
|
||||||
|
every push event.
|
||||||
|
|
||||||
|
I used [Gitea's webhooks format](https://docs.gitea.io/en-us/webhooks/), but
|
||||||
|
I **think** they're similar to GitHub and GitLab's webhooks, so these should
|
||||||
|
work too! (If they don't, **please** file an issue!)
|
||||||
|
|
||||||
|
Don't forget to set the webhook secret to the one you chose above.
|
||||||
|
|
||||||
|
5. Add a `.lohr` file containing the remotes you want to mirror this repo to:
|
||||||
|
|
||||||
|
git@github.com:you/your_repo
|
||||||
|
|
||||||
|
and push it. That's it! `lohr` is mirroring your repo now.
|
||||||
|
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
#### Home directory
|
||||||
|
|
||||||
|
`lohr` needs a place to clone repos and store its data. By default, it's the
|
||||||
|
current directory, but you can set the `LOHR_HOME` environment variable to
|
||||||
|
customize it.
|
||||||
|
|
||||||
|
#### Shared secret
|
||||||
|
|
||||||
|
As shown in the quickstart guide, you **must** set the `LOHR_SECRET` environment
|
||||||
|
variable.
|
||||||
|
|
||||||
|
#### Extra remote configuration
|
||||||
|
|
||||||
|
You can provide `lohr` with a YAML file containing additional configuration. You
|
||||||
|
can pass its path to the `--config` flag when launching `lohr`. If no
|
||||||
|
configuration is provided via a CLI flag, `lohr` will check the `LOHR_CONFIG`
|
||||||
|
environment variable. If the environment variable isn't set either, it will
|
||||||
|
check in `LOHR_HOME` is a `lohr-config.yaml` file exists, and try to load it.
|
||||||
|
|
||||||
|
This file takes the following format:
|
||||||
|
|
||||||
|
``` yaml
|
||||||
|
default_remotes:
|
||||||
|
- "git@github:user"
|
||||||
|
- "git@gitlab:user"
|
||||||
|
|
||||||
|
additional_remotes:
|
||||||
|
- "git@git.sr.ht:~user"
|
||||||
|
|
||||||
|
filters:
|
||||||
|
- FIXME:
|
||||||
|
```
|
||||||
|
|
||||||
|
- `default_remotes` is a list of remotes to use if no `.lohr` file is found in a
|
||||||
|
repository.
|
||||||
|
- `additional_remotes` is a list of remotes to add in any case, whether the
|
||||||
|
original set of remotes is set via `default_remotes` or via a `.lohr` file.
|
||||||
|
- `blacklist` is a list of regular expressions to match against the full
|
||||||
|
repository names. Any that matches will not be mirrored, even if it contains a
|
||||||
|
`.lohr` file.
|
||||||
|
|
||||||
|
Both settings take as input a list of "stems", i.e. incomplete remote addresses,
|
||||||
|
to which the repo's name will be appended (so for example, if my
|
||||||
|
`default_remotes` contains `git@github.com:alarsyo`, and a push event webhook is
|
||||||
|
received for repository `git@gitlab.com:some/long/path/repo_name`, then the
|
||||||
|
mirror destination will be `git@github.com:alarsyo/repo_name`.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
I accept patches anywhere! Feel free to [open a GitHub Pull
|
||||||
|
Request](https://github.com/alarsyo/lohr/pulls), [a GitLab Merge
|
||||||
|
Request](https://gitlab.com/alarsyo/lohr/-/merge_requests), or [send me a patch
|
||||||
|
by email](https://lists.sr.ht/~alarsyo/lohr-dev)!
|
||||||
|
|
||||||
|
## Why lohr?
|
||||||
|
|
||||||
|
I was looking for a cool name, and thought about the Magic Mirror in Snow White.
|
||||||
|
Some **[furious wikipedia
|
||||||
|
searching](https://en.wikipedia.org/wiki/Magic_Mirror_(Snow_White))** later, I
|
||||||
|
found that the Magic Mirror was probably inspired by [the Talking Mirror in Lohr
|
||||||
|
am Main](http://spessartmuseum.de/seiten/schneewittchen_engl.html). That's it,
|
||||||
|
that's the story.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
`lohr` is distributed under the terms of both the MIT license and the Apache
|
||||||
|
License (Version 2.0).
|
||||||
|
|
||||||
|
See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) for details.
|
143
README.org
143
README.org
|
@ -1,143 +0,0 @@
|
||||||
#+title: lohr
|
|
||||||
|
|
||||||
=lohr= is a Git mirroring tool.
|
|
||||||
|
|
||||||
I created it to solve a simple problem I had: I host my own git server at
|
|
||||||
[[https://git.alarsyo.net]], but want to mirror my public projects to GitHub /
|
|
||||||
GitLab, for backup and visibility purposes.
|
|
||||||
|
|
||||||
GitLab has a mirroring setting, but it doesn't allow for multiple mirrors, as
|
|
||||||
far as I know. I also wanted my instance to be the single source of truth.
|
|
||||||
|
|
||||||
** How it works
|
|
||||||
|
|
||||||
Gitea is setup to send webhooks to my =lohr= server on every push update. When
|
|
||||||
=lohr= receives a push, it clones the concerned repository, or updates it if
|
|
||||||
already cloned. Then it pushes the update to *all remotes listed* in the [[file:.lohr][.lohr]]
|
|
||||||
file at the repo root.
|
|
||||||
|
|
||||||
*** Destructive
|
|
||||||
|
|
||||||
This is a very destructive process: anything removed from the single source of
|
|
||||||
truth is effectively removed from any mirror as well.
|
|
||||||
|
|
||||||
** Installing
|
|
||||||
|
|
||||||
=lohr= is [[https://crates.io/crates/lohr][published on crates.io]], so you can install it with ~cargo install~:
|
|
||||||
|
|
||||||
#+begin_src sh
|
|
||||||
$ cargo +nightly install lohr
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
We currently require a nightly compiler because [[https://github.com/SergioBenitez/Rocket][Rocket]] needs one to compile (a
|
|
||||||
0.5.0 which compiles on stable Rust is in the making, stay tuned!). You can
|
|
||||||
install a nightly toolchain with the following command:
|
|
||||||
|
|
||||||
#+begin_src sh
|
|
||||||
$ rustup install nightly
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
** Setup
|
|
||||||
|
|
||||||
*** Quickstart
|
|
||||||
|
|
||||||
Setting up =lohr= should be quite simple:
|
|
||||||
|
|
||||||
1. Create a =Rocket.toml= file and [[https://rocket.rs/v0.4/guide/configuration/][add your configuration]].
|
|
||||||
|
|
||||||
2. Export a secret variable:
|
|
||||||
|
|
||||||
#+begin_src sh
|
|
||||||
$ export LOHR_SECRET=42 # please don't use this secret
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
3. Run =lohr=:
|
|
||||||
|
|
||||||
#+begin_src sh
|
|
||||||
$ cargo run # or `cargo run --release` for production usage
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
4. Configure your favorite git server to send a webhook to =lohr='s address on
|
|
||||||
every push event.
|
|
||||||
|
|
||||||
I used [[https://docs.gitea.io/en-us/webhooks/][Gitea's webhooks format]], but I *think* they're similar to GitHub and
|
|
||||||
GitLab's webhooks, so these should work too! (If they don't, *please* file an
|
|
||||||
issue!)
|
|
||||||
|
|
||||||
Don't forget to set the webhook secret to the one you chose above.
|
|
||||||
|
|
||||||
5. Add a =.lohr= file containing the remotes you want to mirror this repo to:
|
|
||||||
|
|
||||||
#+begin_example
|
|
||||||
git@github.com:you/your_repo
|
|
||||||
#+end_example
|
|
||||||
|
|
||||||
and push it. That's it! =lohr= is mirroring your repo now.
|
|
||||||
|
|
||||||
*** Configuration
|
|
||||||
|
|
||||||
**** Home directory
|
|
||||||
|
|
||||||
=lohr= needs a place to clone repos and store its data. By default, it's the
|
|
||||||
current directory, but you can set the =LOHR_HOME= environment variable to
|
|
||||||
customize it.
|
|
||||||
|
|
||||||
**** Shared secret
|
|
||||||
|
|
||||||
As shown in the quickstart guide, you *must* set the =LOHR_SECRET= environment
|
|
||||||
variable.
|
|
||||||
|
|
||||||
**** Extra remote configuration
|
|
||||||
|
|
||||||
You can provide =lohr= with a YAML file containing additional configuration. You
|
|
||||||
can pass its path to the =--config= flag when launching =lohr=. If no
|
|
||||||
configuration is provided via a CLI flag, =lohr= will check the =LOHR_CONFIG=
|
|
||||||
environment variable. If the environment variable isn't set either, it will
|
|
||||||
check in =LOHR_HOME= is a =lohr-config.yaml= file exists, and try to load it.
|
|
||||||
|
|
||||||
This file takes the following format:
|
|
||||||
|
|
||||||
#+begin_src yaml
|
|
||||||
default_remotes:
|
|
||||||
- "git@github:user"
|
|
||||||
- "git@gitlab:user"
|
|
||||||
|
|
||||||
additional_remotes:
|
|
||||||
- "git@git.sr.ht:~user"
|
|
||||||
|
|
||||||
blacklist:
|
|
||||||
- "private-.*"
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
- ~default_remotes~ is a list of remotes to use if no ~.lohr~ file is found in a
|
|
||||||
repository.
|
|
||||||
- ~additional_remotes~ is a list of remotes to add in any case, whether the
|
|
||||||
original set of remotes is set via ~default_remotes~ or via a =.lohr= file.
|
|
||||||
- ~blacklist~ is a list of regular expressions to match against the full
|
|
||||||
repository names. Any that matches will not be mirrored, even if it contains a
|
|
||||||
`.lohr` file.
|
|
||||||
|
|
||||||
Both settings take as input a list of "stems", i.e. incomplete remote addresses,
|
|
||||||
to which the repo's name will be appended (so for example, if my
|
|
||||||
~default_remotes~ contains ~git@github.com:alarsyo~, and a push event webhook
|
|
||||||
is received for repository =git@gitlab.com:some/long/path/repo_name=, then the
|
|
||||||
mirror destination will be =git@github.com:alarsyo/repo_name=.
|
|
||||||
|
|
||||||
** Contributing
|
|
||||||
|
|
||||||
I accept patches anywhere! Feel free to [[https://github.com/alarsyo/lohr/pulls][open a GitHub Pull Request]], [[https://gitlab.com/alarsyo/lohr/-/merge_requests][a GitLab
|
|
||||||
Merge Request]], or [[https://lists.sr.ht/~alarsyo/lohr-dev][send me a patch by email]]!
|
|
||||||
|
|
||||||
** Why lohr?
|
|
||||||
|
|
||||||
I was looking for a cool name, and thought about the Magic Mirror in Snow White.
|
|
||||||
Some *[[https://en.wikipedia.org/wiki/Magic_Mirror_(Snow_White)][furious wikipedia searching]]* later, I found that the Magic Mirror was
|
|
||||||
probably inspired by [[http://spessartmuseum.de/seiten/schneewittchen_engl.html][the Talking Mirror in Lohr am Main]]. That's it, that's the
|
|
||||||
story.
|
|
||||||
|
|
||||||
** License
|
|
||||||
|
|
||||||
=lohr= is distributed under the terms of both the MIT license and the Apache
|
|
||||||
License (Version 2.0).
|
|
||||||
|
|
||||||
See [[file:LICENSE-APACHE][LICENSE-APACHE]] and [[file:LICENSE-MIT][LICENSE-MIT]] for details.
|
|
39
flake.lock
39
flake.lock
|
@ -31,43 +31,6 @@
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"mozillapkgs": {
|
|
||||||
"flake": false,
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1603906276,
|
|
||||||
"narHash": "sha256-RsNPnEKd7BcogwkqhaV5kI/HuNC4flH/OQCC/4W5y/8=",
|
|
||||||
"owner": "mozilla",
|
|
||||||
"repo": "nixpkgs-mozilla",
|
|
||||||
"rev": "8c007b60731c07dd7a052cce508de3bb1ae849b4",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "mozilla",
|
|
||||||
"repo": "nixpkgs-mozilla",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"naersk": {
|
|
||||||
"inputs": {
|
|
||||||
"nixpkgs": [
|
|
||||||
"nixpkgs"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1614785451,
|
|
||||||
"narHash": "sha256-TPw8kQvr2UNCuvndtY+EjyXp6Q5GEW2l9UafXXh1XmI=",
|
|
||||||
"owner": "nmattia",
|
|
||||||
"repo": "naersk",
|
|
||||||
"rev": "e0fe990b478a66178a58c69cf53daec0478ca6f9",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "nmattia",
|
|
||||||
"ref": "master",
|
|
||||||
"repo": "naersk",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1616670887,
|
"lastModified": 1616670887,
|
||||||
|
@ -88,8 +51,6 @@
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"flake-compat": "flake-compat",
|
"flake-compat": "flake-compat",
|
||||||
"flake-utils": "flake-utils",
|
"flake-utils": "flake-utils",
|
||||||
"mozillapkgs": "mozillapkgs",
|
|
||||||
"naersk": "naersk",
|
|
||||||
"nixpkgs": "nixpkgs"
|
"nixpkgs": "nixpkgs"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
34
flake.nix
34
flake.nix
|
@ -1,13 +1,5 @@
|
||||||
{
|
{
|
||||||
inputs = {
|
inputs = {
|
||||||
naersk = {
|
|
||||||
url = "github:nmattia/naersk/master";
|
|
||||||
inputs.nixpkgs.follows = "nixpkgs";
|
|
||||||
};
|
|
||||||
mozillapkgs = {
|
|
||||||
url = "github:mozilla/nixpkgs-mozilla";
|
|
||||||
flake = false;
|
|
||||||
};
|
|
||||||
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||||
flake-utils.url = "github:numtide/flake-utils";
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
flake-compat = {
|
flake-compat = {
|
||||||
|
@ -16,27 +8,19 @@
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = { self, naersk, mozillapkgs, nixpkgs, flake-utils, ... }:
|
outputs = { self, nixpkgs, flake-utils, ... }:
|
||||||
flake-utils.lib.eachDefaultSystem (system:
|
flake-utils.lib.eachDefaultSystem (system:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs { inherit system; };
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
|
||||||
mozilla = pkgs.callPackage (mozillapkgs + "/package-set.nix") { };
|
|
||||||
rustNightly = (mozilla.rustChannelOf {
|
|
||||||
date = "2021-03-29";
|
|
||||||
channel = "nightly";
|
|
||||||
sha256 = "sha256-Y94CnslybZgiZlNVV6Cg0TUPV2OeDXakPev1kqdt9Kk=";
|
|
||||||
}).rust;
|
|
||||||
|
|
||||||
naersk-lib = pkgs.callPackage naersk {
|
|
||||||
cargo = rustNightly;
|
|
||||||
rustc = rustNightly;
|
|
||||||
};
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
defaultPackage = naersk-lib.buildPackage {
|
defaultPackage = pkgs.rustPlatform.buildRustPackage {
|
||||||
src = ./.;
|
|
||||||
pname = "lohr";
|
pname = "lohr";
|
||||||
|
version = "0.4.0";
|
||||||
|
|
||||||
|
src = ./.;
|
||||||
|
|
||||||
|
cargoSha256 = "sha256-5a2mK+E6LlR5RHDAhHDvnfPNG+0JdvpnL4kuTiz7vVg=";
|
||||||
|
|
||||||
meta = with pkgs.lib; {
|
meta = with pkgs.lib; {
|
||||||
description = "A Git mirroring tool";
|
description = "A Git mirroring tool";
|
||||||
|
@ -52,10 +36,12 @@
|
||||||
|
|
||||||
devShell = pkgs.mkShell {
|
devShell = pkgs.mkShell {
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
|
cargo
|
||||||
|
clippy
|
||||||
nixpkgs-fmt
|
nixpkgs-fmt
|
||||||
pre-commit
|
pre-commit
|
||||||
rustPackages.clippy
|
rustPackages.clippy
|
||||||
rustNightly
|
rustc
|
||||||
rustfmt
|
rustfmt
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
nightly
|
stable
|
||||||
|
|
|
@ -4,7 +4,7 @@ use serde::Deserialize;
|
||||||
pub(crate) struct Repository {
|
pub(crate) struct Repository {
|
||||||
pub(crate) name: String,
|
pub(crate) name: String,
|
||||||
pub(crate) full_name: String,
|
pub(crate) full_name: String,
|
||||||
pub(crate) clone_url: String,
|
pub(crate) ssh_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
|
@ -38,7 +38,7 @@ impl Job {
|
||||||
let output = Command::new("git")
|
let output = Command::new("git")
|
||||||
.arg("clone")
|
.arg("clone")
|
||||||
.arg("--mirror")
|
.arg("--mirror")
|
||||||
.arg(&self.repo.clone_url)
|
.arg(&self.repo.ssh_url)
|
||||||
.arg(format!("{}", self.local_path.as_ref().unwrap().display()))
|
.arg(format!("{}", self.local_path.as_ref().unwrap().display()))
|
||||||
.output()?;
|
.output()?;
|
||||||
|
|
||||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -1,5 +1,3 @@
|
||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -10,7 +8,7 @@ use std::sync::{
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use clap::{App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use rocket::{http::Status, post, routes, State};
|
use rocket::{http::Status, post, routes, State};
|
||||||
|
|
||||||
|
@ -36,12 +34,15 @@ fn gitea_webhook(
|
||||||
config: State<GlobalSettings>,
|
config: State<GlobalSettings>,
|
||||||
) -> Status {
|
) -> Status {
|
||||||
if config
|
if config
|
||||||
.blacklist
|
.filters
|
||||||
.iter()
|
.iter()
|
||||||
.any(|re| re.is_match(&payload.repository.full_name))
|
// Find first filter that matches the given destination
|
||||||
|
.find(|filter| filter.destination.as_ref().map_or(false, |re| re.is_match(&payload.repository.full_name)))
|
||||||
|
// Default to mirroring, unless told not to
|
||||||
|
.map_or(true, |filter| filter.mirror)
|
||||||
{
|
{
|
||||||
info!(
|
info!(
|
||||||
"Ignoring webhook for repo {} which is blacklisted",
|
"Ignoring webhook for repo {} which is marked as not mirrored",
|
||||||
payload.repository.full_name
|
payload.repository.full_name
|
||||||
);
|
);
|
||||||
return Status::Ok;
|
return Status::Ok;
|
||||||
|
@ -92,9 +93,10 @@ fn parse_config(home: &Path, flags: &clap::ArgMatches) -> anyhow::Result<GlobalS
|
||||||
serde_yaml::from_reader(file).context("could not parse configuration file")
|
serde_yaml::from_reader(file).context("could not parse configuration file")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
#[rocket::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
let matches = App::new("lohr")
|
let matches = App::new("lohr")
|
||||||
.version("0.3.1")
|
.version(crate_version!())
|
||||||
.about("Git mirroring daemon")
|
.about("Git mirroring daemon")
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("config")
|
Arg::with_name("config")
|
||||||
|
@ -127,7 +129,8 @@ fn main() -> anyhow::Result<()> {
|
||||||
.manage(JobSender(Mutex::new(sender)))
|
.manage(JobSender(Mutex::new(sender)))
|
||||||
.manage(Secret(secret))
|
.manage(Secret(secret))
|
||||||
.manage(config_state)
|
.manage(config_state)
|
||||||
.launch();
|
.launch()
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,29 @@ pub(crate) struct GlobalSettings {
|
||||||
/// List of remote stems to use for every repository
|
/// List of remote stems to use for every repository
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub additional_remotes: Vec<RepoUrl>,
|
pub additional_remotes: Vec<RepoUrl>,
|
||||||
/// List of regexes, if a repository's name matches any of the, it is not mirrored by `lohr`
|
/// List of filters to blacklist repositories, or modify push options on specific remotes.
|
||||||
/// even if it contains a `.lorh` file.
|
/// Only the first matching filter is applied, so order is important.
|
||||||
#[serde(with = "serde_regex")]
|
#[serde(default)]
|
||||||
pub blacklist: Vec<regex::Regex>,
|
pub filters: Vec<FilterSettings>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Default, Deserialize)]
|
||||||
|
pub(crate) struct FilterSettings {
|
||||||
|
/// Match on the source remote
|
||||||
|
#[serde(with = "serde_regex", default)]
|
||||||
|
pub source: Option<regex::Regex>,
|
||||||
|
/// Match on the destination remote
|
||||||
|
#[serde(with = "serde_regex", default)]
|
||||||
|
pub destination: Option<regex::Regex>,
|
||||||
|
/// Whether to mirror the repository or not
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub mirror: bool,
|
||||||
|
/// Push options to be used for the matched remote
|
||||||
|
#[serde(default)]
|
||||||
|
pub push_options: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workaround for https://github.com/serde-rs/serde/issues/368
|
||||||
|
fn default_true() -> bool {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,17 @@
|
||||||
use std::{
|
use std::{
|
||||||
io::Read,
|
io,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
};
|
};
|
||||||
|
|
||||||
use rocket::{
|
use rocket::{
|
||||||
data::{FromData, Outcome},
|
data::{ByteUnit, FromData, Outcome},
|
||||||
http::ContentType,
|
http::ContentType,
|
||||||
State,
|
State,
|
||||||
};
|
};
|
||||||
use rocket::{
|
use rocket::{http::Status, local_cache};
|
||||||
data::{Transform, Transformed},
|
|
||||||
http::Status,
|
|
||||||
};
|
|
||||||
use rocket::{Data, Request};
|
use rocket::{Data, Request};
|
||||||
|
|
||||||
use anyhow::anyhow;
|
use anyhow::{anyhow, Context};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::Secret;
|
use crate::Secret;
|
||||||
|
@ -53,37 +50,29 @@ impl<T> DerefMut for SignedJson<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const LIMIT: u64 = 1 << 20;
|
const LIMIT: ByteUnit = ByteUnit::Mebibyte(1);
|
||||||
|
|
||||||
|
impl<'r, T: Deserialize<'r>> SignedJson<T> {
|
||||||
|
fn from_str(s: &'r str) -> anyhow::Result<Self> {
|
||||||
|
serde_json::from_str(s)
|
||||||
|
.map(SignedJson)
|
||||||
|
.context("could not parse json")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This is a one to one implementation of request_contrib::Json's FromData, but with HMAC
|
// This is a one to one implementation of request_contrib::Json's FromData, but with HMAC
|
||||||
// validation.
|
// validation.
|
||||||
//
|
//
|
||||||
// Tracking issue for chaining Data guards to avoid this:
|
// Tracking issue for chaining Data guards to avoid this:
|
||||||
// https://github.com/SergioBenitez/Rocket/issues/775
|
// https://github.com/SergioBenitez/Rocket/issues/775
|
||||||
impl<'a, T> FromData<'a> for SignedJson<T>
|
#[rocket::async_trait]
|
||||||
|
impl<'r, T> FromData<'r> for SignedJson<T>
|
||||||
where
|
where
|
||||||
T: Deserialize<'a>,
|
T: Deserialize<'r>,
|
||||||
{
|
{
|
||||||
type Error = anyhow::Error;
|
type Error = anyhow::Error;
|
||||||
type Owned = String;
|
|
||||||
type Borrowed = str;
|
|
||||||
|
|
||||||
fn transform(
|
async fn from_data(request: &'r Request<'_>, data: Data) -> Outcome<Self, Self::Error> {
|
||||||
request: &Request,
|
|
||||||
data: Data,
|
|
||||||
) -> rocket::data::Transform<Outcome<Self::Owned, Self::Error>> {
|
|
||||||
let size_limit = request.limits().get("json").unwrap_or(LIMIT);
|
|
||||||
let mut s = String::with_capacity(512);
|
|
||||||
match data.open().take(size_limit).read_to_string(&mut s) {
|
|
||||||
Ok(_) => Transform::Borrowed(Outcome::Success(s)),
|
|
||||||
Err(e) => Transform::Borrowed(Outcome::Failure((
|
|
||||||
Status::BadRequest,
|
|
||||||
anyhow!("couldn't read json: {}", e),
|
|
||||||
))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_data(request: &Request, o: Transformed<'a, Self>) -> Outcome<Self, Self::Error> {
|
|
||||||
let json_ct = ContentType::new("application", "json");
|
let json_ct = ContentType::new("application", "json");
|
||||||
if request.content_type() != Some(&json_ct) {
|
if request.content_type() != Some(&json_ct) {
|
||||||
return Outcome::Failure((Status::BadRequest, anyhow!("wrong content type")));
|
return Outcome::Failure((Status::BadRequest, anyhow!("wrong content type")));
|
||||||
|
@ -97,26 +86,31 @@ where
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let size_limit = request.limits().get("json").unwrap_or(LIMIT);
|
||||||
|
let content = match data.open(size_limit).into_string().await {
|
||||||
|
Ok(s) if s.is_complete() => s.into_inner(),
|
||||||
|
Ok(_) => {
|
||||||
|
let eof = io::ErrorKind::UnexpectedEof;
|
||||||
|
return Outcome::Failure((
|
||||||
|
Status::PayloadTooLarge,
|
||||||
|
io::Error::new(eof, "data limit exceeded").into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Err(e) => return Outcome::Failure((Status::BadRequest, e.into())),
|
||||||
|
};
|
||||||
|
|
||||||
let signature = signatures[0];
|
let signature = signatures[0];
|
||||||
|
let secret = request.guard::<State<Secret>>().await.unwrap();
|
||||||
|
|
||||||
let content = o.borrowed()?;
|
if !validate_signature(&secret.0, &signature, &content) {
|
||||||
|
|
||||||
let secret = request.guard::<State<Secret>>().unwrap();
|
|
||||||
|
|
||||||
if !validate_signature(&secret.0, &signature, content) {
|
|
||||||
return Outcome::Failure((Status::BadRequest, anyhow!("couldn't verify signature")));
|
return Outcome::Failure((Status::BadRequest, anyhow!("couldn't verify signature")));
|
||||||
}
|
}
|
||||||
|
|
||||||
let content = match serde_json::from_str(content) {
|
let content = match Self::from_str(local_cache!(request, content)) {
|
||||||
Ok(content) => content,
|
Ok(content) => Outcome::Success(content),
|
||||||
Err(e) => {
|
Err(e) => Outcome::Failure((Status::BadRequest, e)),
|
||||||
return Outcome::Failure((
|
|
||||||
Status::BadRequest,
|
|
||||||
anyhow!("couldn't parse json: {}", e),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Outcome::Success(SignedJson(content))
|
content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue