Compare commits
7 Commits
0307e4610e
...
77571e8062
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77571e8062 | ||
|
|
7a4b283ebe | ||
|
|
3f546053f7 | ||
|
|
393306de12 | ||
|
|
8d19fbd549 | ||
|
|
0d5b7b6939 | ||
|
|
41c69a636b |
@@ -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
|
||||
|
||||
127
cmd/iso.go
127
cmd/iso.go
@@ -26,9 +26,9 @@ var (
|
||||
var isoListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all downloaded ISO images",
|
||||
Long: `List all downloaded ISO images across all schematics with their versions and platforms.`,
|
||||
Long: `List all downloaded ISO images with their schematic IDs, versions, and platforms.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
resp, err := apiClient.Get("/api/v1/assets")
|
||||
resp, err := apiClient.Get("/api/v1/pxe/assets")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -37,9 +37,9 @@ var isoListCmd = &cobra.Command{
|
||||
return printJSON(resp.Data)
|
||||
}
|
||||
|
||||
schematics := resp.GetArray("schematics")
|
||||
if len(schematics) == 0 {
|
||||
fmt.Println("No ISOs found")
|
||||
assets := resp.GetArray("assets")
|
||||
if len(assets) == 0 {
|
||||
fmt.Println("No assets found")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -48,22 +48,21 @@ var isoListCmd = &cobra.Command{
|
||||
SchematicID string
|
||||
Version string
|
||||
Platform string
|
||||
Path string
|
||||
Size int64
|
||||
}
|
||||
var isos []isoInfo
|
||||
|
||||
for _, schematic := range schematics {
|
||||
if m, ok := schematic.(map[string]interface{}); ok {
|
||||
for _, asset := range assets {
|
||||
if m, ok := asset.(map[string]interface{}); ok {
|
||||
schematicID := fmt.Sprintf("%v", m["schematic_id"])
|
||||
assets := m["assets"]
|
||||
if assetsList, ok := assets.([]interface{}); ok {
|
||||
for _, asset := range assetsList {
|
||||
if assetMap, ok := asset.(map[string]interface{}); ok {
|
||||
version := fmt.Sprintf("%v", m["version"])
|
||||
assetsList := m["assets"]
|
||||
if assetsArray, ok := assetsList.([]interface{}); ok {
|
||||
for _, assetItem := range assetsArray {
|
||||
if assetMap, ok := assetItem.(map[string]interface{}); ok {
|
||||
if assetType, _ := assetMap["type"].(string); assetType == "iso" {
|
||||
if downloaded, _ := assetMap["downloaded"].(bool); downloaded {
|
||||
path := fmt.Sprintf("%v", assetMap["path"])
|
||||
version := extractVersion(path)
|
||||
platform := extractPlatform(path)
|
||||
size := int64(0)
|
||||
if s, ok := assetMap["size"].(float64); ok {
|
||||
@@ -74,7 +73,6 @@ var isoListCmd = &cobra.Command{
|
||||
SchematicID: schematicID,
|
||||
Version: version,
|
||||
Platform: platform,
|
||||
Path: path,
|
||||
Size: size,
|
||||
})
|
||||
}
|
||||
@@ -90,11 +88,11 @@ var isoListCmd = &cobra.Command{
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("%-10s %-10s %-66s %-10s\n", "VERSION", "PLATFORM", "SCHEMATIC ID", "SIZE")
|
||||
fmt.Printf("%-66s %-10s %-10s %-10s\n", "SCHEMATIC ID", "VERSION", "PLATFORM", "SIZE")
|
||||
fmt.Println("--------------------------------------------------------------------------------------------------------")
|
||||
for _, iso := range isos {
|
||||
sizeMB := float64(iso.Size) / 1024 / 1024
|
||||
fmt.Printf("%-10s %-10s %-66s %.2f MB\n", iso.Version, iso.Platform, iso.SchematicID, sizeMB)
|
||||
fmt.Printf("%-66s %-10s %-10s %.2f MB\n", iso.SchematicID, iso.Version, iso.Platform, sizeMB)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -112,7 +110,6 @@ The ISO can be used to boot bare metal machines.`,
|
||||
version := args[1]
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"version": version,
|
||||
"platform": isoPlatform,
|
||||
"asset_types": []string{"iso"},
|
||||
}
|
||||
@@ -121,14 +118,15 @@ The ISO can be used to boot bare metal machines.`,
|
||||
payload["force"] = true
|
||||
}
|
||||
|
||||
resp, err := apiClient.Post(fmt.Sprintf("/api/v1/assets/%s/download", schematicID), payload)
|
||||
resp, err := apiClient.Post(fmt.Sprintf("/api/v1/pxe/assets/%s/%s/download", schematicID, version), payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Downloading ISO for schematic: %s\n", schematicID)
|
||||
fmt.Printf(" Version: %s\n", version)
|
||||
fmt.Printf(" Platform: %s\n", isoPlatform)
|
||||
fmt.Printf("Downloading ISO:\n")
|
||||
fmt.Printf(" Schematic: %s\n", schematicID)
|
||||
fmt.Printf(" Version: %s\n", version)
|
||||
fmt.Printf(" Platform: %s\n", isoPlatform)
|
||||
|
||||
if msg := resp.GetString("message"); msg != "" {
|
||||
fmt.Printf("\nStatus: %s\n", msg)
|
||||
@@ -139,17 +137,18 @@ The ISO can be used to boot bare metal machines.`,
|
||||
}
|
||||
|
||||
var isoDeleteCmd = &cobra.Command{
|
||||
Use: "delete <schematic-id>",
|
||||
Short: "Delete a schematic and all its assets",
|
||||
Long: `Delete a schematic and all its downloaded assets including ISOs.
|
||||
Use: "delete <schematic-id> <version>",
|
||||
Short: "Delete an asset and all its files",
|
||||
Long: `Delete a specific schematic@version asset and all its downloaded files including ISOs.
|
||||
This operation cannot be undone.`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
schematicID := args[0]
|
||||
version := args[1]
|
||||
|
||||
// Prompt for confirmation unless --force is used
|
||||
if !isoForce {
|
||||
fmt.Printf("Are you sure you want to delete schematic %s and all its assets? (yes/no): ", schematicID)
|
||||
fmt.Printf("Are you sure you want to delete %s@%s and all its assets? (yes/no): ", schematicID, version)
|
||||
reader := bufio.NewReader(cmd.InOrStdin())
|
||||
response, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
@@ -162,12 +161,12 @@ This operation cannot be undone.`,
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := apiClient.Delete(fmt.Sprintf("/api/v1/assets/%s", schematicID))
|
||||
resp, err := apiClient.Delete(fmt.Sprintf("/api/v1/pxe/assets/%s/%s", schematicID, version))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Schematic deleted: %s\n", schematicID)
|
||||
fmt.Printf("Asset deleted: %s@%s\n", schematicID, version)
|
||||
if msg := resp.GetString("message"); msg != "" {
|
||||
fmt.Printf("Status: %s\n", msg)
|
||||
}
|
||||
@@ -177,13 +176,14 @@ This operation cannot be undone.`,
|
||||
}
|
||||
|
||||
var isoInfoCmd = &cobra.Command{
|
||||
Use: "info <schematic-id>",
|
||||
Short: "Show detailed information about ISOs for a schematic",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "info <schematic-id> <version>",
|
||||
Short: "Show detailed information about a specific asset",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
schematicID := args[0]
|
||||
version := args[1]
|
||||
|
||||
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/assets/%s", schematicID))
|
||||
resp, err := apiClient.Get(fmt.Sprintf("/api/v1/pxe/assets/%s/%s", schematicID, version))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -194,64 +194,45 @@ var isoInfoCmd = &cobra.Command{
|
||||
|
||||
fmt.Printf("Schematic ID: %s\n", resp.GetString("schematic_id"))
|
||||
fmt.Printf("Version: %s\n", resp.GetString("version"))
|
||||
|
||||
if instances := resp.GetArray("instances"); len(instances) > 0 {
|
||||
fmt.Printf("\nInstances using this schematic:\n")
|
||||
for _, inst := range instances {
|
||||
fmt.Printf(" - %s\n", inst)
|
||||
}
|
||||
}
|
||||
fmt.Printf("Path: %s\n", resp.GetString("path"))
|
||||
|
||||
if assets := resp.GetArray("assets"); len(assets) > 0 {
|
||||
fmt.Println("\nISO Images:")
|
||||
hasISO := false
|
||||
fmt.Println("\nAssets:")
|
||||
for _, asset := range assets {
|
||||
if a, ok := asset.(map[string]interface{}); ok {
|
||||
if assetType, _ := a["type"].(string); assetType == "iso" {
|
||||
hasISO = true
|
||||
downloaded, _ := a["downloaded"].(bool)
|
||||
path := fmt.Sprintf("%v", a["path"])
|
||||
version := extractVersion(path)
|
||||
platform := extractPlatform(path)
|
||||
assetType, _ := a["type"].(string)
|
||||
downloaded, _ := a["downloaded"].(bool)
|
||||
path := fmt.Sprintf("%v", a["path"])
|
||||
|
||||
if downloaded {
|
||||
size := int64(0)
|
||||
if s, ok := a["size"].(float64); ok {
|
||||
size = int64(s)
|
||||
}
|
||||
sizeMB := float64(size) / 1024 / 1024
|
||||
fmt.Printf(" ✓ %s / %s (%.2f MB)\n", version, platform, sizeMB)
|
||||
fmt.Printf(" Path: %s\n", path)
|
||||
} else {
|
||||
fmt.Printf(" ✗ Not downloaded\n")
|
||||
if downloaded {
|
||||
size := int64(0)
|
||||
if s, ok := a["size"].(float64); ok {
|
||||
size = int64(s)
|
||||
}
|
||||
sizeMB := float64(size) / 1024 / 1024
|
||||
|
||||
if assetType == "iso" {
|
||||
platform := extractPlatform(path)
|
||||
fmt.Printf(" ✓ ISO (%s): %.2f MB\n", platform, sizeMB)
|
||||
} else {
|
||||
fmt.Printf(" ✓ %s: %.2f MB\n", assetType, sizeMB)
|
||||
}
|
||||
fmt.Printf(" Path: %s\n", path)
|
||||
} else {
|
||||
fmt.Printf(" ✗ %s: Not downloaded\n", assetType)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !hasISO {
|
||||
fmt.Println(" No ISO images found")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// extractVersion extracts version from ISO filename (e.g., "talos-v1.11.2-metal-amd64.iso" -> "v1.11.2")
|
||||
func extractVersion(path string) string {
|
||||
filename := filepath.Base(path)
|
||||
re := regexp.MustCompile(`talos-(v\d+\.\d+\.\d+)-metal`)
|
||||
matches := re.FindStringSubmatch(filename)
|
||||
if len(matches) > 1 {
|
||||
return matches[1]
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// extractPlatform extracts platform from ISO filename (e.g., "talos-v1.11.2-metal-amd64.iso" -> "amd64")
|
||||
// extractPlatform extracts platform from filename (e.g., "metal-amd64.iso" -> "amd64")
|
||||
func extractPlatform(path string) string {
|
||||
filename := filepath.Base(path)
|
||||
re := regexp.MustCompile(`-(amd64|arm64)\.iso$`)
|
||||
re := regexp.MustCompile(`-(amd64|arm64)\.`)
|
||||
matches := re.FindStringSubmatch(filename)
|
||||
if len(matches) > 1 {
|
||||
return matches[1]
|
||||
|
||||
165
cmd/node.go
165
cmd/node.go
@@ -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)")
|
||||
|
||||
@@ -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" {
|
||||
|
||||
343
cmd/service.go
343
cmd/service.go
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user