Skip to content

addon

Server side of the Applications addon.

This module contains the server side of the Applications addon. It is responsible for managing settings and initial setup of addon.

Attributes backward compatibility

Current and previous versions of applications addon did use AYON attributes to define applications and tools for a project and task.

This system was replaced with a new system using settings. This change is not 100% backwards compatible, we need to make sure that older versions of the addon don't break initialization.

Older versions of the addon used settings of other versions, but the settings structure did change which can cause that combination of old and new Applications addon on server can cause crashes.

First version introduction settings does support both settings and attributes so the handling of older versions is part of the addon, but following versions have to find some clever way how to avoid the issues.

Version stored under 'ATTRIBUTES_VERSION_MILESTONE' should be last released version that used only old attribute system.

ApplicationsAddon

Bases: BaseServerAddon

Source code in server/addon.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
class ApplicationsAddon(BaseServerAddon):
    settings_model = ApplicationsAddonSettings
    # TODO remove this attribute when attributes support is removed
    has_attributes = True

    async def get_simple_actions(
        self,
        project_name: str | None = None,
        variant: str = "production",
    ) -> list["SimpleActionManifest"]:
        return await get_action_manifests(
            self,
            project_name=project_name,
            variant=variant,
        )

    async def get_dynamic_actions(
        self,
        context: ActionContext,
        variant: str = "production",
    ) -> list["DynamicActionManifest"]:
        return await get_dynamic_action_manifests(
            self,
            context=context,
            variant=variant,
        )

    async def execute_action(
        self,
        executor: "ActionExecutor",
    ) -> "ExecuteResponseModel":
        """Execute an action provided by the addon"""
        context = executor.context
        project_name = context.project_name
        entity_id = context.entity_ids[0]

        if executor.identifier == DEBUG_TERMINAL_ID:
            args = [
                "addon", "applications", "launch-debug-terminal",
                "--project", project_name,
                "--task-id", entity_id,
            ]
            return await executor.get_launcher_action_response(
                args=args
            )

        app_name = entity_id_arg = command = None
        skip_last_workfile = None
        if executor.identifier.startswith(IDENTIFIER_PREFIX):
            app_name = executor.identifier.removeprefix(IDENTIFIER_PREFIX)
            command = "launch-by-id"
            entity_id_arg = "--task-id"
            config = await self.get_action_config(
                executor.identifier,
                executor.context,
                executor.user,
                executor.variant,
            )
            skip_last_workfile = config.get("skip_last_workfile")

        elif executor.identifier.startswith(IDENTIFIER_WORKFILE_PREFIX):
            app_name = executor.identifier.removeprefix(
                IDENTIFIER_WORKFILE_PREFIX
            )
            command = "launch-by-workfile-id"
            entity_id_arg = "--workfile-id"

        if not app_name:
            return await executor.get_simple_response(
                message="Failed to launch application."
                " Unknown action identifier.",
                success=False,
            )

        args = [
            "addon", "applications", command,
            "--app", app_name,
            "--project", project_name,
            entity_id_arg, entity_id,
        ]
        if skip_last_workfile is not None:
            args.extend([
                "--use-last-workfile", str(int(not skip_last_workfile))
            ])
        # 'get_launcher_response' is available since AYON 1.8.3
        if hasattr(executor, "get_launcher_response"):
            return await executor.get_launcher_response(args=args)
        # Backwards compatibility
        return await executor.get_launcher_action_response(args=args)

    async def get_default_settings(self):
        return self.get_settings_model()(**DEFAULT_VALUES)

    async def pre_setup(self):
        """Make sure older version of addon use the new way of attributes."""

        instance = AddonLibrary.getinstance()
        app_defs = instance.data.get(self.name)
        old_addon = app_defs.versions.get("0.1.0")
        if old_addon is not None:
            # Override 'create_applications_attribute' for older versions
            #   - avoid infinite server restart loop
            old_addon.create_applications_attribute = (
                self.create_applications_attribute
            )

        # Update older versions of applications addon to use new
        #   '_update_enums'
        # - new function skips newer addon versions without 'has_attributes'
        version_objs, _invalid_versions = parse_versions(app_defs.versions)
        for addon_version, version_obj in version_objs:
            # Last release with only old attribute system
            if version_obj < ATTRIBUTES_VERSION_MILESTONE:
                addon = app_defs.versions[addon_version]
                addon._update_enums = self._update_enums

    async def create_action_config_hash(
        self,
        identifier: str,
        context: ActionContext,
        user: UserEntity,
        variant: str,
    ) -> str:
        """Create a hash for action config store"""
        if not identifier.startswith(IDENTIFIER_PREFIX):
            return await super().create_action_config_hash(
                identifier, context, user, variant
            )

        # Change identifier to only app name and one task id
        identifier = identifier.removeprefix(IDENTIFIER_PREFIX)
        hash_content = [
            user.name,
            identifier,
            context.project_name,
            context.entity_ids[0],
        ]
        logger.trace(f"Creating config hash from {hash_content}")
        return hash_data(hash_content)

    async def set_action_config(
        self,
        identifier: str,
        context: ActionContext,
        user: UserEntity,
        variant: str,
        config: dict[str, Any],
    ) -> None:
        if not identifier.startswith(IDENTIFIER_PREFIX):
            await super().set_action_config(
                identifier, context, user, variant, config
            )
            return

        if not context.entity_ids:
            return

        # Unset 'skip_last_workfile' if it is set to 'False'
        if config.get("skip_last_workfile") is False:
            config.pop("skip_last_workfile")

        identifier = identifier.removeprefix(IDENTIFIER_PREFIX)
        for entity_id in context.entity_ids:
            config_hash = hash_data([
                user.name,
                identifier,
                context.project_name,
                entity_id,
            ])
            await set_action_config(
                config_hash,
                config,
                addon_name=self.name,
                addon_version=self.version,
                identifier=identifier,
                project_name=context.project_name,
                user_name=user.name,
            )

    async def convert_settings_overrides(
        self,
        source_version: str,
        overrides: dict[str, Any],
    ) -> dict[str, Any]:
        overrides = await super().convert_settings_overrides(
            source_version, overrides
        )
        # Since 1.0.0 the project applications and tools are
        #   using settings instead of attributes.
        # Disable automatically project applications and tools
        #   when converting settings of version < 1.0.0 so we don't break
        #   productions on update
        if parse_version(source_version) < (1, 0, 0):
            prj_apps = overrides.setdefault("project_applications", {})
            prj_apps["enabled"] = False
            prj_tools = overrides.setdefault("project_tools", {})
            prj_tools["enabled"] = False
        return overrides

    # --------------------------------------
    # Backwards compatibility for attributes
    # --------------------------------------
    def _sort_versions(self, addon_versions, reverse=False):
        version_objs, invalid_versions = parse_versions(addon_versions)

        valid_versions = [
            addon_version
            for addon_version, version_obj in (
                sorted(version_objs, key=lambda x: x[1])
            )
        ]
        sorted_versions = list(sorted(invalid_versions)) + valid_versions
        if reverse:
            sorted_versions = reversed(sorted_versions)
        for addon_version in sorted_versions:
            yield addon_version

    def _merge_groups(self, output, new_groups):
        groups_by_name = {
            o_group["name"]: o_group
            for o_group in output
        }
        extend_groups = []
        for new_group in new_groups:
            group_name = new_group["name"]
            if group_name not in groups_by_name:
                extend_groups.append(new_group)
                continue
            existing_group = groups_by_name[group_name]
            existing_variants = existing_group["variants"]
            existing_variants_by_name = {
                variant["name"]: variant
                for variant in existing_variants
            }
            for new_variant in new_group["variants"]:
                if new_variant["name"] not in existing_variants_by_name:
                    existing_variants.append(new_variant)

        output.extend(extend_groups)

    def _get_enum_items_from_groups(self, groups):
        label_by_name = {}
        for group in groups:
            group_name = group["name"]
            group_label = group.get(
                "label", LABELS_BY_GROUP_NAME.get(group_name)
            ) or group_name
            for variant in group["variants"]:
                variant_name = variant["name"]
                if not variant_name:
                    continue
                variant_label = variant["label"] or variant_name
                full_name = f"{group_name}/{variant_name}"
                full_label = f"{group_label} {variant_label}"
                label_by_name[full_name] = full_label

        return [
            {"value": full_name, "label": label_by_name[full_name]}
            for full_name in sorted(label_by_name)
        ]

    def _addon_has_attributes(self, addon, addon_version):
        version_obj = parse_version(addon_version)
        if version_obj is None or version_obj < ATTRIBUTES_VERSION_MILESTONE:
            return True

        return getattr(addon, "has_attributes", False)

    async def _update_enums(self):
        """Updates applications and tools enums based on the addon settings.
        This method is called when the addon is started (after we are sure
        that the 'applications' and 'tools' attributes exist) and when
        the addon settings are updated (using on_settings_updated method).
        """

        instance = AddonLibrary.getinstance()
        app_defs = instance.data.get(self.name)
        all_applications = []
        all_tools = []
        for addon_version in self._sort_versions(
            app_defs.versions.keys(), reverse=True
        ):
            addon = app_defs.versions[addon_version]
            if not self._addon_has_attributes(addon, addon_version):
                continue

            for variant in ("production", "staging"):
                settings_model = await addon.get_studio_settings(variant)
                studio_settings = settings_model.dict()
                application_settings = studio_settings["applications"]
                app_groups = application_settings.pop("additional_apps")
                for group_name, value in application_settings.items():
                    value["name"] = group_name
                    app_groups.append(value)
                self._merge_groups(all_applications, app_groups)
                self._merge_groups(all_tools, studio_settings["tool_groups"])

        apps_attrib_name = "applications"
        tools_attrib_name = "tools"

        apps_enum = self._get_enum_items_from_groups(all_applications)
        tools_enum = self._get_enum_items_from_groups(all_tools)

        apps_attribute_data = {
            "type": "list_of_strings",
            "title": "Applications",
            "enum": apps_enum,
        }
        tools_attribute_data = {
            "type": "list_of_strings",
            "title": "Tools",
            "enum": tools_enum,
        }

        apps_scope = ["project"]
        tools_scope = ["project", "folder", "task"]

        apps_matches = False
        tools_matches = False

        async for row in Postgres.iterate(
            "SELECT name, position, scope, data from public.attributes"
        ):
            if row["name"] == apps_attrib_name:
                # Check if scope is matching ftrack addon requirements
                if (
                    set(row["scope"]) == set(apps_scope)
                    and row["data"].get("enum") == apps_enum
                ):
                    apps_matches = True

            elif row["name"] == tools_attrib_name:
                if (
                    set(row["scope"]) == set(tools_scope)
                    and row["data"].get("enum") == tools_enum
                ):
                    tools_matches = True

        if apps_matches and tools_matches:
            return

        if not apps_matches:
            await Postgres.execute(
                """
                UPDATE attributes SET
                    scope = $1,
                    data = $2
                WHERE
                    name = $3
                """,
                apps_scope,
                apps_attribute_data,
                apps_attrib_name,
            )

        if not tools_matches:
            await Postgres.execute(
                """
                UPDATE attributes SET
                    scope = $1,
                    data = $2
                WHERE
                    name = $3
                """,
                tools_scope,
                tools_attribute_data,
                tools_attrib_name,
            )

        # Reset attributes cache on server
        await attribute_library.load()

