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
}
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 {
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))
return options, nil

View File

@ -260,6 +260,8 @@ millions of trillions.
**--log-opt**=[]
Logging driver specific options.
"path=/var/log/container/mycontainer.json" : Set the path to the container log file.
**--mac-address**=""
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"`
// Cgroup parent of the container
CgroupParent string `json:"cgroupParent"`
// TODO log options - logpath for plaintext, others for log drivers
// LogPath log location
LogPath string `json:"logPath"`
// TODO log options for log drivers
}
// 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
// in runc
func (c *Container) LogPath() string {
// TODO store this in state and allow overriding
return c.logPath()
return c.config.LogPath
}
// 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
HostsPath: "", // can't get yet
StaticDir: config.StaticDir,
LogPath: c.LogPath(),
LogPath: config.LogPath,
Name: config.Name,
Driver: driverData.Name,
MountLabel: config.MountLabel,

View File

@ -91,11 +91,6 @@ func (c *Container) bundlePath() string {
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
func (c *Container) attachSocketPath() string {
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, "-b", ctr.bundlePath())
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, "--socket-dir-path", r.socketsDir)
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
func WithCgroupParent(parent string) CtrCreateOption {
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 == "" {
ctr.config.ShmDir = filepath.Join(ctr.bundlePath(), "shm")
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)
}
// Add the container to the state
// TODO: May be worth looking into recovering from name/ID collisions here
if ctr.config.Pod != "" {
@ -89,7 +91,6 @@ func (r *Runtime) NewContainer(rSpec *spec.Spec, options ...CtrCreateOption) (c
return nil, err
}
}
return ctr, nil
}

View File

@ -15,7 +15,7 @@ import (
// DBSchema is the current DB schema version
// 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
type SQLState struct {
@ -285,7 +285,7 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?
?, ?, ?, ?, ?
);`
addCtrState = `INSERT INTO containerState VALUES (
?, ?, ?, ?, ?,
@ -376,6 +376,7 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
ctr.config.ShmSize,
ctr.config.StaticDir,
string(mounts),
ctr.LogPath(),
boolToSQL(ctr.config.Privileged),
boolToSQL(ctr.config.NoNewPrivs),

View File

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

View File

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

View File

@ -184,4 +184,16 @@ var _ = Describe("Podman run", func() {
Expect(session.ExitCode()).To(Equal(0))
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)
})
})