mirror of
https://github.com/ehang-io/nps.git
synced 2025-07-03 04:53:50 +00:00
add service
This commit is contained in:
parent
a1200ae0ec
commit
4c964af58a
@ -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()
|
||||
}
|
||||
|
||||
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()
|
||||
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")
|
||||
//复制文件到对应目录
|
||||
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)
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
|
||||
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)
|
||||
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
|
||||
})
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user