-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
359 lines (301 loc) · 9.23 KB
/
main.go
File metadata and controls
359 lines (301 loc) · 9.23 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
type Config struct {
Port string
InactivityTimeout time.Duration
LibOpsKeepOnline string
LogLevel string
GoogleProjectID string
GCEZone string
GCEInstance string
}
type ActivityTracker struct {
mu sync.RWMutex
requestCount int64
lastPing time.Time
}
var (
config *Config
tracker *ActivityTracker
shutdownTimer *time.Timer
shutdownMutex sync.Mutex
serverShutdown = make(chan struct{})
// Dependency injection for testing - initialize later to avoid cycle
suspendFunc func() error
)
func init() {
config = loadConfig()
tracker = &ActivityTracker{
lastPing: time.Now(),
}
setupLogging()
// Initialize suspendFunc to avoid initialization cycle
suspendFunc = suspendInstance
}
func loadConfig() *Config {
return &Config{
Port: getEnv("PORT", "8808"),
InactivityTimeout: getDurationEnv("INACTIVITY_TIMEOUT", 90) * time.Second,
LogLevel: getEnv("LOG_LEVEL", "INFO"),
GoogleProjectID: getEnv("GCP_PROJECT", ""),
GCEZone: getEnv("GCP_ZONE", ""),
GCEInstance: getEnv("GCP_INSTANCE_NAME", ""),
LibOpsKeepOnline: getEnv("LIBOPS_KEEP_ONLINE", ""),
}
}
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func getDurationEnv(key string, defaultSeconds int) time.Duration {
if value := getEnv(key, ""); value != "" {
if seconds, err := strconv.Atoi(value); err == nil {
return time.Duration(seconds)
}
}
return time.Duration(defaultSeconds)
}
func setupLogging() {
var level slog.Level
switch strings.ToUpper(config.LogLevel) {
case "DEBUG":
level = slog.LevelDebug
case "WARN":
level = slog.LevelWarn
case "ERROR":
level = slog.LevelError
default:
level = slog.LevelInfo
}
opts := &slog.HandlerOptions{Level: level}
handler := slog.New(slog.NewTextHandler(os.Stdout, opts))
slog.SetDefault(handler)
}
func resetShutdownTimer() {
shutdownMutex.Lock()
defer shutdownMutex.Unlock()
if shutdownTimer != nil {
shutdownTimer.Stop()
}
shutdownTimer = time.AfterFunc(config.InactivityTimeout, func() {
slog.Info("Inactivity timeout reached, initiating shutdown",
"timeout_seconds", int(config.InactivityTimeout.Seconds()))
initiateShutdown()
})
slog.Debug("Shutdown timer reset", "timeout_seconds", int(config.InactivityTimeout.Seconds()))
}
func stopShutdownTimer() {
shutdownMutex.Lock()
defer shutdownMutex.Unlock()
if shutdownTimer != nil {
shutdownTimer.Stop()
shutdownTimer = nil
slog.Debug("Shutdown timer stopped")
}
}
func getLastGitHubActionsActivity() (time.Time, error) {
cmd := exec.Command("docker", "logs", "--tail", "1", "github-actions-runner")
output, err := cmd.Output()
if err != nil {
return time.Time{}, fmt.Errorf("no github-actions-runner logs: %v", err)
}
line := strings.TrimSpace(string(output))
if line == "" {
return time.Time{}, fmt.Errorf("empty github-actions-runner logs")
}
// Parse timestamp from the beginning of the log line
parts := strings.Split(line, ":")
if len(parts) >= 3 {
timeStr := parts[0] + ":" + parts[1] + ":" + parts[2]
if t, err := time.Parse("15:04:05", timeStr); err == nil {
// Add today's date
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.UTC), nil
}
}
return time.Time{}, fmt.Errorf("could not parse github-actions timestamp")
}
func createComputeService(ctx context.Context) (*compute.Service, error) {
// Use Application Default Credentials (ADC)
// This will automatically use:
// 1. GOOGLE_APPLICATION_CREDENTIALS environment variable
// 2. GCE metadata server (when running on GCE)
// 3. gcloud CLI credentials
creds, err := google.FindDefaultCredentials(ctx, compute.ComputeScope)
if err != nil {
return nil, fmt.Errorf("failed to find default credentials: %w", err)
}
service, err := compute.NewService(ctx, option.WithCredentials(creds))
if err != nil {
return nil, fmt.Errorf("failed to create compute service: %w", err)
}
return service, nil
}
func suspendMachine() (*compute.Instance, error) {
ctx := context.Background()
slog.Info("Checking if machine is suspended",
"project", config.GoogleProjectID,
"zone", config.GCEZone,
"instance", config.GCEInstance)
// Create compute service with default credentials
service, err := createComputeService(ctx)
if err != nil {
return nil, fmt.Errorf("createComputeService: %v", err)
}
// Get instance details
instance, err := service.Instances.Get(config.GoogleProjectID, config.GCEZone, config.GCEInstance).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed to get instance: %v", err)
}
// If the machine is running, suspend it
if instance.Status == "RUNNING" {
slog.Info("Instance is RUNNING, suspending instance")
_, err := service.Instances.Suspend(config.GoogleProjectID, config.GCEZone, config.GCEInstance).Context(ctx).Do()
if err != nil {
return instance, fmt.Errorf("failed to suspend instance: %v", err)
}
} else {
slog.Info("Instance is not RUNNING, skipping suspension", "status", instance.Status)
}
return instance, nil
}
func suspendInstance() error {
slog.Info("Attempting to suspend instance directly via GCP API")
// Reset the timer before suspension to prevent immediate shutdown after wake-up
resetShutdownTimer()
_, err := suspendMachine()
if err != nil {
return fmt.Errorf("failed to suspend machine: %v", err)
}
slog.Info("Suspend request completed successfully")
return nil
}
func initiateShutdown() {
tracker.mu.RLock()
lastPing := tracker.lastPing
tracker.mu.RUnlock()
now := time.Now()
duration := now.Sub(lastPing)
// Check GitHub Actions as fallback
if lastGHA, err := getLastGitHubActionsActivity(); err == nil {
ghaDuration := now.Sub(lastGHA)
if ghaDuration < config.InactivityTimeout {
slog.Info("Staying online for GitHub Actions",
"gha_duration_seconds", int(ghaDuration.Seconds()))
// Reset timer for another round
resetShutdownTimer()
return
}
}
slog.Info("Proceeding with shutdown",
"ping_duration_seconds", int(duration.Seconds()))
// Check if we have the required GCP configuration
if config.GoogleProjectID == "" || config.GCEZone == "" || config.GCEInstance == "" {
slog.Warn("Missing GCP configuration, cannot suspend",
"project", config.GoogleProjectID,
"zone", config.GCEZone,
"instance", config.GCEInstance)
} else {
if err := suspendFunc(); err != nil {
slog.Error("Failed to suspend instance", "error", err)
} else {
slog.Info("Suspend request sent successfully")
}
}
// Signal server shutdown (protected by mutex to prevent race condition)
shutdownMutex.Lock()
defer shutdownMutex.Unlock()
select {
case <-serverShutdown:
// Channel already closed, nothing to do
default:
close(serverShutdown)
}
}
func pingHandler(w http.ResponseWriter, r *http.Request) {
tracker.mu.Lock()
tracker.lastPing = time.Now()
tracker.requestCount++
tracker.mu.Unlock()
// Reset the shutdown timer
resetShutdownTimer()
slog.Info("Ping request received",
"remote_addr", r.RemoteAddr,
"user_agent", r.UserAgent(),
"timer_reset", true)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("pong")); err != nil {
slog.Error("Failed to write ping response", "error", err)
http.Error(w, "Failed to write response", http.StatusInternalServerError)
return
}
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
}
func main() {
slog.Info("Lightswitch starting",
"port", config.Port,
"inactivity_timeout", config.InactivityTimeout,
"keep_online", config.LibOpsKeepOnline == "yes")
// Check if this is a paid site that should stay online
if config.LibOpsKeepOnline != "yes" {
slog.Info("Starting inactivity timer", "timeout_seconds", int(config.InactivityTimeout.Seconds()))
resetShutdownTimer()
}
// Setup HTTP handlers
http.HandleFunc("/ping", pingHandler)
http.HandleFunc("/healthcheck", healthHandler)
// Setup HTTP server
server := &http.Server{
Addr: ":" + config.Port,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
// Start server in goroutine
go func() {
slog.Info("HTTP server starting", "port", config.Port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("HTTP server error", "error", err)
}
}()
// Wait for shutdown signal or internal shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
select {
case <-sigChan:
slog.Info("Shutdown signal received")
case <-serverShutdown:
slog.Info("Internal shutdown triggered")
}
slog.Info("Gracefully shutting down...")
// Stop the shutdown timer
stopShutdownTimer()
// Shutdown HTTP server
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
slog.Error("Server shutdown error", "error", err)
}
slog.Info("Lightswitch shutdown complete")
}