From d9d20129945c1051347797a3782d1d134466107d Mon Sep 17 00:00:00 2001 From: Pierre Zemb Date: Sun, 8 Dec 2019 18:39:29 +0100 Subject: [PATCH] Add a Golang sample using docker-compose --- packaging/docker/samples/golang/.env | 26 ++++++ packaging/docker/samples/golang/README.md | 33 ++++++++ .../docker/samples/golang/app/Dockerfile | 46 +++++++++++ packaging/docker/samples/golang/app/go.mod | 24 ++++++ packaging/docker/samples/golang/app/go.sum | 2 + packaging/docker/samples/golang/app/main.go | 80 +++++++++++++++++++ .../docker/samples/golang/app/start.bash | 36 +++++++++ .../docker/samples/golang/docker-compose.yml | 64 +++++++++++++++ 8 files changed, 311 insertions(+) create mode 100644 packaging/docker/samples/golang/.env create mode 100644 packaging/docker/samples/golang/README.md create mode 100644 packaging/docker/samples/golang/app/Dockerfile create mode 100644 packaging/docker/samples/golang/app/go.mod create mode 100644 packaging/docker/samples/golang/app/go.sum create mode 100644 packaging/docker/samples/golang/app/main.go create mode 100755 packaging/docker/samples/golang/app/start.bash create mode 100644 packaging/docker/samples/golang/docker-compose.yml diff --git a/packaging/docker/samples/golang/.env b/packaging/docker/samples/golang/.env new file mode 100644 index 0000000000..3780764c07 --- /dev/null +++ b/packaging/docker/samples/golang/.env @@ -0,0 +1,26 @@ +# .env +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +COMPOSE_PROJECT_NAME=fdbgolangsample + +FDB_API_VERSION=620 +FDB_VERSION=6.2.11 +FDB_COORDINATOR=fdb-coordinator +FDB_NETWORKING_MODE=container +FDB_COORDINATOR_PORT=4500 \ No newline at end of file diff --git a/packaging/docker/samples/golang/README.md b/packaging/docker/samples/golang/README.md new file mode 100644 index 0000000000..0a0c0a87cc --- /dev/null +++ b/packaging/docker/samples/golang/README.md @@ -0,0 +1,33 @@ +# Golang sample using docker-compose + +This contains a sample `docker-compose.yaml` to run a simple golang application running with FoundationDB as a storage backend. + +All variables are located in `.env` file. + +## Start the golang demo + +```bash +docker-compose up -d +``` + +This will start: + +* 1 coordinator, +* 2 fdbservers, +* a golang application. + +You can now head to [http://localhost:8080/counter](http://localhost:8080/counter) and see the counter rising-up after each refresh. + +## Access the FoundationDB cluster + +If you want to access the cluster from your machine, here's a `cluster file` ready for you: + +```bash +echo "docker:docker@127.0.0.1:4500" > docker.cluster +``` + +## Stop the golang demo + +``` +docker-compose down +``` \ No newline at end of file diff --git a/packaging/docker/samples/golang/app/Dockerfile b/packaging/docker/samples/golang/app/Dockerfile new file mode 100644 index 0000000000..d2b2a1ac5d --- /dev/null +++ b/packaging/docker/samples/golang/app/Dockerfile @@ -0,0 +1,46 @@ +# Dockerfile +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# ARG needs to be defined for both FROM instructions, +# see https://github.com/moby/moby/issues/34129 +ARG FDB_VERSION +FROM foundationdb/foundationdb:${FDB_VERSION} as fdb +FROM golang:1.13.4-stretch +ARG FDB_VERSION + +WORKDIR /tmp + +RUN apt update +# dnsutils is needed to have dig installed to create cluster file +RUN apt install -y dnsutils + +RUN wget "https://www.foundationdb.org/downloads/${FDB_VERSION}/ubuntu/installers/foundationdb-clients_${FDB_VERSION}-1_amd64.deb" +RUN dpkg -i foundationdb-clients_${FDB_VERSION}-1_amd64.deb + +COPY --from=fdb /var/fdb/scripts/create_cluster_file.bash / + +WORKDIR /go/src/app +COPY . . + +COPY start.bash /start.bash + +RUN go get -d -v ./... +RUN go install -v ./... + +CMD ["/start.bash"] \ No newline at end of file diff --git a/packaging/docker/samples/golang/app/go.mod b/packaging/docker/samples/golang/app/go.mod new file mode 100644 index 0000000000..609602be19 --- /dev/null +++ b/packaging/docker/samples/golang/app/go.mod @@ -0,0 +1,24 @@ +// go.mod +// +// This source file is part of the FoundationDB open source project +// +// Copyright 2013-2019 Apple Inc. and the FoundationDB project authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +module foundationdb.org/docker/samples/golang/v0/fdb-demo-golang + +go 1.13 + +require github.com/apple/foundationdb/bindings/go v0.0.0-20191129023120-e16ae7cadf80 diff --git a/packaging/docker/samples/golang/app/go.sum b/packaging/docker/samples/golang/app/go.sum new file mode 100644 index 0000000000..7ebbfbaab3 --- /dev/null +++ b/packaging/docker/samples/golang/app/go.sum @@ -0,0 +1,2 @@ +github.com/apple/foundationdb/bindings/go v0.0.0-20191129023120-e16ae7cadf80 h1:VKL6OsaB8X91ijz5DEDOw2lBIxmqTUVm5A//EExEyvo= +github.com/apple/foundationdb/bindings/go v0.0.0-20191129023120-e16ae7cadf80/go.mod h1:OMVSB21p9+xQUIqlGizHPZfjK+SHws1ht+ZytVDoz9U= diff --git a/packaging/docker/samples/golang/app/main.go b/packaging/docker/samples/golang/app/main.go new file mode 100644 index 0000000000..eb953ef927 --- /dev/null +++ b/packaging/docker/samples/golang/app/main.go @@ -0,0 +1,80 @@ +// main.go +// +// This source file is part of the FoundationDB open source project +// +// Copyright 2013-2019 Apple Inc. and the FoundationDB project authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package main + +import ( + "encoding/binary" + "fmt" + "log" + "net/http" + "os" + "strconv" + + "github.com/apple/foundationdb/bindings/go/src/fdb" +) + +var db fdb.Database + +func main() { + + apiVersion, err := strconv.Atoi(os.Getenv("FDB_API_VERSION")) + if err != nil { + log.Fatal("cannot parse FDB_API_VERSION from env") + } + // Different API versions may expose different runtime behaviors. + fdb.MustAPIVersion(apiVersion) + + // Open the default database from the system cluster + db = fdb.MustOpenDatabase(os.Getenv("FDB_CLUSTER_FILE")) + + http.HandleFunc("/counter", incrementCounter) + + fmt.Println("starting webserver") + http.ListenAndServe(":8080", nil) +} + +func incrementCounter(w http.ResponseWriter, r *http.Request) { + ret, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + value := tr.Get(fdb.Key("my-counter")).MustGet() + if len(value) == 0 { + value = intToBytes(0) + } + counter := bytesToInt(value) + counter++ + tr.Set(fdb.Key("my-counter"), intToBytes(counter)) + return intToBytes(counter), nil + }) + + if e != nil { + log.Fatalf("Unable to perform FDB transaction (%v)", e) + } + + fmt.Fprintf(w, "Counter is %d", bytesToInt(ret.([]byte))) +} + +func bytesToInt(buf []byte) int { + return int(binary.BigEndian.Uint32(buf)) +} + +func intToBytes(i int) []byte { + v := uint32(i) + buf := make([]byte, 4) + binary.BigEndian.PutUint32(buf, v) + return buf +} diff --git a/packaging/docker/samples/golang/app/start.bash b/packaging/docker/samples/golang/app/start.bash new file mode 100755 index 0000000000..08e215ffb9 --- /dev/null +++ b/packaging/docker/samples/golang/app/start.bash @@ -0,0 +1,36 @@ +#!/bin/bash + +# start.bash +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -eu; + +/create_cluster_file.bash +FDB_CLUSTER_FILE="${FDB_CLUSTER_FILE:-/etc/foundationdb/fdb.cluster}" + +# Attempt to connect. Configure the database if necessary. +if ! /usr/bin/fdbcli -C $FDB_CLUSTER_FILE --exec status --timeout 3 ; then + echo "creating the database" + if ! fdbcli -C $FDB_CLUSTER_FILE --exec "configure new single memory ; status" --timeout 10 ; then + echo "Unable to configure new FDB cluster." + exit 1 + fi +fi + +/go/bin/fdb-demo-golang \ No newline at end of file diff --git a/packaging/docker/samples/golang/docker-compose.yml b/packaging/docker/samples/golang/docker-compose.yml new file mode 100644 index 0000000000..9d2d11e22a --- /dev/null +++ b/packaging/docker/samples/golang/docker-compose.yml @@ -0,0 +1,64 @@ +# docker-compose.yaml +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2019 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +version: '3' +services: + # Specify three fdbserver processes. + fdb-coordinator: + image: foundationdb/foundationdb:${FDB_VERSION} + ports: + - 4500:4500/tcp + environment: + FDB_COORDINATOR: ${FDB_COORDINATOR} + FDB_NETWORKING_MODE: ${FDB_NETWORKING_MODE} + FDB_COORDINATOR_PORT: ${FDB_COORDINATOR_PORT} + + fdb-server-1: + depends_on: + - fdb-coordinator + image: foundationdb/foundationdb:${FDB_VERSION} + environment: + FDB_COORDINATOR: ${FDB_COORDINATOR} + FDB_NETWORKING_MODE: ${FDB_NETWORKING_MODE} + FDB_COORDINATOR_PORT: ${FDB_COORDINATOR_PORT} + + fdb-server-2: + depends_on: + - fdb-coordinator + image: foundationdb/foundationdb:${FDB_VERSION} + environment: + FDB_COORDINATOR: ${FDB_COORDINATOR} + FDB_NETWORKING_MODE: ${FDB_NETWORKING_MODE} + FDB_COORDINATOR_PORT: ${FDB_COORDINATOR_PORT} + + # Bring up the application so that it depends on the cluster. + app: + depends_on: + - fdb-coordinator + - fdb-server-1 + - fdb-server-2 + build: + context: app + args: + FDB_VERSION: ${FDB_VERSION} + ports: + - 8080:8080/tcp + environment: + FDB_COORDINATOR: ${FDB_COORDINATOR} + FDB_API_VERSION: ${FDB_API_VERSION} \ No newline at end of file