aynic.os 1 year ago
parent 11916a1bc3
commit 15227453ec
  1. 2
      Makefile
  2. 6
      make/apps/def.deploy.mk
  3. 4
      make/apps/def.docker.mk
  4. 6
      make/apps/def.install.mk
  5. 24
      make/apps/install.mk
  6. 60
      make/common.mk
  7. 34
      make/def.mk
  8. 2
      make/env.mk
  9. 2
      make/monorepo/def.mk
  10. 4
      stack/1001pharmadev/.env.dist
  11. 26
      stack/1001pharmadev/mailcatcher.yml
  12. 43
      stack/1001pharmadev/toggle.yml

@ -5,7 +5,7 @@ include make/include.mk
app-bootstrap: setup-sysctl setup-nfsd
app-build: base install-build-parameters
app-build: base install-build-config
$(call make,docker-compose-build docker-compose-up)
$(foreach service,$(or $(SERVICE),$(SERVICES)),$(call make,app-build-$(service)))
$(call make,docker-commit)

@ -1,3 +1,9 @@
CODEDEPLOY_APP_NAME ?= $(APP)
CODEDEPLOY_DEPLOYMENT_GROUP ?= $(CODEDEPLOY_APP_NAME)_$(ENV)
CODEDEPLOY_DEPLOYMENT_CONFIG ?= CodeDeployDefault.AllAtOnce
CODEDEPLOY_DESCRIPTION ?= app: *$(APP)* branch: *$(BRANCH)* env: *$(ENV)* version: *$(VERSION)*
CODEDEPLOY_GITHUB_REPO ?= $(patsubst $(call pop,$(call pop,$(GIT_REPOSITORY)))/%,%,$(GIT_REPOSITORY))
CODEDEPLOY_GITHUB_COMMIT_ID ?= $(COMMIT)
DEPLOY ?= false
DEPLOY_HASH ?= $(shell date +%s)
DEPLOY_HOOK_TEXT ?= app: *$(APP)* branch: *$(BRANCH)* env: *$(ENV)* version: *$(VERSION)* container: *$(CONTAINER)* host: *$(HOST)*

@ -18,7 +18,7 @@ COMPOSE_IGNORE_ORPHANS ?= false
COMPOSE_PROJECT_NAME ?= $(USER)_$(ENV)_$(APP)
COMPOSE_SERVICE_NAME ?= $(subst _,-,$(COMPOSE_PROJECT_NAME))
CONTEXT += COMPOSE_FILE DOCKER_IMAGE_TAG DOCKER_REPOSITORY DOCKER_SERVICE
CONTEXT_DEBUG += DOCKER_REGISTRY_REPOSITORY
CONTEXT_DEBUG += DOCKER_REGISTRY
DOCKER_BUILD_ARGS ?= $(if $(filter $(DOCKER_BUILD_NO_CACHE),true),--pull --no-cache) $(foreach var,$(DOCKER_BUILD_VARS),$(if $($(var)),--build-arg $(var)='$($(var))'))
DOCKER_BUILD_CACHE ?= true
DOCKER_BUILD_NO_CACHE ?= false
@ -145,7 +145,7 @@ define docker-stack-update
$(eval version := $(or $(2),$(if $(findstring :,$(stack)),$(lastword $(subst :, ,$(stack))),latest)))
$(eval path := $(patsubst %/,%,$(or $(3),$(if $(findstring /,$(1)),$(if $(wildcard stack/$(1) stack/$(1).yml),stack/$(if $(findstring .yml,$(1)),$(dir $(1)),$(if $(wildcard stack/$(1).yml),$(dir $(1)),$(1))),$(dir $(1)))),stack/$(name))))
$(eval COMPOSE_FILE += $(wildcard $(path)/$(name).yml $(path)/$(name).$(ENV).yml $(path)/$(name).$(ENV).$(version).yml $(path)/$(name).$(version).yml))
$(if $(wildcard $(path)/.env.dist),$(call .env,,$(path)/.env.dist,$(wildcard $(PARAMETERS)/$(ENV)/$(APP)/.env $(path)/.env.$(ENV) .env)))
$(if $(wildcard $(path)/.env.dist),$(call .env,,$(path)/.env.dist,$(wildcard $(CONFIG)/$(ENV)/$(APP)/.env $(path)/.env.$(ENV) .env)))
endef
# function docker-tag: Tag docker image
define docker-tag

@ -1,8 +1,8 @@
# function install-parameters: copy PARAMETERS files to application config folder
define install-parameters
# function install-config: copy CONFIG files to application config folder
define install-config
$(eval path:=$(or $(1),$(APP)))
$(eval file:=$(or $(2),$(DOCKER_SERVICE)))
$(eval dest:=$(or $(3),config))
$(eval env:=$(or $(4),$(ENV)))
$(if $(wildcard $(dest)/$(file)),,$(if $(wildcard $(PARAMETERS)/$(env)/$(path)/$(file)),$(ECHO) cp -a $(PARAMETERS)/$(env)/$(path)/$(file) $(dest)))
$(if $(wildcard $(dest)/$(file)),,$(if $(wildcard $(CONFIG)/$(env)/$(path)/$(file)),$(ECHO) cp -a $(CONFIG)/$(env)/$(path)/$(file) $(dest)))
endef

@ -24,17 +24,17 @@ install-pgsql-database-%: myos-base
$(call exec,[ $$(PGPASSWORD=$* psql -h postgres -U $* -d $* -c "\d" 2>/dev/null |wc -l) -eq 0 ] && [ -f "${APP_DIR}/$*.pgsql.gz" ] && gzip -cd "${APP_DIR}/$*.pgsql.gz" |PGPASSWORD="postgres" psql -h postgres -U postgres -d $* || true)
$(call exec,[ $$(PGPASSWORD=$* psql -h postgres -U $* -d $* -c "\d" 2>/dev/null |wc -l) -eq 0 ] && [ -f "${APP_DIR}/$*.pgsql" ] && PGPASSWORD="postgres" psql -h postgres -U postgres -c "ALTER ROLE $* WITH SUPERUSER" && PGPASSWORD="postgres" pg_restore -h postgres --no-owner --role=$* -U postgres -d $* ${APP_DIR}/$*.pgsql && PGPASSWORD="postgres" psql -h postgres -U postgres -c "ALTER ROLE $* WITH NOSUPERUSER" || true)
# target install-build-parameters: Call install-parameters with file * and dest build
.PHONY: install-build-parameters
install-build-parameters:
$(call install-parameters,,*,build)
# target install-build-config: Call install-config with file * and dest build
.PHONY: install-build-config
install-build-config:
$(call install-config,,*,build)
# target install-parameters: Call install-parameters
.PHONY: install-parameters
install-parameters:
$(call install-parameters)
# target install-config: Call install-config
.PHONY: install-config
install-config:
$(call install-config)
# target install-parameters-%: Call install-parameters with app %
.PHONY: install-parameters-%
install-parameters-%:
$(call install-parameters,$*)
# target install-config-%: Call install-config with app %
.PHONY: install-config-%
install-config-%:
$(call install-config,$*)

@ -1,15 +1,32 @@
##
# UPDATE
# COMMON
# target $(APP): Call update-app
.PHONY: $(APP)
$(APP): APP_DIR := $(RELATIVE)$(APP)
$(APP): myos-base
$(call update-app)
# target $(CONFIG): Update config files
.PHONY: $(CONFIG)
$(CONFIG): SSH_PUBLIC_HOST_KEYS := $(CONFIG_REMOTE_HOST) $(SSH_BASTION_HOSTNAME) $(SSH_REMOTE_HOSTS)
$(CONFIG): MAKE_VARS += SSH_BASTION_HOSTNAME SSH_BASTION_USERNAME SSH_PRIVATE_IP_RANGE SSH_PUBLIC_HOST_KEYS
$(CONFIG): myos-base
$(call update-app,$(CONFIG_REPOSITORY),$(CONFIG))
# target install-app install-apps: Call install-app for each ARGS
.PHONY: install-app install-apps
install-app install-apps:
install-app install-apps: myos-base install-app-required
$(foreach url,$(ARGS),$(call install-app,$(url)))
# target install-app-%: Call install-app for %
.PHONY: install-app-%
install-app-%:
$(call install-app,$*)
# target install-app-required: Call install-app for each APP_REQUIRED
.PHONY: install-app-required
install-app-required: myos-base
$(foreach url,$(APP_REQUIRED),$(call install-app,$(url)))
# target $(SHARED): Create SHARED folder
$(SHARED):
$(ECHO) mkdir -p $(SHARED)
# target update-apps: Call update-app target for each APPS
.PHONY: update-apps
@ -20,15 +37,13 @@ update-apps:
.PHONY: update-app
update-app: update-app-$(APP_NAME) ;
# target update-app-%: Fire myos-base %
# target update-app-%: Fire %
.PHONY: update-app-%
update-app-%: myos-base % ;
update-app-%: % ;
# target $(APP): Call update-app
.PHONY: $(APP)
$(APP): APP_DIR := $(RELATIVE)$(APP)
$(APP):
$(call update-app)
# target update-config: Fire CONFIG
.PHONY: update-config
update-config: $(CONFIG)
# target update-hosts: Update /etc/hosts
# on local host
@ -39,17 +54,6 @@ ifneq (,$(filter $(ENV),local))
cat */.env 2>/dev/null |grep -Eo 'urlprefix-[^/]+' |sed 's/urlprefix-//' |while read host; do grep $$host /etc/hosts >/dev/null 2>&1 || { echo "Adding $$host to /etc/hosts"; echo 127.0.0.1 $$host |$(ECHO) sudo tee -a /etc/hosts >/dev/null; }; done
endif
# target update-parameters: Fire PARAMETERS
.PHONY: update-parameters
update-parameters: $(PARAMETERS)
# target $(PARAMETERS): Clone or pull parameters files
.PHONY: $(PARAMETERS)
$(PARAMETERS): SSH_PUBLIC_HOST_KEYS := $(PARAMETERS_REMOTE_HOST) $(SSH_BASTION_HOSTNAME) $(SSH_REMOTE_HOSTS)
$(PARAMETERS): MAKE_VARS += SSH_BASTION_HOSTNAME SSH_BASTION_USERNAME SSH_PRIVATE_IP_RANGE SSH_PUBLIC_HOST_KEYS
$(PARAMETERS): myos-base
$(call update-app,$(PARAMETERS),$(PARAMETERS_REPOSITORY))
# target update-remote-%: fetch git remote %
.PHONY: update-remote-%
update-remote-%: myos-base
@ -66,13 +70,9 @@ update-upstream: myos-base .git/refs/remotes/upstream/master
$(call exec,git fetch --prune --tags upstream)
# target .git/refs/remotes/upstream/master: git add upstream APP_UPSTREAM_REPOSITORY
.git/refs/remotes/upstream/master: myos-base
$(call exec,git remote add upstream $(APP_UPSTREAM_REPOSITORY) 2>/dev/null ||:)
.git/refs/remotes/upstream/master:
$(ECHO) git remote add upstream $(APP_UPSTREAM_REPOSITORY) 2>/dev/null ||:
# target shared: Fire SHARED
.PHONY: update-shared
update-shared: $(SHARED)
# target $(SHARED): Create SHARED folder
$(SHARED):
$(ECHO) mkdir -p $(SHARED)

@ -7,9 +7,11 @@ APP_NAME ?= $(APP)
APP_TYPE ?= $(if $(SUBREPO),subrepo) $(if $(filter .,$(MYOS)),myos)
APPS ?= $(if $(MONOREPO),$(sort $(patsubst $(MONOREPO_DIR)/%/.git,%,$(wildcard $(MONOREPO_DIR)/*/.git))))
APPS_NAME ?= $(foreach app,$(APPS),$(or $(shell awk -F '=' '$$1 == "APP" {print $$2}' $(or $(wildcard $(MONOREPO_DIR)/$(app)/.env),$(wildcard $(MONOREPO_DIR)/$(app)/.env.$(ENV)),$(MONOREPO_DIR)/$(app)/.env.dist) 2>/dev/null),$(app)))
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
BRANCH ?= $(GIT_BRANCH)
CMDS ?= exec exec:% exec@% install-app install-apps run run:% run@%
COMMIT ?= $(shell git rev-parse $(BRANCH) 2>/dev/null)
COMMIT ?= $(or $(SUBREPO_COMMIT),$(GIT_COMMIT))
CONFIG ?= $(RELATIVE)config
CONFIG_REPOSITORY ?= $(call pop,$(or $(APP_UPSTREAM_REPOSITORY),$(GIT_UPSTREAM_REPOSITORY)))/$(notdir $(CONFIG))
CONTEXT ?= $(if $(APP),APP BRANCH VERSION) $(shell awk 'BEGIN {FS="="}; $$1 !~ /^(\#|$$)/ {print $$1}' .env.dist 2>/dev/null)
CONTEXT_DEBUG ?= MAKEFILE_LIST env APPS GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME LOG_LEVEL MAKE_DIR MAKE_SUBDIRS MAKE_CMD_ARGS MAKE_ENV_ARGS MONOREPO_DIR UID USER
DEBUG ?= false
@ -20,16 +22,20 @@ DRYRUN ?= false
DRYRUN_IGNORE ?= false
DRYRUN_RECURSIVE ?= false
ENV ?= dist
ENV_FILE ?= $(wildcard $(PARAMETERS)/$(ENV)/$(APP)/.env) .env
ENV_FILE ?= $(wildcard $(CONFIG)/$(ENV)/$(APP)/.env) .env
ENV_LIST ?= debug local tests release master #TODO: staging develop
ENV_RESET ?= false
ENV_VARS ?= APP BRANCH ENV HOSTNAME GID GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME MONOREPO MONOREPO_DIR TAG UID USER VERSION
GID ?= $(shell id -g 2>/dev/null)
GIT_AUTHOR_EMAIL ?= $(shell git config user.email 2>/dev/null)
GIT_AUTHOR_NAME ?= $(shell git config user.name 2>/dev/null)
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
GIT_COMMIT ?= $(shell git rev-parse $(BRANCH) 2>/dev/null)
GIT_REPOSITORY ?= $(if $(SUBREPO),$(shell awk -F ' = ' '$$1 ~ /^[[\s\t]]*remote$$/ {print $$2}' .gitrepo 2>/dev/null),$(shell git config --get remote.origin.url 2>/dev/null))
GIT_TAG ?= $(shell git tag -l --points-at $(BRANCH) 2>/dev/null)
GIT_UPSTREAM_REPOSITORY ?= $(if $(findstring ://,$(GIT_REPOSITORY)),$(call pop,$(call pop,$(GIT_REPOSITORY)))/,$(call pop,$(GIT_REPOSITORY),:):)$(GIT_UPSTREAM_USER)/$(lastword $(subst /, ,$(GIT_REPOSITORY)))
GIT_UPSTREAM_USER ?= $(or $(MONOREPO),$(USER))
GIT_VERSION ?= $(shell git describe --tags $(BRANCH) 2>/dev/null || git rev-parse $(BRANCH) 2>/dev/null)
HOSTNAME ?= $(shell hostname 2>/dev/null |sed 's/\..*//')
LOG_LEVEL ?= $(if $(filter false,$(VERBOSE)),error,$(if $(filter true,$(DEBUG)),debug))
MAKE_ARGS ?= $(foreach var,$(MAKE_VARS),$(if $($(var)),$(var)='$($(var))'))
@ -42,23 +48,22 @@ MAKE_FILE_ARGS ?= $(foreach var,$(filter $(ENV_VARS),$(MAKE_FIL
MAKE_FILE_VARS ?= $(strip $(foreach var, $(filter-out .VARIABLES,$(.VARIABLES)), $(if $(filter file,$(origin $(var))),$(var))))
MAKE_OLDFILE ?= $@
MAKE_TARGETS ?= $(filter-out $(.VARIABLES),$(shell $(MAKE) -qp 2>/dev/null |awk -F':' '/^[a-zA-Z0-9][^$$\#\/\t=]*:([^=]|$$)/ {print $$1}' |sort -u))
MAKE_UNIXTIME ?= $(shell date +%s 2>/dev/null)
MAKE_VARS ?= ENV
MONOREPO ?= $(if $(filter myos,$(MYOS)),$(notdir $(CURDIR)),$(if $(APP),$(notdir $(realpath $(CURDIR)/..))))
MONOREPO_DIR ?= $(if $(MONOREPO),$(if $(filter myos,$(MYOS)),$(realpath $(CURDIR)),$(if $(APP),$(realpath $(CURDIR)/..))))
MYOS ?= $(if $(filter $(MAKE_DIR),$(call pop,$(MAKE_DIR))),.,$(call pop,$(MAKE_DIR)))
PARAMETERS ?= $(RELATIVE)parameters
PARAMETERS_REPOSITORY ?= $(call pop,$(GIT_UPSTREAM_REPOSITORY))/$(notdir $(PARAMETERS))
QUIET ?= $(if $(filter false,$(VERBOSE)),--quiet)
RECURSIVE ?= true
RELATIVE ?= $(if $(filter myos,$(MYOS)),./,../)
SHARED ?= $(RELATIVE)shared
SSH_DIR ?= ${HOME}/.ssh
SUBREPO ?= $(if $(wildcard .gitrepo),$(notdir $(CURDIR)))
TAG ?= $(shell git tag -l --points-at $(BRANCH) 2>/dev/null)
TAG ?= $(GIT_TAG)
UID ?= $(shell id -u 2>/dev/null)
USER ?= $(shell id -nu 2>/dev/null)
VERBOSE ?= false
VERSION ?= $(shell git describe --tags $(BRANCH) 2>/dev/null || git rev-parse $(BRANCH) 2>/dev/null)
VERSION ?= $(GIT_VERSION)
ifeq ($(DOCKER), true)
ENV_ARGS = $(env.docker.args) $(env.docker.dist)
@ -170,21 +175,22 @@ pop = $(patsubst %$(or $(2),/)$(lastword $(subst $(or $(2),/), ,$(1))),%,$(1))
# macro sed: Exec sed script 1 on file 2
sed = $(call exec,sed -i $(SED_SUFFIX) '\''$(1)'\'' $(2))
# function install-app: Exec 'git clone url 1 dir 2'
# function install-app: Exec 'git clone url 1 dir 2' or Call update-app
## it installs application source files
define install-app
$(eval url := $(or $(1), $(APP_REPOSITORY)))
$(eval dir := $(or $(2), $(RELATIVE)$(lastword $(subst /, ,$(url)))))
[ -d $(dir) ] && $(call update-app,$(url),$(dir))
[ -d $(dir) ] || $(call exec,$(ECHO) git clone $(QUIET) $(url) $(dir))
endef
# function update-app: Exec 'cd dir 1 && git pull'
# function update-app: Exec 'cd dir 1 && git pull' or Call install-app
## it updates application source files
define update-app
$(eval dir := $(or $(1), $(APP_DIR)))
$(eval url := $(or $(2), $(APP_REPOSITORY)))
$(call install-app,$(url),$(dir))
$(eval url := $(or $(1), $(APP_REPOSITORY)))
$(eval dir := $(or $(2), $(APP_DIR)))
[ -d $(dir) ] && $(call exec,cd $(dir) && $(ECHO) git pull $(QUIET))
[ -d $(dir) ] || $(call install-app,$(url),$(dir))
endef
# function TARGET:ENV: Create a new target ending with :env
@ -200,9 +206,9 @@ endef
# set ENV=env for targets ending with :env
## for each env in ENV_LIST
## it overrides value of ENV with env
## it adds $(PARAMETERS)/$(env)/$(APP)/.env file to ENV_FILE
## it adds $(CONFIG)/$(env)/$(APP)/.env file to ENV_FILE
## it evals TARGET:ENV
$(foreach env,$(ENV_LIST),$(eval TARGET := %\:$(env)) $(eval ASSIGN_ENV := ENV:=$(env)) $(eval ASSIGN_ENV_FILE := ENV_FILE+=$(wildcard $(PARAMETERS)/$(env)/$(APP)/.env)) $(eval $(TARGET:ENV)))
$(foreach env,$(ENV_LIST),$(eval TARGET := %\:$(env)) $(eval ASSIGN_ENV := ENV:=$(env)) $(eval ASSIGN_ENV_FILE := ENV_FILE+=$(wildcard $(CONFIG)/$(env)/$(APP)/.env)) $(eval $(TARGET:ENV)))
# set ENV=env for targets ending with @env
$(foreach env,$(ENV_LIST),$(eval %@$(env): ENV:=$(env)))

@ -4,7 +4,7 @@
# target .env: Update .env
## it updates .env file when .env.dist file is newer
.env: .env.dist
$(call .env,,,$(wildcard $(PARAMETERS)/$(ENV)/$(APP)/.env .env.$(ENV)))
$(call .env,,,$(wildcard $(CONFIG)/$(ENV)/$(APP)/.env .env.$(ENV)))
# target .env-clean: Remove .env
.PHONY: .env-clean

@ -1,6 +1,6 @@
CMDS += copy master-tag release release-check release-create release-finish subrepo-push subrepo-update
CONTEXT += APPS ENV RELEASE
DIRS ?= $(MAKE_DIR) $(PARAMETERS) $(SHARED)
DIRS ?= $(CONFIG) $(MAKE_DIR) $(SHARED)
RELEASE_UPGRADE ?= $(filter v%, $(shell git tag -l 2>/dev/null |sort -V |awk '/$(RELEASE)/,0'))
RELEASE_VERSION ?= $(firstword $(subst -, ,$(VERSION)))
SUBREPOS ?= $(filter subrepo/%, $(shell git remote 2>/dev/null))

@ -1,4 +0,0 @@
MAILCATCHER_SERVICE_1080_TAGS=urlprefix-mailcatcher.${APP_DOMAIN}/
TOGGLE_API_SERVICE_80_TAGS=urlprefix-toggle-api.${APP_DOMAIN}/
TOGGLE_UI_SERVICE_80_TAGS=urlprefix-toggle-ui.${APP_DOMAIN}/
TOGGLE__API_BASE_URL=http://toggle-api.${APP_DOMAIN}

@ -1,26 +0,0 @@
version: '3.6'
services:
mailcatcher:
image: 1001pharmadev/mailcatcher:latest
labels:
- SERVICE_1025_CHECK_TCP=true
- SERVICE_1025_NAME=${COMPOSE_SERVICE_NAME}-mailcatcher-1025
- SERVICE_1080_CHECK_HTTP=/
- SERVICE_1080_NAME=${COMPOSE_SERVICE_NAME}-mailcatcher-1080
- SERVICE_1080_TAGS=${MAILCATCHER_SERVICE_1080_TAGS}
networks:
- private
- public
ports:
- 1025
- 1080
restart: always
networks:
private:
external: true
name: ${DOCKER_NETWORK_PRIVATE}
public:
external: true
name: ${DOCKER_NETWORK_PUBLIC}

@ -1,43 +0,0 @@
version: '3.6'
services:
toggle-api:
image: 1001pharmadev/qandidate-toggle-api:latest
depends_on:
- redis
environment:
- TOGGLE__REDIS_DSN=tcp://redis:6379
labels:
- SERVICE_80_CHECK_HTTP=/toggles
- SERVICE_80_NAME=${COMPOSE_SERVICE_NAME}-toggle-api-80
- SERVICE_80_TAGS=${TOGGLE_API_SERVICE_80_TAGS}
networks:
- private
- public
ports:
- 80
restart: always
toggle-ui:
image: 1001pharmadev/qandidate-toggle-ui:latest
depends_on:
- toggle-api
environment:
- TOGGLE__API_BASE_URL=${TOGGLE__API_BASE_URL}
labels:
- SERVICE_80_CHECK_HTTP=/
- SERVICE_80_NAME=${COMPOSE_SERVICE_NAME}-toggle-ui-80
- SERVICE_80_TAGS=${TOGGLE_UI_SERVICE_80_TAGS}
networks:
- private
- public
ports:
- 80
restart: always
networks:
private:
external: true
name: ${DOCKER_NETWORK_PRIVATE}
public:
external: true
name: ${DOCKER_NETWORK_PUBLIC}
Loading…
Cancel
Save