1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"github.com/bdw/deploy/internal/ssh"
"github.com/bdw/deploy/internal/state"
)
func runVPS(args []string) {
fs := flag.NewFlagSet("vps", flag.ExitOnError)
host := fs.String("host", "", "VPS host (SSH config alias or user@host)")
fs.Parse(args)
// Load state
st, err := state.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading state: %v\n", err)
os.Exit(1)
}
// Get host from flag or state default
if *host == "" {
*host = st.GetDefaultHost()
}
if *host == "" {
fmt.Fprintf(os.Stderr, "Error: --host is required (no default host set)\n")
fs.Usage()
os.Exit(1)
}
fmt.Printf("Connecting to %s...\n\n", *host)
// Connect to VPS
client, err := ssh.Connect(*host)
if err != nil {
fmt.Fprintf(os.Stderr, "Error connecting to VPS: %v\n", err)
os.Exit(1)
}
defer client.Close()
// Uptime
fmt.Println("UPTIME")
if output, err := client.Run("uptime -p"); err == nil {
fmt.Printf(" %s", output)
}
if output, err := client.Run("uptime -s"); err == nil {
fmt.Printf(" Since: %s", output)
}
fmt.Println()
// Load average
fmt.Println("LOAD")
if output, err := client.Run("cat /proc/loadavg | awk '{print $1, $2, $3}'"); err == nil {
fmt.Printf(" 1m, 5m, 15m: %s", output)
}
fmt.Println()
// Memory
fmt.Println("MEMORY")
if output, err := client.Run("free -h | awk 'NR==2 {print \" Used: \" $3 \" / \" $2}'"); err == nil {
fmt.Print(output)
}
if output, err := client.Run("free -h | awk 'NR==2 {printf \" Available: %s\\n\", $7}'"); err == nil {
fmt.Print(output)
}
fmt.Println()
// Disk
fmt.Println("DISK")
if output, err := client.Run("df -h / | awk 'NR==2 {print \" Used: \" $3 \" / \" $2 \" (\" $5 \")\"}'"); err == nil {
fmt.Print(output)
}
if output, err := client.Run("df -h / | awk 'NR==2 {print \" Available: \" $4}'"); err == nil {
fmt.Print(output)
}
fmt.Println()
// Updates available
fmt.Println("UPDATES")
if output, err := client.Run("[ -f /var/lib/update-notifier/updates-available ] && cat /var/lib/update-notifier/updates-available | head -2 || echo ' (update info not available)'"); err == nil {
fmt.Print(output)
}
fmt.Println()
// Services
fmt.Println("SERVICES")
if output, err := client.Run("systemctl is-active caddy 2>/dev/null && echo ' Caddy: active' || echo ' Caddy: inactive'"); err == nil {
// Parse the output
if output == "active\n" {
fmt.Println(" Caddy: active")
} else {
fmt.Println(" Caddy: inactive")
}
}
// Count deployed apps that are running
hostState := st.GetHost(*host)
if hostState != nil && len(hostState.Apps) > 0 {
activeCount := 0
for name, app := range hostState.Apps {
if app.Type == "app" {
if output, err := client.Run(fmt.Sprintf("systemctl is-active %s 2>/dev/null", name)); err == nil && output == "active\n" {
activeCount++
}
}
}
appCount := 0
for _, app := range hostState.Apps {
if app.Type == "app" {
appCount++
}
}
fmt.Printf(" Deployed apps: %d/%d active\n", activeCount, appCount)
}
}
func runUpdate(args []string) {
fs := flag.NewFlagSet("vps-update", flag.ExitOnError)
host := fs.String("host", "", "VPS host (SSH config alias or user@host)")
yes := fs.Bool("y", false, "Skip confirmation prompt")
fs.Parse(args)
// Load state
st, err := state.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading state: %v\n", err)
os.Exit(1)
}
// Get host from flag or state default
if *host == "" {
*host = st.GetDefaultHost()
}
if *host == "" {
fmt.Fprintf(os.Stderr, "Error: --host is required (no default host set)\n")
fs.Usage()
os.Exit(1)
}
// Confirm unless -y flag is set
if !*yes {
fmt.Printf("This will run apt update && apt upgrade on %s\n", *host)
fmt.Print("Continue? [y/N]: ")
var response string
fmt.Scanln(&response)
if response != "y" && response != "Y" {
fmt.Println("Aborted.")
return
}
}
fmt.Printf("Connecting to %s...\n", *host)
// Connect to VPS
client, err := ssh.Connect(*host)
if err != nil {
fmt.Fprintf(os.Stderr, "Error connecting to VPS: %v\n", err)
os.Exit(1)
}
defer client.Close()
// Run apt update
fmt.Println("\n→ Running apt update...")
if err := client.RunSudoStream("apt update"); err != nil {
fmt.Fprintf(os.Stderr, "Error running apt update: %v\n", err)
os.Exit(1)
}
// Run apt upgrade
fmt.Println("\n→ Running apt upgrade...")
if err := client.RunSudoStream("DEBIAN_FRONTEND=noninteractive apt upgrade -y"); err != nil {
fmt.Fprintf(os.Stderr, "Error running apt upgrade: %v\n", err)
os.Exit(1)
}
// Check if reboot is required
fmt.Println()
if output, err := client.Run("[ -f /var/run/reboot-required ] && echo 'yes' || echo 'no'"); err == nil {
if output == "yes\n" {
fmt.Println("Note: A reboot is required to complete the update.")
}
}
fmt.Println("✓ Update complete")
}
func runSSH(args []string) {
fs := flag.NewFlagSet("vps-ssh", flag.ExitOnError)
host := fs.String("host", "", "VPS host (SSH config alias or user@host)")
fs.Parse(args)
// Load state
st, err := state.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading state: %v\n", err)
os.Exit(1)
}
// Get host from flag or state default
if *host == "" {
*host = st.GetDefaultHost()
}
if *host == "" {
fmt.Fprintf(os.Stderr, "Error: --host is required (no default host set)\n")
fs.Usage()
os.Exit(1)
}
// Launch interactive SSH session
cmd := exec.Command("ssh", *host)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
os.Exit(exitErr.ExitCode())
}
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
|