// 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!")
}
}
}
//版本查詢
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)系作者
- 文/潘曉璐 我一進(jìn)店門城瞎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)渤闷,“玉大人,你說(shuō)我怎么就攤上這事脖镀§” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵蜒灰,是天一觀的道長(zhǎng)弦蹂。 經(jīng)常有香客問(wèn)我,道長(zhǎng)强窖,這世上最難降的妖魔是什么凸椿? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮翅溺,結(jié)果婚禮上脑漫,老公的妹妹穿的比我還像新娘。我一直安慰自己咙崎,他們只是感情好优幸,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著褪猛,像睡著了一般网杆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上伊滋,一...
- 那天碳却,我揣著相機(jī)與錄音,去河邊找鬼笑旺。 笑死昼浦,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的筒主。 我是一名探鬼主播座柱,決...
- 文/蒼蘭香墨 我猛地睜開眼迷帜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了色洞?” 一聲冷哼從身側(cè)響起戏锹,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎火诸,沒(méi)想到半個(gè)月后锦针,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡置蜀,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年奈搜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盯荤。...
- 正文 年R本政府宣布灼卢,位于F島的核電站绍哎,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鞋真。R本人自食惡果不足惜崇堰,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望涩咖。 院中可真熱鬧海诲,春花似錦、人聲如沸檩互。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)盾似。三九已至敬辣,卻和暖如春雪标,著一層夾襖步出監(jiān)牢的瞬間零院,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓嵌牺,卻偏偏與公主長(zhǎng)得像打洼,于是被迫代替她去往敵國(guó)和親龄糊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 寫在之前 從5.1切換到5.7募疮,主要由mysql-server炫惩、mysql-devel、mysql-libs三部分...
- “個(gè)人魅力”重要嗎筋蓖?或許你會(huì)說(shuō):“毋庸置疑!” 公司領(lǐng)導(dǎo)講求個(gè)人魅力退敦,才能創(chuàng)造不一樣的公司文化粘咖,才能深入人心,才能...
- 一侈百、概述 關(guān)于電子書瓮下, 主要還是細(xì)節(jié)的處理, 懵逼设哗, 做到最后直接集成現(xiàn)成的 LSYReader 庫(kù) 主要支持的格...