Make debug log file name configurable

This is a breaking change if you're using --debug-log-dir.
The fix is to replace it with --debug-log and add a '/' at
the end:
  --debug-log-dir=/tmp/runsc ==> --debug-log=/tmp/runsc/

PiperOrigin-RevId: 216761212
Change-Id: I244270a0a522298c48115719fa08dad55e34ade1
This commit is contained in:
Fabricio Voznika
2018-10-11 14:29:37 -07:00
committed by Shentubot
parent 96c68b36f6
commit e68d86e1bd
6 changed files with 35 additions and 26 deletions
+1 -1
View File
@@ -297,7 +297,7 @@ Docker configuration (`/etc/docker/daemon.json`):
"runsc": {
"path": "/usr/local/bin/runsc",
"runtimeArgs": [
"--debug-log-dir=/tmp/runsc",
"--debug-log=/tmp/runsc/",
"--debug",
"--strace"
]
+3 -4
View File
@@ -160,9 +160,8 @@ type Config struct {
// LogFormat is the log format, "text" or "json".
LogFormat string
// DebugLogDir is the directory to log debug information to, if not
// empty.
DebugLogDir string
// DebugLog is the path to log debug information to, if not empty.
DebugLog string
// FileAccess indicates how the filesystem is accessed.
FileAccess FileAccessType
@@ -217,7 +216,7 @@ func (c *Config) ToFlags() []string {
"--debug=" + strconv.FormatBool(c.Debug),
"--log=" + c.LogFilename,
"--log-format=" + c.LogFormat,
"--debug-log-dir=" + c.DebugLogDir,
"--debug-log=" + c.DebugLog,
"--file-access=" + c.FileAccess.String(),
"--overlay=" + strconv.FormatBool(c.Overlay),
"--network=" + c.Network.String(),
+8 -11
View File
@@ -45,10 +45,10 @@ var (
// system that are not covered by the runtime spec.
// Debugging flags.
debugLogDir = flag.String("debug-log-dir", "", "additional location for logs. It creates individual log files per command")
logPackets = flag.Bool("log-packets", false, "enable network packet logging")
logFD = flag.Int("log-fd", -1, "file descriptor to log to. If set, the 'log' flag is ignored.")
debugLogFD = flag.Int("debug-log-fd", -1, "file descriptor to write debug logs to. If set, the 'debug-log-dir' flag is ignored.")
debugLog = flag.String("debug-log", "", "additional location for logs. If it ends with '/', log files are created inside the directory with default names. The following variables are available: %TIMESTAMP%, %COMMAND%.")
logPackets = flag.Bool("log-packets", false, "enable network packet logging")
logFD = flag.Int("log-fd", -1, "file descriptor to log to. If set, the 'log' flag is ignored.")
debugLogFD = flag.Int("debug-log-fd", -1, "file descriptor to write debug logs to. If set, the 'debug-log-dir' flag is ignored.")
// Debugging flags: strace related
strace = flag.Bool("strace", false, "enable strace")
@@ -131,7 +131,7 @@ func main() {
Debug: *debug,
LogFilename: *logFilename,
LogFormat: *logFormat,
DebugLogDir: *debugLogDir,
DebugLog: *debugLog,
FileAccess: fsAccess,
Overlay: *overlay,
Network: netType,
@@ -195,13 +195,10 @@ func main() {
}
e = log.MultiEmitter{e, log.GoogleEmitter{&log.Writer{Next: f}}}
} else if *debugLogDir != "" {
if err := os.MkdirAll(*debugLogDir, 0775); err != nil {
cmd.Fatalf("error creating dir %q: %v", *debugLogDir, err)
}
f, err := specutils.DebugLogFile(*debugLogDir, subcommand)
} else if *debugLog != "" {
f, err := specutils.DebugLogFile(*debugLog, subcommand)
if err != nil {
cmd.Fatalf("error opening debug log file in %q: %v", *debugLogDir, err)
cmd.Fatalf("error opening debug log file in %q: %v", *debugLog, err)
}
e = log.MultiEmitter{e, log.GoogleEmitter{&log.Writer{Next: f}}}
}
+3 -3
View File
@@ -291,10 +291,10 @@ func (s *Sandbox) createSandboxProcess(spec *specs.Spec, conf *boot.Config, bund
cmd.Args = append(cmd.Args, "--log-fd="+strconv.Itoa(nextFD))
nextFD++
}
if conf.DebugLogDir != "" {
debugLogFile, err := specutils.DebugLogFile(conf.DebugLogDir, "boot")
if conf.DebugLog != "" {
debugLogFile, err := specutils.DebugLogFile(conf.DebugLog, "boot")
if err != nil {
return fmt.Errorf("error opening debug log file in %q: %v", conf.DebugLogDir, err)
return fmt.Errorf("error opening debug log file in %q: %v", conf.DebugLog, err)
}
defer debugLogFile.Close()
cmd.ExtraFiles = append(cmd.ExtraFiles, debugLogFile)
+19 -6
View File
@@ -351,12 +351,25 @@ func WaitForReady(pid int, timeout time.Duration, ready func() (bool, error)) er
return backoff.Retry(op, b)
}
// DebugLogFile opens a file in logDir based on the timestamp and subcommand
// for writing.
func DebugLogFile(logDir, subcommand string) (*os.File, error) {
// Format: <debug-log-dir>/runsc.log.<yyyymmdd-hhmmss.uuuuuu>.<command>
filename := fmt.Sprintf("runsc.log.%s.%s", time.Now().Format("20060102-150405.000000"), subcommand)
return os.OpenFile(filepath.Join(logDir, filename), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0664)
// DebugLogFile opens a log file using 'logPattern' as location. If 'logPattern'
// ends with '/', it's used as a directory with default file name.
// 'logPattern' can contain variables that are substitued:
// - %TIMESTAMP%: is replaced with a timestamp using the following format:
// <yyyymmdd-hhmmss.uuuuuu>
// - %COMMAND%: is replaced with 'command'
func DebugLogFile(logPattern, command string) (*os.File, error) {
if strings.HasSuffix(logPattern, "/") {
// Default format: <debug-log>/runsc.log.<yyyymmdd-hhmmss.uuuuuu>.<command>
logPattern += "runsc.log.%TIMESTAMP%.%COMMAND%"
}
logPattern = strings.Replace(logPattern, "%TIMESTAMP%", time.Now().Format("20060102-150405.000000"), -1)
logPattern = strings.Replace(logPattern, "%COMMAND%", command, -1)
dir := filepath.Dir(logPattern)
if err := os.MkdirAll(dir, 0775); err != nil {
return nil, fmt.Errorf("error creating dir %q: %v", dir, err)
}
return os.OpenFile(logPattern, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0664)
}
// Mount creates the mount point and calls Mount with the given flags.
+1 -1
View File
@@ -75,7 +75,7 @@ if [[ ${uninstall} == 0 ]]; then
mkdir -p "${logdir}"
sudo -n chmod a+wx "${logdir}"
declare -r args="--debug-log-dir "${logdir}" --debug --strace --log-packets"
declare -r args="--debug-log '${logdir}/' --debug --strace --log-packets"
sudo -n "${dockercfg}" runtime-add "${runtime}" "${runsc}" ${args}
sudo -n "${dockercfg}" runtime-add "${runtime}"-kvm "${runsc}" --platform=kvm ${args}
sudo -n "${dockercfg}" runtime-add "${runtime}"-hostnet "${runsc}" --network=host ${args}