From 4c964af58a7763ffa506d6a1ff9b6556f9bfe580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Sat, 14 Dec 2019 12:27:38 +0800 Subject: [PATCH] add service --- lib/install/install.go | 147 ++++++++++++++++++++++++----------------- lib/version/version.go | 2 +- 2 files changed, 87 insertions(+), 62 deletions(-) diff --git a/lib/install/install.go b/lib/install/install.go index 7b03581..5f98338 100644 --- a/lib/install/install.go +++ b/lib/install/install.go @@ -1,99 +1,123 @@ package install import ( + "encoding/json" "errors" "fmt" + "github.com/c4milo/unpackit" + "github.com/cnlh/nps/lib/common" "io" "io/ioutil" "log" + "net/http" "os" "path/filepath" + "runtime" "strings" - - "github.com/cnlh/nps/lib/common" ) -func InstallNps() { - unit := `[Unit] -Description=nps - convenient proxy server -Documentation=https://github.com/cnlh/nps/ -After=network-online.target remote-fs.target nss-lookup.target -Wants=network-online.target` - service := `[Service] -Type=simple -KillMode=process -Restart=always -RestartSec=15s -StandardOutput=append:/var/log/nps/nps.log -ExecStartPre=/bin/echo 'Starting nps' -ExecStopPost=/bin/echo 'Stopping nps' -ExecStart=` - install := `[Install] -WantedBy=multi-user.target` +func Update() { + downloadLatest() +} - path := common.GetInstallPath() - if common.FileExists(path) { - log.Fatalf("the path %s has exist, does not support install", path) +type release struct { + TagName string `json:"tag_name"` +} + +func downloadLatest() { + // get version + data, err := http.Get("https://api.github.com/repos/cnlh/nps/releases/latest") + if err != nil { + log.Fatal(err.Error()) } - MkidrDirAll(path, "conf", "web/static", "web/views") + b, err := ioutil.ReadAll(data.Body) + if err != nil { + log.Fatal(err) + } + rl := new(release) + json.Unmarshal(b, &rl) + version := rl.TagName + fmt.Println("the latest version is", version) + filename := runtime.GOOS + "_" + runtime.GOARCH + "_server" + ".tar.gz" + // download latest package + downloadUrl := fmt.Sprintf("https://github.com/cnlh/nps/releases/download/%s/%s", version, filename) + fmt.Println("download package from ", downloadUrl) + resp, err := http.Get(downloadUrl) + if err != nil { + log.Fatal(err.Error()) + } + destPath, err := unpackit.Unpack(resp.Body, "") + if err != nil { + log.Fatal(err) + } + destPath = strings.Replace(destPath, "/web", "", -1) + destPath = strings.Replace(destPath, `\web`, "", -1) + destPath = strings.Replace(destPath, "/views", "", -1) + destPath = strings.Replace(destPath, `\views`, "", -1) //复制文件到对应目录 - if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "views"), filepath.Join(path, "web", "views")); err != nil { - log.Fatalln(err) - } - if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "static"), filepath.Join(path, "web", "static")); err != nil { - log.Fatalln(err) - } - if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil { - log.Fatalln(err) + copyStaticFile(destPath) + fmt.Println("Update completed, please restart") + if common.IsWindows() { + fmt.Println("windows 请将nps_new.exe替换成nps.exe") } +} +func copyStaticFile(srcPath string) string { + path := common.GetInstallPath() + //复制文件到对应目录 + if err := CopyDir(filepath.Join(srcPath, "web", "views"), filepath.Join(path, "web", "views")); err != nil { + log.Fatalln(err) + } + os.Chmod(filepath.Join(path, "web", "views"), 0766) + if err := CopyDir(filepath.Join(srcPath, "web", "static"), filepath.Join(path, "web", "static")); err != nil { + log.Fatalln(err) + } + os.Chmod(filepath.Join(path, "web", "static"), 0766) + binPath, _ := filepath.Abs(os.Args[0]) if !common.IsWindows() { - if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/bin/nps"); err != nil { - if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/local/bin/nps"); err != nil { + if _, err := copyFile(filepath.Join(srcPath, "nps"), "/usr/bin/nps"); err != nil { + if _, err := copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/nps"); err != nil { log.Fatalln(err) } else { - os.Chmod("/usr/local/bin/nps", 0755) - service += "/usr/local/bin/nps" - log.Println("Executable files have been copied to", "/usr/local/bin/nps") + binPath = "/usr/local/bin/nps" } } else { - os.Chmod("/usr/bin/nps", 0755) - service += "/usr/bin/nps" - log.Println("Executable files have been copied to", "/usr/bin/nps") + binPath = "/usr/bin/nps" } - systemd := unit + "\n\n" + service + "\n\n" + install - if _, err := os.Stat("/usr/lib/systemd/system"); os.IsExist(err) { - _ = os.Remove("/usr/lib/systemd/system/nps.service") - err := ioutil.WriteFile("/usr/lib/systemd/system/nps.service", []byte(systemd), 0644) - if err != nil { - log.Println("Write systemd service err ", err) - } - } else if _, err := os.Stat("/lib/systemd/system"); os.IsExist(err) { - _ = os.Remove("/lib/systemd/system/nps.service") - err := ioutil.WriteFile("/lib/systemd/system/nps.service", []byte(systemd), 0644) - if err != nil { - log.Println("Write systemd service err ", err) - } - } else { - log.Println("Write systemd service fail, not found the systemd system path ") - } - - _ = os.Mkdir("/var/log/nps", 644) + } else { + copyFile(filepath.Join(srcPath, "nps.exe"), filepath.Join(common.GetAppPath(), "nps_new.exe")) } + os.Chmod(binPath, 0755) + return binPath +} + +func InstallNps() string { + path := common.GetInstallPath() + if common.FileExists(path) { + MkidrDirAll(path, "web/static", "web/views") + } else { + MkidrDirAll(path, "conf", "web/static", "web/views") + // not copy config if the config file is exist + if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil { + log.Fatalln(err) + } + os.Chmod(filepath.Join(path, "conf"), 0766) + } + binPath := copyStaticFile(common.GetAppPath()) log.Println("install ok!") log.Println("Static files and configuration files in the current directory will be useless") log.Println("The new configuration file is located in", path, "you can edit them") if !common.IsWindows() { log.Println(`You can start with: -sudo systemctl enable|disable|start|stop|restart|status nps -or: -nps test|start|stop|restart|status +nps start|stop|restart|uninstall|update anywhere!`) } else { log.Println(`You can copy executable files to any directory and start working with: -nps.exe test|start|stop|restart|status +nps.exe start|stop|restart|uninstall|update now!`) } + os.Chmod(common.GetLogPath(), 0777) + return binPath } func MkidrDirAll(path string, v ...string) { for _, item := range v { @@ -130,6 +154,7 @@ func CopyDir(srcPath string, destPath string) error { destNewPath := strings.Replace(path, srcPath, destPath, -1) log.Println("copy file ::" + path + " to " + destNewPath) copyFile(path, destNewPath) + os.Chmod(destNewPath, 0766) } return nil }) diff --git a/lib/version/version.go b/lib/version/version.go index b3ffbb1..5c80ede 100644 --- a/lib/version/version.go +++ b/lib/version/version.go @@ -1,6 +1,6 @@ package version -const VERSION = "0.25.1" +const VERSION = "0.25.2" // Compulsory minimum version, Minimum downward compatibility to this version func GetVersion() string {