install.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package install
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/c4milo/unpackit"
  7. "github.com/cnlh/nps/lib/common"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "os"
  13. "path/filepath"
  14. "runtime"
  15. "strings"
  16. )
  17. func Update() {
  18. downloadLatest()
  19. }
  20. type release struct {
  21. TagName string `json:"tag_name"`
  22. }
  23. func downloadLatest() {
  24. // get version
  25. data, err := http.Get("https://api.github.com/repos/cnlh/nps/releases/latest")
  26. if err != nil {
  27. log.Fatal(err.Error())
  28. }
  29. b, err := ioutil.ReadAll(data.Body)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. rl := new(release)
  34. json.Unmarshal(b, &rl)
  35. version := rl.TagName
  36. fmt.Println("the latest version is", version)
  37. filename := runtime.GOOS + "_" + runtime.GOARCH + "_server" + ".tar.gz"
  38. // download latest package
  39. downloadUrl := fmt.Sprintf("https://github.com/cnlh/nps/releases/download/%s/%s", version, filename)
  40. fmt.Println("download package from ", downloadUrl)
  41. resp, err := http.Get(downloadUrl)
  42. if err != nil {
  43. log.Fatal(err.Error())
  44. }
  45. destPath, err := unpackit.Unpack(resp.Body, "")
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. destPath = strings.Replace(destPath, "/web", "", -1)
  50. destPath = strings.Replace(destPath, `\web`, "", -1)
  51. destPath = strings.Replace(destPath, "/views", "", -1)
  52. destPath = strings.Replace(destPath, `\views`, "", -1)
  53. //复制文件到对应目录
  54. copyStaticFile(destPath)
  55. fmt.Println("Update completed, please restart")
  56. if common.IsWindows() {
  57. fmt.Println("windows 请将nps_new.exe替换成nps.exe")
  58. }
  59. }
  60. func copyStaticFile(srcPath string) string {
  61. path := common.GetInstallPath()
  62. //复制文件到对应目录
  63. if err := CopyDir(filepath.Join(srcPath, "web", "views"), filepath.Join(path, "web", "views")); err != nil {
  64. log.Fatalln(err)
  65. }
  66. os.Chmod(filepath.Join(path, "web", "views"), 0766)
  67. if err := CopyDir(filepath.Join(srcPath, "web", "static"), filepath.Join(path, "web", "static")); err != nil {
  68. log.Fatalln(err)
  69. }
  70. os.Chmod(filepath.Join(path, "web", "static"), 0766)
  71. binPath, _ := filepath.Abs(os.Args[0])
  72. if !common.IsWindows() {
  73. if _, err := copyFile(filepath.Join(srcPath, "nps"), "/usr/bin/nps"); err != nil {
  74. if _, err := copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/nps"); err != nil {
  75. log.Fatalln(err)
  76. } else {
  77. binPath = "/usr/local/bin/nps"
  78. }
  79. } else {
  80. binPath = "/usr/bin/nps"
  81. }
  82. } else {
  83. copyFile(filepath.Join(srcPath, "nps.exe"), filepath.Join(common.GetAppPath(), "nps_new.exe"))
  84. }
  85. os.Chmod(binPath, 0755)
  86. return binPath
  87. }
  88. func InstallNps() string {
  89. path := common.GetInstallPath()
  90. if common.FileExists(path) {
  91. MkidrDirAll(path, "web/static", "web/views")
  92. } else {
  93. MkidrDirAll(path, "conf", "web/static", "web/views")
  94. // not copy config if the config file is exist
  95. if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil {
  96. log.Fatalln(err)
  97. }
  98. os.Chmod(filepath.Join(path, "conf"), 0766)
  99. }
  100. binPath := copyStaticFile(common.GetAppPath())
  101. log.Println("install ok!")
  102. log.Println("Static files and configuration files in the current directory will be useless")
  103. log.Println("The new configuration file is located in", path, "you can edit them")
  104. if !common.IsWindows() {
  105. log.Println(`You can start with:
  106. nps start|stop|restart|uninstall|update
  107. anywhere!`)
  108. } else {
  109. log.Println(`You can copy executable files to any directory and start working with:
  110. nps.exe start|stop|restart|uninstall|update
  111. now!`)
  112. }
  113. os.Chmod(common.GetLogPath(), 0777)
  114. return binPath
  115. }
  116. func MkidrDirAll(path string, v ...string) {
  117. for _, item := range v {
  118. if err := os.MkdirAll(filepath.Join(path, item), 0755); err != nil {
  119. log.Fatalf("Failed to create directory %s error:%s", path, err.Error())
  120. }
  121. }
  122. }
  123. func CopyDir(srcPath string, destPath string) error {
  124. //检测目录正确性
  125. if srcInfo, err := os.Stat(srcPath); err != nil {
  126. fmt.Println(err.Error())
  127. return err
  128. } else {
  129. if !srcInfo.IsDir() {
  130. e := errors.New("SrcPath is not the right directory!")
  131. return e
  132. }
  133. }
  134. if destInfo, err := os.Stat(destPath); err != nil {
  135. return err
  136. } else {
  137. if !destInfo.IsDir() {
  138. e := errors.New("DestInfo is not the right directory!")
  139. return e
  140. }
  141. }
  142. err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
  143. if f == nil {
  144. return err
  145. }
  146. if !f.IsDir() {
  147. destNewPath := strings.Replace(path, srcPath, destPath, -1)
  148. log.Println("copy file ::" + path + " to " + destNewPath)
  149. copyFile(path, destNewPath)
  150. os.Chmod(destNewPath, 0766)
  151. }
  152. return nil
  153. })
  154. return err
  155. }
  156. //生成目录并拷贝文件
  157. func copyFile(src, dest string) (w int64, err error) {
  158. srcFile, err := os.Open(src)
  159. if err != nil {
  160. return
  161. }
  162. defer srcFile.Close()
  163. //分割path目录
  164. destSplitPathDirs := strings.Split(dest, string(filepath.Separator))
  165. //检测时候存在目录
  166. destSplitPath := ""
  167. for index, dir := range destSplitPathDirs {
  168. if index < len(destSplitPathDirs)-1 {
  169. destSplitPath = destSplitPath + dir + string(filepath.Separator)
  170. b, _ := pathExists(destSplitPath)
  171. if b == false {
  172. log.Println("mkdir:" + destSplitPath)
  173. //创建目录
  174. err := os.Mkdir(destSplitPath, os.ModePerm)
  175. if err != nil {
  176. log.Fatalln(err)
  177. }
  178. }
  179. }
  180. }
  181. dstFile, err := os.Create(dest)
  182. if err != nil {
  183. return
  184. }
  185. defer dstFile.Close()
  186. return io.Copy(dstFile, srcFile)
  187. }
  188. //检测文件夹路径时候存在
  189. func pathExists(path string) (bool, error) {
  190. _, err := os.Stat(path)
  191. if err == nil {
  192. return true, nil
  193. }
  194. if os.IsNotExist(err) {
  195. return false, nil
  196. }
  197. return false, err
  198. }