reload.go RawHistoryFolder
1package cmd
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "syscall"
8
9 "github.com/spf13/cobra"
10)
11
12var reloadCmd = &cobra.Command{
13 Use: "reload",
14 Short: "Reload configuration and environment variables into running containers",
15 Long: `Reload the Gordon configuration and environment variables from .env files into running containers.
16This command reloads the config file and redeploys all containers with updated environment
17variables from their respective .env files. Containers are recreated to pick up the new configuration and environment variables.`,
18 Run: runReload,
19}
20
21func init() {
22 rootCmd.AddCommand(reloadCmd)
23}
24
25func runReload(cmd *cobra.Command, args []string) {
26 // Find the Gordon PID file to get the process ID
27 pidFile := findGordonPidFile()
28 if pidFile == "" {
29 fmt.Println("Error: Gordon PID file not found. Is Gordon running?")
30 fmt.Println("Please start Gordon with 'gordon start' first.")
31 return
32 }
33
34 // Read the PID from the file
35 pidBytes, err := os.ReadFile(pidFile)
36 if err != nil {
37 fmt.Printf("Error reading Gordon PID file: %v\n", err)
38 return
39 }
40
41 var pid int
42 if _, err := fmt.Sscanf(string(pidBytes), "%d", &pid); err != nil {
43 fmt.Printf("Error parsing Gordon PID: %v\n", err)
44 return
45 }
46
47 // Check if the process is still running
48 process, err := os.FindProcess(pid)
49 if err != nil {
50 fmt.Printf("Error finding Gordon process: %v\n", err)
51 return
52 }
53
54 // Send SIGUSR1 signal to trigger reload
55 if err := process.Signal(syscall.SIGUSR1); err != nil {
56 fmt.Printf("Error sending reload signal to Gordon: %v\n", err)
57 fmt.Println("Gordon may not be running or may not have permission to send signals.")
58 return
59 }
60
61 fmt.Println("Manual reload signal sent to Gordon successfully.")
62 fmt.Println("Check Gordon logs to see the reload progress.")
63}
64
65func findGordonPidFile() string {
66 // Look for PID file in common locations
67 locations := []string{
68 "/tmp/gordon.pid",
69 "/var/run/gordon.pid",
70 filepath.Join(os.TempDir(), "gordon.pid"),
71 }
72
73 // Also check in user's home directory
74 if homeDir, err := os.UserHomeDir(); err == nil {
75 locations = append(locations, filepath.Join(homeDir, ".gordon.pid"))
76 }
77
78 for _, location := range locations {
79 if _, err := os.Stat(location); err == nil {
80 return location
81 }
82 }
83
84 return ""
85}