-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
421 lines (359 loc) · 9.88 KB
/
main.go
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package main
import (
"context"
"embed"
"encoding/json"
"fmt"
"html/template"
"io"
"io/fs"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/rs/zerolog"
"github.com/tr4cks/power/modules"
"github.com/tr4cks/power/modules/ilo"
"github.com/tr4cks/power/modules/wakeonlan"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func init() {
rootCmd.PersistentFlags().StringVar(&configFilePath, "config", path.Join("/etc", fmt.Sprintf("%s.d", appName), "config.yaml"), "YAML configuration file")
rootCmd.PersistentFlags().StringVarP(&moduleName, "module", "m", "", "module for switching the server on or off")
rootCmd.MarkPersistentFlagRequired("module")
}
const appName = "power"
var (
configFilePath string
moduleName string
rootCmd = &cobra.Command{
Use: appName,
Short: "All-in-one tool for remote server power control",
Version: "1.4.0",
Args: cobra.NoArgs,
Run: run,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}
)
type Config struct {
Username string `validate:"required"`
Password string `validate:"required"`
Module map[string]interface{}
Discord *DiscordBotConfig
}
func parseYAMLFile(filePath string) (*Config, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
defer file.Close()
config := Config{}
decoder := yaml.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return nil, fmt.Errorf("error decoding YAML file %q: %w", filePath, err)
}
return &config, nil
}
func parseConfigFile(filePath string) *Config {
config, err := parseYAMLFile(filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse YAML file %q: %s\n", filePath, err)
os.Exit(1)
}
validate := validator.New()
err = validate.Struct(config)
if err != nil {
fmt.Fprintf(os.Stderr, "Error during configuration validation: %s\n", err)
os.Exit(1)
}
return config
}
func createModule(config *Config, moduleName string) modules.Module {
internalModules := map[string]modules.Module{
"ilo": ilo.New(),
"wol": wakeonlan.New(),
}
module, ok := internalModules[moduleName]
if !ok {
moduleNames := make([]string, 0, len(internalModules))
for moduleName := range internalModules {
moduleNames = append(moduleNames, moduleName)
}
fmt.Fprintf(os.Stderr, "Can't find the %q module among the internal modules (available modules: %s)\n", moduleName, strings.Join(moduleNames, ", "))
os.Exit(1)
}
err := module.Init(config.Module)
if err != nil {
fmt.Fprintf(os.Stderr, "Error during module initialization: %s\n", err)
os.Exit(1)
}
return module
}
func run(cmd *cobra.Command, args []string) {
// Logging
configureLoggers()
config := parseConfigFile(configFilePath)
module := createModule(config, moduleName)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
srv := runHttpServer(config, module)
if config.Discord != nil {
discordBot, err := NewDiscordBot(config.Discord, module)
if err != nil {
mainLogger.Fatal().Err(err).Msg("Unable to create discord bot")
}
err = discordBot.Start()
if err != nil {
mainLogger.Fatal().Err(err).Msg("Unable to start discord bot")
}
defer discordBot.Stop()
}
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
mainLogger.Info().Msg("Shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
mainLogger.Fatal().Err(err).Msg("Server forced to shutdown")
}
mainLogger.Info().Msg("Server exiting")
}
//go:embed index.html
var templateFS embed.FS
//go:embed static
var staticFS embed.FS
// loggers
var (
ginLogger zerolog.Logger
mainLogger zerolog.Logger
)
func resolveAddress() string {
port := os.Getenv("PORT")
if port != "" {
mainLogger.
Debug().
Msg(fmt.Sprintf("Environment variable PORT=\"%s\"", port))
return ":" + port
}
mainLogger.
Debug().
Msg("Environment variable PORT is undefined. Using port :8080 by default")
return ":8080"
}
func configureLoggers() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
var outputWriter io.Writer = os.Stderr
if gin.Mode() != "release" {
outputWriter = zerolog.ConsoleWriter{Out: os.Stderr}
}
logger := zerolog.New(outputWriter).With().Timestamp().Logger()
ginLogger = logger.With().Str("scope", "gin").Logger()
mainLogger = logger.With().Str("scope", "main").Logger()
}
func loggerWithZerolog(logger *zerolog.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
c.Next()
latency := time.Since(start)
if raw != "" {
path = path + "?" + raw
}
errorMessage := c.Errors.ByType(gin.ErrorTypePrivate).String()
event := logger.Info()
if errorMessage != "" {
event = logger.Error()
}
event.
Str("client_ip", c.ClientIP()).
Str("method", c.Request.Method).
Str("path", path).
Int("status", c.Writer.Status()).
Int("size", c.Writer.Size()).
Dur("latency", latency).
Msg(errorMessage)
}
}
func runHttpServer(config *Config, module modules.Module) *http.Server {
// Configure Gin
router := gin.New()
router.Use(loggerWithZerolog(&ginLogger))
router.Use(gin.Recovery())
router.SetTrustedProxies(nil)
html := template.Must(template.ParseFS(templateFS, "index.html"))
router.SetHTMLTemplate(html)
// Serve static folder
staticSubtreeFS, err := fs.Sub(staticFS, "static")
if err != nil {
mainLogger.Fatal().Err(err)
}
router.StaticFS("/static", http.FS(staticSubtreeFS))
withServerState := router.Group("/", ServerStateMiddleware(module, &mainLogger))
{
// GET index.html
withServerState.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"power": c.GetBool("power"),
"led": c.GetBool("led"),
})
})
// POST index.html
withServerState.POST("/",
ConditionalMiddleware(func(c *gin.Context) bool { return c.GetBool("power") },
gin.BasicAuth(gin.Accounts{config.Username: config.Password})),
func(c *gin.Context) {
if c.GetBool("power") {
err := module.PowerOff()
if err != nil {
mainLogger.Error().Err(err).Msg("Server shutdown error")
c.HTML(http.StatusOK, "index.html", gin.H{
"power": c.GetBool("power"),
"led": c.GetBool("led"),
"error": true,
})
return
}
} else {
err := module.PowerOn()
if err != nil {
mainLogger.Error().Err(err).Msg("Server power-up error")
c.HTML(http.StatusOK, "index.html", gin.H{
"power": c.GetBool("power"),
"led": c.GetBool("led"),
"error": true,
})
return
}
}
c.Redirect(http.StatusFound, "/")
})
}
api := router.Group("/api")
{
api.POST("/up", func(c *gin.Context) {
err := module.PowerOn()
if err != nil {
mainLogger.Error().Err(err).Msg("Server power-up error")
c.JSON(http.StatusInternalServerError, gin.H{
"status": "ko",
"error": "a problem occurred during server startup",
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
})
api.POST("/down", gin.BasicAuth(gin.Accounts{config.Username: config.Password}), func(c *gin.Context) {
err := module.PowerOff()
if err != nil {
mainLogger.Error().Err(err).Msg("Server shutdown error")
c.JSON(http.StatusInternalServerError, gin.H{
"status": "ko",
"error": "a problem occurred during server shutdown",
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
})
api.GET("/state", ServerStateMiddleware(module, &mainLogger), func(c *gin.Context) {
c.JSON(200, gin.H{
"power": c.GetBool("power"),
"led": c.GetBool("led"),
})
})
}
srv := &http.Server{
Addr: resolveAddress(),
Handler: router,
}
go func() {
err := srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
mainLogger.Fatal().Err(err).Msg("An error occurred while starting the server")
}
}()
return srv
}
func init() {
rootCmd.AddCommand(upCmd, downCmd, stateCmd)
}
var (
upCmd = &cobra.Command{
Use: "up",
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
config := parseConfigFile(configFilePath)
module := createModule(config, moduleName)
err := module.PowerOn()
if err != nil {
fmt.Fprintf(os.Stderr, "Server power-up error: %s\n", err)
os.Exit(1)
}
},
}
downCmd = &cobra.Command{
Use: "down",
Short: "Turn off the server",
Run: func(cmd *cobra.Command, args []string) {
config := parseConfigFile(configFilePath)
module := createModule(config, moduleName)
err := module.PowerOff()
if err != nil {
fmt.Fprintf(os.Stderr, "Server shutdown error: %s\n", err)
os.Exit(1)
}
},
}
stateCmd = &cobra.Command{
Use: "state",
Short: "Fetch the server state",
Run: func(cmd *cobra.Command, args []string) {
config := parseConfigFile(configFilePath)
module := createModule(config, moduleName)
powerState, ledState := module.State()
if powerState.Err != nil {
fmt.Fprintf(os.Stderr, "Failed to retrieve POWER state: %s\n", powerState.Err)
}
if ledState.Err != nil {
fmt.Fprintf(os.Stderr, "Failed to retrieve LED state: %s\n", ledState.Err)
}
state := struct {
Power bool `json:"power"`
Led bool `json:"led"`
}{
Power: powerState.Value,
Led: ledState.Value,
}
jsonString, err := json.Marshal(state)
if err != nil {
fmt.Println("Error during JSON conversion:", err)
os.Exit(1)
}
fmt.Println(string(jsonString))
},
}
)
func main() {
err := rootCmd.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}