From d60349aa813a58f4019f6b803785fed9933f2847 Mon Sep 17 00:00:00 2001 From: Eugene Dementiev Date: Wed, 30 Oct 2019 09:05:19 +1300 Subject: [PATCH] Use execve to reuse the memory. Add a note about init --- Dockerfile | 2 +- README.md | 4 +++- cmd/run.go | 25 +++++-------------------- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index b80ab83..b62191e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.12-alpine as build +FROM golang:1.13-alpine as build RUN apk update && apk add git diff --git a/README.md b/README.md index 658133a..e8ade54 100644 --- a/README.md +++ b/README.md @@ -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.: diff --git a/cmd/run.go b/cmd/run.go index 8a48091..5cf102d 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "os/signal" + "syscall" "github.com/springload/ssm-parent/ssm" @@ -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") - } }, }