Skip to content

Instantly share code, notes, and snippets.

@doevelopper
Last active January 23, 2019 12:25
Show Gist options
  • Save doevelopper/c741ad4133d7afcc71e46002af5badd7 to your computer and use it in GitHub Desktop.
Save doevelopper/c741ad4133d7afcc71e46002af5badd7 to your computer and use it in GitHub Desktop.
# Build tools
#
# Targets (see each target for more information):
#   image:	creates tagged image
#   clean:	removes build artifacts and images
#
# Can be overriden via make release BUMP=

.PHONY: all image clean image-clean image-name

PROXY_URL                   ?= "[http://[user]:[pw]@[proxy-host-corporate]:[proxy-port]"
BUMPVERSION 				?= BUMPVERSION
DTR  				        ?= registry.gear.dtr.acme
BUILD_ARGS                  ?=  --build-arg http_proxy=$(PROXY_URL) \
				--build-arg https_proxy=$(PROXY_URL) \
				--build-arg no_proxy="/var/run/docker.sock,localhost,\
				127.0.0.1,localaddress,.localdomain.com,\
				192.168.*,dtr"
IMAGE       ?= rti-dds
REGISTRY    ?= $(DTR)/$(SSO)
DOCKER_USER := $(shell if [ "$$OSTYPE" != "darwin"* ]; \
		then USER_ARG="--user=`id -u`"; \
		fi; \
		echo "$$USER_ARG")
ARCH        ?= amd64
ALL_ARCH    := amd64 arm aarch64 i386

# Set default base image dynamically for each arch
ifeq ($(ARCH),amd64)
    BASEIMAGE?=alpine
endif

ifeq ($(ARCH),arm)
    BASEIMAGE?=armel/busybox
endif

ifeq ($(ARCH),arm64)
    BASEIMAGE?=aarch64/busybox
endif

TAGS=latest
PROJ_ROOT := "$(shell pwd)"
NB_CORES ?= $(shell N=$$(cat /proc/cpuinfo | grep '^processor' | wc -l); \
		           [ $$N -gt 8 ] && echo 8 || echo $$N)
all: image

image: .image-$(ARCH) .tag.image

.image-$(ARCH):
	@echo  "Building ${IMAGE} ... "
#	docker system prune
	docker build --force-rm $(BUILD_ARGS) -t $(REGISTRY)/$(IMAGE) .

.tag.image:
	@echo  "taging ${IMAGE} ... "
	docker tag $(REGISTRY)/$(IMAGE) $(REGISTRY)/$(IMAGE):$(TAGS)

clean: clean-containers clean-images

clean-images:
	docker rmi -f $(REGISTRY)/$(IMAGE):$$tag;

clean-containers:
	@echo  "taging ${IMAGE} ... "
	docker ps -a | grep $(IMAGE) | awk '{ print $$1 }' | xargs docker rm -f

run:
	@echo  "Running ${IMAGE} to DTR "
	docker run --rm --tty --interactive --volume "$(PWD):/opt" $(REGISTRY)/$(IMAGE)

publish: image
	@echo  "Push ${IMAGE} to DTR "
	docker push $(REGISTRY)/$(IMAGE):$(TAGS)

docker-clean-unused:
	@echo  "Clean unused ... "
	docker system prune --all --force --volumes

docker-clean-none:
	@echo  "Clean unused ... "
	docker run --rm -ti -v "$PWD:/opt/" $(REGISTRY)/$(IMAGE)
@doevelopper
Copy link
Author

doevelopper commented Jan 22, 2019


