// Update project main.go
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
)
func Exec(cmd string) (output []string, err error) {
_output, err := exec.Command("/bin/bash", "-c", cmd).Output()
output = strings.Split(string(_output), "\n")
return
}
func Run(cmd string) (err error) {
err = exec.Command("/bin/bash", "-c", cmd).Run()
return
}
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
}
//版本升級(jí)
func Version_up(s string) {
var cmd string
var version string
hpath := "/root/eGW/.version"
uppath := "/root/eGW/.version/update"
bakpath := "/root/eGW/.version/backup"
//判斷此次升級(jí)和上次升級(jí)的版本是否一致,不一致更新版本號(hào)
cmd = "ls" + " " + uppath
_filename, _ := Exec(cmd)
filename := _filename[0]
if s != filename {
tmp := strings.Split(filename, ".tar.gz")
version = tmp[0]
} else {
version = Version_check()
}
if exist, _ := PathExists(s); exist {
//如果版本升級(jí)文件夾不存在就創(chuàng)建
if exist, _ := PathExists(hpath); exist {
//清空文件夾
cmd = "rm" + " " + "-rf" + " " + hpath + "/*"
Run(cmd)
}
//重新創(chuàng)建文件夾
cmd = "mkdir" + " " + "-p" + " " + uppath
Run(cmd)
cmd = "mkdir" + " " + "-p" + " " + bakpath
Run(cmd)
//備份版本號(hào)
cmd = "touch" + " " + hpath + "/" + version + ".ver"
Run(cmd)
//備份版本文件
cmd = "cp" + " " + s + " " + uppath
Run(cmd)
//解壓版本文件
cmd = "tar" + " " + "-zxvf" + " " + s + " " + "-C" + " " + uppath
Run(cmd)
//獲取版本文件名
cmd = "ls" + " " + uppath
_foldname, _ := Exec(cmd)
foldname := _foldname[0]
//獲取版本內(nèi)所有文件名
cmd = "ls" + " " + uppath + "/" + foldname
filenames, _ := Exec(cmd)
//升級(jí)各個(gè)文件
var update_flag bool = false
for i := 0; i < len(filenames)-1; i++ {
//如果lccmd文件存在則升級(jí)lccmd
switch filenames[i] {
case "lccmd":
//升級(jí)前查看lccmd md5
cmd = "md5sum /usr/sbin/lccmd"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + uppath + "/" + foldname + "/lccmd"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
//升級(jí)標(biāo)志
update_flag = true
//備份原lccmd文件
cmd = "mv" + " " + "/usr/sbin/lccmd" + " " + bakpath
Run(cmd)
//復(fù)制文件到對(duì)應(yīng)目錄并添加執(zhí)行權(quán)限
cmd = "cp" + " " + "-rf" + " " + uppath + "/" + foldname + "/lccmd" + " " + "/usr/sbin/"
Run(cmd)
cmd = "chmod +x /usr/sbin/lccmd"
Run(cmd)
//打印升級(jí)信息
fmt.Println("lccmd is update!")
fmt.Println("升級(jí)前MD5:", output1[0])
fmt.Println("升級(jí)后MD5:", output2[0])
}
case "ltegwd":
//升級(jí)前查看ltegwd md5
cmd = "md5sum /root/eGW/ltegwd"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + uppath + "/" + foldname + "/ltegwd"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
//升級(jí)標(biāo)志
update_flag = true
//備份原ltegwd文件
cmd = "mv" + " " + "/root/eGW/ltegwd" + " " + bakpath
Run(cmd)
//復(fù)制文件到對(duì)應(yīng)目錄并添加執(zhí)行權(quán)限
cmd = "cp" + " " + "-rf" + " " + uppath + "/" + foldname + "/ltegwd" + " " + "/root/eGW/"
Run(cmd)
cmd = "chmod +x /root/eGW/ltegwd"
Run(cmd)
//打印升級(jí)信息
fmt.Println("ltegwd is update!")
fmt.Println("升級(jí)前MD5:", output1[0])
fmt.Println("升級(jí)后MD5:", output2[0])
}
case "gtp-relay.ko":
//升級(jí)前查看gtp-relay.ko md5
cmd = "md5sum /root/eGW/gtp-relay.ko"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + uppath + "/" + foldname + "/ltegwd"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
//升級(jí)標(biāo)志
update_flag = true
//備份原文件
cmd = "mv" + " " + "/root/eGW/gtp-relay.ko" + " " + bakpath
Run(cmd)
//復(fù)制文件到對(duì)應(yīng)目錄并添加執(zhí)行權(quán)限
cmd = "cp" + " " + "-rf" + " " + uppath + "/" + foldname + "/gtp-relay.ko" + " " + "/root/eGW/"
Run(cmd)
//cmd = "chmod +x /root/eGW/gtp-relay.ko "
//Run(cmd)
//打印升級(jí)信息
fmt.Println("gtp-relay.ko is update!")
fmt.Println("升級(jí)前MD5:", output1[0])
fmt.Println("升級(jí)后MD5:", output2[0])
}
default:
fmt.Println("no file to update!")
}
}
//如果有文件升級(jí),則重啟服務(wù)
if update_flag {
//重啟服務(wù)
cmd = "systemctl restart monitor"
Run(cmd)
} else {
fmt.Println("no file need to update!")
}
//刪除安裝文件夾
cmd = "rm" + " " + "-rf" + " " + uppath + "/" + foldname
Run(cmd)
} else {
fmt.Println(s, " is not exist!")
}
}
//版本回退
func Version_back() {
var cmd string
uppath := "/root/eGW/.version/update"
bakpath := "/root/eGW/.version/backup"
//刪除.tar.gz以讀取備份版本號(hào)
cmd = "rm" + " " + "-rf" + " " + uppath + "/*"
Run(cmd)
//獲取回退文件夾內(nèi)所有文件名
cmd = "ls" + " " + bakpath
filenames, _ := Exec(cmd)
for i := 0; i < len(filenames)-1; i++ {
switch filenames[i] {
case "lccmd":
//回退前查看lccmd md5
cmd = "md5sum /usr/sbin/lccmd"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + bakpath + "/lccmd"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
cmd = "cp" + " " + "-rf" + " " + bakpath + "/lccmd" + " " + "/usr/sbin/"
Run(cmd)
fmt.Println("lccmd is back!")
fmt.Println("回退前MD5:", output1[0])
fmt.Println("回退后MD5:", output2[0])
}
case "ltegwd":
//回退前查看ltegwd md5
cmd = "md5sum /root/eGW/ltegwd"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + bakpath + "/ltegwd"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
cmd = "cp" + " " + "-rf" + " " + bakpath + "/ltegwd" + " " + "/root/eGW/"
Run(cmd)
fmt.Println("ltegwd is back!")
fmt.Println("回退前MD5:", output1[0])
fmt.Println("回退后MD5:", output2[0])
}
case "gtp-relay.ko":
//回退前查看gtp-relay.ko md5
cmd = "md5sum /root/eGW/gtp-relay.ko"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + bakpath + "/gtp-relay.ko"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
cmd = "cp" + " " + "-rf" + " " + bakpath + "/gtp-relay.ko" + " " + "/root/eGW/"
Run(cmd)
fmt.Println("gtp-relay.ko is back!")
fmt.Println("回退前MD5:", output1[0])
fmt.Println("回退后MD5:", output2[0])
}
default:
fmt.Println("no file to back update!")
}
}
}
//版本查詢(xún)
func Version_check() string {
var cmd string
version := "unknown"
var v1, v2, v3 bool = true, true, true
hpath := "/root/eGW/.version"
uppath := "/root/eGW/.version/update"
if exist, _ := PathExists(uppath); exist {
cmd = "tar" + " " + "-Pzxvf" + " " + uppath + "/*.tar.gz" + " " + "-C" + " " + uppath
Run(cmd)
cmd = "ls " + uppath
_foldname, _ := Exec(cmd)
if _foldname[0] != "" {
foldname := _foldname[0]
cmd = "ls " + uppath + "/" + foldname
filenames, _ := Exec(cmd)
if len(filenames) > 0 {
for _, v := range filenames {
if v == "lccmd" {
cmd = "md5sum /usr/sbin/lccmd"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + uppath + "/" + foldname + "/lccmd"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
v1 = false
}
}
if v == "ltegwd" {
cmd = "md5sum /root/eGW/ltegwd"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + uppath + "/" + foldname + "/ltegwd"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
v2 = false
}
}
if v == "ltegwd" {
cmd = "md5sum /root/eGW/gtp-relay.ko"
_output1, _ := Exec(cmd)
output1 := strings.Split(_output1[0], " ")
cmd = "md5sum" + " " + uppath + "/" + foldname + "/gtp-relay.ko"
_output2, _ := Exec(cmd)
output2 := strings.Split(_output2[0], " ")
if output1[0] != output2[0] {
v3 = false
}
}
}
if v1 && v2 && v3 {
version = foldname
//fmt.Println("version:", version)
} else {
//fmt.Println("version:", "unknown!")
}
} else {
version = "unknown"
}
cmd = "rm" + " " + "-rf" + " " + uppath + "/" + foldname
Run(cmd)
} else {
//
cmd = "ls" + " " + hpath + "/*.ver"
_version, _ := Exec(cmd)
if _version[0] != "" {
_tmp := strings.Split(_version[0], "/")
tmp := strings.Split(_tmp[4], ".ver")
version = tmp[0]
//fmt.Println(_version[0])
}
}
} else {
//fmt.Println("can not get the version!")
}
return version
}
func main() {
var u string
flag.StringVar(&u, "u", "", "update version")
//flag.String("c", "", "check version")
flag.Parse()
switch {
case u != "last" && u != "":
Version_up(u)
case u == "last":
Version_back()
default:
v := Version_check()
fmt.Println("version:", v)
}
}
GO: 版本升級(jí)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 寫(xiě)在之前 從5.1切換到5.7,主要由mysql-server、mysql-devel、mysql-libs三部分...
- “個(gè)人魅力”重要嗎?或許你會(huì)說(shuō):“毋庸置疑!” 公司領(lǐng)導(dǎo)講求個(gè)人魅力,才能創(chuàng)造不一樣的公司文化,才能深入人心,才能...
- 一、概述 關(guān)于電子書(shū), 主要還是細(xì)節(jié)的處理, 懵逼, 做到最后直接集成現(xiàn)成的 LSYReader 庫(kù) 主要支持的格...