1
0

tasks.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained, an error will be reported. "+
  74. "In this case, you need to modify the configuration file to specify the Nginx PID path." +
  75. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#pidpath"),
  76. CheckFunc: CheckPIDPath,
  77. },
  78. {
  79. Key: "NginxAccessLog-Path",
  80. Name: translation.C("Nginx access log path exists"),
  81. Description: translation.C("Check if the nginx access log path exists. "+
  82. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained or the obtained path does not point to a valid, "+
  83. "existing file, an error will be reported. In this case, you need to modify the configuration file to specify the access log path." +
  84. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#accesslogpath"),
  85. CheckFunc: CheckAccessLogPath,
  86. },
  87. {
  88. Key: "NginxErrorLog-Path",
  89. Name: translation.C("Nginx error log path exists"),
  90. Description: translation.C("Check if the nginx error log path exists. "+
  91. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained or the obtained path does not point to a valid, "+
  92. "existing file, an error will be reported. In this case, you need to modify the configuration file to specify the error log path." +
  93. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#errorlogpath"),
  94. CheckFunc: CheckErrorLogPath,
  95. },
  96. }
  97. var selfCheckTaskMap = orderedmap.NewOrderedMap[string, *Task]()
  98. func init() {
  99. if nginx.IsModuleLoaded(nginx.ModuleStream) {
  100. selfCheckTasks = append(selfCheckTasks, &Task{
  101. Key: "Directory-Streams",
  102. Name: translation.C("Streams directory exists"),
  103. Description: translation.C("Check if the " +
  104. "streams-available and streams-enabled directories are " +
  105. "under the nginx configuration directory"),
  106. CheckFunc: CheckStreamDirectory,
  107. FixFunc: FixStreamDirectory,
  108. }, &Task{
  109. Key: "NginxConf-Streams-Enabled",
  110. Name: translation.C("Nginx.conf includes streams-enabled directory"),
  111. Description: translation.C("Check if the nginx.conf includes the " +
  112. "streams-enabled directory"),
  113. CheckFunc: CheckNginxConfIncludeStreams,
  114. FixFunc: FixNginxConfIncludeStreams,
  115. })
  116. }
  117. if helper.InNginxUIOfficialDocker() {
  118. selfCheckTasks = append(selfCheckTasks, &Task{
  119. Name: translation.C("Docker socket exists"),
  120. Description: translation.C("Check if /var/run/docker.sock exists. "+
  121. "If you are using Nginx UI Official " +
  122. "Docker Image, please make sure the docker socket is mounted like this: `-" +
  123. "v /var/run/docker.sock:/var/run/docker.sock`. "+
  124. "Nginx UI official image uses /var/run/docker.sock to communicate with the host Docker Engine via Docker Client API. "+
  125. "This feature is used to control Nginx in another container and perform container replacement rather than binary replacement "+
  126. "during OTA upgrades of Nginx UI to ensure container dependencies are also upgraded. "+
  127. "If you don't need this feature, please add the environment variable NGINX_UI_IGNORE_DOCKER_SOCKET=true to the container."),
  128. CheckFunc: CheckDockerSocket,
  129. })
  130. }
  131. for _, task := range selfCheckTasks {
  132. selfCheckTaskMap.Set(task.Key, task)
  133. }
  134. }