2019-08-08 05:45:54 +00:00
|
|
|
package cmds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2020-04-27 16:42:52 +00:00
|
|
|
"sync"
|
2019-08-08 05:45:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
|
|
"github.com/natefinch/lumberjack"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2020-07-10 17:34:00 +00:00
|
|
|
"github.com/rancher/spur/cli"
|
2019-08-08 05:45:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Log struct {
|
|
|
|
VLevel int
|
|
|
|
VModule string
|
|
|
|
LogFile string
|
|
|
|
AlsoLogToStderr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
LogConfig Log
|
|
|
|
|
|
|
|
VLevel = cli.IntFlag{
|
|
|
|
Name: "v",
|
2019-10-27 05:53:25 +00:00
|
|
|
Usage: "(logging) Number for the log level verbosity",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.VLevel,
|
|
|
|
}
|
|
|
|
VModule = cli.StringFlag{
|
|
|
|
Name: "vmodule",
|
2019-10-27 05:53:25 +00:00
|
|
|
Usage: "(logging) Comma-separated list of pattern=N settings for file-filtered logging",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.VModule,
|
|
|
|
}
|
|
|
|
LogFile = cli.StringFlag{
|
|
|
|
Name: "log,l",
|
2019-10-27 05:53:25 +00:00
|
|
|
Usage: "(logging) Log to file",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.LogFile,
|
|
|
|
}
|
|
|
|
AlsoLogToStderr = cli.BoolFlag{
|
|
|
|
Name: "alsologtostderr",
|
2019-10-27 05:53:25 +00:00
|
|
|
Usage: "(logging) Log to standard error as well as file (if set)",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.AlsoLogToStderr,
|
|
|
|
}
|
2020-04-27 16:42:52 +00:00
|
|
|
|
|
|
|
logSetupOnce sync.Once
|
2019-08-08 05:45:54 +00:00
|
|
|
)
|
|
|
|
|
2020-07-10 17:34:00 +00:00
|
|
|
func InitLogging(action func(*cli.Context) error) func(*cli.Context) error {
|
|
|
|
return func(ctx *cli.Context) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
reExec bool
|
|
|
|
)
|
|
|
|
logSetupOnce.Do(func() {
|
|
|
|
if LogConfig.LogFile != "" && os.Getenv("_K3S_LOG_REEXEC_") == "" {
|
|
|
|
reExec = true
|
|
|
|
err = runWithLogging()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = checkUnixTimestamp(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setupLogging()
|
|
|
|
})
|
|
|
|
if reExec || err != nil {
|
|
|
|
return err
|
2020-04-27 16:42:52 +00:00
|
|
|
}
|
2020-07-10 17:34:00 +00:00
|
|
|
if action != nil {
|
|
|
|
return action(ctx)
|
2020-04-27 16:42:52 +00:00
|
|
|
}
|
2020-07-10 17:34:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-08 05:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkUnixTimestamp() error {
|
|
|
|
timeNow := time.Now()
|
|
|
|
// check if time before 01/01/1980
|
|
|
|
if timeNow.Before(time.Unix(315532800, 0)) {
|
|
|
|
return fmt.Errorf("server time isn't set properly: %v", timeNow)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runWithLogging() error {
|
|
|
|
var (
|
|
|
|
l io.Writer
|
|
|
|
)
|
|
|
|
l = &lumberjack.Logger{
|
|
|
|
Filename: LogConfig.LogFile,
|
|
|
|
MaxSize: 50,
|
|
|
|
MaxBackups: 3,
|
|
|
|
MaxAge: 28,
|
|
|
|
Compress: true,
|
|
|
|
}
|
|
|
|
if LogConfig.AlsoLogToStderr {
|
|
|
|
l = io.MultiWriter(l, os.Stderr)
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
args := append([]string{version.Program}, os.Args[1:]...)
|
2019-08-08 05:45:54 +00:00
|
|
|
cmd := reexec.Command(args...)
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, "_K3S_LOG_REEXEC_=true")
|
|
|
|
cmd.Stderr = l
|
|
|
|
cmd.Stdout = l
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupLogging() {
|
|
|
|
flag.Set("v", strconv.Itoa(LogConfig.VLevel))
|
|
|
|
flag.Set("vmodule", LogConfig.VModule)
|
2020-04-29 18:36:44 +00:00
|
|
|
flag.Set("alsologtostderr", strconv.FormatBool(Debug))
|
|
|
|
flag.Set("logtostderr", strconv.FormatBool(!Debug))
|
2019-08-08 05:45:54 +00:00
|
|
|
}
|