Skip to content

Commit

Permalink
Merge pull request #121 from rosineygp/feat/function-log
Browse files Browse the repository at this point in the history
Feat/function log
  • Loading branch information
rosineygp authored May 9, 2020
2 parents 616814f + d97c13b commit 420c0f0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 55 deletions.
61 changes: 46 additions & 15 deletions .mkdkr
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,10 @@ _remote_git_clone() {
_FILE="${4}"
_MKDKR_INCLUDE_PATH="${5}"

_pretty "light_cyan" "alias: ${_ALIAS}"
_pretty "light_cyan" "repos: ${_REPOS}"
_pretty "light_cyan" "checkout: ${_CHECKOUT}"
_pretty "light_cyan" "file: ${_FILE}\n"
_pretty "light_cyan" "alias: ${_ALIAS}, repos: ${_REPOS}, checkout: ${_CHECKOUT}, file: ${_FILE}\n"

git clone "${_REPOS}" \
--quiet \
--config advice.detachedHead=false \
--branch "${_CHECKOUT}" \
--depth "${MKDKR_INCLUDE_CLONE_DEPTH:-1}" \
Expand All @@ -167,7 +165,7 @@ _remote_force_tag() {

if [ "$(grep -E '^[A-Z][A-Z\_]+\_TAG\=.*' "${_FILE}")" != "" ]; then
_KEY_VALUE=($(grep -E '^[A-Z][A-Z\_]+\_TAG\=.*' "${_FILE}" | tr '=' '\n'))
_pretty "purple" "replace ${_KEY_VALUE[0]}: ${_KEY_VALUE[1]} to ${_TAG}"
_pretty "purple" "replace: ${_KEY_VALUE[0]}=${_KEY_VALUE[1]} to ${_TAG}"
sed -i -E "s/(^[A-Z][A-Z\_]+\_TAG\=)(.*)/\1${_TAG}/g" "${_FILE}"
fi
}
Expand Down Expand Up @@ -226,14 +224,10 @@ MKDKR_BRANCH_NAME_SLUG=$MKDKR_BRANCH_NAME_SLUG
define dkr =
source .mkdkr
\$(eval MKDKR_JOB_NAME=\$(shell source .mkdkr; _job_uniq_name \$(@)))
\$(eval MKDKR_JOB_STDOUT="${_MKDKR_TMP_DIR}/\${MKDKR_JOB_NAME}.log")
\$(eval MKDKR_TARGET_NAME=\$(@))
trap _job_destroy EXIT
_pretty "orange" "\nstart: \$(@)\n"
endef
define stdout =
if [ -f \$(MKDKR_JOB_STDOUT) ]; then cat \$(MKDKR_JOB_STDOUT); fi
endef
EOF
}

