-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NAS-130513 / 24.10 / Add app events (#14173)
* Add basic service to get docker events * Before pulling images validate docker state * Send docker events for compose based projects * Add util to get app name from project name * Add basics for docker events * Add implementation of app.events.process * Setup docker events after service starts * Top event is redundant * Do not automatically send crud app events * Make sure events are sent on app crud and upgrade/rollback * Fix events being sent when converting from normal app to custom app * Fix converting app job progress bits * No need for redundant option * Remove select as it was for dev purposes * Event lock is not required * Add docker_read role for docker events
- Loading branch information
Showing
10 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from middlewared.service import Service | ||
|
||
from .ix_apps.utils import get_app_name_from_project_name | ||
|
||
|
||
PROCESSING_APP_EVENT = set() | ||
|
||
|
||
class AppEvents(Service): | ||
|
||
class Config: | ||
namespace = 'app.events' | ||
private = True | ||
|
||
async def process(self, app_name, container_event): | ||
if app := await self.middleware.call('app.query', [['id', '=', app_name]]): | ||
self.middleware.send_event( | ||
'app.query', 'CHANGED', id=app_name, fields=app[0], | ||
) | ||
|
||
|
||
async def app_event(middleware, event_type, args): | ||
app_name = get_app_name_from_project_name(args['id']) | ||
if app_name in PROCESSING_APP_EVENT: | ||
return | ||
|
||
PROCESSING_APP_EVENT.add(app_name) | ||
|
||
try: | ||
await middleware.call('app.events.process', app_name, args['fields']) | ||
except Exception as e: | ||
middleware.logger.warning('Unhandled exception: %s', e) | ||
finally: | ||
PROCESSING_APP_EVENT.remove(app_name) | ||
|
||
|
||
async def setup(middleware): | ||
middleware.event_subscribe('docker.events', app_event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
from catalog_reader.library import RE_VERSION # noqa | ||
from middlewared.plugins.apps.schema_utils import CONTEXT_KEY_NAME # noqa | ||
from middlewared.plugins.apps.utils import IX_APPS_MOUNT_PATH, PROJECT_PREFIX, run # noqa | ||
|
||
|
||
def get_app_name_from_project_name(project_name: str) -> str: | ||
return project_name[len(PROJECT_PREFIX):] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from middlewared.plugins.apps.ix_apps.docker.utils import get_docker_client, PROJECT_KEY | ||
from middlewared.service import Service | ||
|
||
|
||
class DockerEventService(Service): | ||
|
||
class Config: | ||
namespace = 'docker.events' | ||
private = True | ||
|
||
def setup(self): | ||
if not self.middleware.call_sync('docker.state.validate', False): | ||
return | ||
|
||
try: | ||
self.process() | ||
except Exception: | ||
if not self.middleware.call('service.started', 'docker'): | ||
# This is okay and can happen when docker is stopped | ||
return | ||
raise | ||
|
||
def process(self): | ||
with get_docker_client() as docker_client: | ||
self.process_internal(docker_client) | ||
|
||
def process_internal(self, client): | ||
for container_event in client.events( | ||
decode=True, filters={ | ||
'type': ['container'], | ||
'event': [ | ||
'create', 'destroy', 'detach', 'die', 'health_status', 'kill', 'unpause', | ||
'oom', 'pause', 'rename', 'resize', 'restart', 'start', 'stop', 'update', | ||
] | ||
} | ||
): | ||
if not isinstance(container_event, dict): | ||
continue | ||
|
||
if project := container_event.get('Actor', {}).get('Attributes', {}).get(PROJECT_KEY): | ||
self.middleware.send_event('docker.events', 'ADDED', id=project, fields=container_event) | ||
|
||
|
||
async def setup(middleware): | ||
middleware.event_register('docker.events', 'Docker container events', roles=['DOCKER_READ']) | ||
# We are going to check in setup docker events if setting up events is relevant or not | ||
middleware.create_task(middleware.call('docker.events.setup')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters