Skip to content

Instantly share code, notes, and snippets.

@hankei6km
Last active February 26, 2018 03:25
Show Gist options
  • Save hankei6km/a671ed2d9406116b3d539a7ad8aae7f3 to your computer and use it in GitHub Desktop.
Save hankei6km/a671ed2d9406116b3d539a7ad8aae7f3 to your computer and use it in GitHub Desktop.
Docker のイベントを監視し、何らかのアクションを実行する(Python版)

Docker のイベントを監視し、何らかのアクションを実行する(Python版)

「Docker のイベントを監視し、何らかのアクションを実行する(ワンライナー版)」を部分的に Python + aiodocker でも試してみた.

試し方

  • Python3.5 以上の環境に aiodocker をインストール.
  • sam_local_aio_listener.py をローカルのファイルとして保存.
  • $ sudo python sam_local_aio_listener.py のように実行する.

これで、$ sam local start-api からコンテナが開始される毎にコンテナ内で ls -l /var/task が実行されるようになる. ただし、ワンライナー版と同様なエラーになることも多い.

License

MIT License

Copyright (c) 2018 hankei6km

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# -*- coding: utf-8 -*-
#
# Copyright (c) 2018 hankei6km
# Licensed under the MIT License. See README.md in
# https://gist.github.com/hankei6km/a671ed2d9406116b3d539a7ad8aae7f3
import asyncio
from json import dumps
from aiodocker.docker import Docker
async def event_listener(docker, container_id):
data = await docker._query(
'containers/{id}/exec'.format(id=container_id),
method='POST',
headers={'content-type': 'application/json'},
data=dumps({
'AttachStdout': True,
'Cmd': ['ls', '-l', '/var/task']
})
)
exec_id = (await data.json())['Id']
data = await docker._query(
'exec/{id}/start'.format(id=exec_id),
method='POST',
headers={'content-type': 'application/json'},
data=dumps({
'Tty': True
})
)
return await data.text()
async def async_loop(loop, docker):
loop.create_task(docker.events.run(
filters=dumps(
{
'type': ['container'],
'event': ['start'],
'image': ['lambci/lambda']
}
)
))
s = docker.events.subscribe(create_task=False)
while True:
event = await s.get()
container_id = event['Actor']['ID']
tl = loop.create_task(event_listener(docker, container_id))
tl.add_done_callback(lambda f: print(f.result()))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
docker = Docker()
loop.run_until_complete(async_loop(loop, docker))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment