2013-12-03 22:19:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-06-09 04:25:38 +00:00
|
|
|
"flag"
|
2013-12-03 22:19:42 +00:00
|
|
|
"fmt"
|
2015-05-30 14:49:49 +00:00
|
|
|
"io"
|
2015-07-15 17:14:11 +00:00
|
|
|
"log"
|
2015-03-22 20:58:40 +00:00
|
|
|
"math/rand"
|
2013-12-03 22:19:42 +00:00
|
|
|
"os"
|
2015-07-15 17:14:11 +00:00
|
|
|
"os/signal"
|
2014-11-21 03:31:04 +00:00
|
|
|
"strings"
|
2015-07-15 17:14:11 +00:00
|
|
|
"syscall"
|
2015-03-22 20:58:40 +00:00
|
|
|
"time"
|
2015-01-24 20:11:03 +00:00
|
|
|
|
2015-06-08 19:07:05 +00:00
|
|
|
"github.com/influxdb/influxdb/cmd/influxd/backup"
|
2015-05-30 14:49:49 +00:00
|
|
|
"github.com/influxdb/influxdb/cmd/influxd/help"
|
2015-06-08 19:07:05 +00:00
|
|
|
"github.com/influxdb/influxdb/cmd/influxd/restore"
|
2015-05-29 19:50:05 +00:00
|
|
|
"github.com/influxdb/influxdb/cmd/influxd/run"
|
2014-12-11 21:49:42 +00:00
|
|
|
)
|
|
|
|
|
2015-06-09 04:25:38 +00:00
|
|
|
// These variables are populated via the Go linker.
|
|
|
|
var (
|
2015-11-07 07:30:27 +00:00
|
|
|
version = "0.9"
|
2015-09-25 06:32:47 +00:00
|
|
|
commit string
|
|
|
|
branch string
|
|
|
|
buildTime string
|
2015-06-09 04:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-09-25 06:32:47 +00:00
|
|
|
// If commit, branch, or build time are not set, make that clear.
|
2015-06-09 04:25:38 +00:00
|
|
|
if commit == "" {
|
|
|
|
commit = "unknown"
|
|
|
|
}
|
2015-08-12 18:14:25 +00:00
|
|
|
if branch == "" {
|
|
|
|
branch = "unknown"
|
|
|
|
}
|
2015-09-25 06:32:47 +00:00
|
|
|
if buildTime == "" {
|
|
|
|
buildTime = "unknown"
|
|
|
|
}
|
2015-06-09 04:25:38 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 22:19:42 +00:00
|
|
|
func main() {
|
2015-03-22 20:58:40 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2014-12-06 01:02:30 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
m := NewMain()
|
|
|
|
if err := m.Run(os.Args[1:]...); err != nil {
|
2015-07-01 20:44:49 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2015-05-30 14:49:49 +00:00
|
|
|
os.Exit(1)
|
2015-02-24 17:47:07 +00:00
|
|
|
}
|
2015-05-30 14:49:49 +00:00
|
|
|
}
|
2015-02-24 17:47:07 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
// Main represents the program execution.
|
|
|
|
type Main struct {
|
2015-07-15 17:14:11 +00:00
|
|
|
Logger *log.Logger
|
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
Stdin io.Reader
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
// NewMain return a new instance of Main.
|
|
|
|
func NewMain() *Main {
|
|
|
|
return &Main{
|
2015-07-15 17:14:11 +00:00
|
|
|
Logger: log.New(os.Stderr, "[run] ", log.LstdFlags),
|
2015-05-30 14:49:49 +00:00
|
|
|
Stdin: os.Stdin,
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
2014-12-06 01:02:30 +00:00
|
|
|
}
|
2015-05-30 14:49:49 +00:00
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
// Run determines and runs the command specified by the CLI args.
|
|
|
|
func (m *Main) Run(args ...string) error {
|
|
|
|
name, args := ParseCommandName(args)
|
2014-12-06 01:02:30 +00:00
|
|
|
|
|
|
|
// Extract name from args.
|
2015-05-30 14:49:49 +00:00
|
|
|
switch name {
|
|
|
|
case "", "run":
|
2015-06-11 04:50:20 +00:00
|
|
|
cmd := run.NewCommand()
|
|
|
|
|
|
|
|
// Tell the server the build details.
|
|
|
|
cmd.Version = version
|
|
|
|
cmd.Commit = commit
|
2015-08-12 18:14:25 +00:00
|
|
|
cmd.Branch = branch
|
2015-09-25 06:32:47 +00:00
|
|
|
cmd.BuildTime = buildTime
|
2015-06-11 04:50:20 +00:00
|
|
|
|
|
|
|
if err := cmd.Run(args...); err != nil {
|
2015-05-30 14:49:49 +00:00
|
|
|
return fmt.Errorf("run: %s", err)
|
2015-03-22 20:58:40 +00:00
|
|
|
}
|
2015-06-05 20:40:18 +00:00
|
|
|
|
2015-07-15 17:14:11 +00:00
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
2015-08-12 22:07:12 +00:00
|
|
|
m.Logger.Println("Listening for signals")
|
2015-07-15 17:14:11 +00:00
|
|
|
|
|
|
|
// Block until one of the signals above is received
|
|
|
|
select {
|
|
|
|
case <-signalCh:
|
2015-08-12 22:07:12 +00:00
|
|
|
m.Logger.Println("Signal received, initializing clean shutdown...")
|
2015-07-15 17:14:11 +00:00
|
|
|
go func() {
|
|
|
|
cmd.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block again until another signal is received, a shutdown timeout elapses,
|
|
|
|
// or the Command is gracefully closed
|
2015-08-12 22:07:12 +00:00
|
|
|
m.Logger.Println("Waiting for clean shutdown...")
|
2015-07-15 17:14:11 +00:00
|
|
|
select {
|
|
|
|
case <-signalCh:
|
|
|
|
m.Logger.Println("second signal received, initializing hard shutdown")
|
|
|
|
case <-time.After(time.Second * 30):
|
|
|
|
m.Logger.Println("time limit reached, initializing hard shutdown")
|
|
|
|
case <-cmd.Closed:
|
|
|
|
m.Logger.Println("server shutdown completed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// goodbye.
|
2015-06-05 20:40:18 +00:00
|
|
|
|
2015-06-08 19:07:05 +00:00
|
|
|
case "backup":
|
|
|
|
name := backup.NewCommand()
|
|
|
|
if err := name.Run(args...); err != nil {
|
|
|
|
return fmt.Errorf("backup: %s", err)
|
|
|
|
}
|
|
|
|
case "restore":
|
|
|
|
name := restore.NewCommand()
|
|
|
|
if err := name.Run(args...); err != nil {
|
|
|
|
return fmt.Errorf("restore: %s", err)
|
|
|
|
}
|
2015-03-16 15:23:53 +00:00
|
|
|
case "config":
|
2015-05-30 14:49:49 +00:00
|
|
|
if err := run.NewPrintConfigCommand().Run(args...); err != nil {
|
|
|
|
return fmt.Errorf("config: %s", err)
|
|
|
|
}
|
|
|
|
case "version":
|
2015-06-09 04:25:38 +00:00
|
|
|
if err := NewVersionCommand().Run(args...); err != nil {
|
2015-05-30 14:49:49 +00:00
|
|
|
return fmt.Errorf("version: %s", err)
|
|
|
|
}
|
|
|
|
case "help":
|
|
|
|
if err := help.NewCommand().Run(args...); err != nil {
|
|
|
|
return fmt.Errorf("help: %s", err)
|
2015-03-25 16:27:48 +00:00
|
|
|
}
|
2015-05-30 16:11:23 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'influxd help' for usage`+"\n\n", name)
|
2014-11-04 20:36:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 16:11:23 +00:00
|
|
|
return nil
|
2015-05-30 14:49:49 +00:00
|
|
|
}
|
2014-10-22 00:20:43 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
// ParseCommandName extracts the command name and args from the args list.
|
|
|
|
func ParseCommandName(args []string) (string, []string) {
|
|
|
|
// Retrieve command name as first argument.
|
|
|
|
var name string
|
|
|
|
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
|
|
|
name = args[0]
|
2013-12-03 22:19:42 +00:00
|
|
|
}
|
2014-11-11 05:25:03 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
// Special case -h immediately following binary name
|
|
|
|
if len(args) > 0 && args[0] == "-h" {
|
|
|
|
name = "help"
|
2014-03-27 23:09:08 +00:00
|
|
|
}
|
2014-03-27 16:24:40 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
// If command is "help" and has an argument then rewrite args to use "-h".
|
|
|
|
if name == "help" && len(args) > 1 {
|
|
|
|
args[0], args[1] = args[1], "-h"
|
|
|
|
name = args[0]
|
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
|
2015-05-30 14:49:49 +00:00
|
|
|
// If a named command is specified then return it with its arguments.
|
|
|
|
if name != "" {
|
|
|
|
return name, args[1:]
|
|
|
|
}
|
|
|
|
return "", args
|
2014-12-06 01:02:30 +00:00
|
|
|
}
|
2015-02-23 20:27:51 +00:00
|
|
|
|
2015-11-07 07:30:27 +00:00
|
|
|
// VersionCommand represents the command executed by "influxd version".
|
2015-06-09 04:25:38 +00:00
|
|
|
type VersionCommand struct {
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersionCommand return a new instance of VersionCommand.
|
|
|
|
func NewVersionCommand() *VersionCommand {
|
|
|
|
return &VersionCommand{
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run prints the current version and commit info.
|
|
|
|
func (cmd *VersionCommand) Run(args ...string) error {
|
|
|
|
// Parse flags in case -h is specified.
|
|
|
|
fs := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
fs.Usage = func() { fmt.Fprintln(cmd.Stderr, strings.TrimSpace(versionUsage)) }
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print version info.
|
2015-09-25 06:32:47 +00:00
|
|
|
fmt.Fprintf(cmd.Stdout, "InfluxDB v%s (git: %s %s, built %s)\n", version, branch, commit, buildTime)
|
2015-06-09 04:25:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var versionUsage = `
|
|
|
|
usage: version
|
|
|
|
|
2015-08-12 18:14:25 +00:00
|
|
|
version displays the InfluxDB version, build branch and git commit hash
|
2015-06-09 04:25:38 +00:00
|
|
|
`
|