cloud-control-panel/upgrade.go

108 lines
3.5 KiB
Go

package main
/*
Check to see if there is a new version to upgrade
*/
import "log"
import "os"
import "time"
import "runtime"
import "git.wit.org/wit/shell"
// import "github.com/davecgh/go-spew/spew"
func upgrade() {
filename := "cloud-control-panel"
if runtime.GOOS == "windows" {
filename = "cloud-control-panel.exe"
}
mirrors := "https://mirrors.wit.org/cloud/control-panel/"
upstreamVERSION := shell.Chomp(shell.Wget(mirrors + "VERSION"))
dir := "/v" + upstreamVERSION + "/linux/"
// TODO: confirm this is correct for MacOS and Windows
if runtime.GOOS == "linux" {
log.Println("upgrade() OS: Linux")
dir = "/v" + upstreamVERSION + "/linux/"
} else if runtime.GOOS == "windows" {
log.Println("upgrade() OS: Windows")
dir = "/v" + upstreamVERSION + "/windows/"
} else if runtime.GOOS == "darwin" {
log.Println("upgrade() OS: " + runtime.GOOS)
dir = "/v" + upstreamVERSION + "/macos/"
}
filenameURL := mirrors + dir + filename
builddateURL := mirrors + dir + "BUILDDATE"
mirrorsBUILDDATE := shell.Chomp(shell.Wget(builddateURL))
epoch := time.Now().Unix()
myBUILDDATE, _ := packrBox.FindString("build.DATE")
myBUILDDATE = shell.Chomp(myBUILDDATE)
myBUILDVERSION, _ := packrBox.FindString("VERSION")
myBUILDVERSION = shell.Chomp(myBUILDVERSION)
config.Version = myBUILDVERSION
config.Builddate = shell.Int64(myBUILDDATE)
config.Goversion = runtime.Version()
log.Println()
log.Println("upstreamVERSION =", upstreamVERSION)
log.Println("builddateURL =", builddateURL)
log.Println("now() =", epoch)
log.Println("myBUILDVERSION =", myBUILDVERSION)
log.Println("myBUILDDATE =", myBUILDDATE)
log.Println("mirrorsBUILDDATE =", mirrorsBUILDDATE)
log.Println()
log.Println("config.Dirty =", config.Dirty)
log.Println("config.Version =", config.Version)
log.Println("config.Goversion =", config.Goversion)
log.Println()
log.Println()
log.Println("Number of CPUs =", runtime.NumCPU())
log.Println("Number of GoRoutines =", runtime.NumGoroutine())
log.Println("runtime.GOARCH =", runtime.GOARCH)
log.Println()
// https://github.com/go-cmd/cmd/issues/20
// TODO: look at go-cmd ?
log.Println("update() compare", mirrorsBUILDDATE, "to", myBUILDDATE)
if (! config.Dirty) {
// See if a newer version of this control panel exists
// if (epoch > time.now().Unix()) // seconds since epoch
if (shell.Int(mirrorsBUILDDATE) > shell.Int(myBUILDDATE)) {
log.Println("update() THERE IS A NEW UPSTREAM VERSION !!!")
log.Println("update() THERE IS A NEW UPSTREAM VERSION !!!")
log.Println("update() THERE IS A NEW UPSTREAM VERSION !!!")
time.Sleep(time.Second * 2)
shell.WgetToFile("/tmp/" + filename, filenameURL)
shell.Run("chmod +x /tmp/" + filename)
// execute the new version here and never return
// TODO: figure out how to nohup here and properly exit
shell.Exec("/tmp/" + filename)
os.Exit(0)
} else {
log.Println("update() UPSTREAM VERSION IS OLDER THAN THIS")
}
} else {
if (shell.Int(mirrorsBUILDDATE) > shell.Int(myBUILDDATE)) {
log.Println("update() dirty=true THERE IS A NEW UPSTREAM VERSION !!!")
log.Println("update() dirty=true THERE IS A NEW UPSTREAM VERSION !!!")
log.Println("update() dirty=true THERE IS A NEW UPSTREAM VERSION !!!")
time.Sleep(time.Second * 2)
} else {
log.Println("update() dirty=true UPSTREAM VERSION IS OLDER THAN THIS")
}
}
time.Sleep(time.Second)
// for {}
}