GO

GO: 版本升级

2017-12-16  本文已影响1321人  随风化作雨
// 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
}

//版本升级
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"

    //判断此次升级和上次升级的版本是否一致,不一致更新版本号
    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 {
        //如果版本升级文件夹不存在就创建
        if exist, _ := PathExists(hpath); exist {
            //清空文件夹
            cmd = "rm" + " " + "-rf" + " " + hpath + "/*"
            Run(cmd)
        }
        //重新创建文件夹
        cmd = "mkdir" + " " + "-p" + " " + uppath
        Run(cmd)
        cmd = "mkdir" + " " + "-p" + " " + bakpath
        Run(cmd)

        //备份版本号
        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]
        //获取版本内所有文件名
        cmd = "ls" + " " + uppath + "/" + foldname
        filenames, _ := Exec(cmd)
        //升级各个文件
        var update_flag bool = false
        for i := 0; i < len(filenames)-1; i++ {
            //如果lccmd文件存在则升级lccmd
            switch filenames[i] {
            case "lccmd":
                //升级前查看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] {
                    //升级标志
                    update_flag = true
                    //备份原lccmd文件
                    cmd = "mv" + " " + "/usr/sbin/lccmd" + " " + bakpath
                    Run(cmd)

                    //复制文件到对应目录并添加执行权限
                    cmd = "cp" + " " + "-rf" + " " + uppath + "/" + foldname + "/lccmd" + " " + "/usr/sbin/"
                    Run(cmd)
                    cmd = "chmod +x /usr/sbin/lccmd"
                    Run(cmd)

                    //打印升级信息
                    fmt.Println("lccmd is update!")
                    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" + " " + uppath + "/" + foldname + "/ltegwd"
                _output2, _ := Exec(cmd)
                output2 := strings.Split(_output2[0], " ")

                if output1[0] != output2[0] {
                    //升级标志
                    update_flag = true
                    //备份原ltegwd文件
                    cmd = "mv" + " " + "/root/eGW/ltegwd" + " " + bakpath
                    Run(cmd)

                    //复制文件到对应目录并添加执行权限
                    cmd = "cp" + " " + "-rf" + " " + uppath + "/" + foldname + "/ltegwd" + " " + "/root/eGW/"
                    Run(cmd)
                    cmd = "chmod +x /root/eGW/ltegwd"
                    Run(cmd)

                    //打印升级信息
                    fmt.Println("ltegwd is update!")
                    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" + " " + uppath + "/" + foldname + "/ltegwd"
                _output2, _ := Exec(cmd)
                output2 := strings.Split(_output2[0], " ")

                if output1[0] != output2[0] {
                    //升级标志
                    update_flag = true
                    //备份原文件
                    cmd = "mv" + " " + "/root/eGW/gtp-relay.ko" + " " + bakpath
                    Run(cmd)

                    //复制文件到对应目录并添加执行权限
                    cmd = "cp" + " " + "-rf" + " " + uppath + "/" + foldname + "/gtp-relay.ko" + " " + "/root/eGW/"
                    Run(cmd)
                    //cmd = "chmod +x /root/eGW/gtp-relay.ko "
                    //Run(cmd)

                    //打印升级信息
                    fmt.Println("gtp-relay.ko is update!")
                    fmt.Println("升级前MD5:", output1[0])
                    fmt.Println("升级后MD5:", output2[0])
                }

            default:
                fmt.Println("no file to update!")
            }
        }
        //如果有文件升级,则重启服务
        if update_flag {
            //重启服务
            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以读取备份版本号
    cmd = "rm" + " " + "-rf" + " " + uppath + "/*"
    Run(cmd)

    //获取回退文件夹内所有文件名
    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)
    }
}

上一篇下一篇

猜你喜欢

热点阅读