tasks.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package self_check
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/helper"
  4. "github.com/0xJacky/Nginx-UI/internal/translation"
  5. "github.com/elliotchance/orderedmap/v3"
  6. "github.com/uozi-tech/cosy"
  7. "github.com/0xJacky/Nginx-UI/internal/nginx"
  8. )
  9. type Task struct {
  10. Key string
  11. Name *translation.Container
  12. Description *translation.Container
  13. CheckFunc func() error
  14. FixFunc func() error
  15. }
  16. type ReportStatus string
  17. const (
  18. ReportStatusSuccess ReportStatus = "success"
  19. ReportStatusWarning ReportStatus = "warning"
  20. ReportStatusError ReportStatus = "error"
  21. )
  22. type Report struct {
  23. Key string `json:"key"`
  24. Name *translation.Container `json:"name"`
  25. Description *translation.Container `json:"description,omitempty"`
  26. Fixable bool `json:"fixable"`
  27. Err *cosy.Error `json:"err,omitempty"`
  28. Status ReportStatus `json:"status"`
  29. }
  30. type Reports []*Report
  31. var selfCheckTasks = []*Task{
  32. {
  33. Key: "Directory-Sites",
  34. Name: translation.C("Sites directory exists"),
  35. Description: translation.C("Check if the " +
  36. "sites-available and sites-enabled directories are " +
  37. "under the nginx configuration directory"),
  38. CheckFunc: CheckSitesDirectory,
  39. FixFunc: FixSitesDirectory,
  40. },
  41. {
  42. Key: "NginxConf-Sites-Enabled",
  43. Name: translation.C("Nginx.conf includes sites-enabled directory"),
  44. Description: translation.C("Check if the nginx.conf includes the " +
  45. "sites-enabled directory"),
  46. CheckFunc: CheckNginxConfIncludeSites,
  47. FixFunc: FixNginxConfIncludeSites,
  48. },
  49. {
  50. Key: "NginxConf-ConfD",
  51. Name: translation.C("Nginx.conf includes conf.d directory"),
  52. Description: translation.C("Check if the nginx.conf includes the " +
  53. "conf.d directory"),
  54. CheckFunc: CheckNginxConfIncludeConfD,
  55. FixFunc: FixNginxConfIncludeConfD,
  56. },
  57. {
  58. Key: "NginxConf-Directory",
  59. Name: translation.C("Nginx configuration directory exists"),
  60. Description: translation.C("Check if the nginx configuration directory exists"),
  61. CheckFunc: CheckConfigDir,
  62. },
  63. {
  64. Key: "NginxConf-Entry-File",
  65. Name: translation.C("Nginx configuration entry file exists"),
  66. Description: translation.C("Check if the nginx configuration entry file exists"),
  67. CheckFunc: CheckConfigEntryFile,
  68. },
  69. {
  70. Key: "NginxPID-Path",
  71. Name: translation.C("Nginx PID path exists"),
  72. Description: translation.C("Check if the nginx PID path exists"),
  73. CheckFunc: CheckPIDPath,
  74. },
  75. {
  76. Key: "NginxAccessLog-Path",
  77. Name: translation.C("Nginx access log path exists"),
  78. Description: translation.C("Check if the nginx access log path exists"),
  79. CheckFunc: CheckAccessLogPath,
  80. },
  81. {
  82. Key: "NginxErrorLog-Path",
  83. Name: translation.C("Nginx error log path exists"),
  84. Description: translation.C("Check if the nginx error log path exists"),
  85. CheckFunc: CheckErrorLogPath,
  86. },
  87. }
  88. var selfCheckTaskMap = orderedmap.NewOrderedMap[string, *Task]()
  89. func init() {
  90. if nginx.IsModuleLoaded(nginx.ModuleStream) {
  91. selfCheckTasks = append(selfCheckTasks, &Task{
  92. Key: "Directory-Streams",
  93. Name: translation.C("Streams directory exists"),
  94. Description: translation.C("Check if the " +
  95. "streams-available and streams-enabled directories are " +
  96. "under the nginx configuration directory"),
  97. CheckFunc: CheckStreamDirectory,
  98. FixFunc: FixStreamDirectory,
  99. }, &Task{
  100. Key: "NginxConf-Streams-Enabled",
  101. Name: translation.C("Nginx.conf includes streams-enabled directory"),
  102. Description: translation.C("Check if the nginx.conf includes the " +
  103. "streams-enabled directory"),
  104. CheckFunc: CheckNginxConfIncludeStreams,
  105. FixFunc: FixNginxConfIncludeStreams,
  106. })
  107. }
  108. if helper.InNginxUIOfficialDocker() {
  109. selfCheckTasks = append(selfCheckTasks, &Task{
  110. Name: translation.C("Docker socket exists"),
  111. Description: translation.C("Check if /var/run/docker.sock exists. If you are using Nginx UI Official " +
  112. "Docker Image, please make sure the docker socket is mounted like this: `-" +
  113. "v /var/run/docker.sock:/var/run/docker.sock`."),
  114. CheckFunc: CheckDockerSocket,
  115. })
  116. }
  117. for _, task := range selfCheckTasks {
  118. selfCheckTaskMap.Set(task.Key, task)
  119. }
  120. }