159 lines
4.3 KiB
Go
159 lines
4.3 KiB
Go
package services
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const maxJeannieResponseBytes = 2 << 20
|
|
|
|
type StatusRow struct {
|
|
Status string `json:"status"`
|
|
Area string `json:"area"`
|
|
Check string `json:"check"`
|
|
Summary string `json:"summary"`
|
|
}
|
|
|
|
type ClusterIdentity struct {
|
|
Profile string `json:"profile"`
|
|
Runtime string `json:"runtime"`
|
|
}
|
|
|
|
type StatusSnapshot struct {
|
|
Title string `json:"title"`
|
|
Mode string `json:"mode"`
|
|
Rows []StatusRow `json:"rows"`
|
|
Cluster ClusterIdentity `json:"cluster"`
|
|
CollectedAt string `json:"collectedAt"`
|
|
}
|
|
|
|
type JeannieClient struct {
|
|
BaseURL string
|
|
Token string
|
|
HTTPClient *http.Client
|
|
PollInterval time.Duration
|
|
}
|
|
|
|
func NewJeannieClient(baseURL string, token string) *JeannieClient {
|
|
return &JeannieClient{
|
|
BaseURL: strings.TrimRight(baseURL, "/"),
|
|
Token: token,
|
|
HTTPClient: &http.Client{Timeout: 10 * time.Second},
|
|
PollInterval: 250 * time.Millisecond,
|
|
}
|
|
}
|
|
|
|
func NewJeannieClientFromEnv() *JeannieClient {
|
|
return NewJeannieClient(
|
|
os.Getenv("OPSPROOF_JEANNIE_URL"),
|
|
os.Getenv("OPSPROOF_JEANNIE_TOKEN"),
|
|
)
|
|
}
|
|
|
|
func (client *JeannieClient) Status(ctx context.Context) (StatusSnapshot, error) {
|
|
if client.BaseURL == "" || client.Token == "" {
|
|
return StatusSnapshot{}, errors.New("Jeannie API connection is not configured")
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
|
|
payload, err := json.Marshal(map[string]any{
|
|
"command": "status",
|
|
"args": []string{"all", "--json"},
|
|
})
|
|
if err != nil {
|
|
return StatusSnapshot{}, fmt.Errorf("encode status request: %w", err)
|
|
}
|
|
|
|
var submitted execution
|
|
if err := client.request(ctx, http.MethodPost, "/v1/execute", bytes.NewReader(payload), &submitted); err != nil {
|
|
return StatusSnapshot{}, fmt.Errorf("submit status request: %w", err)
|
|
}
|
|
if submitted.ID == "" {
|
|
return StatusSnapshot{}, errors.New("Jeannie API returned an empty execution ID")
|
|
}
|
|
|
|
for {
|
|
var current execution
|
|
if err := client.request(ctx, http.MethodGet, "/v1/executions/"+submitted.ID, nil, ¤t); err != nil {
|
|
return StatusSnapshot{}, fmt.Errorf("read status execution: %w", err)
|
|
}
|
|
|
|
switch current.Status {
|
|
case "queued", "running":
|
|
timer := time.NewTimer(client.PollInterval)
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return StatusSnapshot{}, fmt.Errorf("wait for status execution: %w", ctx.Err())
|
|
case <-timer.C:
|
|
}
|
|
case "succeeded", "failed":
|
|
snapshot, err := decodeStatusOutput(current.Output)
|
|
if err != nil {
|
|
return StatusSnapshot{}, err
|
|
}
|
|
snapshot.CollectedAt = time.Now().UTC().Format(time.RFC3339)
|
|
return snapshot, nil
|
|
default:
|
|
return StatusSnapshot{}, fmt.Errorf("Jeannie API returned unknown execution state %q", current.Status)
|
|
}
|
|
}
|
|
}
|
|
|
|
type execution struct {
|
|
ID string `json:"id"`
|
|
Status string `json:"status"`
|
|
Output string `json:"output"`
|
|
}
|
|
|
|
func (client *JeannieClient) request(ctx context.Context, method string, path string, body io.Reader, target any) error {
|
|
request, err := http.NewRequestWithContext(ctx, method, client.BaseURL+path, body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
request.Header.Set("Authorization", "Bearer "+client.Token)
|
|
if body != nil {
|
|
request.Header.Set("Content-Type", "application/json")
|
|
}
|
|
|
|
response, err := client.HTTPClient.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
|
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 4096))
|
|
return fmt.Errorf("Jeannie API returned HTTP %d", response.StatusCode)
|
|
}
|
|
if err := json.NewDecoder(io.LimitReader(response.Body, maxJeannieResponseBytes)).Decode(target); err != nil {
|
|
return fmt.Errorf("decode Jeannie API response: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func decodeStatusOutput(output string) (StatusSnapshot, error) {
|
|
start := strings.Index(output, "{")
|
|
if start < 0 {
|
|
return StatusSnapshot{}, errors.New("Jeannie status output did not contain JSON")
|
|
}
|
|
|
|
var snapshot StatusSnapshot
|
|
if err := json.Unmarshal([]byte(output[start:]), &snapshot); err != nil {
|
|
return StatusSnapshot{}, fmt.Errorf("decode Jeannie status output: %w", err)
|
|
}
|
|
if snapshot.Rows == nil {
|
|
return StatusSnapshot{}, errors.New("Jeannie status output did not contain rows")
|
|
}
|
|
return snapshot, nil
|
|
}
|