Skip to content

Commit

Permalink
Merge pull request #37 from pathwar/dev/moul/entrypoint
Browse files Browse the repository at this point in the history
feat: inject entrypoint (self) in the container before starting
  • Loading branch information
moul committed Feb 14, 2019
2 parents b1d3a14 + e4df693 commit 396284f
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,9 +1,11 @@
entrypoint/out/
vendor/
*~
*#
.#*

/.generated
/.*.generated

# Binaries for programs and plugins
*.exe
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -6,7 +6,7 @@ COPY go.* /go/src/pathwar.pw/
WORKDIR /go/src/pathwar.pw
RUN go get .
COPY . .
RUN touch .generated && make install
RUN touch .proto.generated && make install

# runtime
FROM alpine:3.8
Expand Down
29 changes: 20 additions & 9 deletions Makefile
Expand Up @@ -11,12 +11,18 @@ rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subs
GOPATH ?= $(HOME)/go
BIN = $(GOPATH)/bin/pathwar.pw
SOURCES = $(call rwildcard, ./, *.go)
ENTRYPOINT_SOURCES = ./entrypoint/main.go
OUR_SOURCES = $(filter-out $(call rwildcard,vendor//,*.go),$(SOURCES))
PROTOS = $(call rwildcard, ./, *.proto)
OUR_PROTOS = $(filter-out $(call rwildcard,vendor//,*.proto),$(PROTOS))
GENERATED_FILES = \
GENERATED_PB_FILES = \
$(patsubst %.proto,%.pb.go,$(PROTOS)) \
$(call rwildcard ./, *.gen.go)
ENTRYPOINT_OUT_FILES = \
./entrypoint/out/entrypoint-linux-amd64
GENERATED_FILES = \
$(GENERATED_PB_FILES) \
$(ENTRYPOINT_OUT_FILES)
PROTOC_OPTS = -I/protobuf:vendor:.
RUN_OPTS ?=

Expand All @@ -38,22 +44,23 @@ run: $(BIN)

.PHONY: install
install: $(BIN)
$(BIN): .generated $(OUR_SOURCES)
$(BIN): .proto.generated $(ENTRYPOINT_OUT_FILES) $(OUR_SOURCES)
go install -v

.PHONY: clean
clean:
rm -f $(GENERATED_FILES) .generated
rm -f $(GENERATED_FILES) .proto.generated

.PHONY: _ci_prepare
_ci_prepare:
mkdir -p $(dir $(GENERATED_FILES))
touch $(OUR_PROTOS) $(GENERATED_FILES)
sleep 1
touch .generated
touch .proto.generated

.PHONY: generate
generate: .generated
.generated: $(OUR_PROTOS)
generate: .proto.generated
.proto.generated: $(OUR_PROTOS)
rm -f $(GENERATED_FILES)
go mod vendor
docker run \
Expand All @@ -63,14 +70,18 @@ generate: .generated
--entrypoint="sh" \
--rm \
pathwar/protoc:v1 \
-xec "make _generate"
-xec "make _proto_generate"
touch $@

.PHONY: _generate
_generate: $(GENERATED_FILES)
_proto_generate: $(GENERATED_PB_FILES)

$(ENTRYPOINT_OUT_FILES): $(ENTRYPOINT_SOURCES)
mkdir -p ./entrypoint/out
GOOS=linux GOARCH=amd64 go build -o ./entrypoint/out/entrypoint-linux-amd64 $<

.PHONY: test
test: .generated
test: .proto.generated
go test -v ./...

%.pb.go: %.proto
Expand Down
21 changes: 21 additions & 0 deletions entrypoint/main.go
@@ -0,0 +1,21 @@
package main

import (
"log"
"os"
"os/exec"
"syscall"
)

func main() {
log.Println("Hello world from entrypoint, os.Args:", os.Args)
binary, err := exec.LookPath(os.Args[1])
if err != nil {
panic(err)
}
args := os.Args[1:]
env := os.Environ()
if err := syscall.Exec(binary, args, env); err != nil {
panic(err)
}
}
14 changes: 9 additions & 5 deletions go.mod
@@ -1,25 +1,25 @@
module pathwar.pw

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.11 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v0.7.3-0.20181024220401-bc4c1c238b55
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.3.3 // indirect
github.com/dockerparis/docker-pot v1.4.0
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gofrs/uuid v3.1.0+incompatible // indirect
github.com/gobuffalo/packr/v2 v2.0.0-rc.15
github.com/gogo/gateway v1.0.0
github.com/gogo/protobuf v1.2.0
github.com/golang/protobuf v1.2.0
github.com/google/go-cmp v0.2.0 // indirect
github.com/gorilla/mux v1.7.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190104160321-4832df01553a
github.com/grpc-ecosystem/grpc-gateway v1.6.4
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/gorm v1.9.2
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect
Expand All @@ -28,13 +28,17 @@ require (
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.3.0 // indirect
github.com/spf13/afero v1.2.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.3.1
go.uber.org/zap v1.9.1
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc // indirect
golang.org/x/net v0.0.0-20190110044637-be1c187aa6c6
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
google.golang.org/appengine v1.2.0 // indirect
google.golang.org/genproto v0.0.0-20190108161440-ae2f86662275
google.golang.org/grpc v1.17.0
gotest.tools v2.2.0+incompatible // indirect
moul.io/zapgorm v0.0.0-20181220094309-2149c5ded6fc
)

0 comments on commit 396284f

Please sign in to comment.