Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@samgaw
Copy link
Contributor

@samgaw samgaw commented Jun 7, 2025

This extends the exec action with support for embedding expressions in the config file while maintaining the current behaviour.

- type: exec
    exec:
      expr: |
        if [ "{{ field .cre "Severity" }}" = 0 ]; then
          echo "πŸ”₯ Critical incident: {{ field .cre "Id" }}"
          echo "{{ stripdash (field .cre "Description") }}"
          touch /tmp/alert-{{ field .cre "Id" }}

          {{ range .hits }}
            echo "{{ . }}" >> /tmp/alert-{{ field $.cre "Id" }}
          {{ end }}

          echo "Logged {{ len .hits }} to /tmp/alert-{{ field .cre "Id" }}"
        else
          echo "Low priority incident. Ignoring."
        fi

Expressions are passed via stdio to a runtime which defaults to sh -s if not defined. This means support for a lot of scripting environments & CLI tools out of the box for free.

Likewise with container environments like Docker, k8s etc. It's just passing the command in runtime.

  • Docker: runtime: docker run -i --rm alpine sh -s
  • Podman: runtime: podman exec -i my-container sh -s
  • k8s: runtime: kubectl exec -i my-pod -c my-container -- sh -s

Examples

SSH

runtime: ssh remotehost bash
expr: |
  if [ "{{ field .cre "Severity" }}" = 0 ]; then
    echo "πŸ”₯ Critical incident: {{ field .cre "Id" }}"
    echo "{{ stripdash (field .cre "Description") }}"
    touch /tmp/alert-{{ field .cre "Id" }}

    {{ range .hits }}
      echo "{{ . }}" >> /tmp/alert-{{ field $.cre "Id" }}
    {{ end }}

    echo "Logged {{ len .hits }} to /tmp/alert-{{ field .cre "Id" }}"
  else
    echo "Low priority incident. Ignoring."
  fi

Python

runtime: python3 -
expr: |
  import os

  severity = '{{ field .cre "Severity" }}'
  cre_id = '{{ field .cre "Id" }}'
  description = '''{{ stripdash (field .cre "Description") }}'''
  hit_count = '{{ len .hits }}'

  raw_hits = '''{{ .hits }}'''
  hits = raw_hits.strip().splitlines()

  if severity == "0":
      print(f"πŸ”₯ Critical incident: {cre_id}")
      print(description)

      path = f"/tmp/alert-{cre_id}"
      with open(path, "a") as f:
          for line in hits:
              f.write(line.strip() + "\n")

      print(f"Logged {hit_count} to {path}")
  else:
      print("Low priority incident. Ignoring.")

Ruby

runtime: ruby -
expr: |
	severity = '{{ field .cre "Severity" }}'
    cre_id = '{{ field .cre "Id" }}'
    description = <<~DESC
        {{ stripdash (field .cre "Description") }}
    DESC

    raw_hits = <<~HITS
        {{ .hits }}
    HITS

    hits = raw_hits.lines.map(&:chomp)

    if severity == "0"
        puts "πŸ”₯ Critical incident: #{cre_id}"
        puts description

        path = "/tmp/alert-#{cre_id}"
        File.open(path, "a") do |f|
    	    hits.each { |line| f.puts line }
        end

        puts "Logged {{ len .hits }} to #{path}"
    else
        puts "Low priority incident. Ignoring."
    end

Node

runtime: node -
expr: |
    const fs = require("fs");

    const severity = '{{ field .cre "Severity" }}';
    const creId = '{{ field .cre "Id" }}';
    const description = `{{ stripdash (field .cre "Description") }}`;
    const hits = {{ tojson .hits }};

    if (severity === "0") {
        console.log(`πŸ”₯ Critical incident: ${creId}`);
        console.log(description);

        const path = `/tmp/alert-${creId}.jsonl`;
        for (const hit of hits) {
            fs.appendFileSync(path, JSON.stringify(hit) + "\n");
        }

        console.log(`Logged ${hits.length} to ${path}`);
    } else {
        console.log("Low priority incident. Ignoring.");
    }

samgaw and others added 2 commits June 6, 2025 16:18
Expands existing behaviour by requiring either `path` or `expr` in `exec
block with optional `runtime` argument, passing the expression via
stdin. If `runtime` isn't present the default to `sh`.
```
- type: exec
  exec:
    expr: [...]
    runtime: [...]
```
@tonymeehan tonymeehan merged commit 5d5f9e5 into prequel-dev:main Jun 7, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants