Skip to content

Commit 080e8b0

Browse files
committed
embed runtime files
1 parent a6d4dfe commit 080e8b0

33 files changed

+246
-66
lines changed

Makefile

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,71 @@ PKG := "github.com/OpenFogStack/$(PROJECT_NAME)"
33
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v /ext/ | grep -v _test.go)
44
TEST_DIR := ./test
55

6+
SUPPORTED_ARCH=amd64 arm arm64
7+
RUNTIMES := $(shell find pkg/docker/runtimes -name Dockerfile | xargs -n1 dirname | xargs -n1 basename)
8+
69
OS=$(shell go env GOOS)
710
ARCH=$(shell go env GOARCH)
811

9-
.PHONY: all build start clean
10-
12+
.PHONY: all
1113
all: build
1214

15+
.PHONY: build
1316
build: tinyfaas-${OS}-${ARCH}
1417

18+
.PHONY: start
19+
start: tinyfaas-${OS}-${ARCH}
20+
./$<
21+
22+
.PHONY: test
23+
test: build ${TEST_DIR}/test_all.py
24+
@python3 ${TEST_DIR}/test_all.py
25+
26+
.PHONY: clean
27+
clean: clean.sh
28+
@sh clean.sh
29+
30+
31+
define arch_build
32+
pkg/docker/runtimes-$(arch): $(foreach runtime,$(RUNTIMES),pkg/docker/runtimes-$(arch)/$(runtime))
33+
endef
34+
$(foreach arch,$(SUPPORTED_ARCH),$(eval $(arch_build)))
35+
36+
define runtime_build
37+
.PHONY: pkg/docker/runtimes-$(arch)/$(runtime)
38+
pkg/docker/runtimes-$(arch)/$(runtime): pkg/docker/runtimes-$(arch)/$(runtime)/Dockerfile pkg/docker/runtimes-$(arch)/$(runtime)/blob.tar.gz
39+
40+
pkg/docker/runtimes-$(arch)/$(runtime)/blob.tar.gz: pkg/docker/runtimes/$(runtime)/build.Dockerfile
41+
mkdir -p $$(@D)
42+
cd $$(<D) ; docker build --platform=linux/$(arch) -t tf-build-$(arch)-$(runtime) -f $$(<F) .
43+
docker run -d -t --platform=linux/$(arch) --name $${PROJECT_NAME}-$(runtime) --rm tf-build-$(arch)-$(runtime)
44+
docker export $${PROJECT_NAME}-$(runtime) | gzip > $$@
45+
docker kill $${PROJECT_NAME}-$(runtime)
46+
47+
pkg/docker/runtimes-$(arch)/$(runtime)/Dockerfile: pkg/docker/runtimes/$(runtime)/Dockerfile
48+
mkdir -p $$(@D)
49+
cp -r pkg/docker/runtimes/$(runtime)/Dockerfile $$@
50+
endef
51+
$(foreach arch,$(SUPPORTED_ARCH),$(foreach runtime,$(RUNTIMES),$(eval $(runtime_build))))
52+
1553
# requires protoc, protoc-gen-go and protoc-gen-go-grpc
1654
# install from your package manager, e.g.:
1755
# brew install protobuf
1856
# brew install protoc-gen-go
1957
# brew install protoc-gen-go-grpc
2058
pkg/grpc/tinyfaas/tinyfaas.pb.go pkg/grpc/tinyfaas/tinyfaas_grpc.pb.go: pkg/grpc/tinyfaas/tinyfaas.proto
21-
@protoc -I . $< --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=require_unimplemented_servers=false,paths=source_relative
59+
@protoc -I $(<D) $< --go_out=$(<D) --go_opt=paths=source_relative --go-grpc_out=$(<D) --go-grpc_opt=require_unimplemented_servers=false,paths=source_relative
2260

2361
# requires grpcio-tools and mypy-protobuf
2462
# python3 -m pip install -r requirements.txt
2563
pkg/grpc/tinyfaas/tinyfaas_pb2.py pkg/grpc/tinyfaas/tinyfaas_pb2.pyi pkg/grpc/tinyfaas/tinyfaas_pb2_grpc.py: pkg/grpc/tinyfaas/tinyfaas.proto
26-
@python3 -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. --mypy_out=. $<
64+
@python3 -m grpc_tools.protoc -I $(<D) --python_out=$(<D) --grpc_python_out=$(<D) --mypy_out=$(<D) $<
2765

