516 lines
13 KiB
Go
516 lines
13 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/wild-cloud/wild-central/wild/internal/config"
|
|
)
|
|
|
|
// Service commands
|
|
var serviceCmd = &cobra.Command{
|
|
Use: "service",
|
|
Short: "Manage services",
|
|
}
|
|
|
|
var serviceListCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List services",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
inst, err := getInstanceName()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/apps?category=infrastructure", inst))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if outputFormat == "json" {
|
|
return printJSON(resp.Data)
|
|
}
|
|
|
|
services := resp.GetArray("apps")
|
|
if len(services) == 0 {
|
|
fmt.Println("No services found")
|
|
return nil
|
|
}
|
|
|
|
fmt.Printf("%-20s %-12s\n", "NAME", "STATUS")
|
|
fmt.Println("----------------------------------")
|
|
for _, svc := range services {
|
|
if m, ok := svc.(map[string]any); ok {
|
|
fmt.Printf("%-20s %-12s\n", m["name"], m["status"])
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var (
|
|
fetchFlag bool
|
|
noDeployFlag bool
|
|
// Service logs flags
|
|
tailLines int
|
|
followLogs bool
|
|
containerName string
|
|
previousLogs bool
|
|
sinceDuration string
|
|
// Service update flags
|
|
setFlags []string
|
|
noRedeployFlag bool
|
|
)
|
|
|
|
var serviceInstallCmd = &cobra.Command{
|
|
Use: "install <service>",
|
|
Short: "Install a service",
|
|
Long: `Install and configure a cluster service.
|
|
|
|
This command adds a service to the instance and optionally deploys it:
|
|
1. Add service to instance (fetches from Wild Directory, compiles templates)
|
|
2. Deploy service to cluster (unless --no-deploy)
|
|
|
|
Examples:
|
|
# Add and deploy (most common)
|
|
wild service install metallb
|
|
|
|
# Add only, skip deployment
|
|
wild service install metallb --no-deploy
|
|
|
|
# Force re-fetch from Wild Directory
|
|
wild service install metallb --fetch
|
|
`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
serviceName := args[0]
|
|
|
|
inst, err := getInstanceName()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("Installing service: %s\n", serviceName)
|
|
|
|
// Fetch manifest for display
|
|
manifestResp, err := apiClient.Get(fmt.Sprintf("/api/v1/apps/%s/manifest", serviceName))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to fetch manifest: %w", err)
|
|
}
|
|
name := manifestResp.GetString("name")
|
|
desc := manifestResp.GetString("description")
|
|
if name != "" {
|
|
fmt.Printf("Service: %s - %s\n", name, desc)
|
|
}
|
|
|
|
// Check if config overrides needed
|
|
if defaultConfig, ok := manifestResp.Data["defaultConfig"].(map[string]any); ok && len(defaultConfig) > 0 {
|
|
// Check existing config for this service
|
|
configResp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/config", inst))
|
|
if err == nil {
|
|
existingConfig := config.GetValue(configResp.Data, fmt.Sprintf("apps.%s", serviceName))
|
|
if existingConfig == nil || existingConfig == "null" {
|
|
fmt.Println("\nDefault configuration will be applied:")
|
|
for key, val := range defaultConfig {
|
|
fmt.Printf(" %s: %v\n", key, val)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add service to instance
|
|
fmt.Println("\nAdding service to instance...")
|
|
_, err = apiClient.Post(
|
|
fmt.Sprintf("/api/v1/instances/%s/apps", inst),
|
|
map[string]any{
|
|
"name": serviceName,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to add service: %w", err)
|
|
}
|
|
|
|
// Re-fetch if requested
|
|
if fetchFlag {
|
|
fmt.Println("Fetching fresh templates...")
|
|
_, err = apiClient.Post(fmt.Sprintf("/api/v1/instances/%s/apps/%s/fetch", inst, serviceName), nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to fetch service: %w", err)
|
|
}
|
|
}
|
|
|
|
if noDeployFlag {
|
|
fmt.Printf("\nService added: %s\n", serviceName)
|
|
fmt.Printf(" Templates compiled and ready to deploy\n")
|
|
fmt.Printf(" To deploy: wild service install %s\n", serviceName)
|
|
return nil
|
|
}
|
|
|
|
// Deploy
|
|
fmt.Println("\nDeploying service...")
|
|
deployResp, err := apiClient.Post(
|
|
fmt.Sprintf("/api/v1/instances/%s/apps/%s/deploy", inst, serviceName),
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to deploy service: %w", err)
|
|
}
|
|
|
|
opID := deployResp.GetString("operation_id")
|
|
if opID != "" {
|
|
if err := streamOperationOutput(opID); err != nil {
|
|
fmt.Printf("\nCouldn't stream output: %v\n", err)
|
|
fmt.Printf("Operation ID: %s\n", opID)
|
|
fmt.Printf("Monitor with: wild operation get %s\n", opID)
|
|
} else {
|
|
fmt.Printf("\nService installed successfully: %s\n", serviceName)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var serviceStatusCmd = &cobra.Command{
|
|
Use: "status <service>",
|
|
Short: "Show detailed status of a service",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: runServiceStatus,
|
|
}
|
|
|
|
var serviceLogsCmd = &cobra.Command{
|
|
Use: "logs <service>",
|
|
Short: "View service logs",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: runServiceLogs,
|
|
}
|
|
|
|
var serviceUpdateCmd = &cobra.Command{
|
|
Use: "update <service>",
|
|
Short: "Update service configuration",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: runServiceUpdate,
|
|
}
|
|
|
|
func runServiceStatus(cmd *cobra.Command, args []string) error {
|
|
serviceName := args[0]
|
|
|
|
inst, err := getInstanceName()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/apps/%s/status", inst, serviceName))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if outputFormat == "json" {
|
|
return printJSON(resp.Data)
|
|
}
|
|
|
|
if outputFormat == "yaml" {
|
|
return printYAML(resp.Data)
|
|
}
|
|
|
|
// Pretty print status
|
|
fmt.Printf("Service: %s\n", resp.GetString("name"))
|
|
if namespace := resp.GetString("namespace"); namespace != "" {
|
|
fmt.Printf("Namespace: %s\n", namespace)
|
|
}
|
|
if status := resp.GetString("status"); status != "" {
|
|
fmt.Printf("Status: %s\n", status)
|
|
}
|
|
|
|
// Show replica information
|
|
if replicas := resp.GetMap("replicas"); replicas != nil {
|
|
fmt.Println("\nReplicas:")
|
|
if desired, ok := replicas["desired"].(float64); ok {
|
|
fmt.Printf(" Desired: %.0f\n", desired)
|
|
}
|
|
if current, ok := replicas["current"].(float64); ok {
|
|
fmt.Printf(" Current: %.0f\n", current)
|
|
}
|
|
if ready, ok := replicas["ready"].(float64); ok {
|
|
fmt.Printf(" Ready: %.0f\n", ready)
|
|
}
|
|
if available, ok := replicas["available"].(float64); ok {
|
|
fmt.Printf(" Available: %.0f\n", available)
|
|
}
|
|
}
|
|
|
|
// Show pod information
|
|
if pods := resp.GetArray("pods"); len(pods) > 0 {
|
|
fmt.Println("\nPods:")
|
|
fmt.Printf(" %-40s %-12s %-8s %-10s %-10s\n", "NAME", "STATUS", "READY", "RESTARTS", "AGE")
|
|
fmt.Println(" " + strings.Repeat("-", 90))
|
|
for _, pod := range pods {
|
|
if p, ok := pod.(map[string]any); ok {
|
|
name := p["name"]
|
|
status := p["status"]
|
|
ready := p["ready"]
|
|
restarts := p["restarts"]
|
|
age := p["age"]
|
|
fmt.Printf(" %-40s %-12s %-8v %-10v %-10s\n", name, status, ready, restarts, age)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Show current configuration
|
|
if configMap := resp.GetMap("config"); len(configMap) > 0 {
|
|
fmt.Println("\nConfiguration:")
|
|
for key, value := range configMap {
|
|
fmt.Printf(" %s: %v\n", key, value)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func runServiceLogs(cmd *cobra.Command, args []string) error {
|
|
serviceName := args[0]
|
|
|
|
inst, err := getInstanceName()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Build query parameters
|
|
params := []string{}
|
|
if tailLines > 0 {
|
|
params = append(params, fmt.Sprintf("tail=%d", tailLines))
|
|
}
|
|
if containerName != "" {
|
|
params = append(params, fmt.Sprintf("container=%s", containerName))
|
|
}
|
|
if previousLogs {
|
|
params = append(params, "previous=true")
|
|
}
|
|
if sinceDuration != "" {
|
|
params = append(params, fmt.Sprintf("since=%s", sinceDuration))
|
|
}
|
|
|
|
queryString := ""
|
|
if len(params) > 0 {
|
|
queryString = "?" + strings.Join(params, "&")
|
|
}
|
|
|
|
if followLogs {
|
|
// Streaming mode
|
|
return streamServiceLogs(inst, serviceName, queryString)
|
|
}
|
|
|
|
// Buffered mode
|
|
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/apps/%s/logs%s", inst, serviceName, queryString))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Print logs - API returns logs as an array of lines
|
|
if lines, ok := resp.Data["lines"].([]any); ok {
|
|
for _, line := range lines {
|
|
if lineStr, ok := line.(string); ok {
|
|
fmt.Println(lineStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func streamServiceLogs(instance, serviceName, queryString string) error {
|
|
// Get base URL
|
|
baseURL := daemonURL
|
|
if baseURL == "" {
|
|
baseURL = config.GetDaemonURL()
|
|
}
|
|
|
|
// Build URL with follow=true parameter
|
|
url := fmt.Sprintf("%s/api/v1/instances/%s/apps/%s/logs%s", baseURL, instance, serviceName, queryString)
|
|
if strings.Contains(url, "?") {
|
|
url += "&follow=true"
|
|
} else {
|
|
url += "?follow=true"
|
|
}
|
|
|
|
// Create context that can be cancelled
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// Set up signal handling for graceful shutdown
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
|
go func() {
|
|
<-sigChan
|
|
cancel()
|
|
}()
|
|
|
|
// Create HTTP request
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
// Make request
|
|
client := &http.Client{Timeout: 0} // No timeout for streaming
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("request failed: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("request failed with status %d", resp.StatusCode)
|
|
}
|
|
|
|
// Stream response line by line
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
for scanner.Scan() {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
default:
|
|
line := scanner.Text()
|
|
// SSE events are prefixed with "data: "
|
|
if data, ok := strings.CutPrefix(line, "data: "); ok {
|
|
fmt.Println(data)
|
|
} else if line != "" {
|
|
fmt.Println(line)
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
if ctx.Err() == context.Canceled {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("error reading stream: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func runServiceUpdate(cmd *cobra.Command, args []string) error {
|
|
serviceName := args[0]
|
|
|
|
inst, err := getInstanceName()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
updates := make(map[string]string)
|
|
|
|
// Parse --set flags
|
|
for _, setFlag := range setFlags {
|
|
parts := strings.SplitN(setFlag, "=", 2)
|
|
if len(parts) != 2 {
|
|
return fmt.Errorf("invalid --set format: %s (expected key=value)", setFlag)
|
|
}
|
|
updates[parts[0]] = parts[1]
|
|
}
|
|
|
|
// Interactive mode if no --set flags provided
|
|
if len(updates) == 0 {
|
|
fmt.Printf("Updating service: %s\n", serviceName)
|
|
fmt.Println("Enter configuration values (key=value), empty line to finish:")
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
for {
|
|
fmt.Print("> ")
|
|
if !scanner.Scan() {
|
|
break
|
|
}
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line == "" {
|
|
break
|
|
}
|
|
|
|
parts := strings.SplitN(line, "=", 2)
|
|
if len(parts) != 2 {
|
|
fmt.Println("Invalid format, expected key=value")
|
|
continue
|
|
}
|
|
|
|
updates[parts[0]] = parts[1]
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return fmt.Errorf("error reading input: %w", err)
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
fmt.Println("No updates provided")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Build request body
|
|
requestBody := map[string]any{
|
|
"config": updates,
|
|
"redeploy": !noRedeployFlag,
|
|
}
|
|
|
|
// Make API call
|
|
fmt.Printf("Updating configuration for service: %s\n", serviceName)
|
|
resp, err := apiClient.Patch(
|
|
fmt.Sprintf("/api/v1/instances/%s/apps/%s/config", inst, serviceName),
|
|
requestBody,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Show result
|
|
fmt.Printf("Configuration updated (%d values)\n", len(updates))
|
|
for key, value := range updates {
|
|
fmt.Printf(" %s: %s\n", key, value)
|
|
}
|
|
|
|
// If redeployment triggered, show operation
|
|
if !noRedeployFlag {
|
|
if opID := resp.GetString("operation_id"); opID != "" {
|
|
fmt.Println("\nRedeploying service...")
|
|
if err := streamOperationOutput(opID); err != nil {
|
|
fmt.Printf("\nCouldn't stream output: %v\n", err)
|
|
fmt.Printf("Operation ID: %s\n", opID)
|
|
fmt.Printf("Monitor with: wild operation get %s\n", opID)
|
|
} else {
|
|
fmt.Printf("\n✓ Service redeployed successfully\n")
|
|
}
|
|
}
|
|
} else {
|
|
fmt.Println("\nConfiguration updated without redeployment")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
serviceInstallCmd.Flags().BoolVar(&fetchFlag, "fetch", false, "Fetch fresh templates from directory before installing")
|
|
serviceInstallCmd.Flags().BoolVar(&noDeployFlag, "no-deploy", false, "Configure and compile only, skip deployment")
|
|
|
|
serviceLogsCmd.Flags().IntVar(&tailLines, "tail", 100, "Number of lines to show")
|
|
serviceLogsCmd.Flags().BoolVarP(&followLogs, "follow", "f", false, "Stream logs in real-time")
|
|
serviceLogsCmd.Flags().StringVar(&containerName, "container", "", "Specific container (if service has multiple)")
|
|
serviceLogsCmd.Flags().BoolVar(&previousLogs, "previous", false, "Show logs from previous container instance")
|
|
serviceLogsCmd.Flags().StringVar(&sinceDuration, "since", "", "Show logs since duration (e.g., \"5m\", \"1h\")")
|
|
|
|
serviceUpdateCmd.Flags().StringArrayVar(&setFlags, "set", []string{}, "Set a configuration value (key=value), can be repeated")
|
|
serviceUpdateCmd.Flags().BoolVar(&noRedeployFlag, "no-redeploy", false, "Don't trigger redeployment after update")
|
|
|
|
serviceCmd.AddCommand(serviceListCmd)
|
|
serviceCmd.AddCommand(serviceInstallCmd)
|
|
serviceCmd.AddCommand(serviceStatusCmd)
|
|
serviceCmd.AddCommand(serviceLogsCmd)
|
|
serviceCmd.AddCommand(serviceUpdateCmd)
|
|
}
|