Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding docker automation #48

Merged
merged 4 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.13.x
- 1.14.x
- tip

os:
Expand All @@ -19,6 +19,9 @@ script:
- make style codecov

jobs:
fast_finish: true
allow_failures:
- go: tip
include:
# - stage: integration
# name: "Integration Tests"
Expand Down Expand Up @@ -52,4 +55,14 @@ jobs:
api_key: "$GH_TOKEN"
file_glob: true
file: ./.ignore/*
skip_cleanup: true
skip_cleanup: true
- stage: docker-release
name: "Make Docker Release"
if: branch != master
script: skip
deploy:
- provider: script
script: bash deploy/docker_push
on:
all_branches: true
tags: true
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
- converting from glide to go mod
- updated release pipeline to use travis [#47](https://github.com/xmidt-org/petasos/pull/47)
- adding docker automation [#48](https://github.com/xmidt-org/petasos/pull/48)

## [v0.1.4]
fixed build upload
Expand Down
39 changes: 20 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ GOFMT ?= $(GO)fmt
APP := petasos
DOCKER_ORG := xmidt

PROGVER = $(shell git describe --tags `git rev-list --tags --max-count=1` | tail -1 | sed 's/v\(.*\)/\1/')
VERSION ?= $(shell git describe --tag --always --dirty)
PROGVER ?= $(shell git describe --tags `git rev-list --tags --max-count=1` | tail -1 | sed 's/v\(.*\)/\1/')
BUILDTIME = $(shell date -u '+%Y-%m-%d %H:%M:%S')
GITCOMMIT = $(shell git rev-parse --short HEAD)
GOBUILDFLAGS = -a -ldflags "-w -s -X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)" -o $(APP)

.PHONY: go-mod-vendor
go-mod-vendor:
GO111MODULE=on $(GO) mod vendor
.PHONY: vendor
vendor:
$(GO) mod vendor

.PHONY: build
build: go-mod-vendor
$(GO) build -o $(APP)
build: vendor
CGO_ENABLED=0 $(GO) build $(GOBUILDFLAGS)

.PHONY: version
version:
Expand All @@ -36,39 +38,38 @@ update-version:


.PHONY: install
install: go-mod-vendor
go install -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(PROGVER)"
install: vendor
$(GO) install -ldflags "-w -s -X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)"

.PHONY: release-artifacts
release-artifacts: go-mod-vendor
release-artifacts: vendor
mkdir -p ./.ignore
GOOS=darwin GOARCH=amd64 $(GO) build -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(PROGVER)" -o ./.ignore/$(APP)-$(PROGVER).darwin-amd64
GOOS=linux GOARCH=amd64 $(GO) build -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(PROGVER)" -o ./.ignore/$(APP)-$(PROGVER).linux-amd64
GOOS=darwin GOARCH=amd64 $(GO) build -o ./.ignore/$(APP)-$(PROGVER).darwin-amd64 -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)"
GOOS=linux GOARCH=amd64 $(GO) build -o ./.ignore/$(APP)-$(PROGVER).linux-amd64 -ldflags "-X 'main.BuildTime=$(BUILDTIME)' -X main.GitCommit=$(GITCOMMIT) -X main.Version=$(VERSION)"

.PHONY: docker
docker:
docker build \
--build-arg VERSION=$(PROGVER) \
--build-arg VERSION=$(VERSION) \
--build-arg GITCOMMIT=$(GITCOMMIT) \
--build-arg BUILDTIME='$(BUILDTIME)' \
-f ./deploy/Dockerfile -t $(DOCKER_ORG)/$(APP):$(PROGVER) .

# build docker without running modules
.PHONY: local-docker
local-docker:
docker build \
--build-arg VERSION=$(PROGVER)+local \
--build-arg VERSION=$(VERSION) \
--build-arg GITCOMMIT=$(GITCOMMIT) \
--build-arg BUILDTIME='$(BUILDTIME)' \
-f ./deploy/Dockerfile.local -t $(DOCKER_ORG)/$(APP):local .
-f ./deploy/Dockerfile -t $(DOCKER_ORG)/$(APP):local .

.PHONY: style
style:
style: vendor
! $(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print) | grep '^'

.PHONY: test
test: go-mod-vendor
GO111MODULE=on $(GO) test -v -race -coverprofile=cover.out ./...
test: vendor
$(GO) test -v -race -coverprofile=cover.out ./...

.PHONY: test-cover
test-cover: test
Expand All @@ -84,4 +85,4 @@ it:

.PHONY: clean
clean:
rm -rf ./$(APP) ./OPATH ./coverage.txt ./vendor
rm -rf ./$(APP) ./.ignore ./coverage.txt ./vendor
54 changes: 38 additions & 16 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
FROM golang:alpine as builder
FROM docker.io/library/golang:1.14-alpine as builder

MAINTAINER Jack Murdock <jack_murdock@comcast.com>

WORKDIR /go/src/github.com/xmidt-org/petasos
ARG VERSION=undefined
ARG GITCOMMIT=undefined
ARG BUILDTIME=undefined

RUN apk update && apk upgrade && \
apk add --no-cache bash git openssh
ARG VERSION=unknown
ARG GITCOMMIT=unknown
ARG BUILDTIME=unknown

ADD https://github.com/geofffranks/spruce/releases/download/v1.25.2/spruce-linux-amd64 /usr/local/bin/spruce
RUN chmod +x /usr/local/bin/spruce

RUN apk add --no-cache --no-progress \
ca-certificates \
make \
git \
openssh \
gcc \
libc-dev \
upx

COPY . .
RUN GO111MODULE=on go build -ldflags "-X 'main.BuildTime=${BUILDTIME}' -X main.GitCommit=${GITCOMMIT} -X main.Version=${VERSION}" -o petasos_linux_amd64
RUN make build

FROM alpine:latest

COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /go/src/github.com/xmidt-org/petasos/petasos.yaml /petasos.yaml
COPY --from=builder /go/src/github.com/xmidt-org/petasos/petasos /petasos
COPY --from=builder /go/src/github.com/xmidt-org/petasos/deploy/Dockerfile /go/src/github.com/xmidt-org/petasos/NOTICE /go/src/github.com/xmidt-org/petasos/LICENSE /go/src/github.com/xmidt-org/petasos/CHANGELOG.md /
COPY --from=builder /go/src/github.com/xmidt-org/petasos/deploy/packaging/entrypoint.sh /entrypoint.sh
COPY --from=builder /go/src/github.com/xmidt-org/petasos/deploy/packaging/petasos_spruce.yaml /tmp/petasos_spruce.yaml
COPY --from=builder /usr/local/bin/spruce /spruce

RUN mkdir /etc/petasos/ && touch /etc/petasos/petasos.yaml && chmod 666 /etc/petasos/petasos.yaml

FROM alpine
USER nobody

RUN apk --no-cache add ca-certificates
RUN mkdir -p /etc/petasos
VOLUME /etc/petasos
ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 8080
EXPOSE 6400
EXPOSE 6401
EXPOSE 6402
EXPOSE 6403

COPY --from=builder /go/src/github.com/xmidt-org/petasos/petasos_linux_amd64 /
COPY petasos.yaml /
COPY deploy/Dockerfile NOTICE LICENSE CHANGELOG.md /
ENTRYPOINT ["/petasos_linux_amd64"]
CMD ["/petasos"]
26 changes: 0 additions & 26 deletions deploy/Dockerfile.local

This file was deleted.

16 changes: 16 additions & 0 deletions deploy/docker_push
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh

# upload docker as $TRAVIS_TAG or latest
echo "$DOCKER_TOKEN" | docker login -u "$DOCKER_USERNAME" --password-stdin

LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1` | tail -1 | sed 's/v\(.*\)/\1/')
VERSION_TAG=$(echo "$TRAVIS_TAG" | sed 's/v\(.*\)/\1/')

docker build --build-arg VERSION="$VERSION_TAG" --build-arg GITCOMMIT="`git rev-parse --short HEAD`" --build-arg BUILDTIME="`date -u '+%Y-%m-%d %H:%M:%S'`" -f ./deploy/Dockerfile -t xmidt/petasos:$VERSION_TAG .

docker push xmidt/petasos:$VERSION_TAG

if [[ "$VERSION_TAG" == "$LATEST_TAG" ]]; then
docker tag xmidt/petasos:$VERSION_TAG xmidt/petasos:latest
docker push xmidt/petasos:latest
fi
49 changes: 49 additions & 0 deletions deploy/packaging/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env sh


set -e

# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
[ "${#FUNCNAME[@]}" -ge 2 ] \
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
&& [ "${FUNCNAME[1]}" = 'source' ]
}

# check arguments for an option that would cause /petasos to stop
# return true if there is one
_want_help() {
local arg
for arg; do
case "$arg" in
-'?'|--help|-v)
return 0
;;
esac
done
return 1
}

_main() {
# if command starts with an option, prepend petasos
if [ "${1:0:1}" = '-' ]; then
set -- /petasos "$@"
fi
# skip setup if they aren't running /petasos or want an option that stops /petasos
if [ "$1" = '/petasos' ] && ! _want_help "$@"; then
echo "Entrypoint script for petasos Server ${VERSION} started."

if [ ! -s /etc/petasos/petasos.yaml ]; then
echo "Building out template for file"
/spruce merge --prune service.fixed /petasos.yaml /tmp/petasos_spruce.yaml > /etc/petasos/petasos.yaml
fi
fi

exec "$@"
}

# If we are sourced from elsewhere, don't perform any further actions
if ! _is_sourced; then
_main "$@"
fi