main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "context"
  4. "os"
  5. "os/signal"
  6. "syscall"
  7. "quickip/internal/config"
  8. "quickip/internal/deviceinfo"
  9. "quickip/internal/discovery"
  10. "quickip/internal/httpserver"
  11. "quickip/internal/logger"
  12. applyexecsvc "quickip/internal/network/applyexec"
  13. configreadersvc "quickip/internal/network/configreader"
  14. interfacesvc "quickip/internal/network/interfaces"
  15. netplansvc "quickip/internal/network/netplan"
  16. validatorsvc "quickip/internal/network/validator"
  17. "quickip/internal/systemaction"
  18. "quickip/internal/tasks"
  19. )
  20. func main() {
  21. ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  22. defer stop()
  23. log := logger.New()
  24. cfg := config.Load(os.Args[1:])
  25. if os.Geteuid() != 0 {
  26. log.Warn("agent is not running as root; netplan write/apply and system actions will fail")
  27. }
  28. deviceSvc := deviceinfo.New(cfg)
  29. interfaceSvc := interfacesvc.New(cfg)
  30. configSvc := configreadersvc.New()
  31. validatorSvc := validatorsvc.New()
  32. netplanSvc := netplansvc.New()
  33. applySvc := applyexecsvc.New()
  34. taskSvc := tasks.New()
  35. systemSvc := systemaction.New()
  36. if err := interfaceSvc.EnsureMaintenanceAddress(); err != nil {
  37. log.Error("failed to ensure maintenance address", "error", err.Error())
  38. return
  39. }
  40. httpSrv := httpserver.New(cfg, log, deviceSvc, interfaceSvc, configSvc, validatorSvc, netplanSvc, applySvc, taskSvc, systemSvc)
  41. udpSrv := discovery.New(cfg, log, deviceSvc)
  42. errCh := make(chan error, 2)
  43. go func() { errCh <- httpSrv.Run(ctx) }()
  44. go func() { errCh <- udpSrv.Run(ctx) }()
  45. select {
  46. case <-ctx.Done():
  47. log.Info("agent shutting down")
  48. case err := <-errCh:
  49. if err != nil {
  50. log.Error("agent stopped with error", "error", err.Error())
  51. }
  52. }
  53. }