diff --git a/Makefile b/Makefile index 41b20b0b..0386dda2 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ LDFLAGS := GOOPTS := GO := GOOS=$(OS) GOARCH=$(ARCH) GO15VENDOREXPERIMENT=1 go GOGETTER := GOPATH=$(shell pwd)/.tmpdeps go get -d -GOLDFLAGS := -ldflags "-X main.version=$(BUILDREV)" +GOLDFLAGS := -ldflags "-X mig.ninja/mig.Version=$(BUILDREV)" GOCFLAGS := MKDIR := mkdir INSTALL := install diff --git a/client/mig-action-generator/generator.go b/client/mig-action-generator/generator.go index 3bf693a9..35ae085f 100644 --- a/client/mig-action-generator/generator.go +++ b/client/mig-action-generator/generator.go @@ -10,15 +10,13 @@ import ( "flag" "fmt" "log" - "mig.ninja/mig" - "mig.ninja/mig/client" "net/url" "os" "time" -) -// build version -var version string + "mig.ninja/mig" + "mig.ninja/mig/client" +) func main() { var err error @@ -52,7 +50,7 @@ func main() { flag.Parse() if *showversion { - fmt.Println(version) + fmt.Println(mig.Version) os.Exit(0) } @@ -65,7 +63,7 @@ func main() { if err != nil { panic(err) } - cli, err := client.NewClient(conf, "generator-"+version) + cli, err := client.NewClient(conf, "generator-"+mig.Version) if err != nil { panic(err) } diff --git a/client/mig-action-verifier/verifier.go b/client/mig-action-verifier/verifier.go index 0b5f1ba7..d7fec306 100644 --- a/client/mig-action-verifier/verifier.go +++ b/client/mig-action-verifier/verifier.go @@ -8,9 +8,10 @@ package main import ( "flag" "fmt" + "os" + "mig.ninja/mig" "mig.ninja/mig/client" - "os" ) func main() { @@ -33,8 +34,14 @@ func main() { var actionfile = flag.String("a", "/path/to/action", "Load action from file") var commandfile = flag.String("c", "/path/to/command", "Load command from file") var config = flag.String("conf", homedir+"/.migrc", "Load configuration from file") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(mig.Version) + os.Exit(0) + } + conf, err := client.ReadConfiguration(*config) if err != nil { panic(err) diff --git a/client/mig-console/console.go b/client/mig-console/console.go index 8777ab68..e577de3d 100644 --- a/client/mig-console/console.go +++ b/client/mig-console/console.go @@ -22,9 +22,6 @@ import ( "mig.ninja/mig/client" ) -// build version -var version string - func main() { var err error defer func() { @@ -41,7 +38,7 @@ func main() { flag.Parse() if *showversion { - fmt.Println(version) + fmt.Println(mig.Version) os.Exit(0) } @@ -74,7 +71,7 @@ func main() { if err != nil { panic(err) } - cli, err := client.NewClient(conf, "console-"+version) + cli, err := client.NewClient(conf, "console-"+mig.Version) if err != nil { panic(err) } diff --git a/client/mig/main.go b/client/mig/main.go index 03e7f625..2a09552e 100644 --- a/client/mig/main.go +++ b/client/mig/main.go @@ -19,9 +19,6 @@ import ( "mig.ninja/mig/modules" ) -// build version -var version string - func usage() { fmt.Printf(`%s - Mozilla InvestiGator command line client usage: %s @@ -49,6 +46,7 @@ usage: %s * agents operated by IT: -t "tags#>>'{operator}'='IT'" * run on local system: -t local -v verbose output, includes debug information and raw queries +-V print version Progress information is sent to stderr, silence it with "2>/dev/null". Results are sent to stdout, redirect them with "1>/path/to/file". @@ -77,7 +75,7 @@ func main() { a mig.Action migrc, show, render, target, expiration, afile string printAndExit bool - verbose bool + verbose, showversion bool modargs []string run interface{} ) @@ -97,6 +95,7 @@ func main() { fs.StringVar(&expiration, "e", "300s", "expiration") fs.StringVar(&afile, "i", "/path/to/file", "Load action from file") fs.BoolVar(&verbose, "v", false, "Enable verbose output") + fs.BoolVar(&showversion, "V", false, "Show version") // if first argument is missing, or is help, print help // otherwise, pass the remainder of the arguments to the module for parsing @@ -105,8 +104,8 @@ func main() { usage() } - if len(os.Args) < 2 || os.Args[1] == "-V" { - fmt.Println(version) + if showversion || (len(os.Args) > 1 && (os.Args[1] == "-V" || os.Args[1] == "version")) { + fmt.Println(mig.Version) os.Exit(0) } @@ -234,7 +233,7 @@ readytolaunch: if err != nil { panic(err) } - cli, err = client.NewClient(conf, "cmd-"+version) + cli, err = client.NewClient(conf, "cmd-"+mig.Version) if err != nil { panic(err) } diff --git a/mig-agent/agent.go b/mig-agent/agent.go index a1f467b4..19918582 100644 --- a/mig-agent/agent.go +++ b/mig-agent/agent.go @@ -10,21 +10,19 @@ import ( "encoding/json" "flag" "fmt" - "github.com/jvehent/service-go" - "github.com/streadway/amqp" "io/ioutil" - "mig.ninja/mig" - "mig.ninja/mig/modules" "os" "os/exec" "runtime" "strings" "sync" "time" -) -// build version -var version string + "github.com/jvehent/service-go" + "github.com/streadway/amqp" + "mig.ninja/mig" + "mig.ninja/mig/modules" +) // publication lock is used to prevent publication when the channels are not // available, like during a shutdown @@ -73,7 +71,7 @@ func main() { flag.Parse() if *showversion { - fmt.Println(version) + fmt.Println(mig.Version) os.Exit(0) } @@ -205,7 +203,7 @@ func runAgentCheckin(foreground, upgrading, debug bool) (err error) { fmt.Fprintf(os.Stderr, "Failed to start agent routines: '%v'", err) os.Exit(0) } - ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Mozilla InvestiGator version %s: started agent %s in checkin mode", version, ctx.Agent.Hostname)} + ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Mozilla InvestiGator version %s: started agent %s in checkin mode", mig.Version, ctx.Agent.Hostname)} // The loop below retrieves messages from the relay. If no message is available, // it will timeout and break out of the loop after 10 seconds, causing the agent to exit @@ -279,7 +277,7 @@ func runAgent(foreground, upgrading, debug bool) (err error) { panic(err) } - ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Mozilla InvestiGator version %s: started agent %s", version, ctx.Agent.Hostname)} + ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Mozilla InvestiGator version %s: started agent %s", mig.Version, ctx.Agent.Hostname)} // The agent blocks here until a termination order is received // The order is then evaluated to decide if a new agent must be respawned, or the agent @@ -669,7 +667,7 @@ func heartbeat(ctx Context) (err error) { HeartBeat := mig.Agent{ Name: ctx.Agent.Hostname, Mode: ctx.Agent.Mode, - Version: version, + Version: mig.Version, PID: os.Getpid(), QueueLoc: ctx.Agent.QueueLoc, StartTime: time.Now(), diff --git a/mig-api/api.go b/mig-api/api.go index 2989a2e3..057b68e0 100644 --- a/mig-api/api.go +++ b/mig-api/api.go @@ -8,18 +8,16 @@ package main import ( "flag" "fmt" - "github.com/gorilla/context" - "github.com/gorilla/mux" - "github.com/jvehent/cljs" - "mig.ninja/mig" "net/http" "os" "runtime" "strings" -) -// build version -var version string + "github.com/gorilla/context" + "github.com/gorilla/mux" + "github.com/jvehent/cljs" + "mig.ninja/mig" +) var ctx Context @@ -35,7 +33,7 @@ func main() { flag.Parse() if *showversion { - fmt.Println(version) + fmt.Println(mig.Version) os.Exit(0) } diff --git a/mig-runner/main.go b/mig-runner/main.go index c351b52f..d4e4177d 100644 --- a/mig-runner/main.go +++ b/mig-runner/main.go @@ -9,12 +9,13 @@ import ( "flag" "fmt" "io/ioutil" - "mig.ninja/mig" "os" "os/signal" "path" "sync" "time" + + "mig.ninja/mig" ) var ctx Context @@ -24,8 +25,14 @@ func main() { var err error var config = flag.String("c", "/etc/mig/runner.cfg", "Load configuration from file") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(mig.Version) + os.Exit(0) + } + ctx, err = initContext(*config) if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) diff --git a/mig-scheduler/scheduler.go b/mig-scheduler/scheduler.go index c10edf8d..836414d5 100644 --- a/mig-scheduler/scheduler.go +++ b/mig-scheduler/scheduler.go @@ -19,17 +19,20 @@ import ( "mig.ninja/mig/pgp" ) -// build version -var version string - func main() { cpus := runtime.NumCPU() runtime.GOMAXPROCS(cpus * 2) // command line options var config = flag.String("c", "/etc/mig/scheduler.cfg", "Load configuration from file") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(mig.Version) + os.Exit(0) + } + // The context initialization takes care of parsing the configuration, // and creating connections to database, message broker, syslog, ... fmt.Fprintf(os.Stderr, "Initializing Scheduler context...") diff --git a/modules/file/paramscreator.go b/modules/file/paramscreator.go index 4de60c66..f3fe51ba 100644 --- a/modules/file/paramscreator.go +++ b/modules/file/paramscreator.go @@ -83,7 +83,7 @@ Module documentation is at http://mig.mozilla.org/doc/module_file.html Cheatsheet and examples are at http://mig.mozilla.org/doc/cheatsheet.rst.html `, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, dash, - dash, dash, dash, dash, dash, dash, dash, dash) + dash, dash, dash, dash, dash, dash) return } diff --git a/version.go b/version.go new file mode 100644 index 00000000..88e784eb --- /dev/null +++ b/version.go @@ -0,0 +1,9 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr] + +package mig /* import "mig.ninja/mig" */ + +var Version string = "20150924+637dcd7" diff --git a/workers/mig-worker-agent-intel/main.go b/workers/mig-worker-agent-intel/main.go index fec6aca7..0eba8bf2 100644 --- a/workers/mig-worker-agent-intel/main.go +++ b/workers/mig-worker-agent-intel/main.go @@ -6,16 +6,17 @@ package main import ( - "gopkg.in/gcfg.v1" "encoding/json" "flag" "fmt" - "github.com/jvehent/gozdef" - "mig.ninja/mig" - "mig.ninja/mig/workers" "os" "os/exec" "regexp" + + "github.com/jvehent/gozdef" + "gopkg.in/gcfg.v1" + "mig.ninja/mig" + "mig.ninja/mig/workers" ) const workerName = "agent_intel" @@ -42,7 +43,12 @@ func main() { flag.PrintDefaults() } var configPath = flag.String("c", "/etc/mig/agent-intel-worker.cfg", "Load configuration from file") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(mig.Version) + os.Exit(0) + } err = gcfg.ReadFileInto(&conf, *configPath) if err != nil { panic(err) diff --git a/workers/mig-worker-agent-verif/main.go b/workers/mig-worker-agent-verif/main.go index 1618b732..4e72a4c5 100644 --- a/workers/mig-worker-agent-verif/main.go +++ b/workers/mig-worker-agent-verif/main.go @@ -6,12 +6,13 @@ package main import ( - "gopkg.in/gcfg.v1" "flag" "fmt" + "os" + + "gopkg.in/gcfg.v1" "mig.ninja/mig" "mig.ninja/mig/workers" - "os" ) const workerName = "agent_verif" @@ -31,7 +32,13 @@ func main() { flag.PrintDefaults() } var configPath = flag.String("c", "/etc/mig/agent-verif-worker.cfg", "Load configuration from file") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(mig.Version) + os.Exit(0) + } + err = gcfg.ReadFileInto(&conf, *configPath) if err != nil { panic(err) diff --git a/workers/mig-worker-compliance-item/main.go b/workers/mig-worker-compliance-item/main.go index 2fb465d8..636904e7 100644 --- a/workers/mig-worker-compliance-item/main.go +++ b/workers/mig-worker-compliance-item/main.go @@ -6,19 +6,20 @@ package main import ( - "gopkg.in/gcfg.v1" "encoding/json" "flag" "fmt" - "github.com/jvehent/gozdef" - "mig.ninja/mig" - "mig.ninja/mig/modules" - "mig.ninja/mig/modules/file" - "mig.ninja/mig/workers" "os" "os/exec" "regexp" "time" + + "github.com/jvehent/gozdef" + "gopkg.in/gcfg.v1" + "mig.ninja/mig" + "mig.ninja/mig/modules" + "mig.ninja/mig/modules/file" + "mig.ninja/mig/workers" ) const workerName = "compliance_item" @@ -46,7 +47,13 @@ func main() { flag.PrintDefaults() } var configPath = flag.String("c", "/etc/mig/compliance-item-worker.cfg", "Load configuration from file") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(mig.Version) + os.Exit(0) + } + err = gcfg.ReadFileInto(&conf, *configPath) if err != nil { panic(err)