Compare commits

...

6 Commits

Author SHA1 Message Date
Paul Payne
7a4b283ebe Refactor node discovery command to accept subnet input and improve help documentation 2025-11-04 17:16:36 +00:00
Paul Payne
3f546053f7 Allow cancelling of node discovery. 2025-11-04 16:45:39 +00:00
Paul Payne
393306de12 Instance-namespace utility endpoints. 2025-10-14 21:04:40 +00:00
Paul Payne
8d19fbd549 Fix dashboard token command. 2025-10-14 18:55:43 +00:00
Paul Payne
0d5b7b6939 Fix wild dashboard token. 2025-10-14 07:13:24 +00:00
Paul Payne
41c69a636b Enables --fetch flag in service deploy command. 2025-10-14 05:27:21 +00:00
6 changed files with 565 additions and 47 deletions

View File

@@ -2,3 +2,52 @@
- Go 1.21+
- GNU Make (for build automation)
## Patterns
### Instance-scoped Commands
CLI commands that operate on a specific Wild Cloud instance should follow this pattern:
```go
// In cmd/utility.go
var dashboardTokenCmd = &cobra.Command{
Use: "token",
Short: "Get dashboard token",
RunE: func(cmd *cobra.Command, args []string) error {
// 1. Get instance from CLI context
instanceName, err := getInstanceName()
if err != nil {
return err
}
// 2. Call instance-scoped API endpoint with instance in URL
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/utilities/dashboard/token", instanceName))
if err != nil {
return err
}
// 3. Process response
data := resp.GetMap("data")
if data == nil {
return fmt.Errorf("no data in response")
}
token, ok := data["token"].(string)
if !ok {
return fmt.Errorf("no token in response")
}
// 4. Display result
fmt.Println(token)
return nil
},
}
```
#### Key Principles
1. **Get instance from context**: Use `getInstanceName()` to get the current instance from CLI context
2. **Instance in URL path**: Include the instance name in the API endpoint URL path
3. **Stateless API calls**: Don't rely on server-side session state - pass instance explicitly
4. **Handle errors gracefully**: Return clear error messages if instance is not set or API call fails

View File

