1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
2 | # contributor license agreements. See the NOTICE file distributed with |
||
3 | # this work for additional information regarding copyright ownership. |
||
4 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
||
5 | # (the "License"); you may not use this file except in compliance with |
||
6 | # the License. You may obtain a copy of the License at |
||
7 | # |
||
8 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
9 | # |
||
10 | # Unless required by applicable law or agreed to in writing, software |
||
11 | # distributed under the License is distributed on an "AS IS" BASIS, |
||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
13 | # See the License for the specific language governing permissions and |
||
14 | # limitations under the License. |
||
15 | from __future__ import absolute_import |
||
16 | import os |
||
17 | |||
18 | import st2common.content.utils as content_utils |
||
19 | from st2common import log as logging |
||
20 | from st2common.exceptions.db import StackStormDBObjectNotFoundError |
||
21 | from st2common.models.api.action import RunnerTypeAPI |
||
22 | from st2common.persistence.runner import RunnerType |
||
23 | from st2common.content.loader import RunnersLoader, MetaLoader |
||
24 | from st2common.constants.runners import MANIFEST_FILE_NAME |
||
25 | from st2common.util.action_db import get_runnertype_by_name |
||
26 | import six |
||
27 | |||
28 | __all__ = [ |
||
29 | 'register_runner_types', |
||
30 | ] |
||
31 | |||
32 | |||
33 | LOG = logging.getLogger(__name__) |
||
34 | |||
35 | |||
36 | def register_runners(runner_dirs=None, experimental=False, fail_on_failure=True): |
||
37 | """ Register runners |
||
38 | """ |
||
39 | LOG.debug('Start : register runners') |
||
40 | runner_count = 0 |
||
41 | runner_loader = RunnersLoader() |
||
42 | |||
43 | if not runner_dirs: |
||
44 | runner_dirs = content_utils.get_runners_base_paths() |
||
45 | |||
46 | runners = runner_loader.get_runners(runner_dirs) |
||
47 | |||
48 | for runner, path in six.iteritems(runners): |
||
49 | LOG.debug('Runner "%s"' % (runner)) |
||
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
Loading history...
|
|||
50 | runner_manifest = os.path.join(path, MANIFEST_FILE_NAME) |
||
51 | meta_loader = MetaLoader() |
||
52 | runner_types = meta_loader.load(runner_manifest) |
||
53 | for runner_type in runner_types: |
||
54 | runner_count += register_runner(runner_type, experimental) |
||
55 | |||
56 | LOG.debug('End : register runners') |
||
57 | |||
58 | return runner_count |
||
59 | |||
60 | |||
61 | def register_runner(runner_type, experimental): |
||
62 | # For backward compatibility reasons, we also register runners under the old names |
||
63 | runner_names = [runner_type['name']] + runner_type.get('aliases', []) |
||
64 | for runner_name in runner_names: |
||
65 | runner_type['name'] = runner_name |
||
66 | runner_experimental = runner_type.get('experimental', False) |
||
67 | |||
68 | if runner_experimental and not experimental: |
||
69 | LOG.debug('Skipping experimental runner "%s"' % (runner_name)) |
||
0 ignored issues
–
show
|
|||
70 | continue |
||
71 | |||
72 | # Remove additional, non db-model attributes |
||
73 | non_db_attributes = ['experimental', 'aliases'] |
||
74 | for attribute in non_db_attributes: |
||
75 | if attribute in runner_type: |
||
76 | del runner_type[attribute] |
||
77 | |||
78 | try: |
||
79 | runner_type_db = get_runnertype_by_name(runner_name) |
||
80 | update = True |
||
81 | except StackStormDBObjectNotFoundError: |
||
82 | runner_type_db = None |
||
83 | update = False |
||
84 | |||
85 | # Note: We don't want to overwrite "enabled" attribute which is already in the database |
||
86 | # (aka we don't want to re-enable runner which has been disabled by the user) |
||
87 | if runner_type_db and runner_type_db['enabled'] != runner_type['enabled']: |
||
88 | runner_type['enabled'] = runner_type_db['enabled'] |
||
89 | |||
90 | # If package is not provided, assume it's the same as module name for backward |
||
91 | # compatibility reasons |
||
92 | if not runner_type.get('runner_package', None): |
||
93 | runner_type['runner_package'] = runner_type['runner_module'] |
||
94 | |||
95 | runner_type_api = RunnerTypeAPI(**runner_type) |
||
96 | runner_type_api.validate() |
||
97 | runner_type_model = RunnerTypeAPI.to_model(runner_type_api) |
||
98 | |||
99 | if runner_type_db: |
||
100 | runner_type_model.id = runner_type_db.id |
||
101 | |||
102 | try: |
||
103 | |||
104 | runner_type_db = RunnerType.add_or_update(runner_type_model) |
||
105 | |||
106 | extra = {'runner_type_db': runner_type_db} |
||
107 | if update: |
||
108 | LOG.audit('RunnerType updated. RunnerType %s', runner_type_db, extra=extra) |
||
109 | else: |
||
110 | LOG.audit('RunnerType created. RunnerType %s', runner_type_db, extra=extra) |
||
111 | except Exception: |
||
112 | LOG.exception('Unable to register runner type %s.', runner_type['name']) |
||
113 | return 0 |
||
114 | return 1 |
||
115 | |||
116 | |||
117 | def register_runner_types(experimental=False): |
||
118 | raise NotImplementedError() |
||
119 |