Skip to content

Add event option to run docker exec commands for a container #2

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

* Python 2.7 installed
* PyYaml installed (see: [documentation](http://pyyaml.org/wiki/PyYAML))
* Docker with Docker-composed installed
* Docker with Docker-compose installed

## Example-Run:

```
python main.py example.yaml
python main.py examples/example-containers.yaml
```

## YAML-File

The YAML-File needs two keys, `setup` and `events` on the base level.
The YAML-File needs two keys, `setup` and `events` on the base level.
```yaml
setup:
# docker-compose setup
events:
# processed by event engine
```

The setup part will be piped directly to docker-compose.
The setup part will be piped directly to docker-compose.
This means, the whole part of container setup is managed by docker-compose.
You can use each version of the docker-compose-file and all supported features.

Expand All @@ -44,14 +44,20 @@ Each event can be directly identified with the key and has the following structu
event1:
dependOn: <list-of-events>
seconds: <amount-of-seconds>
command: <some-command>
commands:
- command1
- command2
- ....
docker_exec:
container: <docker node name from setup part>
commands: <commands to be executed in docker exec>
do:
<map of actions>
```

The first three keys describes when the `do`-block should be executed.
The first three keys describes when the `do`-block should be executed.
The order of these conditions is as shown above.
This means for example that after the events this event1 dependsOn have returned, the program waited a few seconds and the command has succesfully been executed the do-part of this event will be executed.
This means for example that after the events this event1 dependsOn have returned, the program waited a few seconds and the commands have succesfully been executed the do-part of this event will be executed.

#### dependOn
`dependOn` takes a list of other events (identified with their keys).
Expand All @@ -73,14 +79,40 @@ seconds: 5
```
This means the execution waits 5 seconds.

#### command
#### commands
It can execute any list of commands and if this command returns `1`, the `do`-block will be executed.

Example:
```yaml
commands:
- ping -c 1 192.168.1.2
- ls
```
This would test if the computer with the IP-address `192.168.1.2` is reachable and execute the `do`-block (if there is a connection) once the command returned `1`. Afterwards it executes ls and executes the `do`-block if ls succeeds.

#### command (outdated)

It can execute any command and if this command returns `1`, the `do`-block will be executed.

Example:
```yaml
command: "ping -c 1 192.168.1.2"
command: ping -c 1 192.168.1.2
```
This would test if the computer with the IP-address `192.168.1.2` is reachable and execute the `do`-block (if there is an connection) once the command returned `1`.

This would test if the computer with the IP-adress `192.168.1.2` is reachable and execute the `do`-block (if there is a connection) once the command returned `1`.

#### docker_exec
`docker_exec` takes the node name specified in the setup part and executes the `command`-collection with `docker exec`

Example:
```yaml
docker_exec:
container: node1
commands:
- ping -c 1 192.168.1.2
- ls
```


#### do
The `do`-block will be executed when all conditions (see above) were processed.
Expand Down Expand Up @@ -139,7 +171,7 @@ The value `container` are the identifiers of the containers, specified in the se
The action will create a new network and add these containers to the network.
The sorted list of the containers is the key of the network (not the name).

The value of `internal` can be `True` or `False`.
The value of `internal` can be `True` or `False`.
It maps to the [internal flag](https://docs.docker.com/engine/reference/commandline/network_create/#network-internal-mode) of docker.

The `mode` can be any of `row`, `ring` or `cluster`.
Expand Down Expand Up @@ -195,7 +227,7 @@ restartContainer: ["Node3"]
This example starts `Node1`, stops `Node2` and restarts `Node3`.

#### delay, duplicate, corrupt and loss
These actions use the linux network emulator `netem`.
These actions use the linux network emulator `netem`.
Make sure that your container support this network emulator when using this command.
Have a look at the [documentation](http://man7.org/linux/man-pages/man8/tc-netem.8.html) for details.

Expand Down
24 changes: 22 additions & 2 deletions eventWorker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import threading
import time
from subprocess import call
from subprocess import call, check_output
from executeAction import executeAction

class eventWorker (threading.Thread):
Expand Down Expand Up @@ -28,9 +28,29 @@ def run(self):
print "execute command: " + self.eventObject["command"]
exitCode = call(self.eventObject["command"], shell=True)
if exitCode != 0:
print "commend exited with exit code " + str(exitCode) + ". Abort."
print "command exited with exit code " + str(exitCode) + ". Abort."
return exitCode

if "commands" in self.eventObject:
for command in self.eventObject["commands"]:
print "execute command: " + command
exitCode = call(command, shell=True)
if exitCode != 0:
print "command exited with exit code " + str(exitCode) + ". Abort."
return exitCode

# TODO support docker exec OPTIONS, see https://docs.docker.com/engine/reference/commandline/exec/
if "docker_exec" in self.eventObject:
docker_exec = self.eventObject["docker_exec"]
container_id = check_output("docker ps -aqf name=" + docker_exec["container"], shell=True).strip()
for command in docker_exec["commands"]:
full_command = "docker exec " + container_id + " " + command
print "run: "+ full_command
exitCode = call(full_command, shell=True)
if exitCode != 0:
print "command exited with exit code " + str(exitCode) + ". Abort."
return exitCode

if "do" in self.eventObject:
doObject = self.eventObject["do"]
for action in doObject:
Expand Down