Skip to content

Commit

Permalink
优化在容器内使用systemctl
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Feb 18, 2022
1 parent e0071bf commit 48e289a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func check() {
core.WritePassword(nil)
trojan.InstallTls()
trojan.InstallMysql()
util.ExecCommand("systemctl restart trojan-web")
util.SystemctlRestart("trojan-web")
}
}

Expand Down
16 changes: 4 additions & 12 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,22 @@ var webCmd = &cobra.Command{
},
}

func controllWeb(key, keyCN string) {
if err := util.ExecCommand(fmt.Sprintf("systemctl %s trojan-web", key)); err != nil {
fmt.Println(util.Red(fmt.Sprintf("%strojan-web失败!", keyCN)))
} else {
fmt.Println(util.Green(fmt.Sprintf("%strojan-web成功!", keyCN)))
}
}

func init() {
webCmd.Flags().StringVarP(&host, "host", "", "0.0.0.0", "web服务监听地址")
webCmd.Flags().IntVarP(&port, "port", "p", 80, "web服务启动端口")
webCmd.Flags().BoolVarP(&ssl, "ssl", "", false, "web服务是否以https方式运行")
webCmd.Flags().IntVarP(&timeout, "timeout", "t", 120, "登录超时时间(min)")
webCmd.AddCommand(&cobra.Command{Use: "stop", Short: "停止trojan-web", Run: func(cmd *cobra.Command, args []string) {
controllWeb("stop", "停止")
util.SystemctlStop("trojan-web")
}})
webCmd.AddCommand(&cobra.Command{Use: "start", Short: "启动trojan-web", Run: func(cmd *cobra.Command, args []string) {
controllWeb("start", "启动")
util.SystemctlStart("trojan-web")
}})
webCmd.AddCommand(&cobra.Command{Use: "restart", Short: "重启trojan-web", Run: func(cmd *cobra.Command, args []string) {
controllWeb("restart", "重启")
util.SystemctlRestart("trojan-web")
}})
webCmd.AddCommand(&cobra.Command{Use: "status", Short: "查看trojan-web状态", Run: func(cmd *cobra.Command, args []string) {
util.ExecCommand("systemctl status trojan-web -l")
fmt.Println(util.SystemctlStatus("trojan-web"))
}})
webCmd.AddCommand(&cobra.Command{Use: "log", Short: "查看trojan-web日志", Run: func(cmd *cobra.Command, args []string) {
util.Log("trojan-web", 300)
Expand Down
8 changes: 4 additions & 4 deletions trojan/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func InstallTrojan(version string) {
}
util.ExecCommand(data)
util.OpenPort(443)
util.ExecCommand("systemctl restart trojan")
util.ExecCommand("systemctl enable trojan")
util.SystemctlRestart("trojan")
util.SystemctlEnable("trojan")
}

// InstallTls 安装证书
Expand Down Expand Up @@ -112,7 +112,7 @@ func InstallTls() {
if !util.IsExists("/root/.acme.sh/acme.sh") {
util.RunWebShell("https://get.acme.sh")
}
util.ExecCommand("systemctl stop trojan-web")
util.SystemctlStop("trojan-web")
util.OpenPort(80)
checkResult := util.ExecCommandWithResult("/root/.acme.sh/acme.sh -v|tr -cd '[0-9]'")
acmeVersion, _ := strconv.Atoi(checkResult)
Expand Down Expand Up @@ -144,7 +144,7 @@ func InstallTls() {
core.WriteTls(crtFile, keyFile, domain)
}
Restart()
util.ExecCommand("systemctl restart trojan-web")
util.SystemctlRestart("trojan-web")
fmt.Println()
}

Expand Down
20 changes: 4 additions & 16 deletions trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,23 @@ func ControllMenu() {
// Restart 重启trojan
func Restart() {
util.OpenPort(core.GetConfig().LocalPort)
if err := util.ExecCommand("systemctl restart trojan"); err != nil {
fmt.Println(util.Red("重启trojan失败!"))
} else {
fmt.Println(util.Green("重启trojan成功!"))
}
util.SystemctlRestart("trojan")
}

// Start 启动trojan
func Start() {
util.OpenPort(core.GetConfig().LocalPort)
if err := util.ExecCommand("systemctl start trojan"); err != nil {
fmt.Println(util.Red("启动trojan失败!"))
} else {
fmt.Println(util.Green("启动trojan成功!"))
}
util.SystemctlStart("trojan")
}

// Stop 停止trojan
func Stop() {
if err := util.ExecCommand("systemctl stop trojan"); err != nil {
fmt.Println(util.Red("停止trojan失败!"))
} else {
fmt.Println(util.Green("停止trojan成功!"))
}
util.SystemctlStop("trojan")
}

// Status 获取trojan状态
func Status(isPrint bool) string {
result := util.ExecCommandWithResult("systemctl status trojan")
result := util.SystemctlStatus("trojan")
if isPrint {
fmt.Println(result)
}
Expand Down
73 changes: 73 additions & 0 deletions util/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,74 @@ import (
"strings"
)

func systemctlReplace(err error) (bool, error) {
var isReplace bool
if err != nil && IsExists("/.dockerenv") && strings.Contains(err.Error(), "Failed to get D-Bus") {
isReplace = true
fmt.Println(Yellow("正在下载并替换适配的systemctl。。"))
if err = ExecCommand("curl -L https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -o /usr/bin/systemctl && chmod +x /usr/bin/systemctl"); err != nil {
return isReplace, err
}
}
return isReplace, err
}

func systemctlBase(name, operate string) error {
err := ExecCommand(fmt.Sprintf("systemctl %s %s", operate, name))
if v, err := systemctlReplace(err); v {
if err = ExecCommand(fmt.Sprintf("systemctl %s %s", operate, name)); err != nil {
return err
}
}
return err
}

// SystemctlStart 服务启动
func SystemctlStart(name string) {
if err := systemctlBase(name, "start"); err != nil {
fmt.Println(Red(fmt.Sprintf("启动%s失败!", name)))
} else {
fmt.Println(Green(fmt.Sprintf("启动%s成功!", name)))
}
}

// SystemctlStop 服务停止
func SystemctlStop(name string) {
if err := systemctlBase(name, "stop"); err != nil {
fmt.Println(Red(fmt.Sprintf("停止%s失败!", name)))
} else {
fmt.Println(Green(fmt.Sprintf("停止%s成功!", name)))
}
}

// SystemctlRestart 服务重启
func SystemctlRestart(name string) {
if err := systemctlBase(name, "restart"); err != nil {
fmt.Println(Red(fmt.Sprintf("重启%s失败!", name)))
} else {
fmt.Println(Green(fmt.Sprintf("重启%s成功!", name)))
}
}

// SystemctlEnable 服务设置开机自启
func SystemctlEnable(name string) {
if err := systemctlBase(name, "enable"); err != nil {
fmt.Println(Red(fmt.Sprintf("设置%s开机自启失败!", name)))
}
}

// SystemctlStatus 服务状态查看
func SystemctlStatus(name string) string {
out, err := exec.Command("bash", "-c", fmt.Sprintf("systemctl status %s", name)).CombinedOutput()
if v, _ := systemctlReplace(err); v {
out, err = exec.Command("bash", "-c", fmt.Sprintf("systemctl status %s", name)).CombinedOutput()
}
if err != nil {
fmt.Println(Red(fmt.Sprintf("查看%s状态失败!", name)))
}
return string(out)
}

// CheckCommandExists 检查命令是否存在
func CheckCommandExists(command string) bool {
if _, err := exec.LookPath(command); err != nil {
Expand Down Expand Up @@ -78,6 +146,11 @@ func ExecCommand(command string) error {
// ExecCommandWithResult 运行命令并获取结果
func ExecCommandWithResult(command string) string {
out, err := exec.Command("bash", "-c", command).CombinedOutput()
if strings.Contains(command, "systemctl") {
if v, _ := systemctlReplace(err); v {
out, err = exec.Command("bash", "-c", command).CombinedOutput()
}
}
if err != nil && !strings.Contains(err.Error(), "exit status") {
fmt.Println("err: " + err.Error())
return ""
Expand Down

0 comments on commit 48e289a

Please sign in to comment.