Skip to content

Commit

Permalink
Allow users to specify logpath
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>

Closes: #135
Approved by: mheon
  • Loading branch information
rhatdan authored and rh-atomic-bot committed Feb 3, 2018
1 parent 6ba6ecf commit 095aaaa
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 15 deletions.
12 changes: 12 additions & 0 deletions cmd/podman/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
4 changes: 4 additions & 0 deletions cmd/podman/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/podman-run.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions libpod/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libpod/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion libpod/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 != "" {
Expand All @@ -89,7 +91,6 @@ func (r *Runtime) NewContainer(rSpec *spec.Spec, options ...CtrCreateOption) (c
return nil, err
}
}

return ctr, nil
}

Expand Down
5 changes: 3 additions & 2 deletions libpod/sql_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -285,7 +285,7 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?
?, ?, ?, ?, ?
);`
addCtrState = `INSERT INTO containerState VALUES (
?, ?, ?, ?, ?,
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions libpod/sql_state_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -362,6 +363,7 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
shmSize int64
staticDir string
mounts string
logPath string

privileged int
noNewPrivs int
Expand Down Expand Up @@ -417,6 +419,7 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
&shmSize,
&staticDir,
&mounts,
&logPath,

&privileged,
&noNewPrivs,
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions libpod/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

})

0 comments on commit 095aaaa

Please sign in to comment.