GO: 版本升级2

// Update project main.go
package main

import (
    "bufio"
    "flag"
    "fmt"
    "io"
    "io/ioutil"
    "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
    verpath := "/root/eGW/.version/versions"
    uppath := "/root/eGW/.version/update"
    bakpath := "/root/eGW/.version/backup"

    if exist, _ := PathExists(s); exist {
        //如果文件夹不存在就创建
        if exist, _ := PathExists(uppath); exist {
            //清空文件夹
            cmd = "rm" + " " + "-rf" + " " + uppath + "/*"
            Run(cmd)
        } else {
            //重新创建文件夹
            cmd = "mkdir" + " " + "-p" + " " + uppath
            Run(cmd)
        }
        if exist, _ := PathExists(bakpath); exist {
            //清空文件夹
            cmd = "rm" + " " + "-rf" + " " + bakpath + "/*"
            Run(cmd)
        } else {
            cmd = "mkdir" + " " + "-p" + " " + bakpath
            Run(cmd)
        }
        if exist, _ := PathExists(verpath); exist {

        } else {
            cmd = "mkdir" + " " + "-p" + " " + verpath
            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]
        //创建版本号文件
        verfile := verpath + "/" + foldname + ".ver"
        if exist, _ := PathExists(verfile); exist {
            cmd = "rm" + " " + "-rf" + " " + verfile
            Run(cmd)
            cmd = "touch" + " " + verfile
            Run(cmd)
        } else {
            cmd = "touch" + " " + verfile
            Run(cmd)
        }
        //获取版本内所有文件名
        cmd = "ls" + " " + uppath + "/" + foldname
        filenames, _ := Exec(cmd)
        //fmt.Println("filenames:", filenames)
        //升级各个文件
        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], " ")
                //fmt.Println(output1)
                cmd = "md5sum" + " " + uppath + "/" + foldname + "/lccmd"
                _output2, _ := Exec(cmd)
                output2 := strings.Split(_output2[0], " ")
                //fmt.Println(output2)

                //写入版本号
                cmd = "echo" + " " + "lccmd:" + output2[0] + " " + ">>" + verfile
                //fmt.Println(cmd)
                Run(cmd)

                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], " ")

                //写入版本号
                cmd = "echo" + " " + "ltegwd:" + output2[0] + " " + ">>" + verfile
                Run(cmd)

                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 + "/gtp-relay.ko"
                _output2, _ := Exec(cmd)
                output2 := strings.Split(_output2[0], " ")

                //写入版本号
                cmd = "echo" + " " + "gtp-relay.ko:" + output2[0] + " " + ">>" + verfile
                Run(cmd)

                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
    bakpath := "/root/eGW/.version/backup"

    //获取回退文件夹内所有文件名
    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
    var version []string
    //version := "unknown"
    verpath := "/root/eGW/.version/versions"
    //ver := make(map[string]string)
    //var vers []map[string]string
    vers := make(map[string]map[string]string)

    files, _ := ioutil.ReadDir(verpath)
    //path, _ := os.Getwd()
    //fmt.Println(path)
    for _, file := range files {
        ver := make(map[string]string)
        if file.IsDir() {
            continue
        } else {
            //fmt.Println(file.Name())
            fname := file.Name()
            //fmt.Println(fname)
            f, err := os.Open(verpath + "/" + fname)
            if err != nil {
                fmt.Println(err)
            }
            defer f.Close()
            rd := bufio.NewReader(f)
            for {
                line, err := rd.ReadString('\n')
                if err != nil || io.EOF == err {
                    break
                }
                //fmt.Println("line:", line)
                name := strings.Split(line, ":")
                tmp := strings.Split(name[1], "\n")
                ver[name[0]] = tmp[0]
                //fmt.Println("ver:", ver)
                vers[fname] = ver

            }
            //vers[fname] = ver
        }

    }
    //fmt.Println(vers)

    //var ver_flag bool = true
    cmd = "md5sum /usr/sbin/lccmd"
    output, _ := Exec(cmd)
    _md5_lccmd := strings.Split(output[0], " ")
    md5_lccmd := _md5_lccmd[0]

    cmd = "md5sum /root/eGW/ltegwd"
    output, _ = Exec(cmd)
    _md5_ltegwd := strings.Split(output[0], " ")
    md5_ltegwd := _md5_ltegwd[0]

    cmd = "md5sum /root/eGW/gtp-relay.ko"
    output, _ = Exec(cmd)
    _md5_gtp_relay := strings.Split(output[0], " ")
    md5_gtp_relay := _md5_gtp_relay[0]

    for k, v := range vers {
        if v["lccmd"] != "" && v["lccmd"] != md5_lccmd {
            //fmt.Println("lccmd is not ok!")
            //fmt.Println(v["lccmd"])
            //fmt.Println(md5_lccmd)
            //break
            continue
        } else {
            if v["ltegwd"] != "" && v["ltegwd"] != md5_ltegwd {
                //fmt.Println("ltegwd is not ok!")
                //break
                continue
            } else {
                if v["gtp-relay.ko"] != "" && v["gtp-relay.ko"] != md5_gtp_relay {
                    //fmt.Println("gtp-relay.ko is not ok!")
                    fmt.Println("can not get the version!")
                    //break
                    continue
                } else {
                    //fmt.Println(k)
                    kk := strings.Split(k, ".ver")
                    version = append(version, kk[0])
                }
            }
        }
    }
    if len(version) == 0 {
        version = append(version, "unknown")
    }
    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 != "":
        fmt.Println(strings.Repeat("*", 80))
        Version_up(u)
        fmt.Println(strings.Repeat("*", 80))
    case u == "last":
        fmt.Println(strings.Repeat("*", 80))
        Version_back()
        fmt.Println(strings.Repeat("*", 80))
    default:
        v := Version_check()
        fmt.Println(strings.Repeat("*", 80))
        fmt.Println("version:", v)
        fmt.Println(strings.Repeat("*", 80))
    }
}

你可能感兴趣的:(GO: 版本升级2)