Allow users to specify logpath

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>

Closes: #135
Approved by: mheon
This commit is contained in:
Daniel J Walsh
2018-01-30 06:23:58 +01:00
committed by Atomic Bot
parent 6ba6ecf59b
commit 095aaaa639
13 changed files with 63 additions and 15 deletions

View File

@ -773,3 +773,15 @@ func stringSlicetoUint32Slice(inputSlice []string) ([]uint32, error) {
} }
return outputSlice, nil return outputSlice, nil
} }
func getLoggingPath(opts []string) string {
for _, opt := range opts {
arr := strings.SplitN(opt, "=", 2)
if len(arr) == 2 {
if strings.TrimSpace(arr[0]) == "path" {
return strings.TrimSpace(arr[1])
}
}
}
return ""
}

View File

@ -608,6 +608,10 @@ func (c *createConfig) GetContainerCreateOptions() ([]libpod.CtrCreateOption, er
if len(c.HostAdd) > 0 { if len(c.HostAdd) > 0 {
options = append(options, libpod.WithHosts(c.HostAdd)) options = append(options, libpod.WithHosts(c.HostAdd))
} }
logPath := getLoggingPath(c.LogDriverOpt)
if logPath != "" {
options = append(options, libpod.WithLogPath(logPath))
}
options = append(options, libpod.WithPrivileged(c.Privileged)) options = append(options, libpod.WithPrivileged(c.Privileged))
return options, nil return options, nil

View File

@ -260,6 +260,8 @@ millions of trillions.
**--log-opt**=[] **--log-opt**=[]
Logging driver specific options. Logging driver specific options.
"path=/var/log/container/mycontainer.json" : Set the path to the container log file.
**--mac-address**="" **--mac-address**=""
Container MAC address (e.g. 92:d0:c6:0a:29:33) Container MAC address (e.g. 92:d0:c6:0a:29:33)

View File

@ -236,8 +236,9 @@ type ContainerConfig struct {
CreatedTime time.Time `json:"createdTime"` CreatedTime time.Time `json:"createdTime"`
// Cgroup parent of the container // Cgroup parent of the container
CgroupParent string `json:"cgroupParent"` CgroupParent string `json:"cgroupParent"`
// LogPath log location
// TODO log options - logpath for plaintext, others for log drivers LogPath string `json:"logPath"`
// TODO log options for log drivers
} }
// ContainerStatus returns a string representation for users // ContainerStatus returns a string representation for users
@ -360,8 +361,7 @@ func (c *Container) RuntimeName() string {
// This file will only be present after Init() is called to create the container // This file will only be present after Init() is called to create the container
// in runc // in runc
func (c *Container) LogPath() string { func (c *Container) LogPath() string {
// TODO store this in state and allow overriding return c.config.LogPath
return c.logPath()
} }
// IPAddress returns the IP address of the container // IPAddress returns the IP address of the container

View File

@ -44,7 +44,7 @@ func (c *Container) getContainerInspectData(size bool, driverData *inspect.Data)
HostnamePath: spec.Annotations["io.kubernetes.cri-o.HostnamePath"], // not sure HostnamePath: spec.Annotations["io.kubernetes.cri-o.HostnamePath"], // not sure
HostsPath: "", // can't get yet HostsPath: "", // can't get yet
StaticDir: config.StaticDir, StaticDir: config.StaticDir,
LogPath: c.LogPath(), LogPath: config.LogPath,
Name: config.Name, Name: config.Name,
Driver: driverData.Name, Driver: driverData.Name,
MountLabel: config.MountLabel, MountLabel: config.MountLabel,

View File

@ -91,11 +91,6 @@ func (c *Container) bundlePath() string {
return c.config.StaticDir return c.config.StaticDir
} }
// The path to the container's logs file
func (c *Container) logPath() string {
return filepath.Join(c.config.StaticDir, "ctr.log")
}
// Retrieves the path of the container's attach socket // Retrieves the path of the container's attach socket
func (c *Container) attachSocketPath() string { func (c *Container) attachSocketPath() string {
return filepath.Join(c.runtime.ociRuntime.socketsDir, c.ID(), "attach") return filepath.Join(c.runtime.ociRuntime.socketsDir, c.ID(), "attach")

View File

@ -174,7 +174,7 @@ func (r *OCIRuntime) createContainer(ctr *Container, cgroupParent string) (err e
args = append(args, "-r", r.path) args = append(args, "-r", r.path)
args = append(args, "-b", ctr.bundlePath()) args = append(args, "-b", ctr.bundlePath())
args = append(args, "-p", filepath.Join(ctr.state.RunDir, "pidfile")) args = append(args, "-p", filepath.Join(ctr.state.RunDir, "pidfile"))
args = append(args, "-l", ctr.logPath()) args = append(args, "-l", ctr.LogPath())
args = append(args, "--exit-dir", r.exitsDir) args = append(args, "--exit-dir", r.exitsDir)
args = append(args, "--socket-dir-path", r.socketsDir) args = append(args, "--socket-dir-path", r.socketsDir)
if ctr.config.Spec.Process.Terminal { if ctr.config.Spec.Process.Terminal {

View File

@ -602,6 +602,22 @@ func WithNetNS(portMappings []ocicni.PortMapping) CtrCreateOption {
} }
} }
// WithLogPath sets the path to the log file
func WithLogPath(path string) CtrCreateOption {
return func(ctr *Container) error {
if ctr.valid {
return ErrCtrFinalized
}
if path == "" {
return errors.Wrapf(ErrInvalidArg, "log path must be set")
}
ctr.config.LogPath = path
return nil
}
}
// WithCgroupParent sets the Cgroup Parent of the new container // WithCgroupParent sets the Cgroup Parent of the new container
func WithCgroupParent(parent string) CtrCreateOption { func WithCgroupParent(parent string) CtrCreateOption {
return func(ctr *Container) error { return func(ctr *Container) error {

View File

@ -62,6 +62,9 @@ func (r *Runtime) NewContainer(rSpec *spec.Spec, options ...CtrCreateOption) (c
} }
}() }()
if ctr.config.LogPath == "" {
ctr.config.LogPath = filepath.Join(ctr.config.StaticDir, "ctr.log")
}
if ctr.config.ShmDir == "" { if ctr.config.ShmDir == "" {
ctr.config.ShmDir = filepath.Join(ctr.bundlePath(), "shm") ctr.config.ShmDir = filepath.Join(ctr.bundlePath(), "shm")
if err := os.MkdirAll(ctr.config.ShmDir, 0700); err != nil { if err := os.MkdirAll(ctr.config.ShmDir, 0700); err != nil {
@ -71,7 +74,6 @@ func (r *Runtime) NewContainer(rSpec *spec.Spec, options ...CtrCreateOption) (c
} }
ctr.config.Mounts = append(ctr.config.Mounts, ctr.config.ShmDir) ctr.config.Mounts = append(ctr.config.Mounts, ctr.config.ShmDir)
} }
// Add the container to the state // Add the container to the state
// TODO: May be worth looking into recovering from name/ID collisions here // TODO: May be worth looking into recovering from name/ID collisions here
if ctr.config.Pod != "" { if ctr.config.Pod != "" {
@ -89,7 +91,6 @@ func (r *Runtime) NewContainer(rSpec *spec.Spec, options ...CtrCreateOption) (c
return nil, err return nil, err
} }
} }
return ctr, nil return ctr, nil
} }

View File

@ -15,7 +15,7 @@ import (
// DBSchema is the current DB schema version // DBSchema is the current DB schema version
// Increments every time a change is made to the database's tables // Increments every time a change is made to the database's tables
const DBSchema = 8 const DBSchema = 9
// SQLState is a state implementation backed by a persistent SQLite3 database // SQLState is a state implementation backed by a persistent SQLite3 database
type SQLState struct { type SQLState struct {
@ -285,7 +285,7 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ? ?, ?, ?, ?, ?
);` );`
addCtrState = `INSERT INTO containerState VALUES ( addCtrState = `INSERT INTO containerState VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
@ -376,6 +376,7 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
ctr.config.ShmSize, ctr.config.ShmSize,
ctr.config.StaticDir, ctr.config.StaticDir,
string(mounts), string(mounts),
ctr.LogPath(),
boolToSQL(ctr.config.Privileged), boolToSQL(ctr.config.Privileged),
boolToSQL(ctr.config.NoNewPrivs), boolToSQL(ctr.config.NoNewPrivs),

View File

@ -178,6 +178,7 @@ func prepareDB(db *sql.DB) (err error) {
ShmSize INTEGER NOT NULL, ShmSize INTEGER NOT NULL,
StaticDir TEXT NOT NULL, StaticDir TEXT NOT NULL,
Mounts TEXT NOT NULL, Mounts TEXT NOT NULL,
LogPath TEXT NOT NULL,
Privileged INTEGER NOT NULL, Privileged INTEGER NOT NULL,
NoNewPrivs INTEGER NOT NULL, NoNewPrivs INTEGER NOT NULL,
@ -362,6 +363,7 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
shmSize int64 shmSize int64
staticDir string staticDir string
mounts string mounts string
logPath string
privileged int privileged int
noNewPrivs int noNewPrivs int
@ -417,6 +419,7 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
&shmSize, &shmSize,
&staticDir, &staticDir,
&mounts, &mounts,
&logPath,
&privileged, &privileged,
&noNewPrivs, &noNewPrivs,
@ -480,6 +483,7 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
ctr.config.ShmDir = shmDir ctr.config.ShmDir = shmDir
ctr.config.ShmSize = shmSize ctr.config.ShmSize = shmSize
ctr.config.StaticDir = staticDir ctr.config.StaticDir = staticDir
ctr.config.LogPath = logPath
ctr.config.Privileged = boolFromSQL(privileged) ctr.config.Privileged = boolFromSQL(privileged)
ctr.config.NoNewPrivs = boolFromSQL(noNewPrivs) ctr.config.NoNewPrivs = boolFromSQL(noNewPrivs)

View File

@ -23,6 +23,7 @@ func getTestContainer(id, name, locksDir string) (*Container, error) {
ImageVolumes: true, ImageVolumes: true,
ReadOnly: true, ReadOnly: true,
StaticDir: "/does/not/exist/", StaticDir: "/does/not/exist/",
LogPath: "/does/not/exist/",
Stdin: true, Stdin: true,
Labels: make(map[string]string), Labels: make(map[string]string),
StopSignal: 0, StopSignal: 0,

View File

@ -184,4 +184,16 @@ var _ = Describe("Podman run", func() {
Expect(session.ExitCode()).To(Equal(0)) Expect(session.ExitCode()).To(Equal(0))
Expect(session.OutputToString()).To(ContainSubstring("15")) Expect(session.OutputToString()).To(ContainSubstring("15"))
}) })
It("podman run log-opt", func() {
log := filepath.Join(podmanTest.TempDir, "/container.log")
session := podmanTest.Podman([]string{"run", "--rm", "--log-opt", fmt.Sprintf("path=%s", log), ALPINE, "ls"})
session.Wait(10)
fmt.Println(session.OutputToString())
Expect(session.ExitCode()).To(Equal(0))
_, err := os.Stat(log)
Expect(err).To(BeNil())
_ = os.Remove(log)
})
}) })