nps/lib/install/install.go

174 lines
4.6 KiB
Go
Raw Normal View History

2019-02-09 01:07:47 -08:00
package install
2019-02-03 01:25:00 -08:00
import (
"errors"
"fmt"
"io"
2019-10-12 07:56:37 -07:00
"io/ioutil"
2019-02-03 01:25:00 -08:00
"log"
"os"
"path/filepath"
"strings"
2019-08-09 20:15:25 -07:00
"github.com/cnlh/nps/lib/common"
2019-02-03 01:25:00 -08:00
)
func InstallNps() {
2019-10-12 07:56:37 -07:00
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`
2019-02-09 01:07:47 -08:00
path := common.GetInstallPath()
2019-04-21 08:03:58 -07:00
if common.FileExists(path) {
log.Fatalf("the path %s has exist, does not support install", path)
}
2019-02-05 08:35:23 -08:00
MkidrDirAll(path, "conf", "web/static", "web/views")
2019-02-03 01:25:00 -08:00
//复制文件到对应目录
2019-02-09 01:07:47 -08:00
if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "views"), filepath.Join(path, "web", "views")); err != nil {
2019-02-03 01:25:00 -08:00
log.Fatalln(err)
}
2019-02-09 01:07:47 -08:00
if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "static"), filepath.Join(path, "web", "static")); err != nil {
2019-02-05 08:35:23 -08:00
log.Fatalln(err)
}
2019-02-09 01:07:47 -08:00
if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil {
2019-02-03 01:25:00 -08:00
log.Fatalln(err)
}
2019-02-09 01:07:47 -08:00
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 {
2019-02-05 08:35:23 -08:00
log.Fatalln(err)
} else {
2019-08-15 19:48:48 -07:00
os.Chmod("/usr/local/bin/nps", 0755)
2019-10-12 07:56:37 -07:00
service += "/usr/local/bin/nps"
2019-02-05 08:35:23 -08:00
log.Println("Executable files have been copied to", "/usr/local/bin/nps")
}
} else {
2019-08-15 19:48:48 -07:00
os.Chmod("/usr/bin/nps", 0755)
2019-10-12 07:56:37 -07:00
service += "/usr/bin/nps"
2019-02-05 08:35:23 -08:00
log.Println("Executable files have been copied to", "/usr/bin/nps")
}
2019-10-12 07:56:37 -07:00
systemd := unit + "\n\n" + service + "\n\n" + install
_ = 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)
}
_ = os.Mkdir("/var/log/nps", 644)
2019-02-05 08:35:23 -08:00
}
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")
2019-02-09 01:07:47 -08:00
if !common.IsWindows() {
2019-10-12 07:56:37 -07:00
log.Println(`You can start with:
sudo systemctl enable|disable|start|stop|restart|status nps
or:
nps test|start|stop|restart|status
anywhere!`)
2019-02-05 08:35:23 -08:00
} else {
2019-10-12 07:56:37 -07:00
log.Println(`You can copy executable files to any directory and start working with:
nps.exe test|start|stop|restart|status
now!`)
2019-02-05 08:35:23 -08:00
}
}
2019-02-16 04:43:26 -08:00
func MkidrDirAll(path string, v ...string) {
2019-02-05 08:35:23 -08:00
for _, item := range v {
if err := os.MkdirAll(filepath.Join(path, item), 0755); err != nil {
log.Fatalf("Failed to create directory %s error:%s", path, err.Error())
}
}
2019-02-03 01:25:00 -08:00
}
func CopyDir(srcPath string, destPath string) error {
//检测目录正确性
if srcInfo, err := os.Stat(srcPath); err != nil {
fmt.Println(err.Error())
return err
} else {
if !srcInfo.IsDir() {
2019-02-05 08:35:23 -08:00
e := errors.New("SrcPath is not the right directory!")
2019-02-03 01:25:00 -08:00
return e
}
}
if destInfo, err := os.Stat(destPath); err != nil {
return err
} else {
if !destInfo.IsDir() {
2019-02-05 08:35:23 -08:00
e := errors.New("DestInfo is not the right directory!")
2019-02-03 01:25:00 -08:00
return e
}
}
err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if !f.IsDir() {
destNewPath := strings.Replace(path, srcPath, destPath, -1)
2019-02-05 08:35:23 -08:00
log.Println("copy file ::" + path + " to " + destNewPath)
2019-02-03 01:25:00 -08:00
copyFile(path, destNewPath)
}
return nil
})
return err
}
//生成目录并拷贝文件
func copyFile(src, dest string) (w int64, err error) {
srcFile, err := os.Open(src)
if err != nil {
return
}
defer srcFile.Close()
//分割path目录
2019-02-05 08:35:23 -08:00
destSplitPathDirs := strings.Split(dest, string(filepath.Separator))
2019-02-03 01:25:00 -08:00
//检测时候存在目录
destSplitPath := ""
for index, dir := range destSplitPathDirs {
if index < len(destSplitPathDirs)-1 {
2019-02-05 08:35:23 -08:00
destSplitPath = destSplitPath + dir + string(filepath.Separator)
2019-02-03 01:25:00 -08:00
b, _ := pathExists(destSplitPath)
if b == false {
2019-02-05 08:35:23 -08:00
log.Println("mkdir:" + destSplitPath)
2019-02-03 01:25:00 -08:00
//创建目录
err := os.Mkdir(destSplitPath, os.ModePerm)
if err != nil {
2019-02-05 08:35:23 -08:00
log.Fatalln(err)
2019-02-03 01:25:00 -08:00
}
}
}
}
dstFile, err := os.Create(dest)
if err != nil {
return
}
defer dstFile.Close()
return io.Copy(dstFile, srcFile)
}
//检测文件夹路径时候存在
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}