Skip to content

Commit

Permalink
use go 1.13, replace glide with modules, add boilerplate docker_build
Browse files Browse the repository at this point in the history
- removed bootstrap make target, since it was only needed to install
  glide and run glide install
- updated Dockerfile stages to optimize dependency caching
- update docker-compose to use the base stage of Dockerfile for
  consistency
  • Loading branch information
jstewmon committed Oct 29, 2019
1 parent 3a85594 commit d3e0b1a
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 242 deletions.
19 changes: 15 additions & 4 deletions .travis.yml
@@ -1,11 +1,22 @@
sudo: required
language: go
go: "1.11"
services: redis-server
services:
- docker
- redis-server
before_install: sudo apt-get update -y && sudo apt-get install stunnel4 -y
install: make bootstrap bootstrap_redis_tls
before_script:
install: make bootstrap_redis_tls
before_script:
- redis-server --port 6380 &
- redis-server --port 6381 --requirepass password123 &
- redis-server --port 6382 --requirepass password123 &
script: make check_format tests
script: make check_format tests docker_build
jobs:
include:
- stage: release
if: tag IS present && !fork
script: skip
deploy:
provider: script
script: make docker_build docker_push

14 changes: 5 additions & 9 deletions Dockerfile
@@ -1,17 +1,13 @@
FROM golang:1.10.4 AS build
FROM golang:1.13.3 AS base
WORKDIR /go/src/github.com/lyft/ratelimit
COPY go.mod go.sum ./
RUN go mod download

FROM base as build
COPY src src
COPY script script
COPY vendor vendor
COPY glide.yaml glide.yaml
COPY glide.lock glide.lock
COPY proto proto

RUN script/install-glide
RUN glide install

RUN CGO_ENABLED=0 GOOS=linux go build -o /usr/local/bin/ratelimit -ldflags="-w -s" -v github.com/lyft/ratelimit/src/service_cmd
RUN CGO_ENABLED=0 GOOS=linux go build -o /usr/local/bin/ratelimit -ldflags="-w -s" -v ./src/service_cmd

FROM alpine:3.8 AS final
RUN apk --no-cache add ca-certificates
Expand Down
27 changes: 10 additions & 17 deletions Makefile
@@ -1,14 +1,11 @@
ifeq ("$(GOPATH)","")
$(error GOPATH must be set)
endif

SHELL := /bin/bash
GOREPO := ${GOPATH}/src/github.com/lyft/ratelimit

.PHONY: bootstrap
bootstrap:
script/install-glide
glide install
export REPOSITORY=ratelimit
include boilerplate/lyft/docker_build/Makefile

.PHONY: update_boilerplate
update_boilerplate:
@boilerplate/update.sh

.PHONY: bootstrap_tests
bootstrap_tests:
Expand Down Expand Up @@ -59,10 +56,10 @@ check_format: docs_format

.PHONY: compile
compile:
mkdir -p ${GOREPO}/bin
cd ${GOREPO}/src/service_cmd && go build -o ratelimit ./ && mv ./ratelimit ${GOREPO}/bin
cd ${GOREPO}/src/client_cmd && go build -o ratelimit_client ./ && mv ./ratelimit_client ${GOREPO}/bin
cd ${GOREPO}/src/config_check_cmd && go build -o ratelimit_config_check ./ && mv ./ratelimit_config_check ${GOREPO}/bin
mkdir -p bin
go build -o bin/ratelimit ./src/service_cmd
go build -o bin/ratelimit_client ./src/client_cmd
go build -o bin/ratelimit_config_check ./src/config_check_cmd

.PHONY: tests_unit
tests_unit: compile
Expand All @@ -71,7 +68,3 @@ tests_unit: compile
.PHONY: tests
tests: compile
go test -race -tags=integration ./...

