diff --git a/.github/workflows/httpbin.yml b/.github/workflows/httpbin.yml new file mode 100644 index 0000000..723dc2f --- /dev/null +++ b/.github/workflows/httpbin.yml @@ -0,0 +1,35 @@ +name: ci +on: + push: + branches: + - main + paths: + - httpbin/** + - .github/workflows/httpbin.yml +jobs: + httpbin-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: httpbin + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Start Httpbin Server + run: | + fluentci run target/wasm32-unknown-unknown/release/httpbin.wasm start + fluentci run target/wasm32-unknown-unknown/release/httpbin.wasm stop httpbin + working-directory: httpbin + 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 29b266c..2b27208 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,16 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "httpbin" +version = "0.1.0" +dependencies = [ + "anyhow", + "extism-pdk", + "fluentci-pdk", + "fluentci-types", +] + [[package]] name = "indexmap" version = "2.2.6" diff --git a/Cargo.toml b/Cargo.toml index 7103d0b..f50c6fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ [workspace] -default-members = ["apache", "redis", "dragonflydb", "nginx", "caddy"] -members = ["apache", "redis", "dragonflydb", "nginx", "caddy"] +default-members = ["apache", "redis", "dragonflydb", "nginx", "caddy", "httpbin"] +members = ["apache", "redis", "dragonflydb", "nginx", "caddy", "httpbin"] resolver = "2" diff --git a/httpbin/Cargo.toml b/httpbin/Cargo.toml new file mode 100644 index 0000000..bffd952 --- /dev/null +++ b/httpbin/Cargo.toml @@ -0,0 +1,20 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Httpbin plugin for FluentCI" +edition = "2021" +license = "MIT" +name = "httpbin" +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/httpbin/LICENSE b/httpbin/LICENSE new file mode 100644 index 0000000..515dd5f --- /dev/null +++ b/httpbin/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/httpbin/README.md b/httpbin/README.md new file mode 100644 index 0000000..0890db0 --- /dev/null +++ b/httpbin/README.md @@ -0,0 +1,53 @@ +# Httpbin Plugin + +[![ci](https://github.com/fluentci-io/services/actions/workflows/httpbin.yml/badge.svg)](https://github.com/fluentci-io/services/actions/workflows/httpbin.yml) + +Caddy service plugin for FluentCI. + +## 🚀 Usage + +Add the following command to your CI configuration file: + +```bash +fluentci run --wasm httpbin start +``` + +## Functions + +| Name | Description | +| ------ | -------------------------------------------------- | +| start | Start Httpbin Server | +| stop | Stops Httpbin 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/httpbin@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: httpbin + args: | + start +``` diff --git a/httpbin/fluentci.toml b/httpbin/fluentci.toml new file mode 100644 index 0000000..1d2041b --- /dev/null +++ b/httpbin/fluentci.toml @@ -0,0 +1,11 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Httpbin Plugin for FluentCI" +keywords = [ + "httpbin", +] +license = "MIT" +name = "httpbin" +version = "0.1.0" diff --git a/httpbin/src/helpers.rs b/httpbin/src/helpers.rs new file mode 100644 index 0000000..45f9599 --- /dev/null +++ b/httpbin/src/helpers.rs @@ -0,0 +1,40 @@ +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"])? + .stdout()?; + + let httpbin_port = dag().get_env("HTTPBIN_PORT")?; + if httpbin_port.is_empty() { + dag().set_envs(vec![("HTTPBIN_PORT".into(), "8080".into())])?; + } + + let stdout = dag() + .flox()? + .with_workdir(".fluentci")? + .with_exec(vec![ + "flox", "install", "python312Packages.httpbin", "python312Packages.gunicorn", "python312Packages.gevent", "overmind", "tmux", "curl", + ])? + .with_exec(vec![ + "grep -q httpin Procfile || echo 'httpbin: gunicorn httpbin:app -k gevent -b 127.0.0.1:$HTTPBIN_PORT' >> Procfile", + ])? + .stdout()?; + + Ok(stdout) +} diff --git a/httpbin/src/lib.rs b/httpbin/src/lib.rs new file mode 100644 index 0000000..7713e1c --- /dev/null +++ b/httpbin/src/lib.rs @@ -0,0 +1,36 @@ +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("HTTPBIN_PORT")?; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci")? + .with_exec(vec!["overmind", "--version"])? + .with_exec(vec!["gunicorn", "--version"])? + .with_exec(vec!["type", "overmind"])? + .with_exec(vec!["type", "gunicorn"])? + .with_exec(vec!["overmind", "start", "-f", "Procfile", "--daemonize"])? + .wait_on(port.parse()?, None)? + .with_exec(vec!["overmind", "status"])? + .with_exec(vec!["curl", "-s", "http://localhost:$HTTPBIN_PORT"])? + .stdout()?; + Ok(stdout) +} + +#[plugin_fn] +pub fn stop(args: String) -> FnResult { + helpers::setup()?; + let stdout = dag() + .flox()? + .with_workdir(".fluentci")? + .with_exec(vec!["overmind", "stop", &args])? + .stdout()?; + Ok(stdout) +} diff --git a/httpbin/target b/httpbin/target new file mode 120000 index 0000000..78bc337 --- /dev/null +++ b/httpbin/target @@ -0,0 +1 @@ +../target \ No newline at end of file