Skip to content

Commit

Permalink
Address FAB-1454 add docker image for fabric-cop
Browse files Browse the repository at this point in the history
Add creation of docker image for fabric-cop, essentially
transposed from fabric docker image build process.

Change-Id: Id26e53e705e289515e9ce28af3df534123f463de
Signed-off-by: Christopher Ferris <[email protected]>
  • Loading branch information
christo4ferris committed Dec 20, 2016
1 parent 053b807 commit 4526770
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin
build
cli/cop/cop
testdata/client.json
*.csr
Expand Down
114 changes: 99 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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
# Copyright IBM Corp All Rights Reserved.
#
# http://www.apache.org/licenses/LICENSE-2.0
# 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
#
# 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.
# 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.
#
# -------------------------------------------------------------
# This makefile defines the following targets
Expand All @@ -24,7 +21,34 @@
# - unit-tests - Performs checks first and runs the go-test based unit tests
# - checks - runs all check conditions (license, format, imports, lint and vet)

all: unit-tests
PROJECT_NAME = hyperledger/fabric-cop
BASE_VERSION = 0.7.0
IS_RELEASE = false

ifneq ($(IS_RELEASE),true)
EXTRA_VERSION ?= snapshot-$(shell git rev-parse --short HEAD)
PROJECT_VERSION=$(BASE_VERSION)-$(EXTRA_VERSION)
else
PROJECT_VERSION=$(BASE_VERSION)
endif

