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

Feature/92 Provide Makefile #94

Merged
merged 8 commits into from Oct 15, 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
24 changes: 24 additions & 0 deletions .mk-lib/CHANGELOG.md
@@ -0,0 +1,24 @@
## 1.0.3 (2008-06-26)
* Added MK_DIR variable linked to .mk-lib dir
* Fixed path to mk-lib into new Makefile
* Added `confirm` target. See Makefile sample

## 1.0.2 (2008-06-08)
* Added showing release notes while upgrading
* Added Makefile.minimal.mk with basic commands
* Changed create Makefile from Makefile.minimal.mk instead of HEADER.mk

## 1.0.1 (2018-06-06)
* Added Makefile.sample.mk
* Added CHANGELOG.md

## 1.0.0 (2018-06-05)
* Added simple installation script
* Added **mk-version** and **mk-upgrade** commands
* Added to self upgrade script with **mk-upgrade** command
* Changed library from *.makefile-lib* to *mk-lib*
* Added support of absolute paths
* Added [website](https://kudlay.pro/docker-compose-makefile/)

## v0.9 (2017-11-21)
* Initial version with basic functionality
6 changes: 6 additions & 0 deletions .mk-lib/HEADER.mk
@@ -0,0 +1,6 @@
# REQUIRED SECTION
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
include $(ROOT_DIR)/.mk-lib/common.mk
# END OF REQUIRED SECTION

# Insert your code here
21 changes: 21 additions & 0 deletions .mk-lib/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Roman Kudlay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions .mk-lib/Makefile.minimal.mk
@@ -0,0 +1,29 @@
# REQUIRED SECTION
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
include $(ROOT_DIR)/.mk-lib/common.mk
# END OF REQUIRED SECTION

.PHONY: help dependencies up start stop restart status ps clean

dependencies: check-dependencies ## Check dependencies

up: ## Start all or c=<name> containers in foreground
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c)

start: ## Start all or c=<name> containers in background
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up -d $(c)

stop: ## Stop all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) stop $(c)

restart: ## Restart all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) stop $(c)
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c) -d

status: ## Show status of containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) ps

ps: status ## Alias of status

clean: confirm ## Clean all data
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) down
29 changes: 29 additions & 0 deletions .mk-lib/Makefile.sample.mk
@@ -0,0 +1,29 @@
# REQUIRED SECTION
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
include $(ROOT_DIR)/.mk-lib/common.mk
# END OF REQUIRED SECTION

.PHONY: help dependencies up start stop restart status ps clean

dependencies: check-dependencies ## Check dependencies

up: ## Start all or c=<name> containers in foreground
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c)

start: ## Start all or c=<name> containers in background
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up -d $(c)

stop: ## Stop all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) stop $(c)

restart: ## Restart all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) stop $(c)
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c) -d

status: ## Show status of containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) ps

ps: status ## Alias of status

clean: confirm ## Clean all data
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) down
95 changes: 95 additions & 0 deletions .mk-lib/README.md
@@ -0,0 +1,95 @@
# Docker compose makefile
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this package, I'll check it later, but it adds a lot of files I don't know what is the added value

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It speeds up targets generation, provides auto help and more enhancements.

Btw, this is not mandatory :)