Expand All @@ -251,7 +245,6 @@ _tmp_dir() {
_header() {
_MKDKR_INCLUDE="${_MKDKR_TMP_DIR}/mkdkr_header_$(_uuid).mk"
_header_render >"${_MKDKR_INCLUDE}"
chmod o+w "${_MKDKR_INCLUDE}"
echo "${_MKDKR_INCLUDE}"
}

Expand All @@ -268,8 +261,8 @@ _job_destroy() {
if [ "${ALIVE[0]}" != "" ]; then
_pretty "cyan" "\ncleanup:"
docker ps | grep "${MKDKR_JOB_NAME}" | awk '{print $1}' | xargs -i{} docker rm -f {} >&2
if [ -f "${_MKDKR_TMP_DIR}/${MKDKR_JOB_NAME}.log" ]; then
rm "${_MKDKR_TMP_DIR}/${MKDKR_JOB_NAME}.log"
if [ -f "${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log" ]; then
rm -v "${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log" >&2
fi
fi

Expand Down Expand Up @@ -303,6 +296,23 @@ slug() {
echo "${1}" | sed -E 's/:|\.|\/|-/_/g'
}

_step_id() {
_STEP_ID=0
if [ -f "${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log" ]; then
_STEP_ID="$(grep -c '^MKDKR_HEREDOC_.*$' ${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log)"
fi

echo "$_STEP_ID"
}

_step_header() {
echo -e "STEP_${1} << MKDKR_HEREDOC_${1}" >> "${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log"
}

_step_footer() {
echo -e "MKDKR_HEREDOC_${1}\n" >> "${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log"
}

instance:() {
OLDIFS=$IFS
IFS='' # Braille Pattern Blank (U+2800)
Expand Down Expand Up @@ -396,9 +406,30 @@ run:() {
mkdir -p "${_MKDKR_TMP_DIR}"
fi

docker exec -i ${CONTAINER_NAME} ${MKDKR_SHELL:-sh} -c "$*" | tee "${_MKDKR_TMP_DIR}/${MKDKR_JOB_NAME}.log"
_STEP_ID=$(_step_id)
_step_header "$_STEP_ID"

docker exec -i ${CONTAINER_NAME} ${MKDKR_SHELL:-sh} -c "$*" | tee -a "${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log"
_EXIT_CODE="${PIPESTATUS[0]}"

_step_footer "$_STEP_ID"
_exit_handler "${_EXIT_CODE}"
}

log: () {
_ID="${1}"

_pretty "purple" "\nlog: ${_ID}"
awk '/^STEP_'"${_ID}"' << MKDKR_HEREDOC_'"${_ID}"'$/ {on=1} on==1 {print} /^MKDKR_HEREDOC_'"${_ID}"'$/ {on=0; print ""}' \
"${_MKDKR_TMP_DIR}/${MKDKR_TARGET_NAME}.log" | \
grep -v 'MKDKR_HEREDOC_'

_EXIT_CODE="${PIPESTATUS[1]}"
if [ "$_EXIT_CODE" != "0" ]; then
_pretty "red" "error: null pointer execption, log id not found."
fi

_exit_handler "${PIPESTATUS[0]}"
_exit_handler "${_EXIT_CODE}"
}

if [ "$1" == "init" ]; then
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ examples.escapes:
make --silent -f examples/escapes.mk redirect_to_outside

examples.stdout:
make --silent -f examples/stdout.mk from-filename
make --silent -f examples/stdout.mk from-function
make --silent -f examples/stdout.mk show-path
make --silent -f examples/stdout.mk parse-output
make --silent -f examples/stdout.mk from
make --silent -f examples/stdout.mk back-to-container

examples.shell:
make --silent -f examples/shell.mk shell
Expand Down
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Table of contents
* [service:](#service)
* [dind:](#dind)
* [run:](#run)
* [log:](#log)
* [Includes](#includes)
* [Explicit](#explicit)
* [Implicit](#implicit)
Expand Down Expand Up @@ -270,6 +271,27 @@ my-run:
run: 'ls -la > myfile'
```

## log:

All output steps executed in a job (except log:) is stored and can be reused during future steps.

**Parameters:**
- Number: The number represent the step in a job and start with 0.

**Return:**
- Text, Multiline text.

**Usage**

```Makefile
my-log:
@$(dkr)
instance: alpine
run: apk add curl jq
run: curl http://example.com
log: 1 \| jq '.'
```

# Includes

Is possible create jobs or fragments of jobs and reuse it in another projects, like a code package library.
Expand Down Expand Up @@ -442,19 +464,20 @@ shell:

## Stdout

Get last command output
Get output by id

Use to filter or apply some logic in last command executed (also outside container)
Use to filter or apply some logic in last command executed

```Makefile
stdout:
@$(dkr)
instance: alpine
run: echo "hello mkdkr!"
cat "$(MKDKR_JOB_STDOUT)"
run: ps -ef
log: 1
```

> `$(MKDKR_JOB_STDOUT)` return path of file
> `log: 1` return stout form second command `ps -ef`

```Makefile
stdout:
Expand All @@ -463,9 +486,9 @@ stdout:
run: apt-get update
run: apt-get install curl -y
run: dpkg -l
$(stdout) | grep -i curl && echo "INSTALLED"
log: 2 | grep -i curl && echo "INSTALLED"
```
> `$(stdout)` return output file using cat
> `log: 2` return stdout from third command `dpkg -l` and apply filter

[Makefile](examples/stdout.mk)

Expand Down
7 changes: 1 addition & 6 deletions examples/simple.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ multi-images:
run: 'curl -s https://i.kym-cdn.com/entries/icons/mobile/000/018/012/this_is_fine.jpg > img.jpg'
instance: ubuntu
run: apt update -qq
run: apt install imagemagick -y
run: convert img.jpg img.png
instance: node:10
run: npm install -g picture-tube
run: picture-tube img.png
run: rm img.jpg img.png
run: ls -la
dind: docker:19
run: docker build -t my/image .
38 changes: 14 additions & 24 deletions examples/stdout.mk
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
include $(shell bash .mkdkr init)

from-filename:
@$(dkr)
instance: alpine
run: echo "hello mkdkr!"
cat "$(MKDKR_JOB_STDOUT)"

from-function:
@$(dkr)
instance: alpine
run: echo "hello mkdkr!"
$(stdout)
run: apk add curl
$(stdout)

show-path:
@$(dkr)
instance: alpine
run: echo "hello mkdkr!"
echo "$(MKDKR_JOB_STDOUT)"

parse-output:
@$(dkr)
instance: debian
run: apt-get update
run: apt-get install curl -y
run: dpkg -l
$(stdout) | grep -i curl && echo "INSTALLED"
run: ls -la
run: printenv
log: 2
log: 1
log: 0

from:
@$(dkr)
instance: alpine
run: echo "passing parameters"
$(MAKE) -f examples/stdout.mk param PARAM="$$(cat $(MKDKR_JOB_STDOUT))"
log: 0
$(MAKE) -f examples/stdout.mk param PARAM="$$(log: 0)"

param:
@$(dkr)
instance: alpine
run: echo $(PARAM)

back-to-container:
@$(dkr)
instance: alpine
run: apk add curl --no-cache
run: apk del curl
run: apk add $$(log: 0 | grep libcurl | cut -d" " -f3)
2 changes: 1 addition & 1 deletion test/unit_job_name
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_mkdkr_job_name() {
[[ "$?" == "0" ]] && \
_pretty "light_purple" "assert ${MKDKR_JOB_NAME} is not null [ OK ]"

assertContains "$MKDKR_JOB_NAME" "$(echo "$NAME" | sed 's/\//_/g')"
assertContains "${MKDKR_JOB_NAME}" "$(echo "$NAME" | sed 's/\//_/g')"
[[ "$?" == "0" ]] && \
_pretty "light_purple" "assert ${MKDKR_JOB_NAME} contains current name [ OK ]"

Expand Down

0 comments on commit 420c0f0

Please sign in to comment.