2017-07-07 21:48:47 +00:00
# resin-supervisor Makefile
#
# If you're looking for an easy way to develop on the supervisor, check ./tools/dev/dindctl, which provides a simplified interface
# to this makefile.
#
# Build targets (require Docker 17.05 or greater):
# * supervisor (default) - builds a resin-supervisor image
# * deploy - pushes a resin-supervisor image to the registry, retrying up to 3 times
# * gosuper - builds the "gosuper" component (a golang image with the Go supervisor component at /go/bin/gosuper and /build/gosuper)
2017-12-14 03:55:26 +00:00
# * nodedeps, nodebuild - builds the node component, with the node_modules and src at /usr/src/app and /build (also includes a rootfs-overlay there)
2017-07-10 16:37:36 +00:00
# * supervisor-dind: build the development docker-in-docker supervisor that run-supervisor uses (requires a SUPERVISOR_IMAGE to be available locally)
2017-07-07 21:48:47 +00:00
#
# Variables for build targets:
# * ARCH: amd64/rpi/i386/armv7hf/armel/aarch64 architecture for which to build the supervisor - default: amd64
# * IMAGE: image to build or deploy - default: resin/$(ARCH)-supervisor:latest
# * MIXPANEL_TOKEN, PUBNUB_SUBSCRIBE_KEY, PUBNUB_PUBLISH_KEY: (optional) default pubnub and mixpanel keys to embed in the supervisor image
# * DISABLE_CACHE: if set to true, run build with no cache - default: false
# * DOCKER_BUILD_OPTIONS: Additional options for docker build, like --cache-from parameters
#
# Test/development targets:
2017-07-10 16:37:36 +00:00
# * run-supervisor, stop-supervisor - build and start or stop a docker-in-docker resin-supervisor (requires aufs, ability to run privileged containers, and a SUPERVISOR_IMAGE to be available locally)
2017-07-07 21:48:47 +00:00
# * format-gosuper, test-gosuper - build a gosuper image and run formatting or unit tests
# * test-integration - run an integration test (see gosuper/supertest). Requires a docker-in-docker supervisor to be running
#
# Variables for test/dev targets:
# * IMAGE: image to build and run (either for run-supervisor or test-gosuper/integration)
2017-07-10 16:37:36 +00:00
# * SUPERVISOR_IMAGE: In run-supervisor and supervisor-dind, the supervisor image to run inside the docker-in-docker image
2017-07-07 21:48:47 +00:00
# * PRELOADED_IMAGE: If true, will preload user app image from tools/dev/apps.json and bind mount apps.json into the docker-in-docker supervisor
# * SUPERVISOR_EXTRA_MOUNTS: Additional bind mount flags for the docker-in-docker supervisor
# * PASSWORDLESS_DROPBEAR: For run-supervisor - start a passwordless ssh daemon in the docker-in-docker supervisor
2017-10-10 17:01:31 +00:00
# * CONTAINER_NAME: For run-supervisor, specify the container name for the docker-in-docker container (default: resin_supervisor_1)
2017-10-10 17:21:21 +00:00
# * CONFIG_FILENAME: For run-supervisor, specify the filename to mount as config.json, relative to tools/dind/ (default: config.json)
2017-07-07 21:48:47 +00:00
#
THIS_FILE := $( lastword $( MAKEFILE_LIST) )
help :
@cat $( THIS_FILE) | awk '{if(/^#/)print;else exit}' | sed 's/\#//'
2016-07-07 20:07:15 +00:00
OS := $( shell uname)
2017-07-07 21:48:47 +00:00
# If we're behind a proxy, use it during build
2016-06-28 18:22:24 +00:00
i f d e f h t t p _ p r o x y
DOCKER_HTTP_PROXY = --build-arg http_proxy = $( http_proxy)
e n d i f
i f d e f h t t p s _ p r o x y
DOCKER_HTTPS_PROXY = --build-arg https_proxy = $( https_proxy)
e n d i f
2016-06-30 17:47:01 +00:00
i f d e f n o _ p r o x y
2016-07-08 01:37:29 +00:00
DOCKER_NO_PROXY = --build-arg no_proxy = $( no_proxy)
2016-06-30 17:47:01 +00:00
e n d i f
2016-06-30 17:37:52 +00:00
i f d e f u s e _ p r o x y _ a t _ r u n t i m e
rt_http_proxy = $( http_proxy)
rt_https_proxy = $( https_proxy)
2016-06-30 17:47:01 +00:00
rt_no_proxy = $( no_proxy)
2016-06-30 17:37:52 +00:00
e n d i f
2017-07-07 21:48:47 +00:00
DISABLE_CACHE ?= 'false'
2014-07-03 15:29:49 +00:00
2016-10-16 08:34:24 +00:00
DOCKER_VERSION := $( shell docker version --format '{{.Server.Version}}' )
DOCKER_MAJOR_VERSION := $( word 1, $( subst ., ,$( DOCKER_VERSION) ) )
DOCKER_MINOR_VERSION := $( word 2, $( subst ., ,$( DOCKER_VERSION) ) )
2017-07-07 21:48:47 +00:00
DOCKER_GE_17_05 := $( shell [ $( DOCKER_MAJOR_VERSION) -gt 17 -o \( $( DOCKER_MAJOR_VERSION) -eq 17 -a $( DOCKER_MINOR_VERSION) -ge 5 \) ] && echo true )
2015-10-14 19:53:33 +00:00
2017-07-07 21:48:47 +00:00
# Default values for Pubnub and Mixpanel keys
PUBNUB_SUBSCRIBE_KEY ?= sub-c-bananas
PUBNUB_PUBLISH_KEY ?= pub-c-bananas
MIXPANEL_TOKEN ?= bananasbananas
2016-07-07 20:07:15 +00:00
2017-07-07 21:48:47 +00:00
# Default architecture and output image
ARCH ?= amd64
IMAGE ?= resin/$( ARCH) -supervisor:master
2016-04-14 13:04:46 +00:00
2017-07-07 21:48:47 +00:00
# Default values for run-supervisor
SUPERVISOR_IMAGE ?= resin/$( ARCH) -supervisor:master
PASSWORDLESS_DROPBEAR ?= false
2017-10-10 17:01:31 +00:00
CONTAINER_NAME ?= resin_supervisor_1
2017-10-10 17:21:21 +00:00
CONFIG_FILENAME ?= config.json
2016-04-14 13:04:46 +00:00
2017-07-07 21:48:47 +00:00
# Bind mounts and variables for the run-supervisor target
2017-10-10 17:21:21 +00:00
SUPERVISOR_DIND_MOUNTS := -v $$ ( pwd ) /../../:/resin-supervisor -v $$ ( pwd ) /$( CONFIG_FILENAME) :/mnt/conf/config.json -v $$ ( pwd ) /config/env:/usr/src/app/config/env -v $$ ( pwd ) /config/localenv:/usr/src/app/config/localenv
2016-07-07 20:07:15 +00:00
i f e q ( $( OS ) , L i n u x )
2016-09-10 10:20:28 +00:00
SUPERVISOR_DIND_MOUNTS := ${ SUPERVISOR_DIND_MOUNTS } -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v /bin/kmod:/bin/kmod
2016-07-07 20:07:15 +00:00
e n d i f
2017-07-07 21:48:47 +00:00
i f e q ( $( PRELOADED_IMAGE ) , t r u e )
2015-09-02 19:19:24 +00:00
SUPERVISOR_DIND_MOUNTS := ${ SUPERVISOR_DIND_MOUNTS } -v $$ ( pwd ) /apps.json:/usr/src/app/config/apps.json
e l s e
PRELOADED_IMAGE =
e n d i f
2017-07-07 21:48:47 +00:00
SUPERVISOR_EXTRA_MOUNTS ?=
2015-07-17 20:47:26 +00:00
2017-07-07 21:48:47 +00:00
i f d e f T A R G E T _ C O M P O N E N T
DOCKER_TARGET_COMPONENT := " --target= ${ TARGET_COMPONENT } "
e l s e
DOCKER_TARGET_COMPONENT :=
e n d i f
# Default target is to build the supervisor image
all : supervisor
2016-07-05 17:48:14 +00:00
2017-07-07 21:48:47 +00:00
# Settings to make the run-supervisor target work behind a proxy
2016-07-08 02:22:59 +00:00
DOCKERD_PROXY = tools/dind/config/services/docker.service.d/proxy.conf
${DOCKERD_PROXY} :
rm -f ${ DOCKERD_PROXY }
if [ -n " ${ rt_http_proxy } " ] ; then \
2016-08-23 13:23:18 +00:00
proxies = " \"HTTP_PROXY= ${ rt_http_proxy } \" " ; \
proxies = " $$ {proxies[*]} \"http_proxy= ${ rt_http_proxy } \" " ; \
2016-07-08 02:22:59 +00:00
fi ; \
if [ -n " ${ rt_https_proxy } " ] ; then \
2016-08-23 13:23:18 +00:00
proxies = " $$ {proxies[*]} \"HTTPS_PROXY= ${ rt_https_proxy } \" " ; \
proxies = " $$ {proxies[*]} \"https_proxy= ${ rt_https_proxy } \" " ; \
2016-07-08 02:22:59 +00:00
fi ; \
if [ -n " ${ rt_no_proxy } " ] ; then \
2016-08-23 13:23:18 +00:00
proxies = " $$ {proxies[*]} \"no_proxy= ${ rt_no_proxy } \" " ; \
2016-07-08 02:22:59 +00:00
fi ; \
2016-08-23 13:23:18 +00:00
if [ -n " ${ proxies } " ] ; then \
2016-07-08 02:22:59 +00:00
echo "[Service]" > ${ DOCKERD_PROXY } ; \
echo " Environment= $$ {proxies[*]} " >> ${ DOCKERD_PROXY } ; \
else \
touch ${ DOCKERD_PROXY } ; \
fi
2017-07-10 16:37:36 +00:00
supervisor-tar :
docker save --output tools/dind/supervisor-image.tar $( SUPERVISOR_IMAGE)
supervisor-dind : ${DOCKERD_PROXY } supervisor -tar
2016-09-06 13:41:17 +00:00
cd tools/dind \
&& docker build \
$( DOCKER_HTTP_PROXY) \
$( DOCKER_HTTPS_PROXY) \
$( DOCKER_NO_PROXY) \
2017-07-07 21:48:47 +00:00
${ DOCKER_BUILD_OPTIONS } \
2016-09-06 13:41:17 +00:00
--no-cache= $( DISABLE_CACHE) \
--build-arg PASSWORDLESS_DROPBEAR = $( PASSWORDLESS_DROPBEAR) \
2017-07-07 21:48:47 +00:00
-t $( IMAGE) .
2015-06-02 12:47:33 +00:00
2016-07-08 02:22:59 +00:00
run-supervisor : stop -supervisor supervisor -dind
2015-06-02 12:47:33 +00:00
cd tools/dind \
2016-07-07 20:07:15 +00:00
&& echo " SUPERVISOR_IMAGE= $( SUPERVISOR_IMAGE) " > config/localenv \
&& echo " PRELOADED_IMAGE= $( PRELOADED_IMAGE) " >> config/localenv \
2016-07-08 02:22:59 +00:00
&& echo " SUPERVISOR_EXTRA_MOUNTS= $( SUPERVISOR_EXTRA_MOUNTS) " >> config/localenv; \
if [ -n " $( rt_http_proxy) " ] ; then \
echo " HTTP_PROXY= $( rt_http_proxy) " >> config/localenv \
&& echo " http_proxy= $( rt_http_proxy) " >> config/localenv; \
fi ; \
if [ -n " $( rt_https_proxy) " ] ; then \
echo " HTTPS_PROXY= $( rt_https_proxy) " >> config/localenv \
&& echo " https_proxy= $( rt_https_proxy) " >> config/localenv; \
fi ; \
if [ -n " $( rt_no_proxy) " ] ; then \
echo " no_proxy= $( rt_no_proxy) " >> config/localenv; \
fi \
2017-10-10 17:01:31 +00:00
&& docker run -d --name $( CONTAINER_NAME) --privileged ${ SUPERVISOR_DIND_MOUNTS } $( IMAGE)
2015-06-02 12:47:33 +00:00
2015-07-16 16:23:14 +00:00
stop-supervisor :
2015-07-31 17:17:57 +00:00
# Stop docker and remove volumes to prevent us from running out of loopback devices,
2015-07-16 20:12:09 +00:00
# as per https://github.com/jpetazzo/dind/issues/19
2017-10-10 17:01:31 +00:00
-docker exec $( CONTAINER_NAME) bash -c "systemctl stop docker" || true
-docker stop $( CONTAINER_NAME) > /dev/null || true
-docker rm -f --volumes $( CONTAINER_NAME) > /dev/null || true
2015-07-16 16:23:14 +00:00
2017-07-07 21:48:47 +00:00
supervisor-image :
i f n e q ( $( DOCKER_GE_ 17_ 05) , t r u e )
@echo "Docker >= 17.05 is needed to build the supervisor"
@exit 1
2016-06-28 18:22:24 +00:00
e n d i f
2016-06-03 17:21:46 +00:00
docker build \
$( DOCKER_HTTP_PROXY) \
$( DOCKER_HTTPS_PROXY) \
$( DOCKER_NO_PROXY) \
2017-07-07 21:48:47 +00:00
$( DOCKER_TARGET_COMPONENT) \
$( DOCKER_BUILD_OPTIONS) \
--no-cache= $( DISABLE_CACHE) \
--build-arg ARCH = $( ARCH) \
--build-arg VERSION = $( shell jq -r .version package.json) \
--build-arg DEFAULT_PUBNUB_PUBLISH_KEY = $( PUBNUB_PUBLISH_KEY) \
--build-arg DEFAULT_PUBNUB_SUBSCRIBE_KEY = $( PUBNUB_SUBSCRIBE_KEY) \
--build-arg DEFAULT_MIXPANEL_TOKEN = $( MIXPANEL_TOKEN) \
2016-06-03 17:21:46 +00:00
-t $( IMAGE) .
2014-07-03 15:29:49 +00:00
2017-07-07 21:48:47 +00:00
supervisor :
@$( MAKE) -f $( THIS_FILE) IMAGE = $( IMAGE) ARCH = $( ARCH) supervisor-image
2016-07-19 21:37:46 +00:00
2017-07-07 21:48:47 +00:00
deploy :
@bash retry_docker_push.sh $( IMAGE)
2017-12-14 03:55:26 +00:00
nodedeps :
$( MAKE) -f $( THIS_FILE) TARGET_COMPONENT = node-deps IMAGE = $( IMAGE) ARCH = $( ARCH) supervisor-image
nodebuild :
$( MAKE) -f $( THIS_FILE) TARGET_COMPONENT = node-build IMAGE = $( IMAGE) ARCH = $( ARCH) supervisor-image
2016-06-03 17:21:46 +00:00
gosuper :
2017-07-07 21:48:47 +00:00
$( MAKE) -f $( THIS_FILE) TARGET_COMPONENT = gosuper IMAGE = $( IMAGE) ARCH = $( ARCH) supervisor-image
2015-07-23 21:14:50 +00:00
2016-09-06 13:41:17 +00:00
test-gosuper : gosuper
docker run \
--rm \
-v /var/run/dbus:/mnt/root/run/dbus \
2017-07-01 06:45:06 +00:00
-e DBUS_SYSTEM_BUS_ADDRESS = "/mnt/root/run/dbus/system_bus_socket" \
2017-07-07 21:48:47 +00:00
$( IMAGE) bash -c \
2016-09-06 13:41:17 +00:00
'./test_formatting.sh && go test -v ./gosuper'
format-gosuper : gosuper
docker run \
--rm \
-v $( shell pwd ) /gosuper:/go/src/resin-supervisor/gosuper \
2017-07-07 21:48:47 +00:00
$( IMAGE) \
2016-09-06 13:41:17 +00:00
go fmt ./...
test-integration : gosuper
docker run \
--rm \
--net= host \
-e SUPERVISOR_IP = " $( shell docker inspect --format '{{ .NetworkSettings.IPAddress }}' resin_supervisor_1) " \
--volumes-from resin_supervisor_1 \
-v /var/run/dbus:/mnt/root/run/dbus \
2017-07-01 06:45:06 +00:00
-e DBUS_SYSTEM_BUS_ADDRESS = "/mnt/root/run/dbus/system_bus_socket" \
2017-07-07 21:48:47 +00:00
$( IMAGE) \
2016-09-06 13:41:17 +00:00
go test -v ./supertest
2015-07-17 20:47:26 +00:00
2018-03-07 04:34:10 +00:00
.PHONY : supervisor deploy nodedeps nodebuild gosuper supervisor -dind run -supervisor