# Check that all dependencies are installed
EXECUTABLES = go docker git curl
K := $(foreach exec,$(EXECUTABLES),\
$(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH: Check dependencies")))

ARCH=$(shell uname -m)
BASEIMAGE_RELEASE = 0.2.2
PKGNAME = github.com/hyperledger/fabric-cop
pkgmap.cop := $(PKGNAME)

IMAGES = cop runtime

include docker-env.mk

all: docker unit-tests

docker: $(patsubst %,build/image/%/$(DUMMY), $(IMAGES))

checks: license vet lint format imports

Expand All @@ -48,6 +72,55 @@ cop:
@mkdir -p bin && cd cli && go build -o ../bin/cop
@echo "Built bin/cop"

# We (re)build a package within a docker context but persist the $GOPATH/pkg
# directory so that subsequent builds are faster
build/docker/bin/cop:
@echo "Building $@"
@mkdir -p build/docker/bin build/docker/cop/pkg
@$(DRUN) \
-v $(abspath build/docker/bin):/opt/gopath/bin \
-v $(abspath build/docker/cop/pkg):/opt/gopath/pkg \
hyperledger/fabric-baseimage:$(BASE_DOCKER_TAG) \
go install -ldflags "$(DOCKER_GO_LDFLAGS)" $(pkgmap.cop)/cli
mv build/docker/bin/cli build/docker/bin/cop
@touch $@

build/docker/busybox:
@echo "Building $@"
@$(DRUN) \
hyperledger/fabric-baseimage:$(BASE_DOCKER_TAG) \
make -f busybox/Makefile install BINDIR=$(@D)

build/image/cop/$(DUMMY): build/image/runtime/$(DUMMY)

# payload definitions'
build/image/cop/payload: build/docker/bin/cop
build/image/runtime/payload: build/docker/busybox

build/image/%/payload:
mkdir -p $@
cp $^ $@

build/image/cop/$(DUMMY): Makefile build/image/cop/payload
@echo "Building docker fabric-cop image"
@cat images/cop/Dockerfile.in \
| sed -e 's/_BASE_TAG_/$(BASE_DOCKER_TAG)/g' \
| sed -e 's/_TAG_/$(DOCKER_TAG)/g' \
> $(@D)/Dockerfile
$(DBUILD) -t $(PROJECT_NAME) $(@D)
docker tag $(PROJECT_NAME) $(PROJECT_NAME):$(DOCKER_TAG)
@touch $@

build/image/runtime/$(DUMMY): Makefile build/image/runtime/payload
@echo "Building docker fabric-cop-runtime image"
@cat images/runtime/Dockerfile.in \
| sed -e 's/_BASE_TAG_/$(BASE_DOCKER_TAG)/g' \
| sed -e 's/_TAG_/$(DOCKER_TAG)/g' \
> $(@D)/Dockerfile
$(DBUILD) -t $(PROJECT_NAME)-runtime $(@D)
docker tag $(PROJECT_NAME)-runtime $(PROJECT_NAME)-runtime:$(DOCKER_TAG)
@touch $@

unit-tests: checks cop
@scripts/run_tests

Expand All @@ -56,4 +129,15 @@ container-tests: ldap-tests
ldap-tests:
@scripts/run_ldap_tests

%-docker-clean:
$(eval TARGET = ${patsubst %-docker-clean,%,${@}})
-docker images -q $(PROJECT_NAME)-$(TARGET) | xargs -I '{}' docker rmi -f '{}'
-@rm -rf build/image/$(TARGET) ||:

docker-clean: $(patsubst %,%-docker-clean, $(IMAGES))

.PHONY: clean
clean: docker-clean
-@rm -rf build bin ||:

.FORCE:
27 changes: 27 additions & 0 deletions busybox/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
BUSYBOX_VER=1.25.1
BUSYBOX_URL=https://www.busybox.net/downloads/busybox-$(BUSYBOX_VER).tar.bz2

OBJDIR=build/busybox-$(BUSYBOX_VER)

all: $(OBJDIR)/busybox

install: $(BINDIR)/busybox

$(BINDIR)/busybox: $(OBJDIR)/busybox
mkdir -p $(@D)
cp $< $@

$(OBJDIR)/.source:
mkdir -p $(@D)
curl -L $(BUSYBOX_URL) | (cd $(@D); tar --strip-components=1 -jx)
touch $@

$(OBJDIR)/.config: $(OBJDIR)/.source
make -C $(@D) defconfig

$(OBJDIR)/busybox: Makefile $(OBJDIR)/.config
make -C $(@D) -l 2.5 -j all LDFLAGS=-static

clean:
-rm -rf $(OBJDIR)

87 changes: 87 additions & 0 deletions docker-env.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

ifneq ($(shell uname),Darwin)
DOCKER_RUN_FLAGS=--user=$(shell id -u)
endif

ifneq ($(http_proxy),)
DOCKER_BUILD_FLAGS+=--build-arg http_proxy=$(http_proxy)
DOCKER_RUN_FLAGS+=-e http_proxy=$(http_proxy)
endif
ifneq ($(https_proxy),)
DOCKER_BUILD_FLAGS+=--build-arg https_proxy=$(https_proxy)
DOCKER_RUN_FLAGS+=-e https_proxy=$(https_proxy)
endif
ifneq ($(HTTP_PROXY),)
DOCKER_BUILD_FLAGS+=--build-arg HTTP_PROXY=$(HTTP_PROXY)
DOCKER_RUN_FLAGS+=-e HTTP_PROXY=$(HTTP_PROXY)
endif
ifneq ($(HTTPS_PROXY),)
DOCKER_BUILD_FLAGS+=--build-arg HTTPS_PROXY=$(HTTPS_PROXY)
DOCKER_RUN_FLAGS+=-e HTTPS_PROXY=$(HTTPS_PROXY)
endif
ifneq ($(no_proxy),)
DOCKER_BUILD_FLAGS+=--build-arg no_proxy=$(no_proxy)
DOCKER_RUN_FLAGS+=-e no_proxy=$(no_proxy)
endif
ifneq ($(NO_PROXY),)
DOCKER_BUILD_FLAGS+=--build-arg NO_PROXY=$(NO_PROXY)
DOCKER_RUN_FLAGS+=-e NO_PROXY=$(NO_PROXY)
endif

DRUN = docker run -i --rm $(DOCKER_RUN_FLAGS) \
-v $(abspath .):/opt/gopath/src/$(PKGNAME) \
-w /opt/gopath/src/$(PKGNAME)

DBUILD = docker build $(DOCKER_BUILD_FLAGS)

DOCKER_TAG=$(ARCH)-$(PROJECT_VERSION)
BASE_DOCKER_TAG=$(ARCH)-$(BASEIMAGE_RELEASE)

DOCKER_GO_LDFLAGS += $(GO_LDFLAGS)
DOCKER_GO_LDFLAGS += -linkmode external -extldflags '-static -lpthread'

#
# What is a .dummy file?
#
# Make is designed to work with files. It uses the presence (or lack thereof)
# and timestamps of files when deciding if a given target needs to be rebuilt.
# Docker containers throw a wrench into the works because the output of docker
# builds do not translate into standard files that makefile rules can evaluate.
# Therefore, we have to fake it. We do this by constructioning our rules such
# as
# my-docker-target/.dummy:
# docker build ...
# touch $@
#
# If the docker-build succeeds, the touch operation creates/updates the .dummy
# file. If it fails, the touch command never runs. This means the .dummy
# file follows relatively 1:1 with the underlying container.
#
# This isn't perfect, however. For instance, someone could delete a docker
# container using docker-rmi outside of the build, and make would be fooled
# into thinking the dependency is statisfied when it really isn't. This is
# our closest approximation we can come up with.
#
# As an aside, also note that we incorporate the version number in the .dummy
# file to differentiate different tags to fix FAB-1145
#
DUMMY = .dummy-$(DOCKER_TAG)



6 changes: 6 additions & 0 deletions images/cop/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM hyperledger/fabric-cop-runtime:_TAG_
ENV COP_HOME /etc/hyperledger/fabric-cop
RUN mkdir -p /etc/hyperledger/fabric-cop
COPY payload/cop /usr/local/bin
EXPOSE 8888
CMD cop
6 changes: 6 additions & 0 deletions images/runtime/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM scratch
COPY payload/busybox /bin/busybox
RUN ["/bin/busybox", "mkdir", "-p", "/usr/bin", "/sbin", "/usr/sbin"]
RUN ["/bin/busybox", "--install"]
RUN mkdir -p /usr/local/bin
ENV PATH=$PATH:/usr/local/bin

0 comments on commit 4526770

Please sign in to comment.