2866
cmd/manager/rproxy-%.bin: pkg/grpc/tinyfaas/tinyfaas_pb2.py pkg/grpc/tinyfaas/tinyfaas_pb2.pyi pkg/grpc/tinyfaas/tinyfaas_pb2_grpc.py pkg/grpc/tinyfaas/tinyfaas.pb.go pkg/grpc/tinyfaas/tinyfaas_grpc.pb.go $(GO_FILES)
2967
GOOS=$(word 1,$(subst -, ,$*)) GOARCH=$(word 2,$(subst -, ,$*)) go build -o $@ -v $(PKG)/cmd/rproxy
3068

31-
tinyfaas-%: cmd/manager/rproxy-%.bin $(GO_FILES)
32-
GOOS=$(word 1,$(subst -, ,$*)) GOARCH=$(word 2,$(subst -, ,$*)) go build -o $@ -v $(PKG)/cmd/manager
33-
34-
start: tinyfaas-${OS}-${ARCH}
35-
./$<
36-
37-
test: build ${TEST_DIR}/test_all.py
38-
@python3 ${TEST_DIR}/test_all.py
69+
tinyfaas-darwin-%: cmd/manager/rproxy-darwin-%.bin pkg/docker/runtimes-% $(GO_FILES)
70+
GOOS=darwin GOARCH=$* go build -o $@ -v $(PKG)/cmd/manager
3971

40-
clean: clean.sh
41-
@sh clean.sh
72+
tinyfaas-linux-%: cmd/manager/rproxy-linux-%.bin pkg/docker/runtimes-% $(GO_FILES)
73+
GOOS=linux GOARCH=$* go build -o $@ -v $(PKG)/cmd/manager

cmd/manager/rproxy-linux-arm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build linux && armv7
2-
// +build linux,armv7
1+
//go:build linux && arm
2+
// +build linux,arm
33

44
package main
55

pkg/docker/handler.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7-
"embed"
87
"fmt"
98
"io"
109
"log"
@@ -24,9 +23,6 @@ import (
2423
"github.com/google/uuid"
2524
)
2625

27-
//go:embed runtimes
28-
var runtimes embed.FS
29-
3026
const (
3127
TmpDir = "./tmp"
3228
containerTimeout = 1
@@ -99,7 +95,7 @@ func (db *DockerBackend) Create(name string, env string, threads int, filedir st
9995
// copy Docker stuff into folder
10096
// cp runtimes/<env>/* <folder>
10197

102-
err = util.CopyDirFromEmbed(runtimes, path.Join("runtimes", dh.env), dh.filePath)
98+
err = util.CopyDirFromEmbed(runtimes, path.Join(runtimesDir, dh.env), dh.filePath)
10399
if err != nil {
104100
return nil, err
105101
}

pkg/docker/runtimes-amd64.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build amd64
2+
// +build amd64
3+
4+
package docker
5+
6+
import "embed"
7+
8+
//go:embed runtimes-amd64
9+
var runtimes embed.FS
10+
11+
const runtimesDir = "runtimes-amd64"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM scratch
2+
3+
ADD blob.tar.gz /
4+
5+
EXPOSE 8000
6+
7+
WORKDIR /usr/src/app
8+
9+
COPY . .
10+
RUN mv fn/* .
11+
RUN chmod +x fn.sh
12+
13+
CMD [ "./handler.bin" ]
7.02 MB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM scratch
2+
3+
ADD blob.tar.gz /
4+
5+
EXPOSE 8000
6+
7+
WORKDIR /usr/src/app
8+
9+
COPY . .
10+
11+
RUN npm install ./fn
12+
13+
CMD [ "node", "functionhandler.js" ]
49.4 MB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM scratch
2+
3+
ADD blob.tar.gz /
4+
5+
EXPOSE 8000
6+
7+
# Create app directory
8+
WORKDIR /usr/src/app
9+
10+
COPY fn/* ./
11+
RUN python -m pip install -r requirements.txt --user
12+
13+
ENV PYTHONUNBUFFERED=1
14+
CMD [ "python3", "functionhandler.py" ]
18.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)