Skip to content

Commit

Permalink
feat: add envconsul service
Browse files Browse the repository at this point in the history
feat: add envconsul service

feat: add envconsul service
  • Loading branch information
tsirysndr committed Jul 16, 2024
1 parent cef06de commit 2e53134
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 5 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/envconsul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: ci
on:
push:
branches:
- main
paths:
- envconsul/**
- .github/workflows/envconsul.yml
jobs:
envconsul-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: envconsul
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Start envconsul
run: |
fluentci run target/wasm32-unknown-unknown/release/envconsul.wasm start
fluentci run target/wasm32-unknown-unknown/release/envconsul.wasm stop
working-directory: envconsul
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NIX_CONFIG: extra-access-tokens = github.com=${{ secrets.GH_ACCESS_TOKEN }}
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ default-members = [
"dragonflydb",
"dynamodb-local",
"elasticmq",
"envconsul",
"etcd",
"httpbin",
"mariadb",
Expand Down Expand Up @@ -34,6 +35,7 @@ members = [
"dragonflydb",
"dynamodb-local",
"elasticmq",
"envconsul",
"etcd",
"httpbin",
"mariadb",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fluentci run --wasm postgres start
| [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 |
| [envconsul](./envconsul) | A tool that manages environment variables using HashiCorp Consul |
| [etcd](./etcd) | A distributed key-value store |
| [httpbin](./httpbin) | HTTP Request & Response Service |
| [mariadb](./mariadb) | The open source relational database |
Expand Down
2 changes: 1 addition & 1 deletion consul/fluentci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ keywords = [
"consul",
]
license = "MIT"
name = "redis"
name = "consul"
version = "0.1.0"
4 changes: 2 additions & 2 deletions consul/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ pub fn setup() -> Result<String, Error> {
.flox()?
.with_workdir(".fluentci")?
.with_exec(vec![
"flox", "install", "consul", "overmind", "tmux", "wget"
"flox", "install", "consul", "overmind", "tmux", "wget", "gettext"
])?
.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",
"grep -q consul: Procfile || echo -e 'consul: consul agent -config-file=$CONSUL_CONFIG \\n' >> Procfile",
])?
.stdout()?;

Expand Down
20 changes: 20 additions & 0 deletions envconsul/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
authors = [
"Tsiry Sandratraina <tsiry.sndr@fluentci.io>",
]
description = "Envconsul plugin for FluentCI"
edition = "2021"
license = "MIT"
name = "envconsul"
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"
19 changes: 19 additions & 0 deletions envconsul/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 Tsiry Sandratraina <tsiry.sndr@fluentci.io>

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.
54 changes: 54 additions & 0 deletions envconsul/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Envconsul Plugin

[![fluentci pipeline](https://shield.fluentci.io/x/envconsul)](https://pkg.fluentci.io/envconsul)
[![ci](https://github.com/fluentci-io/services/actions/workflows/envconsul.yml/badge.svg)](https://github.com/fluentci-io/services/actions/workflows/envconsul.yml)

Envconsul service plugin for FluentCI.

## 🚀 Usage

Add the following command to your CI configuration file:

```bash
fluentci run --wasm envconsul start
```

## Functions

| Name | Description |
| ------ | ---------------------------------------------- |
| start | Start envconsul |
| stop | Stop envconsul |

## 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/envconsul@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: envconsul
args: |
start
```
12 changes: 12 additions & 0 deletions envconsul/fluentci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
authors = [
"Tsiry Sandratraina <tsiry.sndr@fluentci.io>",
]
description = "Envconsul Plugin for FluentCI"
keywords = [
"envconsul",
"consul",
]
license = "MIT"
name = "envconsul"
version = "0.1.0"
55 changes: 55 additions & 0 deletions envconsul/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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<String, Error> {
setup_flox()?;
dag().call(
"https://pkg.fluentci.io/consul@v0.1.0?wasm=1",
"start",
vec![],
)?;

dag()
.pipeline("setup")?
.with_exec(vec!["mkdir", "-p", ".fluentci"])?
.stdout()?;

let prefix = dag().get_env("ENVCONSUL_PREFIX")?;
let app = dag().get_env("ENVCONSUL_APP")?;

if prefix.is_empty() {
dag().set_envs(vec![("ENVCONSUL_PREFIX".into(), "my-app".into())])?;
}

if app.is_empty() {
dag().set_envs(vec![("ENVCONSUL_APP".into(), "env".into())])?;
}

let stdout = dag()
.flox()?
.with_workdir(".fluentci")?
.with_exec(vec![
"flox", "install", "envconsul", "overmind", "tmux"
])?
.with_exec(vec!["consul", "kv", "put", "my-app/address", "1.2.3.4"])?
.with_exec(vec!["consul", "kv", "put", "my-app/port", "80"])?
.with_exec(vec!["consul", "kv", "put", "my-app/max_conns", "5"])?
.with_exec(vec![
"grep -q envconsul: Procfile || echo -e 'envconsul: envconsul -prefix $ENVCONSUL_PREFIX $ENVCONSUL_APP > envconsul.log \\n' >> Procfile",
])?
.stdout()?;

Ok(stdout)
}
43 changes: 43 additions & 0 deletions envconsul/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use extism_pdk::*;
use fluentci_pdk::dag;

pub mod helpers;

#[plugin_fn]
pub fn start(_args: String) -> FnResult<String> {
helpers::setup()?;

let stdout = dag()
.flox()?
.with_workdir(".fluentci")?
.with_exec(vec!["overmind", "--version"])?
.with_exec(vec!["envconsul", "-version"])?
.with_exec(vec!["type", "overmind"])?
.with_exec(vec!["type", "envconsul"])?
.with_exec(vec![
"overmind start -f Procfile --daemonize || overmind restart envconsul",
])?
.with_exec(vec!["overmind", "status"])?
.with_exec(vec!["sleep", "2"])?
.with_exec(vec!["cat", "envconsul.log"])?
.stdout()?;
Ok(stdout)
}

#[plugin_fn]
pub fn stop(args: String) -> FnResult<String> {
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)
}
1 change: 1 addition & 0 deletions envconsul/target
2 changes: 1 addition & 1 deletion nginx/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn setup() -> Result<String, Error> {
let stdout = dag()
.flox()?
.with_workdir(".fluentci")?
.with_exec(vec!["flox", "install", "nginx", "overmind", "tmux", "wget", "curl"])?
.with_exec(vec!["flox", "install", "nginx", "overmind", "tmux", "wget", "curl", "gettext"])?
.with_exec(vec!["[ -f ../nginx.template ] || wget https://raw.githubusercontent.com/fluentci-io/services/main/nginx/nginx.template -O ../nginx.template"])?
.with_exec(vec!["[ -f fastcgi.conf ] || wget https://raw.githubusercontent.com/fluentci-io/services/main/nginx/fastcgi.conf"])?
.with_exec(vec!["[ -f ../index.html ] || wget https://raw.githubusercontent.com/fluentci-io/services/main/nginx/web/index.html -O ../index.html"])?
Expand Down
3 changes: 2 additions & 1 deletion vault/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ pub fn setup() -> Result<String, Error> {
.with_exec(vec![
"type jq > /dev/null 2>/dev/null || pkgx install jq",
])?
.with_exec(vec!["type envsubst > /dev/null 2>/dev/null || pkgx install gnu.org/gettext"])?
.with_exec(vec![
"[ -d ../data ] || mkdir -p ../data",
])?
.with_exec(vec!["[ -f config.hcl.template ] || pkgx wget https://raw.githubusercontent.com/fluentci-io/services/main/vault/config.hcl.template"])?
.with_exec(vec!["[ -f ../config.hcl ] || envsubst < config.hcl.template > ../config.hcl "])?
.with_exec(vec![
"grep -q vault Procfile || echo -e 'vault: vault server -config=../config.hcl \\n' >> Procfile",
"grep -q vault: Procfile || echo -e 'vault: vault server -config=../config.hcl \\n' >> Procfile",
])?
.stdout()?;

Expand Down

0 comments on commit 2e53134

Please sign in to comment.