Skip to content

Commit

Permalink
feat: for simple job not necessary pass a action (#35)
Browse files Browse the repository at this point in the history
- Now job is default for launch action
- Add trap in documentation (missed)
- Improvement of docs
- Add trap and implict-job at pipeline
  • Loading branch information
rosineygp authored Jan 4, 2020
1 parent 56b9ec3 commit 525f3c4
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:
- run:
name: shell
command: make shell
- run:
name: trap
command: make trap
- run:
name: implicit-job
command: make implicit-job
- run:
name: escapes
command: make escapes
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
run: make dind
- name: shell
run: make shell
- name: trap
run: make trap
- name: implicit-job
run: make implicit-job
- name: escapes
run: make escapes
- name: pipeline
Expand Down
10 changes: 10 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ shell:
script:
- make shell

trap:
stage: scenarios
script:
- make trap

implicit-job:
stage: scenarios
script:
- make implicit-job

escapes:
stage: scenarios
script:
Expand Down
15 changes: 11 additions & 4 deletions .mkdkr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

_prn_exec() {
echo -e "\e[32m${*}\e[0m"
echo -e "\e[32m${*}\e[0m"
}

_launch() {
Expand Down Expand Up @@ -32,7 +32,7 @@ _service() {
"${IMAGE}"
}

_destroy_on_exit () {
_destroy_on_exit() {
ALIVE=$(docker ps | grep "${JOB_NAME}" | awk '{print $1}')
if [ "${ALIVE[0]}" != "" ]; then
.
Expand All @@ -54,19 +54,26 @@ _destroy_on_exit () {
ARGS="${*:3}"
COMMAND="sleep ${MKDKR_TTL:-3600}"

if [ "${TYPE}" != "job" ] &&
[ "${TYPE}" != "service" ] &&
[ "${TYPE}" != "privileged" ]; then
ARGS="${*:2}"
IMAGE="${TYPE}"
TYPE="job"
fi

_prn_exec "... ${TYPE} ${IMAGE} ${ARGS[*]}"

if [ "${TYPE}" == "service" ]; then
_service "service_${JOB_NAME}" "${IMAGE}" "${ARGS[*]}"
else
else
if [ "${TYPE}" == "privileged" ]; then
ARGS=("${ARGS[@]}" "--privileged" "-v" "/var/run/docker.sock:/var/run/docker.sock")
fi
_launch "job_${JOB_NAME}" "${IMAGE}" "${COMMAND}" "${ARGS[*]}"
fi
}


# shellcheck disable=SC1036
..() {
CONTAINER_NAME="job_${JOB_NAME}"
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ jobs:
- stage: scenarios
name: escapes
script: make escapes
- stage: scenarios
name: trap
script: make trap
- stage: scenarios
name: implicit-job
script: make implicit-job
- stage: scenarios
name: pipeline
script: make examples/pipeline
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ shell:
trap:
make --silent -f examples/trap.mk

implicit-job:
make --silent -f examples/implicit-job.mk

examples/pipeline:
@cd examples && make --silent -f pipeline.mk pipeline

scenarios: simple service dind escapes shell examples/pipeline
scenarios: simple service dind escapes shell trap implicit-job examples/pipeline

brainfuck:
@$(.)
Expand Down
78 changes: 74 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Table of contents
* [DIND](#dind)
* [Escapes](#escapes)
* [Shell](#shell)
* [Trap](#trap)
* [Implicit Job](#implicit-job)
* [Pipelines](#pipelines)
* [Generators](#generators)
* [Gitlab CI](#gitlab-ci)
Expand Down Expand Up @@ -149,21 +151,43 @@ job:

> Automatically load after call `@$(.)`.

**Usage**

```Bash
.... my-awesome-job # name a job
```

## ••• 3 dots

[required] Create a docker container, it can set as simple job, service or privileged job.

**Parameters:**
- String, ACTION *: Actions is the mode that container will run it can be a:
- job: simple docker container
- String, ACTION: Actions is the mode that container will run it can be a:
- job **[default]**: simple docker container
- service: is like a job, but run in detached mode
- privileged: is a job but with docker socket access

> if any action was passed, it will be a job
- String, IMAGE *: any docker image name
- String|Array, ARGS: additional docker init args like (--cpus 1 --memory 64MB)

**Return:**
- String, Container Id

**Usage**

```Bash
... job alpine # simple job
... ubuntu:18.04 # if it's a simple job, pass action [job] is not required
... centeos:7 \
--cpus 2 \
--memory 1024MB \
-e PASSWORD=$$PASSWORD # container parameters
... service nginx # create a service
... privileged docker:19 # create a job with docker demon access
```

## •• 2 dots

[required] Execute a command inside docker container (job or privileged).
Expand All @@ -174,6 +198,15 @@ job:
**Return:**
- String, Command(s) output

**Usage**

```Bash
.. apk add curl # run a command inside container
.. ls -la > myfile # run a command inside container and redirect output to host
.. 'ls -la > myfile' # run a command inside container and redirect output to container
.. 'apt-get update && \
apt-get install -y curl' # just need '' cause && redirect outside container
```

## • 1 dot

Expand All @@ -187,6 +220,12 @@ job:
**Return:**
- String, Container Id

**Usage**

```Bash
. # Kill 'Em All
```

# Examples

## Simple
Expand All @@ -203,7 +242,6 @@ simple:

## Service


```Makefile
service:
@$(.)
Expand Down Expand Up @@ -259,7 +297,39 @@ shell:

> More examples at file

[Makefile](examples/shell.mk)
## Trap

Prevent keep container running when after error or exit.

```Makefile
broken:
@$(.)
... service nginx
... job alpine
.. ps -ef

```

> Job finished without call **.**, now trap close it correctly.
[Makefile](examples/trap.mk)

## Implicit Job

When start a new job if [action](#-3-dots) passed, it will create a container as a simple job.

```Makefile
implicit-job:
@$(.)
... alpine --memory 32MB
.. echo "hello nano job"
.

```

> implicit='Less code!'
[Makefile](examples/implicit-job.mk)

## Pipeline

Expand Down
15 changes: 15 additions & 0 deletions examples/implicit-job.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.EXPORT_ALL_VARIABLES:
.ONESHELL:
SHELL = /bin/bash

define . =
source .mkdkr
$(eval JOB_NAME=$(shell bash -c 'source .mkdkr; .... $(@)'))
trap '_destroy_on_exit' EXIT
endef

implicit-job:
@$(.)
... alpine --memory 32MB
.. echo "hello nano job"
.
2 changes: 1 addition & 1 deletion examples/trap.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ define . =
trap '_destroy_on_exit' EXIT
endef

# job withou destroy at end
# job without destroy at end
broken:
@$(.)
... service nginx
Expand Down

0 comments on commit 525f3c4

Please sign in to comment.