.PHONY: docker
docker: tests
docker build . -t lyft/ratelimit:`git rev-parse HEAD`
8 changes: 2 additions & 6 deletions README.md
Expand Up @@ -67,10 +67,6 @@ go [here](https://golang.org/doc/install).
redis-server --port 6379 &
redis-server --port 6380 &
```
* To setup for the first time (only done once):
```bash
make bootstrap
```
* To compile:
```bash
make compile
Expand All @@ -96,8 +92,8 @@ go [here](https://golang.org/doc/install).
The docker-compose setup has three containers: redis, ratelimit-build, and ratelimit. In order to run the docker-compose setup from the root of the repo, run

```bash
glide install
docker-compose up
docker-compose run --rm ratelimit-build
docker-compose up ratelimit
```

The ratelimit-build container will build the ratelimit binary. Then via a shared volume the binary will be shared with the ratelimit container. This dual container setup is used in order to use a
Expand Down
12 changes: 12 additions & 0 deletions boilerplate/lyft/docker_build/Makefile
@@ -0,0 +1,12 @@
# WARNING: THIS FILE IS MANAGED IN THE 'BOILERPLATE' REPO AND COPIED TO OTHER REPOSITORIES.
# ONLY EDIT THIS FILE FROM WITHIN THE 'LYFT/BOILERPLATE' REPOSITORY:
#
# TO OPT OUT OF UPDATES, SEE https://github.com/lyft/boilerplate/blob/master/Readme.rst

.PHONY: docker_build
docker_build:
IMAGE_NAME=$$REPOSITORY ./boilerplate/lyft/docker_build/docker_build.sh

.PHONY: dockerhub_push
dockerhub_push:
IMAGE_NAME=lyft/$$REPOSITORY REGISTRY=docker.io ./boilerplate/lyft/docker_build/docker_build.sh
23 changes: 23 additions & 0 deletions boilerplate/lyft/docker_build/Readme.rst
@@ -0,0 +1,23 @@
Docker Build and Push
~~~~~~~~~~~~~~~~~~~~~

Provides a ``make docker_build`` target that builds your image locally.

Provides a ``make dockerhub_push`` target that pushes your final image to Dockerhub.

The Dockerhub image will tagged ``<REPOSITORY>:<GIT COMMIT SHA>``

If git head has a git tag, the Dockerhub image will also be tagged ``<IMAGE>:<GIT_TAG>``.

**To Enable:**

Add ``lyft/docker_build`` to your ``boilerplate/update.cfg`` file.

Add ``include boilerplate/lyft/docker_build/Makefile`` in your main ``Makefile`` _after_ your REPOSITORY environment variable

::

REPOSITORY=<myreponame>
include boilerplate/lyft/docker_build/Makefile

(this ensures the extra Make targets get included in your main Makefile)
67 changes: 67 additions & 0 deletions boilerplate/lyft/docker_build/docker_build.sh
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

# WARNING: THIS FILE IS MANAGED IN THE 'BOILERPLATE' REPO AND COPIED TO OTHER REPOSITORIES.
# ONLY EDIT THIS FILE FROM WITHIN THE 'LYFT/BOILERPLATE' REPOSITORY:
#
# TO OPT OUT OF UPDATES, SEE https://github.com/lyft/boilerplate/blob/master/Readme.rst

set -e

echo ""
echo "------------------------------------"
echo " DOCKER BUILD"
echo "------------------------------------"
echo ""

if [ -n "$REGISTRY" ]; then
# Do not push if there are unstaged git changes
CHANGED=$(git status --porcelain)
if [ -n "$CHANGED" ]; then
echo "Please commit git changes before pushing to a registry"
exit 1
fi
fi


GIT_SHA=$(git rev-parse HEAD)

IMAGE_TAG_SUFFIX=""
# for intermediate build phases, append -$BUILD_PHASE to all image tags
if [ -n "$BUILD_PHASE" ]; then
IMAGE_TAG_SUFFIX="-${BUILD_PHASE}"
fi

IMAGE_TAG_WITH_SHA="${IMAGE_NAME}:${GIT_SHA}${IMAGE_TAG_SUFFIX}"

RELEASE_SEMVER=$(git describe --tags --exact-match "$GIT_SHA" 2>/dev/null) || true
if [ -n "$RELEASE_SEMVER" ]; then
IMAGE_TAG_WITH_SEMVER="${IMAGE_NAME}:${RELEASE_SEMVER}${IMAGE_TAG_SUFFIX}"
fi

# build the image
# passing no build phase will build the final image
docker build -t "$IMAGE_TAG_WITH_SHA" --target=${BUILD_PHASE} .
echo "${IMAGE_TAG_WITH_SHA} built locally."

# if REGISTRY specified, push the images to the remote registy
if [ -n "$REGISTRY" ]; then

if [ -n "${DOCKER_REGISTRY_PASSWORD}" ]; then
docker login --username="$DOCKER_REGISTRY_USERNAME" --password="$DOCKER_REGISTRY_PASSWORD"
fi

docker tag "$IMAGE_TAG_WITH_SHA" "${REGISTRY}/${IMAGE_TAG_WITH_SHA}"

docker push "${REGISTRY}/${IMAGE_TAG_WITH_SHA}"
echo "${REGISTRY}/${IMAGE_TAG_WITH_SHA} pushed to remote."

# If the current commit has a semver tag, also push the images with the semver tag
if [ -n "$RELEASE_SEMVER" ]; then

docker tag "$IMAGE_TAG_WITH_SHA" "${REGISTRY}/${IMAGE_TAG_WITH_SEMVER}"

docker push "${REGISTRY}/${IMAGE_TAG_WITH_SEMVER}"
echo "${REGISTRY}/${IMAGE_TAG_WITH_SEMVER} pushed to remote."

fi
fi
1 change: 1 addition & 0 deletions boilerplate/update.cfg
@@ -0,0 +1 @@
lyft/docker_build
53 changes: 53 additions & 0 deletions boilerplate/update.sh
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

# WARNING: THIS FILE IS MANAGED IN THE 'BOILERPLATE' REPO AND COPIED TO OTHER REPOSITORIES.
# ONLY EDIT THIS FILE FROM WITHIN THE 'LYFT/BOILERPLATE' REPOSITORY:
#
# TO OPT OUT OF UPDATES, SEE https://github.com/lyft/boilerplate/blob/master/Readme.rst

set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"

OUT="$(mktemp -d)"
git clone git@github.com:lyft/boilerplate.git "${OUT}"

echo "Updating the update.sh script."
cp "${OUT}/boilerplate/update.sh" "${DIR}/update.sh"
echo ""


CONFIG_FILE="${DIR}/update.cfg"
README="https://github.com/lyft/boilerplate/blob/master/Readme.rst"

if [ ! -f "$CONFIG_FILE" ]; then
echo "$CONFIG_FILE not found."
echo "This file is required in order to select which features to include."
echo "See $README for more details."
exit 1
fi

if [ -z "$REPOSITORY" ]; then
echo '$REPOSITORY is required to run this script'
echo "See $README for more details."
exit 1
fi

while read directory; do
echo "***********************************************************************************"
echo "$directory is configured in update.cfg."
echo "-----------------------------------------------------------------------------------"
echo "syncing files from source."
dir_path="${OUT}/boilerplate/${directory}"
rm -rf "${DIR}/${directory}"
mkdir -p $(dirname "${DIR}/${directory}")
cp -r "$dir_path" "${DIR}/${directory}"
if [ -f "${DIR}/${directory}/update.sh" ]; then
echo "executing ${DIR}/${directory}/update.sh"
"${DIR}/${directory}/update.sh"
fi
echo "***********************************************************************************"
echo ""
done < "$CONFIG_FILE"

rm -rf "${OUT}"
17 changes: 11 additions & 6 deletions docker-compose.yml
@@ -1,4 +1,4 @@
version: "3"
version: "2.4"
services:

redis:
Expand All @@ -9,26 +9,31 @@ services:
- 6379:6379
networks:
- ratelimit-network
healthcheck:
test: redis-cli ping

# minimal container that builds the ratelimit service binary and exits.
ratelimit-build:
image: golang:1.10-alpine
working_dir: /go/src/github.com/lyft/ratelimit
build:
context: .
target: base
command: go build -o /usr/local/bin/ratelimit /go/src/github.com/lyft/ratelimit/src/service_cmd/main.go
volumes:
- .:/go/src/github.com/lyft/ratelimit
- binary:/usr/local/bin/

ratelimit:
image: alpine:3.6
build:
context: .
target: base
command: /usr/local/bin/ratelimit
ports:
- 8080:8080
- 8081:8081
- 6070:6070
depends_on:
- redis
- ratelimit-build
redis:
condition: service_healthy
networks:
- ratelimit-network
volumes:
Expand Down

0 comments on commit d3e0b1a

Please sign in to comment.