install.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package install
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "github.com/cnlh/nps/lib/common"
  12. )
  13. func InstallNps() {
  14. unit := `[Unit]
  15. Description=nps - convenient proxy server
  16. Documentation=https://github.com/cnlh/nps/
  17. After=network-online.target remote-fs.target nss-lookup.target
  18. Wants=network-online.target`
  19. service := `[Service]
  20. Type=simple
  21. KillMode=process
  22. Restart=always
  23. RestartSec=15s
  24. StandardOutput=append:/var/log/nps/nps.log
  25. ExecStartPre=/bin/echo 'Starting nps'
  26. ExecStopPost=/bin/echo 'Stopping nps'
  27. ExecStart=`
  28. install := `[Install]
  29. WantedBy=multi-user.target`
  30. path := common.GetInstallPath()
  31. if common.FileExists(path) {
  32. log.Fatalf("the path %s has exist, does not support install", path)
  33. }
  34. MkidrDirAll(path, "conf", "web/static", "web/views")
  35. //复制文件到对应目录
  36. if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "views"), filepath.Join(path, "web", "views")); err != nil {
  37. log.Fatalln(err)
  38. }
  39. if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "static"), filepath.Join(path, "web", "static")); err != nil {
  40. log.Fatalln(err)
  41. }
  42. if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil {
  43. log.Fatalln(err)
  44. }
  45. if !common.IsWindows() {
  46. if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/bin/nps"); err != nil {
  47. if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/local/bin/nps"); err != nil {
  48. log.Fatalln(err)
  49. } else {
  50. os.Chmod("/usr/local/bin/nps", 0755)
  51. service += "/usr/local/bin/nps"
  52. log.Println("Executable files have been copied to", "/usr/local/bin/nps")
  53. }
  54. } else {
  55. os.Chmod("/usr/bin/nps", 0755)
  56. service += "/usr/bin/nps"
  57. log.Println("Executable files have been copied to", "/usr/bin/nps")
  58. }
  59. systemd := unit + "\n\n" + service + "\n\n" + install
  60. if _, err := os.Stat("/usr/lib/systemd/system"); os.IsExist(err) {
  61. _ = os.Remove("/usr/lib/systemd/system/nps.service")
  62. err := ioutil.WriteFile("/usr/lib/systemd/system/nps.service", []byte(systemd), 0644)
  63. if err != nil {
  64. log.Println("Write systemd service err ", err)
  65. }
  66. } else if _, err := os.Stat("/lib/systemd/system"); os.IsExist(err) {
  67. _ = os.Remove("/lib/systemd/system/nps.service")
  68. err := ioutil.WriteFile("/lib/systemd/system/nps.service", []byte(systemd), 0644)
  69. if err != nil {
  70. log.Println("Write systemd service err ", err)
  71. }
  72. } else {
  73. log.Println("Write systemd service fail, not found the systemd system path ")
  74. }
  75. _ = os.Mkdir("/var/log/nps", 644)
  76. }
  77. log.Println("install ok!")
  78. log.Println("Static files and configuration files in the current directory will be useless")
  79. log.Println("The new configuration file is located in", path, "you can edit them")
  80. if !common.IsWindows() {
  81. log.Println(`You can start with:
  82. sudo systemctl enable|disable|start|stop|restart|status nps
  83. or:
  84. nps test|start|stop|restart|status
  85. anywhere!`)
  86. } else {
  87. log.Println(`You can copy executable files to any directory and start working with:
  88. nps.exe test|start|stop|restart|status
  89. now!`)
  90. }
  91. }
  92. func MkidrDirAll(path string, v ...string) {
  93. for _, item := range v {
  94. if err := os.MkdirAll(filepath.Join(path, item), 0755); err != nil {
  95. log.Fatalf("Failed to create directory %s error:%s", path, err.Error())
  96. }
  97. }
  98. }
  99. func CopyDir(srcPath string, destPath string) error {
  100. //检测目录正确性
  101. if srcInfo, err := os.Stat(srcPath); err != nil {
  102. fmt.Println(err.Error())
  103. return err
  104. } else {
  105. if !srcInfo.IsDir() {
  106. e := errors.New("SrcPath is not the right directory!")
  107. return e
  108. }
  109. }
  110. if destInfo, err := os.Stat(destPath); err != nil {
  111. return err
  112. } else {
  113. if !destInfo.IsDir() {
  114. e := errors.New("DestInfo is not the right directory!")
  115. return e
  116. }
  117. }
  118. err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
  119. if f == nil {
  120. return err
  121. }
  122. if !f.IsDir() {
  123. destNewPath := strings.Replace(path, srcPath, destPath, -1)
  124. log.Println("copy file ::" + path + " to " + destNewPath)
  125. copyFile(path, destNewPath)
  126. }
  127. return nil
  128. })
  129. return err
  130. }
  131. //生成目录并拷贝文件
  132. func copyFile(src, dest string) (w int64, err error) {
  133. srcFile, err := os.Open(src)
  134. if err != nil {
  135. return
  136. }
  137. defer srcFile.Close()
  138. //分割path目录
  139. destSplitPathDirs := strings.Split(dest, string(filepath.Separator))
  140. //检测时候存在目录
  141. destSplitPath := ""
  142. for index, dir := range destSplitPathDirs {
  143. if index < len(destSplitPathDirs)-1 {
  144. destSplitPath = destSplitPath + dir + string(filepath.Separator)
  145. b, _ := pathExists(destSplitPath)
  146. if b == false {
  147. log.Println("mkdir:" + destSplitPath)
  148. //创建目录
  149. err := os.Mkdir(destSplitPath, os.ModePerm)
  150. if err != nil {
  151. log.Fatalln(err)
  152. }
  153. }
  154. }
  155. }
  156. dstFile, err := os.Create(dest)
  157. if err != nil {
  158. return
  159. }
  160. defer dstFile.Close()
  161. return io.Copy(dstFile, srcFile)
  162. }
  163. //检测文件夹路径时候存在
  164. func pathExists(path string) (bool, error) {
  165. _, err := os.Stat(path)
  166. if err == nil {
  167. return true, nil
  168. }
  169. if os.IsNotExist(err) {
  170. return false, nil
  171. }
  172. return false, err
  173. }