From b890b9dc602a0b9ff02ea8fced2aaa50f1e40b7b Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Tue, 16 Jul 2024 11:19:26 +0000 Subject: [PATCH] feat: add consul service update README.md fix consul config update default consul config update default consul config --- .github/workflows/consul.yml | 35 +++++++++++++++++ Cargo.lock | 10 +++++ Cargo.toml | 2 + README.md | 1 + consul/Cargo.toml | 20 ++++++++++ consul/LICENSE | 19 ++++++++++ consul/README.md | 54 ++++++++++++++++++++++++++ consul/consul-config.json | 12 ++++++ consul/consul-config.json.template | 12 ++++++ consul/fluentci.toml | 11 ++++++ consul/src/helpers.rs | 61 ++++++++++++++++++++++++++++++ consul/src/lib.rs | 46 ++++++++++++++++++++++ consul/target | 1 + 13 files changed, 284 insertions(+) create mode 100644 .github/workflows/consul.yml create mode 100644 consul/Cargo.toml create mode 100644 consul/LICENSE create mode 100644 consul/README.md create mode 100644 consul/consul-config.json create mode 100644 consul/consul-config.json.template create mode 100644 consul/fluentci.toml create mode 100644 consul/src/helpers.rs create mode 100644 consul/src/lib.rs create mode 120000 consul/target diff --git a/.github/workflows/consul.yml b/.github/workflows/consul.yml new file mode 100644 index 0000000..278e75a --- /dev/null +++ b/.github/workflows/consul.yml @@ -0,0 +1,35 @@ +name: ci +on: + push: + branches: + - main + paths: + - consul/** + - .github/workflows/consul.yml +jobs: + consul-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: consul + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Start consul Server + run: | + fluentci run target/wasm32-unknown-unknown/release/consul.wasm start + fluentci run target/wasm32-unknown-unknown/release/consul.wasm stop + working-directory: consul + 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 c662b5f..1f99f6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,6 +58,16 @@ dependencies = [ "fluentci-types", ] +[[package]] +name = "consul" +version = "0.1.0" +dependencies = [ + "anyhow", + "extism-pdk", + "fluentci-pdk", + "fluentci-types", +] + [[package]] name = "dragonflydb" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 99cf059..c0db357 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ default-members = [ "apache", "caddy", + "consul", "dragonflydb", "dynamodb-local", "elasticmq", @@ -29,6 +30,7 @@ default-members = [ members = [ "apache", "caddy", + "consul", "dragonflydb", "dynamodb-local", "elasticmq", diff --git a/README.md b/README.md index 4c9e2bb..2cbbb3d 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ fluentci run --wasm postgres start |------------------------------|--------------------| | [apache](./apache) | A powerful and flexible HTTP/1.1 compliant web server | | [caddy](./caddy) | Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS | +| [consul](./consul) | A distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure | | [dragonflydb](./dragonflydb) | A modern replacement for Redis and Memcached | | [dynamodb-local](./dynamodb-local) | A local version of Amazon DynamoDB | | [elasticmq](./elasticmq/) | A message queue system with the same semantics as Amazon SQS | diff --git a/consul/Cargo.toml b/consul/Cargo.toml new file mode 100644 index 0000000..574cae2 --- /dev/null +++ b/consul/Cargo.toml @@ -0,0 +1,20 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Consul plugin for FluentCI" +edition = "2021" +license = "MIT" +name = "consul" +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/consul/LICENSE b/consul/LICENSE new file mode 100644 index 0000000..515dd5f --- /dev/null +++ b/consul/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/consul/README.md b/consul/README.md new file mode 100644 index 0000000..9bc036f --- /dev/null +++ b/consul/README.md @@ -0,0 +1,54 @@ +# Consul Plugin + +[![fluentci pipeline](https://shield.fluentci.io/x/consul)](https://pkg.fluentci.io/consul) +[![ci](https://github.com/fluentci-io/services/actions/workflows/consul.yml/badge.svg)](https://github.com/fluentci-io/services/actions/workflows/consul.yml) + +Consul service plugin for FluentCI. + +## 🚀 Usage + +Add the following command to your CI configuration file: + +```bash +fluentci run --wasm consul start +``` + +## Functions + +| Name | Description | +| ------ | -------------------------------------------------- | +| start | Start consul server | +| stop | Stop consul 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/consul@v0.1.1?wasm=1", "start", vec![])?; +``` + +## 📚 Examples + +Github Actions: + +```yaml +- name: Setup Fluent CI CLI + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: consul + args: | + start +``` diff --git a/consul/consul-config.json b/consul/consul-config.json new file mode 100644 index 0000000..e3fa0ce --- /dev/null +++ b/consul/consul-config.json @@ -0,0 +1,12 @@ +{ + "server": true, + "bind_addr": "127.0.0.1", + "bootstrap_expect": 1, + "data_dir": "./consul-data", + "ui_config": { + "enabled": true + }, + "ports": { + "http": 8500 + } +} \ No newline at end of file diff --git a/consul/consul-config.json.template b/consul/consul-config.json.template new file mode 100644 index 0000000..706d196 --- /dev/null +++ b/consul/consul-config.json.template @@ -0,0 +1,12 @@ +{ + "server": true, + "bind_addr": "$CONSUL_BIND_ADDR", + "bootstrap_expect": 1, + "data_dir": "$CONSUL_DATA_DIR", + "ui_config": { + "enabled": true + }, + "ports": { + "http": $CONSUL_HTTP_PORT + } +} \ No newline at end of file diff --git a/consul/fluentci.toml b/consul/fluentci.toml new file mode 100644 index 0000000..d41f7e6 --- /dev/null +++ b/consul/fluentci.toml @@ -0,0 +1,11 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Consul Plugin for FluentCI" +keywords = [ + "consul", +] +license = "MIT" +name = "redis" +version = "0.1.0" diff --git a/consul/src/helpers.rs b/consul/src/helpers.rs new file mode 100644 index 0000000..9785610 --- /dev/null +++ b/consul/src/helpers.rs @@ -0,0 +1,61 @@ +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 consul_config = dag().get_env("CONSUL_CONFIG")?; + let consul_port = dag().get_env("CONSUL_HTTP_PORT")?; + let consul_data_dir = dag().get_env("CONSUL_DATA_DIR")?; + let consul_bind_addr = dag().get_env("CONSUL_BIND_ADDR")?; + + if consul_port.is_empty() { + dag().set_envs(vec![("CONSUL_HTTP_PORT".into(), "8500".into())])?; + } + + if consul_data_dir.is_empty() { + dag().set_envs(vec![("CONSUL_DATA_DIR".into(), "../consul-data".into())])?; + } + + if consul_bind_addr.is_empty() { + dag().set_envs(vec![("CONSUL_BIND_ADDR".into(), "127.0.0.1".into())])?; + } + + if consul_config.is_empty() { + dag().set_envs(vec![( + "CONSUL_CONFIG".into(), + "../consul-config.json".into(), + )])?; + } + + let stdout = dag() + .flox()? + .with_workdir(".fluentci")? + .with_exec(vec![ + "flox", "install", "consul", "overmind", "tmux", "wget" + ])? + .with_exec(vec!["[ -f consul-config.json.template ] || wget https://raw.githubusercontent.com/fluentci-io/services/main/consul/consul-config.json.template"])? + .with_exec(vec!["[ -f $CONSUL_CONFIG ] || envsubst < consul-config.json.template > $CONSUL_CONFIG "])? + .with_exec(vec![ + "grep -q consul Procfile || echo -e 'consul: consul agent -config-file=$CONSUL_CONFIG \\n' >> Procfile", + ])? + .stdout()?; + + Ok(stdout) +} diff --git a/consul/src/lib.rs b/consul/src/lib.rs new file mode 100644 index 0000000..27b0390 --- /dev/null +++ b/consul/src/lib.rs @@ -0,0 +1,46 @@ +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("CONSUL_HTTP_PORT")?; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci")? + .with_exec(vec!["overmind", "--version"])? + .with_exec(vec!["consul", "version"])? + .with_exec(vec!["type", "overmind"])? + .with_exec(vec!["type", "consul"])? + .with_exec(vec![ + "overmind start -f Procfile --daemonize || overmind restart consul", + ])? + .wait_on(port.parse()?, None)? + .with_exec(vec!["overmind", "status"])? + .with_exec(vec!["consul", "kv", "put", "redis/config/minconns", "1"])? + .with_exec(vec!["consul", "kv", "get", "redis/config/minconns"])? + .stdout()?; + Ok(stdout) +} + +#[plugin_fn] +pub fn stop(args: String) -> FnResult { + helpers::setup()?; + + let args = if args.is_empty() { + "consul".to_string() + } else { + args + }; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci")? + .with_exec(vec!["overmind", "stop", &args])? + .stdout()?; + Ok(stdout) +} diff --git a/consul/target b/consul/target new file mode 120000 index 0000000..78bc337 --- /dev/null +++ b/consul/target @@ -0,0 +1 @@ +../target \ No newline at end of file