From 54ee424f5f40dbb912ec8ee19e86558786db8fd1 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Fri, 19 Jul 2024 15:48:49 +0000 Subject: [PATCH] feat: add tidb service --- .github/workflows/tidb.yml | 35 ++++++++++++++++++++++++ Cargo.lock | 10 +++++++ Cargo.toml | 2 ++ README.md | 1 + tidb/Cargo.toml | 20 ++++++++++++++ tidb/LICENSE | 19 ++++++++++++++ tidb/README.md | 54 ++++++++++++++++++++++++++++++++++++++ tidb/fluentci.toml | 11 ++++++++ tidb/src/helpers.rs | 54 ++++++++++++++++++++++++++++++++++++++ tidb/src/lib.rs | 45 +++++++++++++++++++++++++++++++ tidb/target | 1 + 11 files changed, 252 insertions(+) create mode 100644 .github/workflows/tidb.yml create mode 100644 tidb/Cargo.toml create mode 100644 tidb/LICENSE create mode 100644 tidb/README.md create mode 100644 tidb/fluentci.toml create mode 100644 tidb/src/helpers.rs create mode 100644 tidb/src/lib.rs create mode 120000 tidb/target diff --git a/.github/workflows/tidb.yml b/.github/workflows/tidb.yml new file mode 100644 index 0000000..2acbd77 --- /dev/null +++ b/.github/workflows/tidb.yml @@ -0,0 +1,35 @@ +name: ci +on: + push: + branches: + - main + paths: + - tidb/** + - .github/workflows/tidb.yml +jobs: + tidb-test: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: Setup Fluent CI + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: rust + args: | + target_add wasm32-unknown-unknown + build --release --target wasm32-unknown-unknown + working-directory: tidb + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Start tidb Server + run: | + fluentci run target/wasm32-unknown-unknown/release/tidb.wasm start + fluentci run target/wasm32-unknown-unknown/release/tidb.wasm stop + working-directory: tidb + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NIX_CONFIG: extra-access-tokens = github.com=${{ secrets.GH_ACCESS_TOKEN }} diff --git a/Cargo.lock b/Cargo.lock index e877dc1..278fe10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -672,6 +672,16 @@ dependencies = [ "fluentci-types", ] +[[package]] +name = "tidb" +version = "0.1.0" +dependencies = [ + "anyhow", + "extism-pdk", + "fluentci-pdk", + "fluentci-types", +] + [[package]] name = "toml_datetime" version = "0.6.6" diff --git a/Cargo.toml b/Cargo.toml index ed5e499..1ac0f83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ default-members = [ "redis", "stripe-mock", "temporal", + "tidb", "typesense", "vault", ] @@ -66,6 +67,7 @@ members = [ "redis", "stripe-mock", "temporal", + "tidb", "typesense", "vault", ] diff --git a/README.md b/README.md index 6340d40..1c17961 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ fluentci run --wasm postgres start | [redis](./redis) | An in-memory database that persists on disk | | [stripe-mock](./stripe-mock/) | A mock HTTP server that responds like the real Stripe API. | | [temporal](./temporal) | A distributed, scalable, durable, and highly available orchestration engine | +| [tidb](./tidb) | A distributed SQL database | | [typesense](./typesense) | A fast, typo-tolerant search engine for building delightful search experiences | | [vault](./vault) | A tool for managing secrets and protecting sensitive data | diff --git a/tidb/Cargo.toml b/tidb/Cargo.toml new file mode 100644 index 0000000..de45d24 --- /dev/null +++ b/tidb/Cargo.toml @@ -0,0 +1,20 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "TiDB plugin for FluentCI" +edition = "2021" +license = "MIT" +name = "tidb" +version = "0.1.0" + +[lib] +crate-type = [ + "cdylib", +] + +[dependencies] +anyhow = "1.0.82" +extism-pdk = "1.1.0" +fluentci-pdk = "0.2.1" +fluentci-types = "0.1.7" diff --git a/tidb/LICENSE b/tidb/LICENSE new file mode 100644 index 0000000..515dd5f --- /dev/null +++ b/tidb/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Tsiry Sandratraina + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/tidb/README.md b/tidb/README.md new file mode 100644 index 0000000..73efbb6 --- /dev/null +++ b/tidb/README.md @@ -0,0 +1,54 @@ +# TiDB Local Plugin + +[![fluentci pipeline](https://shield.fluentci.io/x/tidb)](https://pkg.fluentci.io/tidb) +[![ci](https://github.com/fluentci-io/services/actions/workflows/tidb.yml/badge.svg)](https://github.com/fluentci-io/services/actions/workflows/tidb.yml) + +TiDB service plugin for FluentCI. + +## 🚀 Usage + +Add the following command to your CI configuration file: + +```bash +fluentci run --wasm tidb start +``` + +## Functions + +| Name | Description | +| ------ | --------------------------------------------| +| start | Start TiDB Local Server | +| stop | Stop TiDB Local Server | + +## Code Usage + +Add `fluentci-pdk` crate to your `Cargo.toml`: + +```toml +[dependencies] +fluentci-pdk = "0.2.1" +``` + +Use the following code to call the plugin: + +```rust +use fluentci_pdk::dag; + +// ... + +dag().call("https://pkg.fluentci.io/tidb@v0.1.0?wasm=1", "start", vec![])?; +``` + +## 📚 Examples + +Github Actions: + +```yaml +- name: Setup Fluent CI CLI + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: tidb + args: | + start +``` diff --git a/tidb/fluentci.toml b/tidb/fluentci.toml new file mode 100644 index 0000000..9aa798c --- /dev/null +++ b/tidb/fluentci.toml @@ -0,0 +1,11 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "TiDB Plugin for FluentCI" +keywords = [ + "tidb", +] +license = "MIT" +name = "tidb" +version = "0.1.0" diff --git a/tidb/src/helpers.rs b/tidb/src/helpers.rs new file mode 100644 index 0000000..3d1ea02 --- /dev/null +++ b/tidb/src/helpers.rs @@ -0,0 +1,54 @@ +use anyhow::Error; +use fluentci_pdk::dag; + +pub fn setup_flox() -> Result<(), Error> { + let os = dag().get_os()?; + if os == "macos" { + dag() + .pipeline("setup-flox")? + .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)""#])? + .with_exec(vec!["type flox > /dev/null 2> /dev/null || brew install flox"])? + .stdout()?; + } + Ok(()) +} + +pub fn setup() -> Result { + setup_flox()?; + dag() + .pipeline("setup")? + .with_exec(vec!["mkdir", "-p", ".fluentci/tidb"])? + .stdout()?; + + let pwd = dag().get_env("PWD")?; + let tidb_port = dag().get_env("TIDB_PORT")?; + let data_dir = dag().get_env("TIDB_DATADIR")?; + + if tidb_port.is_empty() { + dag().set_envs(vec![("TIDB_PORT".into(), "4000".into())])?; + } + + if data_dir.is_empty() { + dag().set_envs(vec![("TIDB_DATADIR".into(), format!("{}/tidb", pwd))])?; + } + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/tidb")? + .with_exec(vec![ + "[ -d $TIDB_DATADIR ] || mkdir -p $TIDB_DATADIR", + ])? + .with_exec(vec![ + "flox", + "install", + "tidb", + "overmind", + "tmux", + ])? + .with_exec(vec![ + "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", + ])? + .stdout()?; + + Ok(stdout) +} diff --git a/tidb/src/lib.rs b/tidb/src/lib.rs new file mode 100644 index 0000000..3c1a732 --- /dev/null +++ b/tidb/src/lib.rs @@ -0,0 +1,45 @@ +use extism_pdk::*; +use fluentci_pdk::dag; + +pub mod helpers; + +#[plugin_fn] +pub fn start(_args: String) -> FnResult { + helpers::setup()?; + + let port = dag().get_env("TIDB_PORT")?; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/tidb")? + .with_exec(vec!["overmind", "--version"])? + .with_exec(vec!["type", "overmind"])? + .with_exec(vec!["type", "tidb-server"])? + .with_exec(vec!["tidb-server", "-V"])? + .with_exec(vec!["echo -e \"TiDB starting on port $TIDB_PORT\""])? + .with_exec(vec![ + "overmind start -f Procfile --daemonize || flox activate -- overmind restart tidb", + ])? + .wait_on(port.parse()?, None)? + .with_exec(vec!["overmind", "status"])? + .stdout()?; + Ok(stdout) +} + +#[plugin_fn] +pub fn stop(args: String) -> FnResult { + helpers::setup()?; + + let args = if args.is_empty() { + "tidb".to_string() + } else { + args + }; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/tidb")? + .with_exec(vec!["overmind", "stop", &args])? + .stdout()?; + Ok(stdout) +} diff --git a/tidb/target b/tidb/target new file mode 120000 index 0000000..78bc337 --- /dev/null +++ b/tidb/target @@ -0,0 +1 @@ +../target \ No newline at end of file