Skip to content

Commit

Permalink
Check for new versions and upgrade from webgui. closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
hakwerk committed Apr 13, 2020
1 parent a2f50c3 commit 5d23559
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 41 deletions.
8 changes: 3 additions & 5 deletions commander
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,13 @@ case $txt in
"server-shutdown")
halt
;;
"version-update")
/home/labca/labca/install &>>$LOGFILE
;;
*)
echo "Unknown command '$txt'. ERROR!"
exit 1
;;
esac

echo "ok"


# TODO:
# upgrade the labca stuff

102 changes: 90 additions & 12 deletions gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
Expand All @@ -11,7 +12,9 @@ import (
"errors"
"fmt"
"github.com/biz/templates"
"github.com/dustin/go-humanize"
_ "github.com/go-sql-driver/mysql"
"github.com/google/go-github/github"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
Expand Down Expand Up @@ -39,20 +42,23 @@ import (
)

const (
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
updateInterval = 24 * time.Hour
)

var (
appSession *sessions.Session
restartSecret string
sessionStore *sessions.CookieStore
tmpls *templates.Templates
version string
dbConn string
dbType string
isDev bool
appSession *sessions.Session
restartSecret string
sessionStore *sessions.CookieStore
tmpls *templates.Templates
version string
dbConn string
dbType string
isDev bool
updateAvailable bool
updateChecked time.Time

upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
Expand Down Expand Up @@ -232,6 +238,43 @@ func errorHandler(w http.ResponseWriter, r *http.Request, err error, status int)
}
}

func checkUpdates(forced bool) ([]string, []string) {
var versions []string
var descriptions []string

if forced || updateChecked.Add(updateInterval).Before(time.Now()) {
latest := ""
newer := true

client := github.NewClient(nil)

if releases, _, err := client.Repositories.ListReleases(context.Background(), "hakwerk", "labca", nil); err == nil {
for i := 0; i < len(releases); i++ {
release := releases[i]
if !*release.Draft {
if !*release.Prerelease || isDev {
if latest == "" {
latest = *release.Name
}
if *release.Name == version {
newer = false
}
if newer {
versions = append(versions, *release.Name)
descriptions = append(descriptions, *release.Body)
}
}
}
}

updateChecked = time.Now()
updateAvailable = (len(releases) > 0) && (latest != version)
}
}

return versions, descriptions
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
if !viper.GetBool("config.complete") {
http.Redirect(w, r, r.Header.Get("X-Request-Base")+"/setup", http.StatusFound)
Expand All @@ -240,6 +283,10 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {

dashboardData, err := CollectDashboardData(w, r)
if err == nil {
checkUpdates(false)
dashboardData["UpdateAvailable"] = updateAvailable
dashboardData["UpdateChecked"] = humanize.RelTime(updateChecked, time.Now(), "", "")

render(w, r, "dashboard", dashboardData)
}
}
Expand Down Expand Up @@ -798,6 +845,24 @@ func (res *Result) ManageComponents(w http.ResponseWriter, r *http.Request, acti
}
}

func _checkUpdatesHandler(w http.ResponseWriter, r *http.Request) {
res := struct {
Success bool
UpdateAvailable bool
UpdateChecked string
Versions []string
Descriptions []string
Errors map[string]string
}{Success: true, Errors: make(map[string]string)}

res.Versions, res.Descriptions = checkUpdates(true)
res.UpdateAvailable = updateAvailable
res.UpdateChecked = humanize.RelTime(updateChecked, time.Now(), "", "")

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
}

func _managePostDispatch(w http.ResponseWriter, r *http.Request, action string) bool {
if action == "backup-restore" || action == "backup-delete" || action == "backup-now" {
_backupHandler(w, r)
Expand Down Expand Up @@ -829,6 +894,11 @@ func _managePostDispatch(w http.ResponseWriter, r *http.Request, action string)
return true
}

if action == "version-check" {
_checkUpdatesHandler(w, r)
return true
}

return false
}

Expand Down Expand Up @@ -858,6 +928,8 @@ func _managePost(w http.ResponseWriter, r *http.Request) {
"update-config",
"update-email",
"send-email",
"version-check",
"version-update",
} {
if a == action {
actionKnown = true
Expand All @@ -878,7 +950,7 @@ func _managePost(w http.ResponseWriter, r *http.Request) {
res.Message = "Command failed - see LabCA log for any details"
}

if action != "server-restart" && action != "server-shutdown" {
if action != "server-restart" && action != "server-shutdown" && action != "version-update" {
res.ManageComponents(w, r, action)
}

Expand All @@ -890,6 +962,10 @@ func _manageGet(w http.ResponseWriter, r *http.Request) {
manageData := make(map[string]interface{})
manageData["RequestBase"] = r.Header.Get("X-Request-Base")

checkUpdates(false)
manageData["UpdateAvailable"] = updateAvailable
manageData["UpdateChecked"] = humanize.RelTime(updateChecked, time.Now(), "", "")

components := _parseComponents(getLog(w, r, "components"))
for i := 0; i < len(components); i++ {
if components[i].Name == "NGINX Webserver" {
Expand Down Expand Up @@ -2292,6 +2368,8 @@ func init() {
dbType = viper.GetString("db.type")

version = viper.GetString("version")

updateAvailable = false
}

func main() {
Expand Down
1 change: 1 addition & 0 deletions gui/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if [ ! -e bin/labca ]; then
go get github.com/biz/templates
go get github.com/go-sql-driver/mysql
go get github.com/dustin/go-humanize
go get github.com/google/go-github/github
go get github.com/gorilla/mux
go get github.com/gorilla/securecookie
go get github.com/gorilla/sessions
Expand Down
8 changes: 7 additions & 1 deletion gui/templates/views/dashboard.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-fw fa-desktop"></i> System Overview
<span class="pull-left"><i class="fa fa-fw fa-desktop"></i> System Overview</span>
{{ if .UpdateAvailable }}
<span class="pull-right">
<a class="update" href="{{ .RequestBase }}/manage"><i class="fa fa-fw fa-rocket"></i> <i>update available!</i></a>
</span>
{{ end }}
<div class="clearfix"></div>
</div>
<div class="panel-body">
<div class="table-responsive">
Expand Down
Loading

0 comments on commit 5d23559

Please sign in to comment.