mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-18 14:47:03 +00:00
chasquid: Include build and version information
This patch adds the possibility of setting build and version information, which will be exported and displayed in the monitoring http server.
This commit is contained in:
67
chasquid.go
67
chasquid.go
@@ -1,14 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"expvar"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -30,11 +33,35 @@ import (
|
|||||||
var (
|
var (
|
||||||
configDir = flag.String("config_dir", "/etc/chasquid",
|
configDir = flag.String("config_dir", "/etc/chasquid",
|
||||||
"configuration directory")
|
"configuration directory")
|
||||||
|
showVer = flag.Bool("version", false, "show version and exit")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Build information, overridden at build time using
|
||||||
|
// -ldflags="-X main.version=blah".
|
||||||
|
var (
|
||||||
|
version = "undefined"
|
||||||
|
sourceDateTs = "0"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
versionVar = expvar.NewString("chasquid/version")
|
||||||
|
|
||||||
|
sourceDate time.Time
|
||||||
|
sourceDateVar = expvar.NewString("chasquid/sourceDateStr")
|
||||||
|
sourceDateTsVar = expvar.NewInt("chasquid/sourceDateTimestamp")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
parseVersionInfo()
|
||||||
|
if *showVer {
|
||||||
|
fmt.Printf("chasquid %s (source date: %s)\n", version, sourceDate)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
glog.Infof("chasquid starting (version %s)", version)
|
||||||
|
|
||||||
setupSignalHandling()
|
setupSignalHandling()
|
||||||
|
|
||||||
defer glog.Flush()
|
defer glog.Flush()
|
||||||
@@ -223,15 +250,40 @@ func mustReadDir(path string) []os.FileInfo {
|
|||||||
return dirs
|
return dirs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseVersionInfo() {
|
||||||
|
versionVar.Set(version)
|
||||||
|
|
||||||
|
sdts, err := strconv.ParseInt(sourceDateTs, 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceDate = time.Unix(sdts, 0)
|
||||||
|
sourceDateVar.Set(sourceDate.Format("2006-01-02 15:04:05 -0700"))
|
||||||
|
sourceDateTsVar.Set(sdts)
|
||||||
|
}
|
||||||
|
|
||||||
func launchMonitoringServer(addr string) {
|
func launchMonitoringServer(addr string) {
|
||||||
glog.Infof("Monitoring HTTP server listening on %s", addr)
|
glog.Infof("Monitoring HTTP server listening on %s", addr)
|
||||||
|
|
||||||
|
indexData := struct {
|
||||||
|
Version string
|
||||||
|
SourceDate time.Time
|
||||||
|
StartTime time.Time
|
||||||
|
}{
|
||||||
|
Version: version,
|
||||||
|
SourceDate: sourceDate,
|
||||||
|
StartTime: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Write([]byte(monitoringHTMLIndex))
|
if err := monitoringHTMLIndex.Execute(w, indexData); err != nil {
|
||||||
|
glog.Infof("monitoring handler error: %v", err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
flags := dumpFlags()
|
flags := dumpFlags()
|
||||||
@@ -243,13 +295,22 @@ func launchMonitoringServer(addr string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Static index for the monitoring website.
|
// Static index for the monitoring website.
|
||||||
const monitoringHTMLIndex = `<!DOCTYPE html>
|
var monitoringHTMLIndex = template.Must(template.New("index").Funcs(
|
||||||
|
template.FuncMap{"since": time.Since}).Parse(
|
||||||
|
`<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>chasquid monitoring</title>
|
<title>chasquid monitoring</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>chasquid monitoring</h1>
|
<h1>chasquid monitoring</h1>
|
||||||
|
|
||||||
|
chasquid {{.Version}}<br>
|
||||||
|
source date {{.SourceDate.Format "2006-01-02 15:04:05 -0700"}}<p>
|
||||||
|
|
||||||
|
started {{.StartTime.Format "Mon, 2006-01-02 15:04:05 -0700"}}<br>
|
||||||
|
up since {{.StartTime | since}}<p>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="/debug/queue">queue</a>
|
<li><a href="/debug/queue">queue</a>
|
||||||
<li><a href="/debug/vars">exported variables</a>
|
<li><a href="/debug/vars">exported variables</a>
|
||||||
@@ -270,7 +331,7 @@ const monitoringHTMLIndex = `<!DOCTYPE html>
|
|||||||
</ul>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`
|
`))
|
||||||
|
|
||||||
// dumpFlags to a string, for troubleshooting purposes.
|
// dumpFlags to a string, for troubleshooting purposes.
|
||||||
func dumpFlags() string {
|
func dumpFlags() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user