Skip to content

Commit

Permalink
优化docker运行
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Mar 2, 2020
1 parent 3937bb5 commit 900e4c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 58 deletions.
11 changes: 9 additions & 2 deletions trojan/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,15 @@ func InstallTls() {
}

func InstallMysql() {
var mysql core.Mysql
choice := util.LoopInput("请选择: ", []string{"安装docker版mysql", "输入自定义mysql连接"}, true)
var (
mysql core.Mysql
choice int
)
if util.IsExists("/.dockerenv") {
choice = 2
} else {
choice = util.LoopInput("请选择: ", []string{"安装docker版mysql", "输入自定义mysql连接"}, true)
}
if choice < 0 {
return
} else if choice == 1 {
Expand Down
49 changes: 10 additions & 39 deletions trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,29 @@ func ControllMenu() {
}

func Restart() {
if util.IsExists("/.dockerenv") {
Stop()
Start()
fmt.Println(util.Green("重启trojan成功!"))
if err := util.ExecCommand("systemctl restart trojan"); err != nil {
fmt.Println(util.Red("重启trojan失败!"))
} else {
if err := util.ExecCommand("systemctl restart trojan"); err != nil {
fmt.Println(util.Red("重启trojan失败!"))
} else {
fmt.Println(util.Green("重启trojan成功!"))
}
fmt.Println(util.Green("重启trojan成功!"))
}
}

func Start() {
if util.IsExists("/.dockerenv") {
check := util.ExecCommandWithResult(`ps aux|grep "/usr/bin/trojan/trojan"|grep -v grep`)
if check != "" {
Stop()
}
util.ExecCommand(`echo "" > /.run.log`)
util.StartProcess("/usr/bin/trojan/trojan", "-c", "/usr/local/etc/trojan/config.json", "-l", "/.run.log")
fmt.Println(util.Green("启动trojan成功!"))
if err := util.ExecCommand("systemctl start trojan"); err != nil {
fmt.Println(util.Red("启动trojan失败!"))
} else {
if err := util.ExecCommand("systemctl start trojan"); err != nil {
fmt.Println(util.Red("启动trojan失败!"))
} else {
fmt.Println(util.Green("启动trojan成功!"))
}
fmt.Println(util.Green("启动trojan成功!"))
}
}

func Stop() {
if util.IsExists("/.dockerenv") {
util.ExecCommandWithResult(`ps aux|grep "/usr/bin/trojan/trojan"|grep -v grep|awk '{print $2}'|xargs -r kill -9`)
fmt.Println(util.Green("停止trojan成功!"))
if err := util.ExecCommand("systemctl stop trojan"); err != nil {
fmt.Println(util.Red("停止trojan失败!"))
} else {
if err := util.ExecCommand("systemctl stop trojan"); err != nil {
fmt.Println(util.Red("停止trojan失败!"))
} else {
fmt.Println(util.Green("停止trojan成功!"))
}
fmt.Println(util.Green("停止trojan成功!"))
}
}

func Status() {
if util.IsExists("/.dockerenv") {
if util.ExecCommandWithResult("cat /.run.log|grep FATAL") != "" {
fmt.Println(util.ExecCommandWithResult("cat /.run.log"))
} else {
fmt.Println(util.Green("trojan running..."))
}
} else {
util.ExecCommand("systemctl status trojan")
}
util.ExecCommand("systemctl status trojan")
}
17 changes: 0 additions & 17 deletions util/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
)
Expand Down Expand Up @@ -86,19 +85,3 @@ func ExecCommandWithResult(command string) string {
}
return string(out)
}

func StartProcess(name string, args ...string) {
env := os.Environ()
procAttr := &os.ProcAttr{
Env: env,
Files: []*os.File{
os.Stdin,
os.Stdout,
os.Stderr,
},
}
_, err := os.StartProcess(name, args, procAttr)
if err != nil {
fmt.Println(err)
}
}

0 comments on commit 900e4c2

Please sign in to comment.