134
examples/example_usage.go
Archivo normal
134
examples/example_usage.go
Archivo normal
@@ -0,0 +1,134 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/yourusername/buque/internal/config"
|
||||
"github.com/yourusername/buque/internal/docker"
|
||||
"github.com/yourusername/buque/internal/models"
|
||||
"github.com/yourusername/buque/internal/stats"
|
||||
)
|
||||
|
||||
// This is an example of using Buque as a library in your own Go code
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
// Initialize configuration manager
|
||||
configMgr := config.NewManager("")
|
||||
cfg, err := configMgr.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
// Create Docker Compose manager
|
||||
compose, err := docker.NewComposeManager()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create compose manager: %v", err)
|
||||
}
|
||||
|
||||
// Create stats collector
|
||||
collector, err := stats.NewCollector()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create stats collector: %v", err)
|
||||
}
|
||||
defer collector.Close()
|
||||
|
||||
// Example 1: Start all enabled environments
|
||||
fmt.Println("Starting all enabled environments...")
|
||||
for _, env := range cfg.Environments {
|
||||
if !env.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("Starting %s...\n", env.Name)
|
||||
if err := compose.Up(ctx, env, true); err != nil {
|
||||
log.Printf("Failed to start %s: %v", env.Name, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("✓ %s started\n", env.Name)
|
||||
}
|
||||
|
||||
// Wait a bit for containers to start
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
// Example 2: Collect and display statistics
|
||||
fmt.Println("\nCollecting container statistics...")
|
||||
containerStats, err := collector.CollectAll(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to collect stats: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("\nRunning containers: %d\n", len(containerStats))
|
||||
for _, stat := range containerStats {
|
||||
fmt.Printf(" %s: CPU=%.2f%% Memory=%s\n",
|
||||
stat.Name,
|
||||
stat.CPUPercentage,
|
||||
stats.FormatBytes(stat.MemoryUsage))
|
||||
}
|
||||
|
||||
// Example 3: Get aggregated statistics
|
||||
aggStats, err := collector.GetAggregatedStats(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get aggregated stats: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("\nAggregated Statistics:\n")
|
||||
fmt.Printf(" Total Containers: %d\n", aggStats.TotalContainers)
|
||||
fmt.Printf(" Total CPU: %.2f%%\n", aggStats.TotalCPUPercent)
|
||||
fmt.Printf(" Total Memory: %s\n", stats.FormatBytes(aggStats.TotalMemoryUsage))
|
||||
|
||||
// Example 4: Add a new environment programmatically
|
||||
newEnv := models.Environment{
|
||||
Name: "test-app",
|
||||
Path: "/path/to/test-app",
|
||||
ComposeFile: "docker-compose.yml",
|
||||
Enabled: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := configMgr.AddEnvironment(newEnv); err != nil {
|
||||
log.Printf("Failed to add environment: %v", err)
|
||||
} else {
|
||||
fmt.Printf("\n✓ Added new environment: %s\n", newEnv.Name)
|
||||
}
|
||||
|
||||
// Example 5: Pull images for an environment
|
||||
if len(cfg.Environments) > 0 {
|
||||
env := cfg.Environments[0]
|
||||
fmt.Printf("\nPulling images for %s...\n", env.Name)
|
||||
if err := compose.Pull(ctx, env); err != nil {
|
||||
log.Printf("Failed to pull images: %v", err)
|
||||
} else {
|
||||
fmt.Printf("✓ Images pulled successfully\n")
|
||||
}
|
||||
}
|
||||
|
||||
// Example 6: Continuous monitoring (runs for 30 seconds)
|
||||
fmt.Println("\nStarting continuous monitoring for 30 seconds...")
|
||||
monitorCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err = collector.MonitorContinuously(monitorCtx, 5*time.Second, func(stats []models.ContainerStats) {
|
||||
fmt.Printf("\n[%s] Active containers: %d\n",
|
||||
time.Now().Format("15:04:05"),
|
||||
len(stats))
|
||||
|
||||
for _, stat := range stats {
|
||||
fmt.Printf(" %s: CPU=%.1f%% Mem=%.1f%%\n",
|
||||
stat.Name,
|
||||
stat.CPUPercentage,
|
||||
stat.MemoryPercent)
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil && err != context.DeadlineExceeded {
|
||||
log.Printf("Monitoring error: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("\nExample completed!")
|
||||
}
|
||||
Referencia en una nueva incidencia
Block a user