add service

This commit is contained in:
刘河 2019-12-14 12:27:38 +08:00
parent a1200ae0ec
commit 4c964af58a
2 changed files with 87 additions and 62 deletions

View File

@ -1,99 +1,123 @@
package install package install
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/c4milo/unpackit"
"github.com/cnlh/nps/lib/common"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"github.com/cnlh/nps/lib/common"
) )
func InstallNps() { func Update() {
unit := `[Unit] downloadLatest()
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`
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())
}
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)
//复制文件到对应目录
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(srcPath, "nps"), "/usr/bin/nps"); err != nil {
if _, err := copyFile(filepath.Join(srcPath, "nps"), "/usr/local/bin/nps"); err != nil {
log.Fatalln(err)
} else {
binPath = "/usr/local/bin/nps"
}
} else {
binPath = "/usr/bin/nps"
}
} 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() path := common.GetInstallPath()
if common.FileExists(path) { if common.FileExists(path) {
log.Fatalf("the path %s has exist, does not support install", path) MkidrDirAll(path, "web/static", "web/views")
} } else {
MkidrDirAll(path, "conf", "web/static", "web/views") MkidrDirAll(path, "conf", "web/static", "web/views")
//复制文件到对应目录 // not copy config if the config file is exist
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 { if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
os.Chmod(filepath.Join(path, "conf"), 0766)
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 {
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")
}
} else {
os.Chmod("/usr/bin/nps", 0755)
service += "/usr/bin/nps"
log.Println("Executable files have been copied to", "/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)
} }
binPath := copyStaticFile(common.GetAppPath())
log.Println("install ok!") log.Println("install ok!")
log.Println("Static files and configuration files in the current directory will be useless") 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") log.Println("The new configuration file is located in", path, "you can edit them")
if !common.IsWindows() { if !common.IsWindows() {
log.Println(`You can start with: log.Println(`You can start with:
sudo systemctl enable|disable|start|stop|restart|status nps nps start|stop|restart|uninstall|update
or:
nps test|start|stop|restart|status
anywhere!`) anywhere!`)
} else { } else {
log.Println(`You can copy executable files to any directory and start working with: 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!`) now!`)
} }
os.Chmod(common.GetLogPath(), 0777)
return binPath
} }
func MkidrDirAll(path string, v ...string) { func MkidrDirAll(path string, v ...string) {
for _, item := range v { for _, item := range v {
@ -130,6 +154,7 @@ func CopyDir(srcPath string, destPath string) error {
destNewPath := strings.Replace(path, srcPath, destPath, -1) destNewPath := strings.Replace(path, srcPath, destPath, -1)
log.Println("copy file ::" + path + " to " + destNewPath) log.Println("copy file ::" + path + " to " + destNewPath)
copyFile(path, destNewPath) copyFile(path, destNewPath)
os.Chmod(destNewPath, 0766)
} }
return nil return nil
}) })

View File

@ -1,6 +1,6 @@
package version package version
const VERSION = "0.25.1" const VERSION = "0.25.2"
// Compulsory minimum version, Minimum downward compatibility to this version // Compulsory minimum version, Minimum downward compatibility to this version
func GetVersion() string { func GetVersion() string {