Skip to content

Commit 54ee424

Browse files
committed
feat: add tidb service
1 parent 7b08beb commit 54ee424

File tree

11 files changed

+252
-0
lines changed

11 files changed

+252
-0
lines changed

.github/workflows/tidb.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: ci
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- tidb/**
8+
- .github/workflows/tidb.yml
9+
jobs:
10+
tidb-test:
11+
strategy:
12+
matrix:
13+
os: [ubuntu-latest, macos-latest]
14+
runs-on: ${{ matrix.os }}
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Setup Fluent CI
18+
uses: fluentci-io/setup-fluentci@v5
19+
with:
20+
wasm: true
21+
plugin: rust
22+
args: |
23+
target_add wasm32-unknown-unknown
24+
build --release --target wasm32-unknown-unknown
25+
working-directory: tidb
26+
env:
27+
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
- name: Start tidb Server
29+
run: |
30+
fluentci run target/wasm32-unknown-unknown/release/tidb.wasm start
31+
fluentci run target/wasm32-unknown-unknown/release/tidb.wasm stop
32+
working-directory: tidb
33+
env:
34+
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
NIX_CONFIG: extra-access-tokens = github.com=${{ secrets.GH_ACCESS_TOKEN }}

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ default-members = [
3131
"redis",
3232
"stripe-mock",
3333
"temporal",
34+
"tidb",
3435
"typesense",
3536
"vault",
3637
]
@@ -66,6 +67,7 @@ members = [
6667
"redis",
6768
"stripe-mock",
6869
"temporal",
70+
"tidb",
6971
"typesense",
7072
"vault",
7173
]

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ fluentci run --wasm postgres start
6464
| [redis](./redis) | An in-memory database that persists on disk |
6565
| [stripe-mock](./stripe-mock/) | A mock HTTP server that responds like the real Stripe API. |
6666
| [temporal](./temporal) | A distributed, scalable, durable, and highly available orchestration engine |
67+
| [tidb](./tidb) | A distributed SQL database |
6768
| [typesense](./typesense) | A fast, typo-tolerant search engine for building delightful search experiences |
6869
| [vault](./vault) | A tool for managing secrets and protecting sensitive data |
6970

tidb/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
authors = [
3+
"Tsiry Sandratraina <tsiry.sndr@fluentci.io>",
4+
]
5+
description = "TiDB plugin for FluentCI"
6+
edition = "2021"
7+
license = "MIT"
8+
name = "tidb"
9+
version = "0.1.0"
10+
11+
[lib]
12+
crate-type = [
13+
"cdylib",
14+
]
15+
16+
[dependencies]
17+
anyhow = "1.0.82"
18+
extism-pdk = "1.1.0"
19+
fluentci-pdk = "0.2.1"
20+
fluentci-types = "0.1.7"

tidb/LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2024 Tsiry Sandratraina <tsiry.sndr@fluentci.io>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

tidb/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# TiDB Local Plugin
2+
3+
[![fluentci pipeline](https://shield.fluentci.io/x/tidb)](https://pkg.fluentci.io/tidb)
4+
[![ci](https://github.com/fluentci-io/services/actions/workflows/tidb.yml/badge.svg)](https://github.com/fluentci-io/services/actions/workflows/tidb.yml)
5+
6+
TiDB service plugin for FluentCI.
7+
8+
## 🚀 Usage
9+
10+
Add the following command to your CI configuration file:
11+
12+
```bash
13+
fluentci run --wasm tidb start
14+
```
15+
16+
## Functions
17+
18+
| Name | Description |
19+
| ------ | --------------------------------------------|
20+
| start | Start TiDB Local Server |
21+
| stop | Stop TiDB Local Server |
22+
23+
## Code Usage
24+
25+
Add `fluentci-pdk` crate to your `Cargo.toml`:
26+
27+
```toml
28+
[dependencies]
29+
fluentci-pdk = "0.2.1"
30+
```
31+
32+
Use the following code to call the plugin:
33+
34+
```rust
35+
use fluentci_pdk::dag;
36+
37+
// ...
38+
39+
dag().call("https://pkg.fluentci.io/tidb@v0.1.0?wasm=1", "start", vec![])?;
40+
```
41+
42+
## 📚 Examples
43+
44+
Github Actions:
45+
46+
```yaml
47+
- name: Setup Fluent CI CLI
48+
uses: fluentci-io/setup-fluentci@v5
49+
with:
50+
wasm: true
51+
plugin: tidb
52+
args: |
53+
start
54+
```

tidb/fluentci.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
authors = [
3+
"Tsiry Sandratraina <tsiry.sndr@fluentci.io>",
4+
]
5+
description = "TiDB Plugin for FluentCI"
6+
keywords = [
7+
"tidb",
8+
]
9+
license = "MIT"
10+
name = "tidb"
11+
version = "0.1.0"

tidb/src/helpers.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use anyhow::Error;
2+
use fluentci_pdk::dag;
3+
4+
pub fn setup_flox() -> Result<(), Error> {
5+
let os = dag().get_os()?;
6+
if os == "macos" {
7+
dag()
8+
.pipeline("setup-flox")?
9+
.with_exec(vec![r#"type brew > /dev/null 2> /dev/null || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)""#])?
10+
.with_exec(vec!["type flox > /dev/null 2> /dev/null || brew install flox"])?
11+
.stdout()?;
12+
}
13+
Ok(())
14+
}
15+
16+
pub fn setup() -> Result<String, Error> {
17+
setup_flox()?;
18+
dag()
19+
.pipeline("setup")?
20+
.with_exec(vec!["mkdir", "-p", ".fluentci/tidb"])?
21+
.stdout()?;
22+
23+
let pwd = dag().get_env("PWD")?;
24+
let tidb_port = dag().get_env("TIDB_PORT")?;
25+
let data_dir = dag().get_env("TIDB_DATADIR")?;
26+
27+
if tidb_port.is_empty() {
28+
dag().set_envs(vec![("TIDB_PORT".into(), "4000".into())])?;
29+
}
30+
31+
if data_dir.is_empty() {
32+
dag().set_envs(vec![("TIDB_DATADIR".into(), format!("{}/tidb", pwd))])?;
33+
}
34+
35+
let stdout = dag()
36+
.flox()?
37+
.with_workdir(".fluentci/tidb")?
38+
.with_exec(vec![
39+
"[ -d $TIDB_DATADIR ] || mkdir -p $TIDB_DATADIR",
40+
])?
41+
.with_exec(vec![
42+
"flox",
43+
"install",
44+
"tidb",
45+
"overmind",
46+
"tmux",
47+
])?
48+
.with_exec(vec![
49+
"grep -q tidb: Procfile || echo -e 'tidb: tidb-server -path $TIDB_DATADIR -temp-dir $TIDB_DATADIR -socket $TIDB_DATADIR/tidb.sock -P $TIDB_PORT \\n' >> Procfile",
50+
])?
51+
.stdout()?;
52+
53+
Ok(stdout)
54+
}

tidb/src/lib.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use extism_pdk::*;
2+
use fluentci_pdk::dag;
3+
4+
pub mod helpers;
5+
6+
#[plugin_fn]
7+
pub fn start(_args: String) -> FnResult<String> {
8+
helpers::setup()?;
9+
10+
let port = dag().get_env("TIDB_PORT")?;
11+
12+
let stdout = dag()
13+
.flox()?
14+
.with_workdir(".fluentci/tidb")?
15+
.with_exec(vec!["overmind", "--version"])?
16+
.with_exec(vec!["type", "overmind"])?
17+
.with_exec(vec!["type", "tidb-server"])?
18+
.with_exec(vec!["tidb-server", "-V"])?
19+
.with_exec(vec!["echo -e \"TiDB starting on port $TIDB_PORT\""])?
20+
.with_exec(vec![
21+
"overmind start -f Procfile --daemonize || flox activate -- overmind restart tidb",
22+
])?
23+
.wait_on(port.parse()?, None)?
24+
.with_exec(vec!["overmind", "status"])?
25+
.stdout()?;
26+
Ok(stdout)
27+
}
28+
29+
#[plugin_fn]
30+
pub fn stop(args: String) -> FnResult<String> {
31+
helpers::setup()?;
32+
33+
let args = if args.is_empty() {
34+
"tidb".to_string()
35+
} else {
36+
args
37+
};
38+
39+
let stdout = dag()
40+
.flox()?
41+
.with_workdir(".fluentci/tidb")?
42+
.with_exec(vec!["overmind", "stop", &args])?
43+
.stdout()?;
44+
Ok(stdout)
45+
}

tidb/target

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../target

0 commit comments

Comments
 (0)