IMAGE_NAME=dev_$(GIT_BRANCH)
IMAGE_VERSION=0.1.1
CONTAINER_NAME="poc"
RED=\033[91m
BLUE=\033[36m
COLOR_RESET=\033[0m

# HELP
# This will output the help for each task
# thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: help

.DEFAULT_GOAL := help


save:
	@echo "$(BLUE)Saving current image, please wait...$(COLOR_RESET)"
	docker save $(IMAGE_NAME):$(IMAGE_VERSION) | gzip > $(IMAGE_NAME).bin

load:
	@echo "$(BLUE)Loading current image, please wait...$(COLOR_RESET)"
	docker load < $(IMAGE_NAME).bin

list: list_all_containers list_volumes

list_all_containers:
	@echo "$(BLUE)Containers on system:$(COLOR_RESET)"
	docker ps -a

list_volumes:
	@echo "$(BLUE)Volumes on system:$(COLOR_RESET)"
	@docker volume ls
	@echo "$(RED)Dangling volumes (may be deleted via 'docker volume rm xxx'):$(COLOR_RESET)"
	@docker volume ls -f dangling=true

container_info:
	docker inspect $(IMAGE_NAME)_cont

enter:
	@echo "Enter in the container..."
	docker exec -it $(IMAGE_NAME)_cont /bin/bash


build:
	@#docker build -t samifier:latest .
	docker-compose build

run:
	@# The name of the image generated by docker-compose depends on the folder name by default but you.
	@# You can override it by using '--project-name' parameter or 'image' directive in yml file.
	@# docker-compose up -d, will start the containers in the background and leave them running.
	@#By default, if there are existing containers for a service, docker-compose up will stop and recreate them
	@#(preserving mounted volumes with volumes-from), so that changes in docker-compose.yml are picked up.
	@#If you do not want containers stopped and recreated, use docker-compose up --no-recreate.
	@#This will still start any stopped containers, if needed.
	@#docker run -d -p 8888:80 $(IMAGE_NAME)
	@#docker run -i -t -p 8888:80 $(IMAGE_NAME)
	docker-compose up --no-recreate

    docker run \
        --env HTTP_PROXY="[http://[user]:[pw]@[proxy-host-corporate]:[proxy-port]" \
        --env HTTPS_PROXY="[http://[user]:[pw]@[proxy-host-corporate]:[proxy-port]" \
        --env FTP_PROXY="[http://[user]:[pw]@[proxy-host-corporate]:[proxy-port]" \
        --env NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com,3.*,192.168.*"

help: ## show make targets.
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf " \033[36m%-20s\033[0m  %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@echo "Command:	Result:"
	@echo "<make help>	Display this help."
	@echo "<make version>	Display informations about the image."
	@echo "<make all>	Build and run the image in new container."
	@echo "<make build>	Build the image."
	@echo "<make run>	Run the image in new container."
	@echo "<make enter>	Enter in the current container as root."
	@echo "<make save>	Save the image to an archive."
	@echo "<make load>	Load the archive."
	@echo "<make clean>	Remove containers, backups, logs, repositories."
	@echo "<make list>			List all containers and volumes."
	@echo "<make list_all_containers>	List all containers."
	@echo "<make list_volumes>		List all volumes."
	@echo
	@echo "<make backup_all>		Do backup of databases and files uploaded."
	@echo "<make restore_all>		Do a restoration of all data previously backed up."
	@echo "<make remove_container> 	   Remove $(IMAGE_NAME)_cont from the system except backups."
	@echo "<make remove_container_and_volume> Remove the container and the backups."
	@echo "<make list_volumes>		   List backup volumes on the system."
	@echo "<make container_info>		   Inspect samifier container."
	@echo
	@echo "Once the image is built and the container is up,"
	@echo "you can access the web server through your browser at 'http://samifier.inria.fr:8888/app_dev.php'."
	@echo "WebDAV is configured on port 8080 for an access to Symfony directory."
	@echo "PostgreSQL is listening on port 5432/tcp."
	@echo "MongoDB is listening on port 27017/tcp."

version:
	@docker inspect --format='Description: 	{{.Config.Labels.Description}}' $(IMAGE_NAME):$(IMAGE_VERSION)
	@docker inspect --format='Vendor: 	{{.Config.Labels.Vendor}}' $(IMAGE_NAME):$(IMAGE_VERSION)
	@docker inspect --format='Authors: 	{{.Author}}' $(IMAGE_NAME):$(IMAGE_VERSION)
	@docker inspect --format='Version: 	{{.Config.Labels.Version}}' $(IMAGE_NAME):$(IMAGE_VERSION)

	@docker inspect --format='DockerVersion:	{{.DockerVersion}}' $(IMAGE_NAME):$(IMAGE_VERSION)
	@docker inspect --format='Architecture:	{{.Architecture}}' $(IMAGE_NAME):$(IMAGE_VERSION)
	@docker inspect --format='OS: 		{{.Os}}' $(IMAGE_NAME):$(IMAGE_VERSION)
	@docker inspect --format='Size: 		{{.Size}} bytes' $(IMAGE_NAME):$(IMAGE_VERSION)

@doevelopper
Copy link
Author

# HELP
# This will output the help for each task
# thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
#https://github.com/cretinon/jinade_sickrage
#https://github.com/altvnv/MakefileLib/blob/master/Makefile.mk

.PHONY: all image clean image-clean image-name
.PHONY: build build-arm push push-arm shell shell-arm run run-arm start start-arm stop stop-arm rm rm-arm release release-arm logs inspect
.DEFAULT_GOAL := help

RED=\033[91m
BLUE=\033[36m
COLOR_RESET=\033[0m

ENVDIR="./envs/"
LOGDIR="./logs/"
GID=$(shell id -g)
UID=$(shell id -u)
USER=$(shell id -u -n)

DOCKER = docker
DOCKERFILE?=Dockerfile
DOCKER_WORK_DIR = /var/wotkspace

BUILDFLAGS := --rm --force-rm --compress -f $(CURDIR)/$(ARCH)/$(DISTRIB)/Dockerfile -t $(IMAGETAG) \
	--build-arg ARCH=$(ARCH) \
	--build-arg DOCKERSRC=$(DOCKERSRC) \
	--build-arg USERNAME=$(DOCKER_USER) \
	--build-arg DISTRIB=$(DISTRIB) \
	--build-arg PUID=$(PUID) \
	--build-arg PGID=$(PGID) \
	--label org.label-schema.build-date=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \
	--label org.label-schema.name=$(OPSYS)_$(SVCNAME) \
	--label org.label-schema.schema-version="1.0" \
	--label org.label-schema.url="https://github.com/$(GITHUB_USER)/$(OPSYS)_$(SVCNAME)" \
	--label org.label-schema.usage="https://github.com/$(GITHUB_USER)/$(OPSYS)_$(SVCNAME)" \
	--label org.label-schema.vcs-ref=$(shell git rev-parse --short HEAD) \
	--label org.label-schema.vcs-url="https://github.com/$(GITHUB_USER)/$(OPSYS)_$(SVCNAME)" \
	--label org.label-schema.vendor=$(DOCKER_USER)
run: $(DOCKERFILE)
	@echo  "Running ${IMAGE} to DTR "
	docker run --rm --tty --interactive --volume "$(PWD):/home/workspace" $(REGISTRY)/$(IMAGE)

build: $(DOCKERFILE)
	@echo  "Building ${IMAGE}"

.PHONY: help
help: ## Shows make targets.
	@echo '------------------------------------------------- Help -------------------------------------------------'
#	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {sub("\\\\n",sprintf("\n%22c"," "), $$2);printf " \033[36m%-20s\033[0m  %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@echo ""
	@echo "<make help>	Display this help."

@doevelopper
Copy link
Author

 generate:
      @echo "Generation make build system..."
       cmake -E make_directory build
       cmake -E chdir build cmake -DNDDSHOME=/opt/rti_connext_dds-5.3.1 
       -DCONNEXTDDS_ARCH=x64Linux3gcc5.4.0 -DRTICODEGEN_DIR=$NDDSHOME/bin 
       -DCMAKE_BUILD_TYPE=DEBUG ..
    cmake -DNDDSHOME=/opt/rti_connext_dds-5.3.1 -DCONNEXTDDS_ARCH=x64Linux3gcc5.4.0 
      -DRTICODEGEN_DIR=$NDDSHOME/bin -DCMAKE_BUILD_TYPE=DEBUG ..
cmake -G"Unix Makefiles" -Bbuild -DNDDSHOME=/opt/rti_connext_dds-5.3.1 
    -DCONNEXTDDS_ARCH=x64Linux3gcc5.4.0 -DRTICODEGEN_DIR=$NDDSHOME/bin 
-DCMAKE_BUILD_TYPE=DEBUG  -H.
build:
     cmake --build build --target all --config Debug -- -j8
all:
    @echo "Silent goal use make generate instead"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment