Skip to content

Commit

Permalink
Use execve to reuse the memory. Add a note about init
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Dementiev committed Oct 29, 2019
1 parent 896d1f9 commit d60349a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.12-alpine as build
FROM golang:1.13-alpine as build

RUN apk update && apk add git

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ SSM Parent

This is mostly a parent process for Docker with one addition: it can read from AWS SSM Parameter store.

Please note, that it still requires a proper `init` process, for example the one embedded into Docker can be used with `docker run --init`.

The way it works is that ssm-parent can be used as an entrypoint for Docker. Firstly, it retrieves all specified parameters, then injects them to the environment,
and finally runs the command.
and finally runs the command using `execve` syscall.

All parameters must be in JSON format, i.e.:

Expand Down
25 changes: 5 additions & 20 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"os/exec"
"os/signal"
"syscall"

"github.com/springload/ssm-parent/ssm"

Expand Down Expand Up @@ -43,34 +44,18 @@ var runCmd = &cobra.Command{
if err != nil {
ctx.WithError(err).Fatal("Cant find the command")
}
cmdArgs = append(cmdArgs, args[:1]...)

c := make(chan os.Signal, 1)
signal.Notify(c)
if expand {
cmdArgs = ssm.ExpandArgs(args[1:])
cmdArgs = append(cmdArgs, ssm.ExpandArgs(args[1:])...)
} else {
cmdArgs = args[1:]
cmdArgs = append(cmdArgs, args[1:]...)
}

cmd := exec.Command(command, cmdArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()

if err := cmd.Start(); err != nil {
if err := syscall.Exec(command, cmdArgs, os.Environ()); err != nil {
ctx.WithError(err).Fatal("Can't run the command")
}

go func() {
for sig := range c {
cmd.Process.Signal(sig)
}
}()

if err := cmd.Wait(); err != nil {
ctx.WithError(err).Fatal("The command exited with an error")
}
},
}

Expand Down

0 comments on commit d60349a

Please sign in to comment.