Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
Add pid file support
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Aug 7, 2014
1 parent e7eea7c commit 50f992a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ The main magic of how this works is that the container processes are moved from

The above will use the `name=systemd` and `cpu` cgroups of systemd but then use Docker's cgroups for all the others, like the freezer cgroup.

Pid File
--------

If for whatever reason you want to create a pid file for the container PID, you can. Just add `--pid-file` as below

`ExecStart=/opt/bin/systemd-docker --pid-file=/var/run/%n.pid --env run --rm --name %n nginx`

systemd-notify support
----------------------

Expand Down
22 changes: 21 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Context struct {
NotifySocket string
Cmd *exec.Cmd
Pid int
PidFile string
Client *dockerClient.Client
}

Expand Down Expand Up @@ -72,10 +73,11 @@ func parseContext(args []string) (*Context, error) {
AllCgroups: false,
}

flags := flag.NewFlagSet("run", flag.ContinueOnError)
flags := flag.NewFlagSet("systemd-docker", flag.ContinueOnError)

var flCgroups opts.ListOpts

flags.StringVar(&c.PidFile, []string{"p", "-pid-file"}, "", "pipe file")
flags.BoolVar(&c.Logs, []string{"l", "-logs"}, true, "pipe logs")
flags.BoolVar(&c.Notify, []string{"n", "-notify"}, false, "setup systemd notify for container")
flags.BoolVar(&c.Env, []string{"e", "-env"}, false, "inherit environment variable")
Expand Down Expand Up @@ -443,6 +445,19 @@ func notify(c *Context) error {
return nil
}

func pidFile(c *Context) error {
if len(c.PidFile) == 0 || c.Pid <= 0 {
return nil
}

err := ioutil.WriteFile(c.PidFile, []byte(strconv.Itoa(c.Pid)), 0644)
if err != nil {
return err
}

return nil
}

func pipeLogs(c *Context) error {
if !c.Logs {
return nil
Expand Down Expand Up @@ -527,6 +542,11 @@ func mainWithArgs(args []string) (*Context, error) {
return c, err
}

err = pidFile(c)
if err != nil {
return c, err
}

go pipeLogs(c)

err = keepAlive(c)
Expand Down
39 changes: 39 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"strconv"
"syscall"
"testing"

Expand Down Expand Up @@ -452,3 +454,40 @@ func TestNamedContainerAttach(t *testing.T) {

deleteTestContainer(t)
}

func Exist(path string) bool {
_, err := os.Stat(path)
return os.IsExist(err)
}

func TestPidFile(t *testing.T) {
client, err := getClient(&Context{})
if err != nil {
t.Fatal(err)
}

pidFileName := "./pid-file"

os.Remove(pidFileName)

c, err := mainWithArgs([]string{"--logs=false", "--pid-file", "./pid-file", "run", "--rm", "busybox", "echo", "hi"})
if err != nil {
t.Fatal(err)
}

_, err = client.InspectContainer(c.Id)
if err == nil {
t.Fatal("Container should not exist")
}

bytes, err := ioutil.ReadFile(pidFileName)
if err != nil {
t.Fatal(err)
}

if string(bytes) != strconv.Itoa(c.Pid) {
t.Fatal("Failed to write pid file")
}

os.Remove(pidFileName)
}

0 comments on commit 50f992a

Please sign in to comment.