[![Build Status](https://travis-ci.org/krom/docker-compose-makefile.svg?branch=master)](https://travis-ci.org/krom/docker-compose-makefile)
[![Release](https://img.shields.io/github/release/krom/docker-compose-makefile.svg)](https://github.com/krom/docker-compose-makefile/releases/latest)
[![Commits since last release](https://img.shields.io/github/commits-since/krom/docker-compose-makefile/latest.svg)](https://github.com/krom/docker-compose-makefile/commits/master)
[![Github All Releases](https://img.shields.io/github/downloads/krom/docker-compose-makefile/total.svg)](https://github.com/krom/docker-compose-makefile)
[![GitHub issues](https://img.shields.io/github/issues/krom/docker-compose-makefile.svg)](https://github.com/krom/docker-compose-makefile/issues)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/krom/docker-compose-makefile.svg)](https://github.com/krom/docker-compose-makefile/pulls)
[![license](https://img.shields.io/github/license/krom/docker-compose-makefile.svg)](https://github.com/krom/docker-compose-makefile/blob/master/LICENSE)
[![GitHub stars](https://img.shields.io/github/stars/krom/docker-compose-makefile.svg?style=social&label=Stars)](https://github.com/krom/docker-compose-makefile/stargazers)

Template and lib for docker-compose

## INSTALLATION
### INSTALLATION
To install mk-lib run command
```bash
curl -sL https://git.io/vh4Gn | sh
```

### UPGRADE
To upgrade existing mk-lib run command
```bash
make mk-upgrade
```

## USAGE
![Screen](https://raw.githubusercontent.com/krom/docker-compose-makefile/master/docs/screencast.gif)

**Common** (see [samples](https://github.com/krom/docker-compose-makefile/tree/master/samples))
- `make console` - open container's console

**From Makefile.minimal.mk** (see [samples](https://github.com/krom/docker-compose-makefile/tree/master/samples))
- `make start` - start all containers
- `make start` c=hello** - start container hello
- `make stop` - stop all containers
- `make status` - show list of containers with statuses
- `make clean` - clean all data

**From this library**
- `make help` - show help (see above)
- `make mk-upgrade` - check for updates of mk-lib
- `make mk-version` - show current version of mk-lib

### VARIABLES
* **ROOT_DIR** - full path to dir with *Makefile*
* **MK_DIR** - fill path to *.mk-lib* dir
* **DOCKER_COMPOSE** - docker-compose executable command
* **DOCKER_COMPOSE_FILE** - docker-compose.yml file

## SAMPLES

Basic commands (you can copy and paste it into your Makefile)

```makefile
up: ## Start all or c=<name> containers in foreground
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c)

start: ## Start all or c=<name> containers in background
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up -d $(c)

stop: ## Stop all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) stop $(c)

status: ## Show status of containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) ps

restart: ## Restart all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) stop $(c)
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c) -d

clean: confirm ## Clean all data
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) down
```
You may see samples [here](https://github.com/krom/docker-compose-makefile/tree/master/samples)

## CUSTOMIZATION
You can create _.make.env_ file in directory with Makefile or **current** directory

Available variables

* **DOCKER_COMPOSE** = {docker-compose executable command}
* **DOCKER_COMPOSE_FILE** = {custom docker-compose.yml file}

## TO-DO
- check dependencies
- update readme

## CHANGELOG
See [CHANGELOG](CHANGELOG.md)

## LICENSE
MIT (see [LICENSE](LICENSE))

## AUTHOR
[Roman Kudlay](http://roman.kudlay.pro) ([roman@kudlay.pro](mailto:roman@kudlay.pro))
25 changes: 25 additions & 0 deletions .mk-lib/common.mk
@@ -0,0 +1,25 @@
MK_DIR := $(ROOT_DIR)/.mk-lib
include $(MK_DIR)/variables.mk
-include $(MK_DIR)/version.mk
-include $(ROOT_DIR)/.make.env
-include .make.env

f ?= $(DOCKER_COMPOSE_FILE)
DOCKER_COMPOSE_FILE := $(f)

.DEFAULT_GOAL := help

help: ##@other Show this help.
@perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)

confirm:
@( read -p "$(RED)Are you sure? [y/N]$(RESET): " sure && case "$$sure" in [yY]) true;; *) false;; esac )

mk-upgrade: ##@other Check for updates of mk-lib
@MK_VERSION=$(MK_VERSION) MK_REPO=$(MK_REPO) $(MK_DIR)/self-upgrade.sh

mk-version: ##@other Show current version of mk-lib
@echo $(MK_VERSION)

check-dependencies:
@echo Checking dependencies
28 changes: 28 additions & 0 deletions .mk-lib/self-upgrade.sh
@@ -0,0 +1,28 @@
#!/usr/bin/env sh
# Get some property from release
# Usage:
# get_latest_release user/repo tag_name
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep "\"$2\":" | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}

# Getting last version
NEW_VER=$(get_latest_release $MK_REPO tag_name)

if [ $NEW_VER = $MK_VERSION ]; then
echo 'Up to date';
else
echo "A new version is available"
body=$(get_latest_release $MK_REPO body)
echo "Upgrading from $MK_VERSION to $NEW_VER

Release notes
************************
$body
************************
";
# Downloading and executing upgrade script
curl -sL https://raw.githubusercontent.com/$MK_REPO/master/scripts/upgrade.sh | ( echo "MK_REPO=$MK_REPO; MK_VERSION=$NEW_VER; "; cat - ) | sh
fi;
27 changes: 27 additions & 0 deletions .mk-lib/variables.mk
@@ -0,0 +1,27 @@
#COLORS
GREEN := $(shell tput -Txterm setaf 2)
WHITE := $(shell tput -Txterm setaf 7)
YELLOW := $(shell tput -Txterm setaf 3)
RED := $(shell tput -Txterm setaf 1)
RESET := $(shell tput -Txterm sgr0)

# Add the following 'help' target to your Makefile
# And add help text after each target name starting with '\#\#'
# A category can be added with @category
HELP_FUN = \
%help; \
while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
print "usage: make [target]\n\n"; \
for (sort keys %help) { \
print "${WHITE}$$_:${RESET}\n"; \
for (@{$$help{$$_}}) { \
$$sep = " " x (32 - length $$_->[0]); \
print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \
}; \
print "\n"; }

#DEFAULT variables
PIP := pip
DOCKER_COMPOSE := docker-compose
DOCKER_COMPOSE_FILE := $(ROOT_DIR)/docker-compose.yml
MK_REPO := krom/docker-compose-makefile
1 change: 1 addition & 0 deletions .mk-lib/version.mk
@@ -0,0 +1 @@
MK_VERSION := v1.0.3
8 changes: 8 additions & 0 deletions Dockerfile.test
@@ -0,0 +1,8 @@
FROM anem-per-feina:local
LABEL maintainer="GeeksCAT<info@geekscat.org>"

WORKDIR /anem-per-feina/

COPY requirements-test.txt ./

RUN pip install --no-cache-dir -r requirements-test.txt
36 changes: 36 additions & 0 deletions Makefile
@@ -0,0 +1,36 @@
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
include $(ROOT_DIR)/.mk-lib/common.mk

.PHONY: help start serve test stop restart status ps clean purge build

start: ## Start all or c=<name> containers in FOREGROUND
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c)

serve: ## Start all or c=<name> containers in BACKGROUND
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up $(c) -d

test: ## Execute tests
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have another option to execute an specific test or all tests from one folder as you can do it in pytest. What do you think?
I've seen that you used the django command to run the tests, but it will trigger the pytests? Can you add a parameter to add the specific test path or folder path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this is just an starting point :)

Tomorrow I'll explore options to inject strings or define specific string modifiers (filters).

Regarding the executed tests, it's not hardcoded, it's configurable on the invoked task so can be ammended without effort.

Currently there are no tests, pytest has not been configured and maybe it's better to deal with this in another PR

@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) run --rm --entrypoint "invoke test" test

# start: ## Start all or c=<name> containers in background
# @$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) up -d $(c)

stop: ## Stop all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) stop $(c)

restart: ## Restart all or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) restart $(c)

status: ## Show status of containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) ps

ps: status ## Alias of status

clean: confirm ## Clean all containers (keeping volumes)
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) down

purge: confirm ## Purge all containers and volumes
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) down -v

build: ## (re)Build all images or c=<name> containers
@$(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) build $(c)