Commit d60fd3fd by 浔阳陌客

2022/04/29 自我更新

parent 12edcf66
package main package main
import ( import (
"fmt"
"os" "os"
"path" "path/filepath"
"strings" "runtime"
) )
func getAppFullName() string { func getAppFullPathName() string {
return os.Args[0] return os.Args[0]
} }
func getAppName() string { func getAppCurrentPath() string {
return path.Base(os.Args[0]) var abWorkPath string
}
func getAppNameNewVersion() string {
app := getAppName()
switch OS_Type { switch OS_Type {
case OS_Type_Windows: case OS_Type_Windows:
return strings.TrimSuffix(app, path.Ext(app)) exePath, err := os.Executable()
if err != nil {
return `.\`
}
abWorkPath, _ = filepath.EvalSymlinks(filepath.Dir(exePath))
default: default:
return app + "_new" _, filename, _, ok := runtime.Caller(0)
if ok {
abWorkPath = filepath.Dir(filename)
}
} }
return abWorkPath
}
func getAppAbsNewVersionPath() string {
// Windows 的绝对路径 在$env:userprofile/ci
// macOS 的路径是运行程序本身的绝对路径
// Linux 的路径是运行程序本身的绝对路径
if OS_Type == OS_Type_Windows {
return fmt.Sprintf("%s%s%s", Dir_Main, Dir_symbol, Application_CI_NewVersion)
}
return fmt.Sprintf("%s%s%s", Dir_App_Current, Dir_symbol, Application_CI_NewVersion)
}
func getAppAbsPath() string {
if OS_Type == OS_Type_Windows {
return fmt.Sprintf("%s%s%s", Dir_Main, Dir_symbol, Application_CI_FullName)
}
return fmt.Sprintf("%s%s%s", Dir_App_Current, Dir_symbol, Application_CI_FullName)
}
func getAppCurrentName() string {
return filepath.Base(os.Args[0])
} }
...@@ -5,6 +5,10 @@ case "$way" in ...@@ -5,6 +5,10 @@ case "$way" in
win) win)
CGO_ENABLED=0 GOOS=windows go build -o ci_windows.exe *.go CGO_ENABLED=0 GOOS=windows go build -o ci_windows.exe *.go
;; ;;
mac)
CGO_ENABLED=0 GOOS=darwin go build -o ci_macos *.go
;;
*) *)
os=(windows darwin linux) os=(windows darwin linux)
for i in "${os[@]}" ;do for i in "${os[@]}" ;do
......
...@@ -2,31 +2,39 @@ package main ...@@ -2,31 +2,39 @@ package main
import ( import (
"flag" "flag"
"fmt"
"os"
"strings"
pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public" pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public"
) )
var OS_Type int var OS_Type int
const Application_CI = "ci"
var Application_CI_FullName string
var Application_CI_NewVersion string
var cmd_new string
const OS_Type_Windows = pub.OS_Type_Windows const OS_Type_Windows = pub.OS_Type_Windows
const OS_Type_Linux = pub.OS_Type_Linux const OS_Type_Linux = pub.OS_Type_Linux
const OS_Type_MacOS = pub.OS_Type_MacOS const OS_Type_MacOS = pub.OS_Type_MacOS
var Dir_symbol string
var Dir_Main string
var Dir_App_Current string
var logfile string
const INFO = pub.INFO const INFO = pub.INFO
const ERROR = pub.ERROR const ERROR = pub.ERROR
var deptstring string var deptstring string
var regionstring string var regionstring string
var ciName_self string
var ciName_new string
func envModify(target *string, s string) {
if *target == "" {
*target = s
}
}
func parseFlag() bool { func parseFlag() bool {
var mode bool
var mode, version bool
regionstring = pub.JoinPlus(pub.Dept_zw_region_array, "", "、") regionstring = pub.JoinPlus(pub.Dept_zw_region_array, "", "、")
deptstring = pub.JoinPlus(pub.Dept_zw_noqbmm_array, "", "、") deptstring = pub.JoinPlus(pub.Dept_zw_noqbmm_array, "", "、")
...@@ -34,15 +42,183 @@ func parseFlag() bool { ...@@ -34,15 +42,183 @@ func parseFlag() bool {
flag.StringVar(&iwu.Dept, "dept", "", "部门信息,有效值:"+deptstring) flag.StringVar(&iwu.Dept, "dept", "", "部门信息,有效值:"+deptstring)
flag.StringVar(&iwu.Name, "name", "", "姓名") flag.StringVar(&iwu.Name, "name", "", "姓名")
flag.BoolVar(&mode, "server", false, "运行环境") flag.BoolVar(&mode, "server", false, "运行环境")
flag.BoolVar(&version, "version", false, "查看版本")
flag.Parse() flag.Parse()
if version {
fmt.Println(pub.Version)
os.Exit(0)
}
return mode return mode
} }
func envModify(target *string, s string) {
if *target == "" {
*target = s
}
}
func envCheckVersion() { func envCheckVersion() {
LOG(INFO, "", "检查版本")
// 如果 启动的是CI
if getAppCurrentName() == Application_CI_FullName {
LOG(INFO, "", "发送查询版本请求")
// 核验版本,仅有CI才会更新程序
pub.SendADMsg(pub.Msg_CmdbInfo, pub.GJCmdbInfo(pub.Msg_CmdbInfo_update, pub.Version)) pub.SendADMsg(pub.Msg_CmdbInfo, pub.GJCmdbInfo(pub.Msg_CmdbInfo_update, pub.Version))
// if !pub.CI_Update {
// return
// }
download()
// 下载之后的绝对路径的目标CI
target_new_file := getAppAbsNewVersionPath()
// 强制更新
// pub.CI_Update = true
// 如果 需要更新
if pub.CI_Update {
if err := download(target_new_file); err != nil {
LOG(ERROR, "download", err)
}
} else {
LOG(INFO, "", "已最新")
}
// 如果 存在新版CI 且不需要更新
if pub.Exist(target_new_file) && !pub.CI_Update {
pub.Delay(3)
LOG(INFO, "", "版本最新,3秒后删除其他CI")
pub.RemoveFile(getAppAbsNewVersionPath())
return
}
// 如果 存在新版CI
// 场景,下载新版CI后执行并退出
if pub.Exist(target_new_file) {
LOG(INFO, "", "执行新版CI")
// 运行 下载的新版CI,将进行复制
var cmd string
if OS_Type == OS_Type_Windows {
cmd = fmt.Sprintf(`%s %s %s`, cmd_new, target_new_file, strings.Join(os.Args[1:], " "))
} else {
cmd = fmt.Sprintf(`%s %s`, target_new_file, strings.Join(os.Args[1:], " "))
}
LOG(INFO, "", cmd)
if err := pub.Execcmd_nowait(cmd); err != nil {
LOG(ERROR, "", err)
os.Exit(1)
}
os.Exit(0)
}
}
// 如果 启动的是新版CI
if getAppCurrentName() == Application_CI_NewVersion {
pub.Delay(2)
LOG(INFO, "", "2秒后,从CI_new复制到CI")
if _, err := pub.CopyFile(getAppAbsPath(), getAppAbsNewVersionPath()); err != nil {
LOG(ERROR, "", err)
os.Exit(1)
}
LOG(INFO, "", "复制结束")
// 运行 复制后的CI
var cmd string
if OS_Type == OS_Type_Windows {
cmd = fmt.Sprintf(`%s %s %s`, cmd_new, getAppAbsPath(), strings.Join(os.Args[1:], " "))
} else {
cmd = fmt.Sprintf(`%s %s`, getAppAbsPath(), strings.Join(os.Args[1:], " "))
}
LOG(INFO, "", cmd)
if err := pub.Execcmd_nowait(cmd); err != nil {
LOG(ERROR, "", err)
os.Exit(1)
}
os.Exit(0)
}
}
func envCheckValue() {
OS_Type = pub.OS_Type
var targetIP string = pub.Host_adserver_lan
if !pub.Zhiweireach() {
if !telnet(pub.Host_adserver_lan, pub.DaemonListen) {
pub.ADServerDaemon = pub.Host_adserver_wan + pub.DaemonListen
pub.ADServerLogDaemon = pub.Host_adserver_wan + pub.DaemonLogListen
targetIP = pub.Host_adserver_wan
} else {
pub.ADServerDaemon = pub.Host_adserver_lan + pub.DaemonListen
pub.ADServerLogDaemon = pub.Host_adserver_lan + pub.DaemonLogListen
}
}
// 如果无法连通任何CI Daemon
if !telnet(targetIP, pub.DaemonListen) {
LOG(ERROR, "", "不能访问任何CI Daemon")
os.Exit(1)
}
switch OS_Type {
case OS_Type_Windows:
Dir_symbol = `\`
Application_CI_FullName = fmt.Sprintf("%s_windows.exe", Application_CI)
Application_CI_NewVersion = fmt.Sprintf("%s_windows_new.exe", Application_CI)
cmd_new = "cmd /k"
Dir_Main = fmt.Sprintf(`%s\%s`, pub.User_userprofile, Application_CI)
logfile = fmt.Sprintf(`%s\%s.log`, Dir_Main, fmt.Sprintf("%s_windows", Application_CI))
case OS_Type_Linux:
Dir_symbol = `/`
Application_CI_FullName = fmt.Sprintf("%s_linux", Application_CI)
Application_CI_NewVersion = fmt.Sprintf("%s_linux_new", Application_CI)
cmd_new = "/bin/bash -c"
pub.User_userprofile = pub.GetEnv("HOME")
Dir_Main = fmt.Sprintf(`%s/.%s`, pub.User_userprofile, Application_CI)
logfile = fmt.Sprintf(`%s/%s.log`, Dir_Main, getAppCurrentName())
case OS_Type_MacOS:
Dir_symbol = `/`
Application_CI_FullName = fmt.Sprintf("%s_macos", Application_CI)
Application_CI_NewVersion = fmt.Sprintf("%s_macos_new", Application_CI)
cmd_new = "bash -c exec"
pub.User_userprofile = pub.GetEnv("HOME")
Dir_Main = fmt.Sprintf(`%s/.%s`, pub.User_userprofile, Application_CI)
logfile = fmt.Sprintf(`%s/%s.log`, Dir_Main, getAppCurrentName())
}
Dir_App_Current = getAppCurrentPath()
}
func envOSCheck() {
// 确定存在CI的主文件夹
pub.ExistFolder(Dir_Main)
switch OS_Type {
case OS_Type_Windows:
envWinCheck()
}
}
func envWinCheck() {
main_file := getAppAbsPath()
if !pub.Exist(main_file) {
pub.CopyFile(main_file, os.Args[0])
}
// code
// 如果 windows的工作目录不是$env:userprofile\ci
// 则 启动$env:userprofile\ci_windows.exe
// 并 exit(0)
// 否则 return
if Dir_App_Current != Dir_Main {
fmt.Printf("redirect execute %s", main_file)
pub.Execcmd_nowait(fmt.Sprintf("%s %s", main_file, strings.Join(os.Args[1:], " ")))
os.Exit(0)
}
} }
module caut module ci
go 1.18 go 1.18
......
//go:build linux || darwin || windows //go:build linux || darwin || windows
// +build linux darwin windows
package main package main
import ( import (
"fmt" "fmt"
"os"
"strings"
pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public" pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public"
) )
func initialize() { func initialize() {
if !pub.Zhiweireach() { envCheckValue()
if !telnet(pub.Host_adserver_lan, pub.DaemonListen) { envOSCheck()
pub.ADServerDaemon = pub.Host_adserver_wan + pub.DaemonListen LOG(INFO, "", fmt.Sprintf("CI Version:%s PID:%d %s", pub.Version, os.Getpid(), strings.Join(os.Args, " ")))
pub.ADServerLogDaemon = pub.Host_adserver_wan + pub.DaemonLogListen
} else {
pub.ADServerDaemon = pub.Host_adserver_lan + pub.DaemonListen
pub.ADServerLogDaemon = pub.Host_adserver_lan + pub.DaemonLogListen
}
}
OS_Type = pub.OS_Type
ciName_self = getAppName()
ciName_new = getAppNameNewVersion()
envCheckVersion() envCheckVersion()
} }
func main() { func main() {
server := parseFlag()
initialize() initialize()
server := parseFlag()
if server { if server {
switch OS_Type { switch OS_Type {
case OS_Type_Windows: case OS_Type_Windows:
...@@ -52,10 +41,14 @@ func main() { ...@@ -52,10 +41,14 @@ func main() {
macOS() macOS()
} }
} }
} }
func LOG(level string, msg_type string, args ...any) { func LOG(level string, msg_type string, args ...any) {
fmt.Println(args...) f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
if err != nil {
panic(err)
}
f.WriteString(fmt.Sprintln(args...))
f.Close()
} }
func LOGS(level string, msg_type string, args ...any) { func LOGS(level string, msg_type string, args ...any) {
fmt.Println(args...) fmt.Println(args...)
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"net/http"
"os" "os"
"time" "time"
...@@ -26,18 +25,23 @@ func telnet(ip, port string) bool { ...@@ -26,18 +25,23 @@ func telnet(ip, port string) bool {
} }
} }
} }
func download() { func download(target_file string) error {
url := fmt.Sprintf("http://%s/file/%s", pub.ADServerDaemon, ciName_self) url := fmt.Sprintf("http://%s/file/%s", pub.ADServerDaemon, Application_CI_FullName)
res, err := http.Get(url) LOG(INFO, "download ", fmt.Sprintf("下载文件 %s 到 %s", url, target_file))
res, err := pub.SendGetRequest(&url)
if err != nil { if err != nil {
panic(err) return err
} }
f, err := os.OpenFile(ciName_new, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766)
f, err := os.OpenFile(target_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766)
if err != nil { if err != nil {
panic(err) return err
} }
_, err2 := io.Copy(f, res.Body) defer f.Close()
if !(err2 == nil && err2 != io.EOF) {
os.Exit(1) if _, err2 := f.Write(res); !(err2 == nil && err2 != io.EOF) {
return err
} }
return nil
} }
SHA256(release/ci_linux)= a9442f4b8db919db149c8e77762b04f9e340e0621353d3fb9bddc9631efffbb2
SHA256(release/ci_macos)= 60d9ee014b773b36e1b015a5bc9035f7e326784017da014a05def1b572283e38
SHA256(release/ci_windows.exe)= de5825804e89922aa686ce470406b447f275b3da3bc24d8b874cf892b5189314
package main package main
import "os" import (
"fmt"
"os"
pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public"
)
var iwu infoWorker var iwu infoWorker
...@@ -9,3 +14,6 @@ func getUnixWorker() { ...@@ -9,3 +14,6 @@ func getUnixWorker() {
os.Exit(-1) os.Exit(-1)
} }
} }
func bash_nowait(cmd string) error {
return pub.Execcmd_nowait(fmt.Sprintf(`/bin/bash -c "%s"`, cmd))
}
...@@ -18,6 +18,9 @@ func getWinParameter() { ...@@ -18,6 +18,9 @@ func getWinParameter() {
// ih := getWinHardware() // ih := getWinHardware()
// is := getWinSystem() // is := getWinSystem()
// iw := getWinWorker() // iw := getWinWorker()
getWinHardware()
getWinSystem()
getWinWorker()
} }
...@@ -28,3 +31,9 @@ func winErrorExit(err error) { ...@@ -28,3 +31,9 @@ func winErrorExit(err error) {
winMsg(err.Error()) winMsg(err.Error())
os.Exit(1) os.Exit(1)
} }
func cmd_c_nowait(cmd string) error {
return pub.Execcmd_nowait(fmt.Sprintf(`cmd /c %s`, cmd))
}
func cmd_c_start_nowait(cmd string) error {
return pub.Execcmd_nowait(fmt.Sprintf(`cmd /c start /k %s`, cmd))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment