wip
This commit is contained in:
+8
-12
@@ -1,15 +1,11 @@
|
||||
# target ansible: Fire docker-build-ansible, Call ansible ANSIBLE_ARGS ARGS or ansible-run target
|
||||
.PHONY: ansible
|
||||
ansible: docker-build-ansible
|
||||
ifneq ($(ARGS),)
|
||||
ansible: $(if $(DOCKER_RUN),docker-build-ansible)
|
||||
$(call ansible,$(ANSIBLE_ARGS) $(ARGS))
|
||||
else
|
||||
$(call make,ansible-run)
|
||||
endif
|
||||
|
||||
# target ansible-playbook: Call ansible-playbook ANSIBLE_ARGS ARGS
|
||||
.PHONY: ansible-playbook
|
||||
ansible-playbook:
|
||||
ansible-playbook: $(if $(DOCKER_RUN),docker-build-ansible)
|
||||
$(call ansible-playbook,$(ANSIBLE_ARGS) $(ARGS))
|
||||
|
||||
# target ansible-pull: Call ansible-pull ANSIBLE_GIT_REPOSITORY ANSIBLE_PLAYBOOK
|
||||
@@ -17,16 +13,16 @@ ansible-playbook:
|
||||
ansible-pull:
|
||||
$(call ansible-pull,--url $(ANSIBLE_GIT_REPOSITORY) $(if $(ANSIBLE_GIT_KEY_FILE),--key-file $(ANSIBLE_GIT_KEY_FILE)) $(if $(ANSIBLE_GIT_VERSION),--checkout $(ANSIBLE_GIT_VERSION)) $(if $(ANSIBLE_GIT_DIRECTORY),--directory $(ANSIBLE_GIT_DIRECTORY)) $(if $(ANSIBLE_TAGS),--tags $(ANSIBLE_TAGS)) $(if $(ANSIBLE_EXTRA_VARS),--extra-vars '$(ANSIBLE_EXTRA_VARS)') $(if $(findstring true,$(FORCE)),--force) $(if $(findstring true,$(DRYRUN)),--check) --full $(if $(ANSIBLE_INVENTORY),--inventory $(ANSIBLE_INVENTORY)) $(ANSIBLE_PLAYBOOK))
|
||||
|
||||
# target ansible-pull@%: Fire get-PrivateIpAddress-% for SERVER_NAME, Call ssh-exec make ansible-pull DOCKER_IMAGE_TAG
|
||||
# target ansible-pull@%: Fire ssh-get-PrivateIpAddress-% for SERVER_NAME, Call ssh-exec make ansible-pull DOCKER_IMAGE_TAG
|
||||
.PHONY: ansible-pull@%
|
||||
ansible-pull@%: get-PrivateIpAddress-$(SERVER_NAME)
|
||||
ansible-pull@%: ssh-get-PrivateIpAddress-$(SERVER_NAME)
|
||||
$(call ssh-exec,$(AWS_INSTANCE_IP),make ansible-pull ANSIBLE_DOCKER_IMAGE_TAG=$(ANSIBLE_DOCKER_IMAGE_TAG) ANSIBLE_TAGS=$(ANSIBLE_TAGS) FORCE=$(FORCE))
|
||||
|
||||
# target ansible-run: Fire base-ssh-add docker-build-ansible ansible-run-localhost
|
||||
# target ansible-run: Fire ssh-add ansible-run-localhost
|
||||
.PHONY: ansible-run
|
||||
ansible-run: base-ssh-add docker-build-ansible ansible-run-localhost
|
||||
ansible-run: ssh-add ansible-run-localhost
|
||||
|
||||
# target ansible-run-%: Call ansible-playbook ANSIBLE_PLAYBOOK
|
||||
# target ansible-run-%: Fire docker-build-ansible, Call ansible-playbook ANSIBLE_PLAYBOOK
|
||||
.PHONY: ansible-run-%
|
||||
ansible-run-%:
|
||||
ansible-run-%: $(if $(DOCKER_RUN),docker-build-ansible)
|
||||
$(call ansible-playbook,$(if $(ANSIBLE_TAGS),--tags $(ANSIBLE_TAGS)) $(if $(ANSIBLE_EXTRA_VARS),--extra-vars '$(patsubst target=localhost,target=$*,$(ANSIBLE_EXTRA_VARS))') $(if $(findstring true,$(DRYRUN)),--check) $(if $(ANSIBLE_INVENTORY),--inventory $(ANSIBLE_INVENTORY)) $(ANSIBLE_PLAYBOOK))
|
||||
|
||||
+4
-19
@@ -23,34 +23,19 @@ CMDS += ansible ansible-playbook
|
||||
DOCKER_RUN_OPTIONS += --add-host=host.docker.internal:$(DOCKER_INTERNAL_DOCKER_HOST)
|
||||
ENV_VARS += ANSIBLE_AWS_ACCESS_KEY_ID ANSIBLE_AWS_DEFAULT_OUTPUT ANSIBLE_AWS_DEFAULT_REGION ANSIBLE_AWS_SECRET_ACCESS_KEY ANSIBLE_CONFIG ANSIBLE_DISKS_NFS_DISK ANSIBLE_DISKS_NFS_OPTIONS ANSIBLE_DISKS_NFS_PATH ANSIBLE_DOCKER_IMAGE_TAG ANSIBLE_DOCKER_REGISTRY ANSIBLE_EXTRA_VARS ANSIBLE_GIT_DIRECTORY ANSIBLE_GIT_KEY_FILE ANSIBLE_GIT_REPOSITORY ANSIBLE_GIT_VERSION ANSIBLE_INVENTORY ANSIBLE_PLAYBOOK ANSIBLE_SSH_PRIVATE_KEYS ANSIBLE_USERNAME ANSIBLE_VERBOSE
|
||||
|
||||
ifeq ($(DOCKER), true)
|
||||
define ansible
|
||||
$(call INFO,ansible,$(1))
|
||||
$(call run,$(DOCKER_REPOSITORY)/ansible:$(DOCKER_IMAGE_TAG) $(ANSIBLE_ARGS) -i $(ANSIBLE_INVENTORY)/.host.docker.internal $(ANSIBLE_VERBOSE) $(1))
|
||||
endef
|
||||
define ansible-playbook
|
||||
$(call INFO,ansible-playbook,$(1))
|
||||
$(call run,--entrypoint=ansible-playbook $(DOCKER_REPOSITORY)/ansible:$(DOCKER_IMAGE_TAG) $(ANSIBLE_ARGS) -i $(ANSIBLE_INVENTORY)/.host.docker.internal $(ANSIBLE_VERBOSE) $(1))
|
||||
endef
|
||||
define ansible-pull
|
||||
$(call INFO,ansible-pull,$(1))
|
||||
# TODO : run ansible in docker and target localhost outside docker
|
||||
$(call env-exec,ansible-pull $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(1))
|
||||
endef
|
||||
else
|
||||
# function ansible: Call run ansible ANSIBLE_ARGS with arg 1
|
||||
define ansible
|
||||
$(call INFO,ansible,$(1))
|
||||
$(call run,ansible $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(1))
|
||||
$(RUN) $(call run,ansible $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(if $(DOCKER_RUN),-i $(ANSIBLE_INVENTORY)/.host.docker.internal) $(1),$(DOCKER_REPOSITORY)/)
|
||||
endef
|
||||
# function ansible-playbook: Call run ansible-playbook ANSIBLE_ARGS with arg 1
|
||||
define ansible-playbook
|
||||
$(call INFO,ansible-playbook,$(1))
|
||||
$(call run,ansible-playbook $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(1))
|
||||
$(RUN) $(call run,ansible$(if $(DOCKER_RUN),,-playbook) $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(if $(DOCKER_RUN),-i $(ANSIBLE_INVENTORY)/.host.docker.internal) $(1),--entrypoint=ansible-playbook $(DOCKER_REPOSITORY)/)
|
||||
endef
|
||||
# function ansible-pull: Call run ansible-pull ANSIBLE_ARGS with arg 1
|
||||
## TODO: run ansible in docker and target localhost outside docker when DOCKER=true
|
||||
define ansible-pull
|
||||
$(call INFO,ansible-pull,$(1))
|
||||
$(call run,ansible-pull $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(1))
|
||||
$(RUN) $(call env-run,ansible-pull $(ANSIBLE_ARGS) $(ANSIBLE_VERBOSE) $(1))
|
||||
endef
|
||||
endif
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
# shellcheck shell=sh
|
||||
# test current shell flags
|
||||
## rc.sh calls user defined functions
|
||||
# author: Yann "aya" Autissier
|
||||
# license: MIT
|
||||
# updated: 2021/03/04
|
||||
|
||||
case $- in
|
||||
# if we are in an interactive shell
|
||||
*i*)
|
||||
# load user stuff from files ~/.rc.d/*
|
||||
# load user stuff from ~/.rc.d/* files
|
||||
for file in "${HOME}"/.rc.d/*; do
|
||||
# read files only
|
||||
if [ -f "${file}" ]; then
|
||||
@@ -19,7 +23,7 @@ case $- in
|
||||
command -v "${func_name}" >/dev/null 2>&1 && "${func_name}" "${func_args}"
|
||||
fi
|
||||
done
|
||||
# load user stuff from env vars RC_*
|
||||
# load user stuff from RC_* env vars
|
||||
IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}"; for line in $(printenv 2>/dev/null |awk '$0 ~ /^RC_[1-9A-Z_]*=/'); do
|
||||
func_name=$(printf '%s\n' "${line%%=*}" |awk '{print tolower(substr($0,4))}')
|
||||
eval func_args=\$"${line%%=*}"
|
||||
|
||||
@@ -1,218 +1,283 @@
|
||||
# shellcheck shell=sh
|
||||
## rc_function.sh defines customs shell functions
|
||||
# author: Yann "aya" Autissier
|
||||
# license: MIT
|
||||
# updated: 2021/03/04
|
||||
|
||||
## force() runs a command sine die
|
||||
force() {
|
||||
if [ $# -gt 0 ]; then
|
||||
while true; do
|
||||
"$@"
|
||||
sleep 1
|
||||
done
|
||||
fi
|
||||
if [ $# -gt 0 ]; then
|
||||
while true; do
|
||||
"$@"
|
||||
sleep 1
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
## force8() runs a command sine die if not already running
|
||||
force8() {
|
||||
if [ $# -gt 0 ]; then
|
||||
while true; do
|
||||
# awk expression to match $@
|
||||
[ "$(ps wwx -o args 2>/dev/null |awk -v field="${PS_X_FIELD:-1}" '
|
||||
BEGIN {nargs=split("'"$*"'",args)}
|
||||
# if match first field
|
||||
$field == args[1] {
|
||||
matched=1;
|
||||
# match following fields
|
||||
for (i=1;i<=NF-field;i++) {
|
||||
if ($(i+field) == args[i+1]) {matched++}
|
||||
}
|
||||
# all fields matched
|
||||
if (matched == nargs) {found++}
|
||||
}
|
||||
END {print found+0}'
|
||||
)" = 0 ] && "$@"
|
||||
sleep 1
|
||||
done
|
||||
fi
|
||||
if [ $# -gt 0 ]; then
|
||||
while true; do
|
||||
# awk expression to match $@
|
||||
[ "$(ps wwx -o args 2>/dev/null |awk -v field="${PS_X_FIELD:-1}" '
|
||||
BEGIN { nargs=split("'"$*"'",args); }
|
||||
# first field matched
|
||||
$field == args[1] {
|
||||
matched=1;
|
||||
# match following fields
|
||||
for (i=1;i<=NF-field;i++) {
|
||||
if ($(i+field) == args[i+1]) { matched++; }
|
||||
};
|
||||
# all fields matched
|
||||
if (matched == nargs) { found++; }
|
||||
}
|
||||
END { print found+0; }'
|
||||
)" = 0 ] && "$@"
|
||||
sleep 1
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
## load_average() prints the current load average
|
||||
load_average() {
|
||||
awk '{printf "%.1f\n" $1}' /proc/loadavg 2>/dev/null \
|
||||
|| uptime 2>/dev/null |awk '{printf "%.1f\n", $(NF-2)}'
|
||||
}
|
||||
awk '{printf "%.1f\n" $1}' /proc/loadavg 2>/dev/null\
|
||||
|| uptime 2>/dev/null |awk '{printf "%.1f\n", $(NF-2)}'
|
||||
}
|
||||
|
||||
## process_count() prints number of "processes"/"running processes"/"D-state"
|
||||
process_count() {
|
||||
ps ax -o stat 2>/dev/null |awk '
|
||||
$1 ~ /R/ {process_running++};
|
||||
$1 ~ /D/ {process_dstate++};
|
||||
END {
|
||||
print NR-1"/"process_running+0"/"process_dstate+0;
|
||||
}'
|
||||
ps ax -o stat 2>/dev/null |awk '
|
||||
$1 ~ /R/ {process_running++};
|
||||
$1 ~ /D/ {process_dstate++};
|
||||
END { print NR-1"/"process_running+0"/"process_dstate+0; }'
|
||||
}
|
||||
|
||||
## prompt_set() exports custom PROMPT_COMMAND
|
||||
prompt_set() {
|
||||
case "${TERM}" in
|
||||
screen*)
|
||||
ESCAPE_CODE_DCS="\033k"
|
||||
ESCAPE_CODE_ST="\033\\"
|
||||
;;
|
||||
linux*|xterm*|rxvt*)
|
||||
ESCAPE_CODE_DCS="\033]0;"
|
||||
ESCAPE_CODE_ST="\007"
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
# in a screen
|
||||
if [ -n "${STY}" ]; then
|
||||
export PROMPT_COMMAND='printf "${ESCAPE_CODE_DCS:-\033]0;}%s${ESCAPE_CODE_ST:-\007}" "${PWD##*/}"'
|
||||
else
|
||||
export PROMPT_COMMAND='printf "${ESCAPE_CODE_DCS:-\033]0;}%s@%s:%s${ESCAPE_CODE_ST:-\007}" "${USER}" "${HOSTNAME%%.*}" "${PWD##*/}"'
|
||||
fi
|
||||
unset ESCAPE_CODE_DCS ESCAPE_CODE_ST
|
||||
case "${TERM}" in
|
||||
screen*)
|
||||
ESCAPE_CODE_DCS="\033k"
|
||||
ESCAPE_CODE_ST="\033\\"
|
||||
;;
|
||||
linux*|xterm*|rxvt*)
|
||||
ESCAPE_CODE_DCS="\033]0;"
|
||||
ESCAPE_CODE_ST="\007"
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
# in a screen
|
||||
if [ -n "${STY}" ]; then
|
||||
export PROMPT_COMMAND='printf\
|
||||
"${ESCAPE_CODE_DCS:-\033]0;}%s${ESCAPE_CODE_ST:-\007}"\
|
||||
"${PWD##*/}"'
|
||||
else
|
||||
export PROMPT_COMMAND='printf\
|
||||
"${ESCAPE_CODE_DCS:-\033]0;}%s@%s:%s${ESCAPE_CODE_ST:-\007}"\
|
||||
"${USER}"\
|
||||
"${HOSTNAME%%.*}"\
|
||||
"${PWD##*/}"'
|
||||
fi
|
||||
unset ESCAPE_CODE_DCS ESCAPE_CODE_ST
|
||||
}
|
||||
|
||||
## ps1_set() exports custom PS1
|
||||
ps1_set() {
|
||||
case "$0" in
|
||||
*sh)
|
||||
COLOR_DGRAY="\[\033[1;30m\]"
|
||||
COLOR_RED="\[\033[01;31m\]"
|
||||
COLOR_GREEN="\[\033[01;32m\]"
|
||||
COLOR_BROWN="\[\033[0;33m\]"
|
||||
COLOR_YELLOW="\[\033[01;33m\]"
|
||||
COLOR_BLUE="\[\033[01;34m\]"
|
||||
COLOR_CYAN="\[\033[0;36m\]"
|
||||
COLOR_GRAY="\[\033[0;37m\]"
|
||||
COLOR_NC="\[\033[0m\]"
|
||||
;;
|
||||
case "$0" in
|
||||
*sh)
|
||||
COLOR_DGRAY="\[\033[1;30m\]"
|
||||
COLOR_RED="\[\033[01;31m\]"
|
||||
COLOR_GREEN="\[\033[01;32m\]"
|
||||
COLOR_BROWN="\[\033[0;33m\]"
|
||||
COLOR_YELLOW="\[\033[01;33m\]"
|
||||
COLOR_BLUE="\[\033[01;34m\]"
|
||||
COLOR_CYAN="\[\033[0;36m\]"
|
||||
COLOR_GRAY="\[\033[0;37m\]"
|
||||
COLOR_RESET="\[\033[0m\]"
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
|
||||
PS1_STATUS="\$?"
|
||||
PS1_COUNT="${COLOR_DGRAY}[\`
|
||||
case \"$PS1_STATUS\" in
|
||||
0)
|
||||
printf \"${COLOR_BLUE}${PS1_STATUS}\";;
|
||||
1)
|
||||
printf \"${COLOR_YELLOW}${PS1_STATUS}\";;
|
||||
*)
|
||||
;;
|
||||
printf \"${COLOR_RED}${PS1_STATUS}\";;
|
||||
esac
|
||||
|
||||
PS1_COUNT="${COLOR_DGRAY}[${COLOR_BLUE}\$(process_count 2>/dev/null)${COLOR_DGRAY}|${COLOR_BLUE}\$(user_count 2>/dev/null)${COLOR_DGRAY}|${COLOR_BLUE}\$(load_average 2>/dev/null)${COLOR_DGRAY}]${COLOR_NC}"
|
||||
PS1_END="${COLOR_DGRAY}\$(if [ \"\$(id -u)\" = 0 ]; then printf \"#\"; else printf \"\$\"; fi)${COLOR_NC}"
|
||||
if type __git_ps1 >/dev/null 2>&1; then
|
||||
PS1_GIT="\$(__git_ps1 2>/dev/null \" (%s)\")"
|
||||
type process_count >/dev/null 2>&1 && printf\
|
||||
\"${COLOR_DGRAY}|${COLOR_BLUE}%s\"\
|
||||
\"\$(process_count 2>/dev/null)\"
|
||||
type user_count >/dev/null 2>&1 && printf\
|
||||
\"${PS1_COUNT}${COLOR_DGRAY}|${COLOR_BLUE}%s\"\
|
||||
\"\$(user_count 2>/dev/null)\"
|
||||
type load_average >/dev/null 2>&1 && printf\
|
||||
\"${PS1_COUNT}${COLOR_DGRAY}|${COLOR_BLUE}%s\"\
|
||||
\"\$(load_average 2>/dev/null)\"
|
||||
\`${COLOR_DGRAY}]${COLOR_RESET}"
|
||||
PS1_END="${COLOR_DGRAY}\$(
|
||||
if [ \"\$(id -u)\" = 0 ]; then
|
||||
printf \"#\";
|
||||
else
|
||||
PS1_GIT="\$(BRANCH=\$(git rev-parse --abbrev-ref HEAD 2>/dev/null); [ -n \"\${BRANCH}\" ] && printf \" (\${BRANCH})\")"
|
||||
printf \"\$\";
|
||||
fi
|
||||
PS1_GIT="${COLOR_CYAN}${PS1_GIT}${COLOR_NC}"
|
||||
PS1_HOSTNAME_COLOR="\`case \"\${ENV}\" in [Pp][Rr][0Oo][Dd]*) printf \"${COLOR_RED}\";; *) if [ -n \"\${ENV}\" ]; then printf \"${COLOR_YELLOW}\"; else printf \"${COLOR_GREEN}\"; fi;; esac\`"
|
||||
PS1_HOSTNAME="${PS1_HOSTNAME_COLOR}\$(hostname |sed 's/\..*//')${COLOR_NC}"
|
||||
PS1_USER_COLOR="\$(if [ \"\$(id -u)\" = 0 ]; then printf \"${COLOR_RED}\"; else printf \"${COLOR_BROWN}\"; fi)"
|
||||
PS1_USER="${PS1_USER_COLOR}\$(id -nu)${COLOR_NC}"
|
||||
PS1_WORKDIR="${COLOR_GRAY}\$(pwd |sed 's|^'\${HOME}'\(/.*\)*$|~\1|')${COLOR_NC}"
|
||||
|
||||
export PS1="${PS1_COUNT}${PS1_USER}${COLOR_DGRAY}@${PS1_HOSTNAME}${COLOR_DGRAY}:${PS1_WORKDIR}${PS1_GIT}${PS1_END} "
|
||||
unset PS1_COUNT PS1_END PS1_GIT PS1_HOSTNAME PS1_HOSTNAME_COLOR PS1_USER PS1_USER_COLOR PS1_WORKDIR
|
||||
)${COLOR_RESET}"
|
||||
PS1_GIT="\$(
|
||||
if type __git_ps1 >/dev/null 2>&1; then
|
||||
printf \"\$(__git_ps1 2>/dev/null \" (%s)\")\"
|
||||
else
|
||||
printf \"\$(BRANCH=\$(git rev-parse --abbrev-ref HEAD 2>/dev/null);\
|
||||
[ -n \"\${BRANCH}\" ] && printf \" (\${BRANCH})\")\"
|
||||
fi
|
||||
)"
|
||||
PS1_GIT="${COLOR_CYAN}${PS1_GIT}${COLOR_RESET}"
|
||||
PS1_HOSTNAME_COLOR="\`case \"\${ENV}\" in
|
||||
[Pp][Rr][0Oo][Dd]*)
|
||||
printf \"${COLOR_RED}\";;
|
||||
*)
|
||||
if [ -n \"\${ENV}\" ]; then
|
||||
printf \"${COLOR_YELLOW}\";
|
||||
else
|
||||
printf \"${COLOR_GREEN}\";
|
||||
fi;;
|
||||
esac\`"
|
||||
PS1_HOSTNAME="${PS1_HOSTNAME_COLOR}\$(hostname |sed 's/\..*//')${COLOR_RESET}"
|
||||
PS1_USER_COLOR="\$(
|
||||
if [ \"\$(id -u)\" = 0 ]; then
|
||||
printf \"${COLOR_RED}\";
|
||||
else
|
||||
printf \"${COLOR_BROWN}\";
|
||||
fi
|
||||
)"
|
||||
PS1_USER="${PS1_USER_COLOR}\$(id -nu):\$(id -u)${COLOR_RESET}"
|
||||
PS1_WORKDIR="${COLOR_GRAY}\$(
|
||||
pwd |sed 's|^'\${HOME}'\(/.*\)*$|~\1|'
|
||||
)${COLOR_RESET}"
|
||||
PS1="${PS1_COUNT}${PS1_USER}${COLOR_DGRAY}@${PS1_HOSTNAME}"
|
||||
PS1="${PS1}${COLOR_DGRAY}:${PS1_WORKDIR}${PS1_GIT}${PS1_END} "
|
||||
export 'PS1'
|
||||
unset PS1_COUNT PS1_END PS1_GIT PS1_HOSTNAME PS1_HOSTNAME_COLOR\
|
||||
PS1_USER PS1_USER_COLOR PS1_STATUS PS1_WORKDIR
|
||||
}
|
||||
|
||||
## screen_attach() attaches existing screen session or creates a new one
|
||||
screen_attach() {
|
||||
command -v screen >/dev/null 2>&1 || return
|
||||
SCREEN_SESSION="$(id -nu)@$(hostname |sed 's/\..*//')"
|
||||
if [ -z "${STY}" ]; then
|
||||
# attach screen in tmux window 0 only ;)
|
||||
[ -n "${TMUX}" ] && [ "$(tmux list-window 2>/dev/null |awk '$NF == "(active)" {print $1}' |sed 's/:$//')" != "0" ] && return
|
||||
printf 'Attaching screen.' && sleep 1 && printf '.' && sleep 1 && printf '.' && sleep 1
|
||||
exec screen -xRR -S "${SCREEN_SESSION}"
|
||||
fi
|
||||
unset SCREEN_SESSION
|
||||
command -v screen >/dev/null 2>&1 || return
|
||||
SCREEN_SESSION="$(id -nu)@$(hostname |sed 's/\..*//')"
|
||||
if [ -z "${STY}" ]; then
|
||||
# attach screen in tmux window 0 only ;)
|
||||
[ -n "${TMUX}" ] \
|
||||
&& [ "$(tmux list-window 2>/dev/null |awk '$NF == "(active)" {print $1}'\
|
||||
|sed 's/:$//')" != "0" ] \
|
||||
&& return
|
||||
printf 'Attaching screen.' && sleep 1\
|
||||
&& printf '.' && sleep 1\
|
||||
&& printf '.' && sleep 1
|
||||
exec screen -xRR -S "${SCREEN_SESSION}"
|
||||
fi
|
||||
unset SCREEN_SESSION
|
||||
}
|
||||
|
||||
## screen_detach() detaches current screen session
|
||||
screen_detach() {
|
||||
screen -d
|
||||
screen -d
|
||||
}
|
||||
|
||||
|
||||
## ssh_add() loads all private keys in ~/.ssh/ to ssh agent
|
||||
ssh_add() {
|
||||
command -v ssh-agent >/dev/null 2>&1 && command -v ssh-add >/dev/null 2>&1 || return
|
||||
SSH_AGENT_DIR="/tmp/ssh-$(id -u)"
|
||||
SSH_AGENT_SOCK="${SSH_AGENT_DIR}/agent@$(hostname |sed 's/\..*//')"
|
||||
# launch a new agent
|
||||
if [ -z "${SSH_AUTH_SOCK}" ]; then
|
||||
[ ! -d "${SSH_AGENT_DIR}" ] && mkdir -p "${SSH_AGENT_DIR}" 2>/dev/null && chmod 0700 "${SSH_AGENT_DIR}"
|
||||
# search for an already running agent
|
||||
if ps wwx -o args |awk '$1 ~ "ssh-agent$" && $3 == "'"${SSH_AGENT_SOCK}"'"' |wc -l |grep -q 0; then
|
||||
rm -f "${SSH_AGENT_SOCK}"
|
||||
ssh-agent -a "${SSH_AGENT_SOCK}" >/dev/null 2>&1
|
||||
fi
|
||||
command -v ssh-agent >/dev/null 2>&1 && command -v ssh-add >/dev/null 2>&1 || return
|
||||
SSH_AGENT_DIR="/tmp/ssh-$(id -u)"
|
||||
SSH_AGENT_SOCK="${SSH_AGENT_DIR}/agent@$(hostname |sed 's/\..*//')"
|
||||
# launch a new agent
|
||||
if [ -z "${SSH_AUTH_SOCK}" ]; then
|
||||
[ ! -d "${SSH_AGENT_DIR}" ] \
|
||||
&& mkdir -p "${SSH_AGENT_DIR}" 2>/dev/null\
|
||||
&& chmod 0700 "${SSH_AGENT_DIR}"
|
||||
# search for an already running agent
|
||||
if ps wwx -o args |awk '$1 ~ "ssh-agent$" && $3 == "'"${SSH_AGENT_SOCK}"'"' |wc -l |grep -q 0; then
|
||||
rm -f "${SSH_AGENT_SOCK}"
|
||||
ssh-agent -a "${SSH_AGENT_SOCK}" >/dev/null 2>&1
|
||||
fi
|
||||
# attach to agent
|
||||
export SSH_AUTH_SOCK="${SSH_AUTH_SOCK:-${SSH_AGENT_SOCK}}"
|
||||
# list private keys to add
|
||||
# shellcheck disable=SC2068
|
||||
for dir in ${@:-${HOME}/.ssh}; do
|
||||
if [ "${SSH_ADD_RECURSIVE:-}" = true ]; then
|
||||
GREP_RECURSIVE_FLAG="r"
|
||||
else
|
||||
GREP_RECURSIVE_CHAR="*"
|
||||
fi
|
||||
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}"/"${GREP_RECURSIVE_CHAR:-}" 2>/dev/null |grep -vw "${dir}"/id_rsa)"
|
||||
done
|
||||
# shellcheck disable=SC2086
|
||||
printf '%s\n' ${SSH_PRIVATE_KEYS} |while read -r file; do
|
||||
[ -r "${file}" ] || continue
|
||||
# add private key to agent
|
||||
ssh-add -l |grep -q "$(ssh-keygen -lf "${file}" 2>/dev/null |awk '{print $2}')" 2>/dev/null || ssh-add "${file}"
|
||||
done
|
||||
unset GREP_RECURSIVE_CHAR GREP_RECURSIVE_FLAG SSH_AGENT_DIR SSH_AGENT_SOCK SSH_PRIVATE_KEYS
|
||||
fi
|
||||
# attach to agent
|
||||
export SSH_AUTH_SOCK="${SSH_AUTH_SOCK:-${SSH_AGENT_SOCK}}"
|
||||
# list private keys to add
|
||||
# shellcheck disable=SC2068
|
||||
for dir in ${@:-${HOME}/.ssh}; do
|
||||
if [ "${SSH_ADD_RECURSIVE:-}" = true ]; then
|
||||
GREP_RECURSIVE_FLAG="r"
|
||||
else
|
||||
GREP_RECURSIVE_CHAR="*"
|
||||
fi
|
||||
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}"/"${GREP_RECURSIVE_CHAR:-}" 2>/dev/null |grep -vw "${dir}"/id_rsa)"
|
||||
done
|
||||
# shellcheck disable=SC2086
|
||||
printf '%s\n' ${SSH_PRIVATE_KEYS} |while read -r file; do
|
||||
[ -r "${file}" ] || continue
|
||||
# add private key to agent
|
||||
ssh-add -l |grep -q "$(ssh-keygen -lf "${file}" 2>/dev/null |awk '{print $2}')" 2>/dev/null || ssh-add "${file}"
|
||||
done
|
||||
unset GREP_RECURSIVE_CHAR GREP_RECURSIVE_FLAG SSH_AGENT_DIR SSH_AGENT_SOCK SSH_PRIVATE_KEYS
|
||||
}
|
||||
|
||||
## ssh_del() removes all private keys in ~/.ssh/ from ssh agent
|
||||
ssh_del() {
|
||||
command -v ssh-add >/dev/null 2>&1 || return
|
||||
# attach to agent
|
||||
if [ -z "${SSH_AUTH_SOCK}" ]; then
|
||||
return
|
||||
command -v ssh-add >/dev/null 2>&1 || return
|
||||
# attach to agent
|
||||
if [ -z "${SSH_AUTH_SOCK}" ]; then
|
||||
return
|
||||
fi
|
||||
# list private keys to del
|
||||
# shellcheck disable=SC2068
|
||||
for dir in ${@:-${HOME}/.ssh}; do
|
||||
if [ "${SSH_DEL_RECURSIVE:-}" = true ]; then
|
||||
GREP_RECURSIVE_FLAG="r"
|
||||
else
|
||||
GREP_RECURSIVE_CHAR="*"
|
||||
fi
|
||||
# list private keys to del
|
||||
# shellcheck disable=SC2068
|
||||
for dir in ${@:-${HOME}/.ssh}; do
|
||||
if [ "${SSH_DEL_RECURSIVE:-}" = true ]; then
|
||||
GREP_RECURSIVE_FLAG="r"
|
||||
else
|
||||
GREP_RECURSIVE_CHAR="*"
|
||||
fi
|
||||
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}"/"${GREP_RECURSIVE_CHAR:-}" 2>/dev/null |grep -vw "${dir}"/id_rsa)"
|
||||
done
|
||||
# shellcheck disable=SC2086
|
||||
printf '%s\n' ${SSH_PRIVATE_KEYS} |while read -r file; do
|
||||
[ -r "${file}" ] || continue
|
||||
# remove private key from agent
|
||||
ssh-add -l |grep -q "$(ssh-keygen -lf "${file}" 2>/dev/null |awk '{print $2}')" 2>/dev/null && ssh-add -d "${file}"
|
||||
done
|
||||
unset GREP_RECURSIVE_CHAR GREP_RECURSIVE_FLAG SSH_PRIVATE_KEYS
|
||||
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}"/"${GREP_RECURSIVE_CHAR:-}" 2>/dev/null |grep -vw "${dir}"/id_rsa)"
|
||||
done
|
||||
# shellcheck disable=SC2086
|
||||
printf '%s\n' ${SSH_PRIVATE_KEYS} |while read -r file; do
|
||||
[ -r "${file}" ] || continue
|
||||
# remove private key from agent
|
||||
ssh-add -l |grep -q "$(ssh-keygen -lf "${file}" 2>/dev/null |awk '{print $2}')" 2>/dev/null && ssh-add -d "${file}"
|
||||
done
|
||||
unset GREP_RECURSIVE_CHAR GREP_RECURSIVE_FLAG SSH_PRIVATE_KEYS
|
||||
}
|
||||
|
||||
## tmux_attach() attaches existing tmux session or creates a new one
|
||||
tmux_attach() {
|
||||
command -v tmux >/dev/null 2>&1 || return
|
||||
TMUX_SESSION="$(id -nu)@$(hostname |sed 's/\..*//')"
|
||||
if [ -z "${TMUX}" ]; then
|
||||
printf 'Attaching tmux.' && sleep 1 && printf '.' && sleep 1 && printf '.' && sleep 1
|
||||
exec tmux -L"${TMUX_SESSION}" new-session -A -s"${TMUX_SESSION}"
|
||||
fi
|
||||
unset TMUX_SESSION
|
||||
command -v tmux >/dev/null 2>&1 || return
|
||||
TMUX_SESSION="$(id -nu)@$(hostname |sed 's/\..*//')"
|
||||
if [ -z "${TMUX}" ]; then
|
||||
printf 'Attaching tmux.' && sleep 1\
|
||||
&& printf '.' && sleep 1\
|
||||
&& printf '.' && sleep 1
|
||||
exec tmux -L"${TMUX_SESSION}" new-session -A -s"${TMUX_SESSION}"
|
||||
fi
|
||||
unset TMUX_SESSION
|
||||
}
|
||||
|
||||
## tmux_detach() detaches current tmux session
|
||||
tmux_detach() {
|
||||
tmux detach
|
||||
tmux detach
|
||||
}
|
||||
|
||||
## user_count() prints number of "users sessions"/"users"/"logged users"
|
||||
user_count() {
|
||||
ps ax -o user,tty 2>/dev/null |awk '
|
||||
$2 ~ /^(pts|tty)/ {users_session++; logged[$1]++;};
|
||||
{count[$1]++;}
|
||||
END {
|
||||
for (uc in count) {c = c" "uc;}; users_count=split(c,v," ")-1;
|
||||
for (ul in logged) {l = l" "ul;}; users_logged=split(l,v," ")-1;
|
||||
print users_session+0"/"users_count"/"users_logged;
|
||||
}'
|
||||
ps ax -o user,tty 2>/dev/null |awk '
|
||||
$2 ~ /^(pts|tty)/ { users_session++; logged[$1]++; };
|
||||
{ count[$1]++; }
|
||||
END {
|
||||
for (uc in count) { c = c" "uc; }; users_count=split(c,v," ")-1;
|
||||
for (ul in logged) { l = l" "ul; }; users_logged=split(l,v," ")-1;
|
||||
print users_session+0"/"users_count"/"users_logged;
|
||||
}'
|
||||
}
|
||||
|
||||
# vim:ts=2:sw=2:sts=2:et
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
USER := aynicos
|
||||
APP ?= $(shell hostname -s)
|
||||
CMDS := exec
|
||||
SERVICE ?= php
|
||||
SERVICE ?= cli
|
||||
-include .env
|
||||
|
||||
ansible:
|
||||
@@ -14,16 +12,16 @@ ansible-pull:
|
||||
@$(foreach var,$(ENV_USER_VARS),$(if $($(var)),$(var)='$($(var))')) $(if $(ANSIBLE_CONFIG),ANSIBLE_CONFIG=$(ANSIBLE_GIT_DIRECTORY)/$(ANSIBLE_CONFIG)) ansible-pull --url $(ANSIBLE_GIT_REPOSITORY) $(if $(ANSIBLE_GIT_KEY_FILE),--key-file $(ANSIBLE_GIT_KEY_FILE)) $(if $(ANSIBLE_GIT_VERSION),--checkout $(ANSIBLE_GIT_VERSION)) $(if $(ANSIBLE_GIT_DIRECTORY),--directory $(ANSIBLE_GIT_DIRECTORY)) $(if $(ANSIBLE_TAGS),--tags $(ANSIBLE_TAGS)) $(if $(ANSIBLE_EXTRA_VARS),--extra-vars '$(ANSIBLE_EXTRA_VARS)') $(if $(findstring true,$(FORCE)),--force) $(if $(findstring true,$(DRYRUN)),--check) --full $(if $(ANSIBLE_INVENTORY),--inventory $(ANSIBLE_INVENTORY)) $(ANSIBLE_VERBOSE) $(ANSIBLE_PLAYBOOK)
|
||||
|
||||
exec:
|
||||
@make -C $(ANSIBLE_GIT_DIRECTORY) exec ARGS='$(ARGS)' DOCKER_NAME=$(USER)_$(ENV)_$(APP)_$(SERVICE) ENV=$(ENV) DOCKER_RUN_WORKDIR=
|
||||
@make -C $(ANSIBLE_GIT_DIRECTORY) exec ARGS='$(ARGS)' DOCKER_NAME=$(COMPOSE_PROJECT_NAME)_$(SERVICE) ENV=$(ENV) DOCKER_RUN_WORKDIR=
|
||||
|
||||
connect:
|
||||
@docker exec -it $(USER)_$(ENV)_$(APP)_$(SERVICE) /bin/bash || true
|
||||
@docker exec -it $(COMPOSE_PROJECT_NAME)_$(SERVICE) /bin/bash || true
|
||||
|
||||
logs:
|
||||
@docker logs --follow --tail 100 $(USER)_$(ENV)_$(APP)_$(SERVICE) || true
|
||||
@docker logs --follow --tail 100 $(COMPOSE_PROJECT_NAME)_$(SERVICE) || true
|
||||
|
||||
logs-nofollow:
|
||||
@docker logs --tail 100 $(USER)_$(ENV)_$(APP)_$(SERVICE) || true
|
||||
@docker logs --tail 100 $(COMPOSE_PROJECT_NAME)_$(SERVICE) || true
|
||||
|
||||
run:
|
||||
@$(ARGS)
|
||||
|
||||
Reference in New Issue
Block a user