create_action_config_hash(identifier, context, user, variant) async

Create a hash for action config store

Source code in server/addon.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
async def create_action_config_hash(
    self,
    identifier: str,
    context: ActionContext,
    user: UserEntity,
    variant: str,
) -> str:
    """Create a hash for action config store"""
    if not identifier.startswith(IDENTIFIER_PREFIX):
        return await super().create_action_config_hash(
            identifier, context, user, variant
        )

    # Change identifier to only app name and one task id
    identifier = identifier.removeprefix(IDENTIFIER_PREFIX)
    hash_content = [
        user.name,
        identifier,
        context.project_name,
        context.entity_ids[0],
    ]
    logger.trace(f"Creating config hash from {hash_content}")
    return hash_data(hash_content)

execute_action(executor) async

Execute an action provided by the addon

Source code in server/addon.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
async def execute_action(
    self,
    executor: "ActionExecutor",
) -> "ExecuteResponseModel":
    """Execute an action provided by the addon"""
    context = executor.context
    project_name = context.project_name
    entity_id = context.entity_ids[0]

    if executor.identifier == DEBUG_TERMINAL_ID:
        args = [
            "addon", "applications", "launch-debug-terminal",
            "--project", project_name,
            "--task-id", entity_id,
        ]
        return await executor.get_launcher_action_response(
            args=args
        )

    app_name = entity_id_arg = command = None
    skip_last_workfile = None
    if executor.identifier.startswith(IDENTIFIER_PREFIX):
        app_name = executor.identifier.removeprefix(IDENTIFIER_PREFIX)
        command = "launch-by-id"
        entity_id_arg = "--task-id"
        config = await self.get_action_config(
            executor.identifier,
            executor.context,
            executor.user,
            executor.variant,
        )
        skip_last_workfile = config.get("skip_last_workfile")

    elif executor.identifier.startswith(IDENTIFIER_WORKFILE_PREFIX):
        app_name = executor.identifier.removeprefix(
            IDENTIFIER_WORKFILE_PREFIX
        )
        command = "launch-by-workfile-id"
        entity_id_arg = "--workfile-id"

    if not app_name:
        return await executor.get_simple_response(
            message="Failed to launch application."
            " Unknown action identifier.",
            success=False,
        )

    args = [
        "addon", "applications", command,
        "--app", app_name,
        "--project", project_name,
        entity_id_arg, entity_id,
    ]
    if skip_last_workfile is not None:
        args.extend([
            "--use-last-workfile", str(int(not skip_last_workfile))
        ])
    # 'get_launcher_response' is available since AYON 1.8.3
    if hasattr(executor, "get_launcher_response"):
        return await executor.get_launcher_response(args=args)
    # Backwards compatibility
    return await executor.get_launcher_action_response(args=args)

pre_setup() async

Make sure older version of addon use the new way of attributes.

Source code in server/addon.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
async def pre_setup(self):
    """Make sure older version of addon use the new way of attributes."""

    instance = AddonLibrary.getinstance()
    app_defs = instance.data.get(self.name)
    old_addon = app_defs.versions.get("0.1.0")
    if old_addon is not None:
        # Override 'create_applications_attribute' for older versions
        #   - avoid infinite server restart loop
        old_addon.create_applications_attribute = (
            self.create_applications_attribute
        )

    # Update older versions of applications addon to use new
    #   '_update_enums'
    # - new function skips newer addon versions without 'has_attributes'
    version_objs, _invalid_versions = parse_versions(app_defs.versions)
    for addon_version, version_obj in version_objs:
        # Last release with only old attribute system
        if version_obj < ATTRIBUTES_VERSION_MILESTONE:
            addon = app_defs.versions[addon_version]
            addon._update_enums = self._update_enums