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 | import os |
||
16 | |||
17 | import st2common.content.utils as content_utils |
||
18 | from st2common import log as logging |
||
19 | from st2common.exceptions.db import StackStormDBObjectNotFoundError |
||
20 | from st2common.models.api.action import RunnerTypeAPI |
||
21 | from st2common.persistence.runner import RunnerType |
||
22 | from st2common.content.loader import RunnersLoader, MetaLoader |
||
23 | from st2common.constants.runners import MANIFEST_FILE_NAME |
||
24 | from st2common.util.action_db import get_runnertype_by_name |
||
25 | |||
26 | __all__ = [ |
||
27 | 'register_runner_types', |
||
28 | ] |
||
29 | |||
30 | |||
31 | LOG = logging.getLogger(__name__) |
||
32 | |||
33 | |||
34 | def register_runners(runner_dirs=None, experimental=False, fail_on_failure=True): |
||
35 | """ Register runners |
||
36 | """ |
||
37 | LOG.debug('Start : register runners') |
||
38 | runner_count = 0 |
||
39 | runner_loader = RunnersLoader() |
||
40 | |||
41 | if not runner_dirs: |
||
42 | runner_dirs = content_utils.get_runners_base_paths() |
||
43 | |||
44 | runners = runner_loader.get_runners(runner_dirs) |
||
45 | |||
46 | for runner, path in runners.iteritems(): |
||
47 | LOG.debug('Runner "%s"' % (runner)) |
||
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
![]() |
|||
48 | runner_manifest = os.path.join(path, MANIFEST_FILE_NAME) |
||
49 | meta_loader = MetaLoader() |
||
50 | runner_types = meta_loader.load(runner_manifest) |
||
51 | for runner_type in runner_types: |
||
52 | runner_count += register_runner(runner_type, experimental) |
||
53 | |||
54 | LOG.debug('End : register runners') |
||
55 | |||
56 | return runner_count |
||
57 | |||
58 | |||
59 | def register_runner(runner_type, experimental): |
||
60 | # For backward compatibility reasons, we also register runners under the old names |
||
61 | runner_names = [runner_type['name']] + runner_type.get('aliases', []) |
||
62 | for runner_name in runner_names: |
||
63 | runner_type['name'] = runner_name |
||
64 | runner_experimental = runner_type.get('experimental', False) |
||
65 | |||
66 | if runner_experimental and not experimental: |
||
67 | LOG.debug('Skipping experimental runner "%s"' % (runner_name)) |
||
0 ignored issues
–
show
|
|||
68 | continue |
||
69 | |||
70 | # Remove additional, non db-model attributes |
||
71 | non_db_attributes = ['experimental', 'aliases'] |
||
72 | for attribute in non_db_attributes: |
||
73 | if attribute in runner_type: |
||
74 | del runner_type[attribute] |
||
75 | |||
76 | try: |
||
77 | runner_type_db = get_runnertype_by_name(runner_name) |
||
78 | update = True |
||
79 | except StackStormDBObjectNotFoundError: |
||
80 | runner_type_db = None |
||
81 | update = False |
||
82 | |||
83 | # Note: We don't want to overwrite "enabled" attribute which is already in the database |
||
84 | # (aka we don't want to re-enable runner which has been disabled by the user) |
||
85 | if runner_type_db and runner_type_db['enabled'] != runner_type['enabled']: |
||
86 | runner_type['enabled'] = runner_type_db['enabled'] |
||
87 | |||
88 | runner_type_api = RunnerTypeAPI(**runner_type) |
||
89 | runner_type_api.validate() |
||
90 | runner_type_model = RunnerTypeAPI.to_model(runner_type_api) |
||
91 | |||
92 | if runner_type_db: |
||
93 | runner_type_model.id = runner_type_db.id |
||
94 | |||
95 | try: |
||
96 | |||
97 | runner_type_db = RunnerType.add_or_update(runner_type_model) |
||
98 | |||
99 | extra = {'runner_type_db': runner_type_db} |
||
100 | if update: |
||
101 | LOG.audit('RunnerType updated. RunnerType %s', runner_type_db, extra=extra) |
||
102 | else: |
||
103 | LOG.audit('RunnerType created. RunnerType %s', runner_type_db, extra=extra) |
||
104 | except Exception: |
||
105 | LOG.exception('Unable to register runner type %s.', runner_type['name']) |
||
106 | return 0 |
||
107 | return 1 |
||
108 | |||
109 | |||
110 | def register_runner_types(experimental=False): |
||
111 | raise NotImplementedError() |
||
112 |