1
0

tasks.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package self_check
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/helper"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/0xJacky/Nginx-UI/internal/translation"
  6. "github.com/elliotchance/orderedmap/v3"
  7. "github.com/uozi-tech/cosy"
  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: "Directory-ConfD",
  51. Name: translation.C("Conf.d directory exists"),
  52. Description: translation.C("Check if the conf.d directory is under the nginx configuration directory"),
  53. CheckFunc: CheckConfDirectory,
  54. FixFunc: FixConfDirectory,
  55. },
  56. {
  57. Key: "NginxConf-ConfD-Include",
  58. Name: translation.C("Nginx.conf includes conf.d directory"),
  59. Description: translation.C("Check if the nginx.conf includes the " +
  60. "conf.d directory"),
  61. CheckFunc: CheckNginxConfIncludeConfD,
  62. FixFunc: FixNginxConfIncludeConfD,
  63. },
  64. {
  65. Key: "NginxConf-Directory",
  66. Name: translation.C("Nginx configuration directory exists"),
  67. Description: translation.C("Check if the nginx configuration directory exists"),
  68. CheckFunc: CheckConfigDir,
  69. },
  70. {
  71. Key: "NginxConf-Entry-File",
  72. Name: translation.C("Nginx configuration entry file exists"),
  73. Description: translation.C("Check if the nginx configuration entry file exists"),
  74. CheckFunc: CheckConfigEntryFile,
  75. },
  76. {
  77. Key: "NginxPID-Path",
  78. Name: translation.C("Nginx PID path exists"),
  79. Description: translation.C("Check if the nginx PID path exists. " +
  80. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained, an error will be reported. " +
  81. "In this case, you need to modify the configuration file to specify the Nginx PID path." +
  82. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#pidpath"),
  83. CheckFunc: CheckPIDPath,
  84. },
  85. {
  86. Key: "NginxSbin-Path",
  87. Name: translation.C("Nginx sbin path exists"),
  88. Description: translation.C("Check if the nginx sbin path exists"),
  89. CheckFunc: CheckSbinPath,
  90. },
  91. {
  92. Key: "NginxAccessLog-Path",
  93. Name: translation.C("Nginx access log path exists"),
  94. Description: translation.C("Check if the nginx access log path exists. " +
  95. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained or the obtained path does not point to a valid, " +
  96. "existing file, an error will be reported. In this case, you need to modify the configuration file to specify the access log path." +
  97. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#accesslogpath"),
  98. CheckFunc: CheckAccessLogPath,
  99. },
  100. {
  101. Key: "NginxErrorLog-Path",
  102. Name: translation.C("Nginx error log path exists"),
  103. Description: translation.C("Check if the nginx error log path exists. " +
  104. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained or the obtained path does not point to a valid, " +
  105. "existing file, an error will be reported. In this case, you need to modify the configuration file to specify the error log path. " +
  106. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#errorlogpath"),
  107. CheckFunc: CheckErrorLogPath,
  108. },
  109. }
  110. var selfCheckTaskMap = orderedmap.NewOrderedMap[string, *Task]()
  111. func Init() {
  112. if nginx.IsModuleLoaded(nginx.ModuleStream) {
  113. selfCheckTasks = append(selfCheckTasks, &Task{
  114. Key: "Directory-Streams",
  115. Name: translation.C("Streams directory exists"),
  116. Description: translation.C("Check if the " +
  117. "streams-available and streams-enabled directories are " +
  118. "under the nginx configuration directory"),
  119. CheckFunc: CheckStreamDirectory,
  120. FixFunc: FixStreamDirectory,
  121. }, &Task{
  122. Key: "NginxConf-Streams-Enabled",
  123. Name: translation.C("Nginx.conf includes streams-enabled directory"),
  124. Description: translation.C("Check if the nginx.conf includes the " +
  125. "streams-enabled directory"),
  126. CheckFunc: CheckNginxConfIncludeStreams,
  127. FixFunc: FixNginxConfIncludeStreams,
  128. })
  129. }
  130. if helper.InNginxUIOfficialDocker() {
  131. selfCheckTasks = append(selfCheckTasks, &Task{
  132. Name: translation.C("Docker socket exists"),
  133. Description: translation.C("Check if /var/run/docker.sock exists. " +
  134. "If you are using Nginx UI Official " +
  135. "Docker Image, please make sure the docker socket is mounted like this: `-" +
  136. "v /var/run/docker.sock:/var/run/docker.sock`. " +
  137. "Nginx UI official image uses /var/run/docker.sock to communicate with the host Docker Engine via Docker Client API. " +
  138. "This feature is used to control Nginx in another container and perform container replacement rather than binary replacement " +
  139. "during OTA upgrades of Nginx UI to ensure container dependencies are also upgraded. " +
  140. "If you don't need this feature, please add the environment variable NGINX_UI_IGNORE_DOCKER_SOCKET=true to the container."),
  141. CheckFunc: CheckDockerSocket,
  142. })
  143. }
  144. for _, task := range selfCheckTasks {
  145. selfCheckTaskMap.Set(task.Key, task)
  146. }
  147. }