@@ -1,7 +1,11 @@
package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/spf13/cobra"
@@ -14,56 +18,120 @@ var nodeCmd = &cobra.Command{
}
var nodeDiscoverCmd = &cobra.Command{
Use: "discover <ip>...",
Use: "discover [subnet]",
Short: "Discover nodes on network",
Long: "Discover nodes on the network by scanning the provided IP addresses or ranges",
Args: cobra.MinimumNArgs(1),
Long: `Discover nodes on the network by scanning a subnet or auto-detecting local networks.
If a subnet is provided (e.g., 192.168.1.0/24), only that subnet will be scanned.
If no subnet is provided, all local networks will be automatically detected and scanned.
Examples:
wild node discover # Auto-detect local networks
wild node discover 192.168.1.0/24 # Scan specific subnet`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inst, err := getInstanceName()
if err != nil {
return err
}
fmt.Printf("Starting discovery for %d IP(s)...\n", len(args))
_, err = apiClient.Post(fmt.Sprintf("/api/v1/instances/%s/nodes/discover", inst), map[string]interface{}{
"ip_list": args,
})
// Check if --cancel flag is set
shouldCancel, _ := cmd.Flags().GetBool("cancel")
if shouldCancel {
// Cancel any running discovery first
_, err := apiClient.Post(fmt.Sprintf("/api/v1/instances/%s/discovery/cancel", inst), nil)
if err != nil {
// Ignore error if no discovery is running
fmt.Println("No active discovery to cancel, starting new discovery...")
} else {
fmt.Println("Cancelled previous discovery, starting new discovery...")
}
}
// Build request body
body := map[string]interface{}{}
if len(args) > 0 {
body["subnet"] = args[0]
fmt.Printf("Starting discovery for subnet %s...\n", args[0])
} else {
fmt.Println("Starting discovery (auto-detecting local networks)...")
}
_, err = apiClient.Post(fmt.Sprintf("/api/v1/instances/%s/nodes/discover", inst), body)
if err != nil {
return err
}
// Poll for completion
fmt.Println("Scanning nodes...")
for {
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/discovery", inst))
if err != nil {
return err
}
// Set up signal handling for Ctrl-C
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
active, _ := resp.Data["active"].(bool)
if !active {
// Discovery complete
nodesFound := resp.GetArray("nodes_found")
if len(nodesFound) == 0 {
fmt.Println("\nNo Talos nodes found")
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
// Handle signals in a goroutine
go func() {
<-sigChan
fmt.Println("\n\nCancelling discovery...")
cancel()
// Call cancel API
_, err := apiClient.Post(fmt.Sprintf("/api/v1/instances/%s/discovery/cancel", inst), nil)
if err != nil {
fmt.Printf("Failed to cancel discovery: %v\n", err)
} else {
fmt.Println("Discovery cancelled successfully")
}
os.Exit(0)
}()
// Poll for completion
fmt.Println("Scanning nodes... (Press Ctrl-C to cancel)")
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/discovery", inst))
if err != nil {
return err
}
active, _ := resp.Data["active"].(bool)
if !active {
// Discovery complete
nodesFound := resp.GetArray("nodes_found")
if len(nodesFound) == 0 {
fmt.Println("\nNo Talos nodes found")
return nil
}
fmt.Printf("\nFound %d node(s) in maintenance mode:\n\n", len(nodesFound))
fmt.Printf("%-15s %-15s %-15s\n", "IP", "VERSION", "HOSTNAME")
fmt.Println("-----------------------------------------------------")
for _, node := range nodesFound {
if m, ok := node.(map[string]interface{}); ok {
version := m["version"]
if version == nil {
version = ""
}
hostname := m["hostname"]
if hostname == nil {
hostname = ""
}
fmt.Printf("%-15s %-15s %-15s\n",
m["ip"], version, hostname)
}
}
return nil
}
fmt.Printf("\nFound %d node(s):\n\n", len(nodesFound))
fmt.Printf("%-15s %-12s %-10s\n", "IP", "INTERFACE", "VERSION")
fmt.Println("-----------------------------------------------")
for _, node := range nodesFound {
if m, ok := node.(map[string]interface{}); ok {
fmt.Printf("%-15s %-12s %-10s\n",
m["ip"], m["interface"], m["version"])
}
}
return nil
// Still running, show progress
fmt.Print(".")
}
// Still running, wait a bit
fmt.Print(".")
time.Sleep(500 * time.Millisecond)
}
},
}
@@ -431,6 +499,31 @@ You can use it manually to update templates.`,
},
}
var nodeCancelDiscoveryCmd = &cobra.Command{
Use: "cancel-discovery",
Short: "Cancel active node discovery",
Long: `Cancel an active node discovery operation.
Use this if discovery gets stuck or you want to stop a running scan.
Example:
wild node cancel-discovery`,
RunE: func(cmd *cobra.Command, args []string) error {
inst, err := getInstanceName()
if err != nil {
return err
}
_, err = apiClient.Post(fmt.Sprintf("/api/v1/instances/%s/discovery/cancel", inst), nil)
if err != nil {
return err
}
fmt.Println("Discovery cancelled successfully")
return nil
},
}
var nodeDeleteCmd = &cobra.Command{
Use: "delete <hostname>",
Short: "Delete a node",
@@ -453,6 +546,7 @@ var nodeDeleteCmd = &cobra.Command{
func init() {
nodeCmd.AddCommand(nodeDiscoverCmd)
nodeCmd.AddCommand(nodeCancelDiscoveryCmd)
nodeCmd.AddCommand(nodeDetectCmd)
nodeCmd.AddCommand(nodeListCmd)
nodeCmd.AddCommand(nodeShowCmd)
@@ -462,6 +556,9 @@ func init() {
nodeCmd.AddCommand(nodeFetchTemplatesCmd)
nodeCmd.AddCommand(nodeDeleteCmd)
// Add flags to node discover command
nodeDiscoverCmd.Flags().Bool("cancel", false, "Cancel any running discovery before starting")
// Add flags to node add command
nodeAddCmd.Flags().String("target-ip", "", "Target IP address for production")
nodeAddCmd.Flags().String("current-ip", "", "Current IP address (for maintenance mode)")

View File

@@ -26,7 +26,7 @@ var operationGetCmd = &cobra.Command{
return err
}
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/operations/%s?instance=%s", args[0], inst))
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/operations/%s", inst, args[0]))
if err != nil {
return err
}
@@ -43,7 +43,12 @@ var operationListCmd = &cobra.Command{
Use: "list",
Short: "List operations",
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := apiClient.Get("/api/v1/operations")
inst, err := getInstanceName()
if err != nil {
return err
}
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/operations", inst))
if err != nil {
return err
}
@@ -90,7 +95,7 @@ func streamOperationOutput(opID string) error {
}
// Connect to SSE stream
url := fmt.Sprintf("%s/api/v1/operations/%s/stream?instance=%s", baseURL, opID, inst)
url := fmt.Sprintf("%s/api/v1/instances/%s/operations/%s/stream", baseURL, inst, opID)
client := sse.NewClient(url)
events := make(chan *sse.Event)
@@ -105,7 +110,7 @@ func streamOperationOutput(opID string) error {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/operations/%s?instance=%s", opID, inst))
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/operations/%s", inst, opID))
if err == nil {
status := resp.GetString("status")
if status == "completed" || status == "failed" {

View File

@@ -1,7 +1,14 @@
package cmd
import (
"bufio"
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/spf13/cobra"
@@ -76,6 +83,15 @@ type ConfigUpdate struct {
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{
@@ -315,10 +331,337 @@ Examples:
},
}
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/services/%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]interface{}); 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"); configMap != nil && 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/services/%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"].([]interface{}); 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/services/%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 strings.HasPrefix(line, "data: ") {
fmt.Println(strings.TrimPrefix(line, "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]interface{}{
"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/services/%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)
}

View File

@@ -38,12 +38,28 @@ var dashboardTokenCmd = &cobra.Command{
Use: "token",
Short: "Get dashboard token",
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := apiClient.Get("/api/v1/utilities/dashboard/token")
instanceName, err := getInstanceName()
if err != nil {
return err
}
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/utilities/dashboard/token", instanceName))
if err != nil {
return err
}
fmt.Println(resp.GetString("token"))
// API returns: {"success": true, "data": {"token": "..."}}
// Client parses this into Data map, so we need to get "data" then "token"
data := resp.GetMap("data")
if data == nil {
return fmt.Errorf("no data in response")
}
token, ok := data["token"].(string)
if !ok {
return fmt.Errorf("no token in response")
}
fmt.Println(token)
return nil
},
}
@@ -56,7 +72,12 @@ var nodeIPCmd = &cobra.Command{
Use: "node-ip",
Short: "Get control plane IP",
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := apiClient.Get("/api/v1/utilities/controlplane/ip")
inst, err := getInstanceName()
if err != nil {
return err
}
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/utilities/controlplane/ip", inst))
if err != nil {
return err
}

View File

@@ -24,13 +24,16 @@ var versionCmd = &cobra.Command{
// If connected to daemon, show cluster versions
if apiClient != nil {
resp, err := apiClient.Get("/api/v1/utilities/version")
inst, err := getInstanceName()
if err == nil {
if k8s, ok := resp.Data["kubernetes"].(string); ok {
fmt.Printf("Kubernetes: %s\n", k8s)
}
if talos, ok := resp.Data["talos"].(string); ok && talos != "" {
fmt.Printf("Talos: %s\n", talos)
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/utilities/version", inst))
if err == nil {
if k8s, ok := resp.Data["kubernetes"].(string); ok {
fmt.Printf("Kubernetes: %s\n", k8s)
}
if talos, ok := resp.Data["talos"].(string); ok && talos != "" {
fmt.Printf("Talos: %s\n", talos)
}
}
}
}