We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
st2-register-content should fail on failure (exit with non zero) by default
Right now it doesn't which means it easy to miss some (important) errors.

The change is not backward compatible, but it's a good one (imo). We can also add `--register-no-failure-on-failure` flag for users which desire old behavior.

See https://github.com/StackStorm/st2/issues/2827#issuecomment-235047780.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `st2common/st2common/content/bootstrap.py`
Content:
```
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 
16 import os
17 import sys
18 import logging
19 
20 from oslo_config import cfg
21 
22 import st2common
23 from st2common import config
24 from st2common.script_setup import setup as common_setup
25 from st2common.script_setup import teardown as common_teardown
26 from st2common.bootstrap.base import ResourceRegistrar
27 import st2common.bootstrap.triggersregistrar as triggers_registrar
28 import st2common.bootstrap.sensorsregistrar as sensors_registrar
29 import st2common.bootstrap.actionsregistrar as actions_registrar
30 import st2common.bootstrap.aliasesregistrar as aliases_registrar
31 import st2common.bootstrap.policiesregistrar as policies_registrar
32 import st2common.bootstrap.runnersregistrar as runners_registrar
33 import st2common.bootstrap.rulesregistrar as rules_registrar
34 import st2common.bootstrap.ruletypesregistrar as rule_types_registrar
35 import st2common.bootstrap.configsregistrar as configs_registrar
36 import st2common.content.utils as content_utils
37 from st2common.util.virtualenvs import setup_pack_virtualenv
38 
39 __all__ = [
40     'main'
41 ]
42 
43 LOG = logging.getLogger('st2common.content.bootstrap')
44 
45 cfg.CONF.register_cli_opt(cfg.BoolOpt('experimental', default=False))
46 
47 
48 def register_opts():
49     content_opts = [
50         cfg.BoolOpt('all', default=False, help='Register sensors, actions and rules.'),
51         cfg.BoolOpt('triggers', default=False, help='Register triggers.'),
52         cfg.BoolOpt('sensors', default=False, help='Register sensors.'),
53         cfg.BoolOpt('actions', default=False, help='Register actions.'),
54         cfg.BoolOpt('rules', default=False, help='Register rules.'),
55         cfg.BoolOpt('aliases', default=False, help='Register aliases.'),
56         cfg.BoolOpt('policies', default=False, help='Register policies.'),
57         cfg.BoolOpt('configs', default=False, help='Register and load pack configs.'),
58 
59         cfg.StrOpt('pack', default=None, help='Directory to the pack to register content from.'),
60         cfg.BoolOpt('setup-virtualenvs', default=False, help=('Setup Python virtual environments '
61                                                               'all the Python runner actions.')),
62 
63         cfg.BoolOpt('fail-on-failure', default=False, help=('Exit with non-zero of resource '
64                                                             'registration fails.'))
65     ]
66     try:
67         cfg.CONF.register_cli_opts(content_opts, group='register')
68     except:
69         sys.stderr.write('Failed registering opts.\n')
70 register_opts()
71 
72 
73 def setup_virtualenvs():
74     """
75     Setup Python virtual environments for all the registered or the provided pack.
76     """
77 
78     LOG.info('=========================================================')
79     LOG.info('########### Setting up virtual environments #############')
80     LOG.info('=========================================================')
81 
82     pack_dir = cfg.CONF.register.pack
83     fail_on_failure = cfg.CONF.register.fail_on_failure
84 
85     registrar = ResourceRegistrar()
86 
87     if pack_dir:
88         pack_name = os.path.basename(pack_dir)
89         pack_names = [pack_name]
90 
91         # 1. Register pack
92         registrar.register_pack(pack_name=pack_name, pack_dir=pack_dir)
93     else:
94         # 1. Register pack
95         base_dirs = content_utils.get_packs_base_paths()
96         registrar.register_packs(base_dirs=base_dirs)
97 
98         # 2. Retrieve available packs (aka packs which have been registered)
99         pack_names = registrar.get_registered_packs()
100 
101     setup_count = 0
102     for pack_name in pack_names:
103         try:
104             setup_pack_virtualenv(pack_name=pack_name, update=True, logger=LOG)
105         except Exception as e:
106             exc_info = not fail_on_failure
107             LOG.warning('Failed to setup virtualenv for pack "%s": %s', pack_name, e,
108                         exc_info=exc_info)
109 
110             if fail_on_failure:
111                 raise e
112         else:
113             setup_count += 1
114 
115     LOG.info('Setup virtualenv for %s pack(s).' % (setup_count))
116 
117 
118 def register_triggers():
119     pack_dir = cfg.CONF.register.pack
120     fail_on_failure = cfg.CONF.register.fail_on_failure
121 
122     registered_count = 0
123 
124     try:
125         LOG.info('=========================================================')
126         LOG.info('############## Registering triggers #####################')
127         LOG.info('=========================================================')
128         registered_count = triggers_registrar.register_triggers(pack_dir=pack_dir,
129                                                                 fail_on_failure=fail_on_failure)
130     except Exception as e:
131         exc_info = not fail_on_failure
132         LOG.warning('Failed to register sensors: %s', e, exc_info=exc_info)
133 
134         if fail_on_failure:
135             raise e
136 
137     LOG.info('Registered %s triggers.' % (registered_count))
138 
139 
140 def register_sensors():
141     pack_dir = cfg.CONF.register.pack
142     fail_on_failure = cfg.CONF.register.fail_on_failure
143 
144     registered_count = 0
145 
146     try:
147         LOG.info('=========================================================')
148         LOG.info('############## Registering sensors ######################')
149         LOG.info('=========================================================')
150         registered_count = sensors_registrar.register_sensors(pack_dir=pack_dir,
151                                                               fail_on_failure=fail_on_failure)
152     except Exception as e:
153         exc_info = not fail_on_failure
154         LOG.warning('Failed to register sensors: %s', e, exc_info=exc_info)
155 
156         if fail_on_failure:
157             raise e
158 
159     LOG.info('Registered %s sensors.' % (registered_count))
160 
161 
162 def register_actions():
163     # Register runnertypes and actions. The order is important because actions require action
164     # types to be present in the system.
165     pack_dir = cfg.CONF.register.pack
166     fail_on_failure = cfg.CONF.register.fail_on_failure
167 
168     registered_count = 0
169 
170     # 1. Register runner types
171     try:
172         LOG.info('=========================================================')
173         LOG.info('############## Registering actions ######################')
174         LOG.info('=========================================================')
175         runners_registrar.register_runner_types(experimental=cfg.CONF.experimental)
176     except Exception as e:
177         LOG.warning('Failed to register runner types: %s', e, exc_info=True)
178         LOG.warning('Not registering stock runners .')
179         return
180 
181     # 2. Register actions
182     try:
183         registered_count = actions_registrar.register_actions(pack_dir=pack_dir,
184                                                               fail_on_failure=fail_on_failure)
185     except Exception as e:
186         exc_info = not fail_on_failure
187         LOG.warning('Failed to register actions: %s', e, exc_info=exc_info)
188 
189         if fail_on_failure:
190             raise e
191 
192     LOG.info('Registered %s actions.' % (registered_count))
193 
194 
195 def register_rules():
196     # Register ruletypes and rules.
197     pack_dir = cfg.CONF.register.pack
198     fail_on_failure = cfg.CONF.register.fail_on_failure
199 
200     registered_count = 0
201 
202     try:
203         LOG.info('=========================================================')
204         LOG.info('############## Registering rules ########################')
205         LOG.info('=========================================================')
206         rule_types_registrar.register_rule_types()
207     except Exception as e:
208         LOG.warning('Failed to register rule types: %s', e, exc_info=True)
209         return
210 
211     try:
212         registered_count = rules_registrar.register_rules(pack_dir=pack_dir,
213                                                           fail_on_failure=fail_on_failure)
214     except Exception as e:
215         exc_info = not fail_on_failure
216         LOG.warning('Failed to register rules: %s', e, exc_info=exc_info)
217 
218         if fail_on_failure:
219             raise e
220 
221     LOG.info('Registered %s rules.', registered_count)
222 
223 
224 def register_aliases():
225     pack_dir = cfg.CONF.register.pack
226     fail_on_failure = cfg.CONF.register.fail_on_failure
227 
228     registered_count = 0
229 
230     try:
231         LOG.info('=========================================================')
232         LOG.info('############## Registering aliases ######################')
233         LOG.info('=========================================================')
234         registered_count = aliases_registrar.register_aliases(pack_dir=pack_dir,
235                                                               fail_on_failure=fail_on_failure)
236     except Exception as e:
237         if fail_on_failure:
238             raise e
239 
240         LOG.warning('Failed to register aliases.', exc_info=True)
241 
242     LOG.info('Registered %s aliases.', registered_count)
243 
244 
245 def register_policies():
246     # Register policy types and policies.
247     pack_dir = cfg.CONF.register.pack
248     fail_on_failure = cfg.CONF.register.fail_on_failure
249 
250     registered_type_count = 0
251 
252     try:
253         LOG.info('=========================================================')
254         LOG.info('############## Registering policy types #################')
255         LOG.info('=========================================================')
256         registered_type_count = policies_registrar.register_policy_types(st2common)
257     except Exception:
258         LOG.warning('Failed to register policy types.', exc_info=True)
259 
260     LOG.info('Registered %s policy types.', registered_type_count)
261 
262     registered_count = 0
263     try:
264         LOG.info('=========================================================')
265         LOG.info('############## Registering policies #####################')
266         LOG.info('=========================================================')
267         registered_count = policies_registrar.register_policies(pack_dir=pack_dir,
268                                                                 fail_on_failure=fail_on_failure)
269     except Exception as e:
270         exc_info = not fail_on_failure
271         LOG.warning('Failed to register policies: %s', e, exc_info=exc_info)
272 
273         if fail_on_failure:
274             raise e
275 
276     LOG.info('Registered %s policies.', registered_count)
277 
278 
279 def register_configs():
280     pack_dir = cfg.CONF.register.pack
281     fail_on_failure = cfg.CONF.register.fail_on_failure
282 
283     registered_count = 0
284 
285     try:
286         LOG.info('=========================================================')
287         LOG.info('############## Registering configs ######################')
288         LOG.info('=========================================================')
289         registered_count = configs_registrar.register_configs(pack_dir=pack_dir,
290                                                               fail_on_failure=fail_on_failure)
291     except Exception as e:
292         exc_info = not fail_on_failure
293         LOG.warning('Failed to register configs: %s', e, exc_info=exc_info)
294 
295         if fail_on_failure:
296             raise e
297 
298     LOG.info('Registered %s configs.' % (registered_count))
299 
300 
301 def register_content():
302     register_all = cfg.CONF.register.all
303 
304     if register_all:
305         register_triggers()
306         register_sensors()
307         register_actions()
308         register_rules()
309         register_aliases()
310         register_policies()
311         register_configs()
312 
313     if cfg.CONF.register.triggers and not register_all:
314         register_triggers()
315 
316     if cfg.CONF.register.sensors and not register_all:
317         register_sensors()
318 
319     if cfg.CONF.register.actions and not register_all:
320         register_actions()
321 
322     if cfg.CONF.register.rules and not register_all:
323         register_rules()
324 
325     if cfg.CONF.register.aliases and not register_all:
326         register_aliases()
327 
328     if cfg.CONF.register.policies and not register_all:
329         register_policies()
330 
331     if cfg.CONF.register.configs and not register_all:
332         register_configs()
333 
334     if cfg.CONF.register.setup_virtualenvs:
335         setup_virtualenvs()
336 
337 
338 def setup(argv):
339     common_setup(config=config, setup_db=True, register_mq_exchanges=True)
340 
341 
342 def teardown():
343     common_teardown()
344 
345 
346 def main(argv):
347     setup(argv)
348     register_content()
349     teardown()
350 
351 
352 # This script registers actions and rules from content-packs.
353 if __name__ == '__main__':
354     main(sys.argv[1:])
355
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/st2common/st2common/content/bootstrap.py b/st2common/st2common/content/bootstrap.py
--- a/st2common/st2common/content/bootstrap.py
+++ b/st2common/st2common/content/bootstrap.py
@@ -60,8 +60,12 @@
         cfg.BoolOpt('setup-virtualenvs', default=False, help=('Setup Python virtual environments '
                                                               'all the Python runner actions.')),
 
-        cfg.BoolOpt('fail-on-failure', default=False, help=('Exit with non-zero of resource '
-                                                            'registration fails.'))
+        cfg.BoolOpt('no-fail-on-failure', default=False,
+                    help=('Don\'t exit with non-zero if some resource registration fails.')),
+        # Note: Fail on failure is now a default behavior. This flag is only left here for backward
+        # compatibility reasons, but it's not actually used.
+        cfg.BoolOpt('fail-on-failure', default=False,
+                    help=('Exit with non-zero if some resource registration fails.'))
     ]
     try:
         cfg.CONF.register_cli_opts(content_opts, group='register')
@@ -80,7 +84,7 @@
     LOG.info('=========================================================')
 
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registrar = ResourceRegistrar()
 
@@ -117,7 +121,7 @@
 
 def register_triggers():
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registered_count = 0
 
@@ -139,7 +143,7 @@
 
 def register_sensors():
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registered_count = 0
 
@@ -163,7 +167,7 @@
     # Register runnertypes and actions. The order is important because actions require action
     # types to be present in the system.
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registered_count = 0
 
@@ -195,7 +199,7 @@
 def register_rules():
     # Register ruletypes and rules.
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registered_count = 0
 
@@ -223,7 +227,7 @@
 
 def register_aliases():
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registered_count = 0
 
@@ -245,7 +249,7 @@
 def register_policies():
     # Register policy types and policies.
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registered_type_count = 0
 
@@ -278,7 +282,7 @@
 
 def register_configs():
     pack_dir = cfg.CONF.register.pack
-    fail_on_failure = cfg.CONF.register.fail_on_failure
+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure
 
     registered_count = 0

{"golden_diff": "diff --git a/st2common/st2common/content/bootstrap.py b/st2common/st2common/content/bootstrap.py\n--- a/st2common/st2common/content/bootstrap.py\n+++ b/st2common/st2common/content/bootstrap.py\n@@ -60,8 +60,12 @@\n         cfg.BoolOpt('setup-virtualenvs', default=False, help=('Setup Python virtual environments '\n                                                               'all the Python runner actions.')),\n \n-        cfg.BoolOpt('fail-on-failure', default=False, help=('Exit with non-zero of resource '\n-                                                            'registration fails.'))\n+        cfg.BoolOpt('no-fail-on-failure', default=False,\n+                    help=('Don\\'t exit with non-zero if some resource registration fails.')),\n+        # Note: Fail on failure is now a default behavior. This flag is only left here for backward\n+        # compatibility reasons, but it's not actually used.\n+        cfg.BoolOpt('fail-on-failure', default=False,\n+                    help=('Exit with non-zero if some resource registration fails.'))\n     ]\n     try:\n         cfg.CONF.register_cli_opts(content_opts, group='register')\n@@ -80,7 +84,7 @@\n     LOG.info('=========================================================')\n \n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registrar = ResourceRegistrar()\n \n@@ -117,7 +121,7 @@\n \n def register_triggers():\n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registered_count = 0\n \n@@ -139,7 +143,7 @@\n \n def register_sensors():\n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registered_count = 0\n \n@@ -163,7 +167,7 @@\n     # Register runnertypes and actions. The order is important because actions require action\n     # types to be present in the system.\n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registered_count = 0\n \n@@ -195,7 +199,7 @@\n def register_rules():\n     # Register ruletypes and rules.\n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registered_count = 0\n \n@@ -223,7 +227,7 @@\n \n def register_aliases():\n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registered_count = 0\n \n@@ -245,7 +249,7 @@\n def register_policies():\n     # Register policy types and policies.\n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registered_type_count = 0\n \n@@ -278,7 +282,7 @@\n \n def register_configs():\n     pack_dir = cfg.CONF.register.pack\n-    fail_on_failure = cfg.CONF.register.fail_on_failure\n+    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n \n     registered_count = 0\n", "issue": "st2-register-content should fail on failure (exit with non zero) by default\nRight now it doesn't which means it easy to miss some (important) errors.\n\nThe change is not backward compatible, but it's a good one (imo). We can also add `--register-no-failure-on-failure` flag for users which desire old behavior.\n\nSee https://github.com/StackStorm/st2/issues/2827#issuecomment-235047780.\n\n", "before_files": [{"content": "# Licensed to the StackStorm, Inc ('StackStorm') under one or more\n# contributor license agreements.  See the NOTICE file distributed with\n# this work for additional information regarding copyright ownership.\n# The ASF licenses this file to You under the Apache License, Version 2.0\n# (the \"License\"); you may not use this file except in compliance with\n# the License.  You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport os\nimport sys\nimport logging\n\nfrom oslo_config import cfg\n\nimport st2common\nfrom st2common import config\nfrom st2common.script_setup import setup as common_setup\nfrom st2common.script_setup import teardown as common_teardown\nfrom st2common.bootstrap.base import ResourceRegistrar\nimport st2common.bootstrap.triggersregistrar as triggers_registrar\nimport st2common.bootstrap.sensorsregistrar as sensors_registrar\nimport st2common.bootstrap.actionsregistrar as actions_registrar\nimport st2common.bootstrap.aliasesregistrar as aliases_registrar\nimport st2common.bootstrap.policiesregistrar as policies_registrar\nimport st2common.bootstrap.runnersregistrar as runners_registrar\nimport st2common.bootstrap.rulesregistrar as rules_registrar\nimport st2common.bootstrap.ruletypesregistrar as rule_types_registrar\nimport st2common.bootstrap.configsregistrar as configs_registrar\nimport st2common.content.utils as content_utils\nfrom st2common.util.virtualenvs import setup_pack_virtualenv\n\n__all__ = [\n    'main'\n]\n\nLOG = logging.getLogger('st2common.content.bootstrap')\n\ncfg.CONF.register_cli_opt(cfg.BoolOpt('experimental', default=False))\n\n\ndef register_opts():\n    content_opts = [\n        cfg.BoolOpt('all', default=False, help='Register sensors, actions and rules.'),\n        cfg.BoolOpt('triggers', default=False, help='Register triggers.'),\n        cfg.BoolOpt('sensors', default=False, help='Register sensors.'),\n        cfg.BoolOpt('actions', default=False, help='Register actions.'),\n        cfg.BoolOpt('rules', default=False, help='Register rules.'),\n        cfg.BoolOpt('aliases', default=False, help='Register aliases.'),\n        cfg.BoolOpt('policies', default=False, help='Register policies.'),\n        cfg.BoolOpt('configs', default=False, help='Register and load pack configs.'),\n\n        cfg.StrOpt('pack', default=None, help='Directory to the pack to register content from.'),\n        cfg.BoolOpt('setup-virtualenvs', default=False, help=('Setup Python virtual environments '\n                                                              'all the Python runner actions.')),\n\n        cfg.BoolOpt('fail-on-failure', default=False, help=('Exit with non-zero of resource '\n                                                            'registration fails.'))\n    ]\n    try:\n        cfg.CONF.register_cli_opts(content_opts, group='register')\n    except:\n        sys.stderr.write('Failed registering opts.\\n')\nregister_opts()\n\n\ndef setup_virtualenvs():\n    \"\"\"\n    Setup Python virtual environments for all the registered or the provided pack.\n    \"\"\"\n\n    LOG.info('=========================================================')\n    LOG.info('########### Setting up virtual environments #############')\n    LOG.info('=========================================================')\n\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registrar = ResourceRegistrar()\n\n    if pack_dir:\n        pack_name = os.path.basename(pack_dir)\n        pack_names = [pack_name]\n\n        # 1. Register pack\n        registrar.register_pack(pack_name=pack_name, pack_dir=pack_dir)\n    else:\n        # 1. Register pack\n        base_dirs = content_utils.get_packs_base_paths()\n        registrar.register_packs(base_dirs=base_dirs)\n\n        # 2. Retrieve available packs (aka packs which have been registered)\n        pack_names = registrar.get_registered_packs()\n\n    setup_count = 0\n    for pack_name in pack_names:\n        try:\n            setup_pack_virtualenv(pack_name=pack_name, update=True, logger=LOG)\n        except Exception as e:\n            exc_info = not fail_on_failure\n            LOG.warning('Failed to setup virtualenv for pack \"%s\": %s', pack_name, e,\n                        exc_info=exc_info)\n\n            if fail_on_failure:\n                raise e\n        else:\n            setup_count += 1\n\n    LOG.info('Setup virtualenv for %s pack(s).' % (setup_count))\n\n\ndef register_triggers():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering triggers #####################')\n        LOG.info('=========================================================')\n        registered_count = triggers_registrar.register_triggers(pack_dir=pack_dir,\n                                                                fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register sensors: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s triggers.' % (registered_count))\n\n\ndef register_sensors():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering sensors ######################')\n        LOG.info('=========================================================')\n        registered_count = sensors_registrar.register_sensors(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register sensors: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s sensors.' % (registered_count))\n\n\ndef register_actions():\n    # Register runnertypes and actions. The order is important because actions require action\n    # types to be present in the system.\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registered_count = 0\n\n    # 1. Register runner types\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering actions ######################')\n        LOG.info('=========================================================')\n        runners_registrar.register_runner_types(experimental=cfg.CONF.experimental)\n    except Exception as e:\n        LOG.warning('Failed to register runner types: %s', e, exc_info=True)\n        LOG.warning('Not registering stock runners .')\n        return\n\n    # 2. Register actions\n    try:\n        registered_count = actions_registrar.register_actions(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register actions: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s actions.' % (registered_count))\n\n\ndef register_rules():\n    # Register ruletypes and rules.\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering rules ########################')\n        LOG.info('=========================================================')\n        rule_types_registrar.register_rule_types()\n    except Exception as e:\n        LOG.warning('Failed to register rule types: %s', e, exc_info=True)\n        return\n\n    try:\n        registered_count = rules_registrar.register_rules(pack_dir=pack_dir,\n                                                          fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register rules: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s rules.', registered_count)\n\n\ndef register_aliases():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering aliases ######################')\n        LOG.info('=========================================================')\n        registered_count = aliases_registrar.register_aliases(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        if fail_on_failure:\n            raise e\n\n        LOG.warning('Failed to register aliases.', exc_info=True)\n\n    LOG.info('Registered %s aliases.', registered_count)\n\n\ndef register_policies():\n    # Register policy types and policies.\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registered_type_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering policy types #################')\n        LOG.info('=========================================================')\n        registered_type_count = policies_registrar.register_policy_types(st2common)\n    except Exception:\n        LOG.warning('Failed to register policy types.', exc_info=True)\n\n    LOG.info('Registered %s policy types.', registered_type_count)\n\n    registered_count = 0\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering policies #####################')\n        LOG.info('=========================================================')\n        registered_count = policies_registrar.register_policies(pack_dir=pack_dir,\n                                                                fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register policies: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s policies.', registered_count)\n\n\ndef register_configs():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = cfg.CONF.register.fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering configs ######################')\n        LOG.info('=========================================================')\n        registered_count = configs_registrar.register_configs(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register configs: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s configs.' % (registered_count))\n\n\ndef register_content():\n    register_all = cfg.CONF.register.all\n\n    if register_all:\n        register_triggers()\n        register_sensors()\n        register_actions()\n        register_rules()\n        register_aliases()\n        register_policies()\n        register_configs()\n\n    if cfg.CONF.register.triggers and not register_all:\n        register_triggers()\n\n    if cfg.CONF.register.sensors and not register_all:\n        register_sensors()\n\n    if cfg.CONF.register.actions and not register_all:\n        register_actions()\n\n    if cfg.CONF.register.rules and not register_all:\n        register_rules()\n\n    if cfg.CONF.register.aliases and not register_all:\n        register_aliases()\n\n    if cfg.CONF.register.policies and not register_all:\n        register_policies()\n\n    if cfg.CONF.register.configs and not register_all:\n        register_configs()\n\n    if cfg.CONF.register.setup_virtualenvs:\n        setup_virtualenvs()\n\n\ndef setup(argv):\n    common_setup(config=config, setup_db=True, register_mq_exchanges=True)\n\n\ndef teardown():\n    common_teardown()\n\n\ndef main(argv):\n    setup(argv)\n    register_content()\n    teardown()\n\n\n# This script registers actions and rules from content-packs.\nif __name__ == '__main__':\n    main(sys.argv[1:])\n", "path": "st2common/st2common/content/bootstrap.py"}], "after_files": [{"content": "# Licensed to the StackStorm, Inc ('StackStorm') under one or more\n# contributor license agreements.  See the NOTICE file distributed with\n# this work for additional information regarding copyright ownership.\n# The ASF licenses this file to You under the Apache License, Version 2.0\n# (the \"License\"); you may not use this file except in compliance with\n# the License.  You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport os\nimport sys\nimport logging\n\nfrom oslo_config import cfg\n\nimport st2common\nfrom st2common import config\nfrom st2common.script_setup import setup as common_setup\nfrom st2common.script_setup import teardown as common_teardown\nfrom st2common.bootstrap.base import ResourceRegistrar\nimport st2common.bootstrap.triggersregistrar as triggers_registrar\nimport st2common.bootstrap.sensorsregistrar as sensors_registrar\nimport st2common.bootstrap.actionsregistrar as actions_registrar\nimport st2common.bootstrap.aliasesregistrar as aliases_registrar\nimport st2common.bootstrap.policiesregistrar as policies_registrar\nimport st2common.bootstrap.runnersregistrar as runners_registrar\nimport st2common.bootstrap.rulesregistrar as rules_registrar\nimport st2common.bootstrap.ruletypesregistrar as rule_types_registrar\nimport st2common.bootstrap.configsregistrar as configs_registrar\nimport st2common.content.utils as content_utils\nfrom st2common.util.virtualenvs import setup_pack_virtualenv\n\n__all__ = [\n    'main'\n]\n\nLOG = logging.getLogger('st2common.content.bootstrap')\n\ncfg.CONF.register_cli_opt(cfg.BoolOpt('experimental', default=False))\n\n\ndef register_opts():\n    content_opts = [\n        cfg.BoolOpt('all', default=False, help='Register sensors, actions and rules.'),\n        cfg.BoolOpt('triggers', default=False, help='Register triggers.'),\n        cfg.BoolOpt('sensors', default=False, help='Register sensors.'),\n        cfg.BoolOpt('actions', default=False, help='Register actions.'),\n        cfg.BoolOpt('rules', default=False, help='Register rules.'),\n        cfg.BoolOpt('aliases', default=False, help='Register aliases.'),\n        cfg.BoolOpt('policies', default=False, help='Register policies.'),\n        cfg.BoolOpt('configs', default=False, help='Register and load pack configs.'),\n\n        cfg.StrOpt('pack', default=None, help='Directory to the pack to register content from.'),\n        cfg.BoolOpt('setup-virtualenvs', default=False, help=('Setup Python virtual environments '\n                                                              'all the Python runner actions.')),\n\n        cfg.BoolOpt('no-fail-on-failure', default=False,\n                    help=('Don\\'t exit with non-zero if some resource registration fails.')),\n        # Note: Fail on failure is now a default behavior. This flag is only left here for backward\n        # compatibility reasons, but it's not actually used.\n        cfg.BoolOpt('fail-on-failure', default=False,\n                    help=('Exit with non-zero if some resource registration fails.'))\n    ]\n    try:\n        cfg.CONF.register_cli_opts(content_opts, group='register')\n    except:\n        sys.stderr.write('Failed registering opts.\\n')\nregister_opts()\n\n\ndef setup_virtualenvs():\n    \"\"\"\n    Setup Python virtual environments for all the registered or the provided pack.\n    \"\"\"\n\n    LOG.info('=========================================================')\n    LOG.info('########### Setting up virtual environments #############')\n    LOG.info('=========================================================')\n\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registrar = ResourceRegistrar()\n\n    if pack_dir:\n        pack_name = os.path.basename(pack_dir)\n        pack_names = [pack_name]\n\n        # 1. Register pack\n        registrar.register_pack(pack_name=pack_name, pack_dir=pack_dir)\n    else:\n        # 1. Register pack\n        base_dirs = content_utils.get_packs_base_paths()\n        registrar.register_packs(base_dirs=base_dirs)\n\n        # 2. Retrieve available packs (aka packs which have been registered)\n        pack_names = registrar.get_registered_packs()\n\n    setup_count = 0\n    for pack_name in pack_names:\n        try:\n            setup_pack_virtualenv(pack_name=pack_name, update=True, logger=LOG)\n        except Exception as e:\n            exc_info = not fail_on_failure\n            LOG.warning('Failed to setup virtualenv for pack \"%s\": %s', pack_name, e,\n                        exc_info=exc_info)\n\n            if fail_on_failure:\n                raise e\n        else:\n            setup_count += 1\n\n    LOG.info('Setup virtualenv for %s pack(s).' % (setup_count))\n\n\ndef register_triggers():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering triggers #####################')\n        LOG.info('=========================================================')\n        registered_count = triggers_registrar.register_triggers(pack_dir=pack_dir,\n                                                                fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register sensors: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s triggers.' % (registered_count))\n\n\ndef register_sensors():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering sensors ######################')\n        LOG.info('=========================================================')\n        registered_count = sensors_registrar.register_sensors(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register sensors: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s sensors.' % (registered_count))\n\n\ndef register_actions():\n    # Register runnertypes and actions. The order is important because actions require action\n    # types to be present in the system.\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registered_count = 0\n\n    # 1. Register runner types\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering actions ######################')\n        LOG.info('=========================================================')\n        runners_registrar.register_runner_types(experimental=cfg.CONF.experimental)\n    except Exception as e:\n        LOG.warning('Failed to register runner types: %s', e, exc_info=True)\n        LOG.warning('Not registering stock runners .')\n        return\n\n    # 2. Register actions\n    try:\n        registered_count = actions_registrar.register_actions(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register actions: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s actions.' % (registered_count))\n\n\ndef register_rules():\n    # Register ruletypes and rules.\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering rules ########################')\n        LOG.info('=========================================================')\n        rule_types_registrar.register_rule_types()\n    except Exception as e:\n        LOG.warning('Failed to register rule types: %s', e, exc_info=True)\n        return\n\n    try:\n        registered_count = rules_registrar.register_rules(pack_dir=pack_dir,\n                                                          fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register rules: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s rules.', registered_count)\n\n\ndef register_aliases():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering aliases ######################')\n        LOG.info('=========================================================')\n        registered_count = aliases_registrar.register_aliases(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        if fail_on_failure:\n            raise e\n\n        LOG.warning('Failed to register aliases.', exc_info=True)\n\n    LOG.info('Registered %s aliases.', registered_count)\n\n\ndef register_policies():\n    # Register policy types and policies.\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registered_type_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering policy types #################')\n        LOG.info('=========================================================')\n        registered_type_count = policies_registrar.register_policy_types(st2common)\n    except Exception:\n        LOG.warning('Failed to register policy types.', exc_info=True)\n\n    LOG.info('Registered %s policy types.', registered_type_count)\n\n    registered_count = 0\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering policies #####################')\n        LOG.info('=========================================================')\n        registered_count = policies_registrar.register_policies(pack_dir=pack_dir,\n                                                                fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register policies: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s policies.', registered_count)\n\n\ndef register_configs():\n    pack_dir = cfg.CONF.register.pack\n    fail_on_failure = not cfg.CONF.register.no_fail_on_failure\n\n    registered_count = 0\n\n    try:\n        LOG.info('=========================================================')\n        LOG.info('############## Registering configs ######################')\n        LOG.info('=========================================================')\n        registered_count = configs_registrar.register_configs(pack_dir=pack_dir,\n                                                              fail_on_failure=fail_on_failure)\n    except Exception as e:\n        exc_info = not fail_on_failure\n        LOG.warning('Failed to register configs: %s', e, exc_info=exc_info)\n\n        if fail_on_failure:\n            raise e\n\n    LOG.info('Registered %s configs.' % (registered_count))\n\n\ndef register_content():\n    register_all = cfg.CONF.register.all\n\n    if register_all:\n        register_triggers()\n        register_sensors()\n        register_actions()\n        register_rules()\n        register_aliases()\n        register_policies()\n        register_configs()\n\n    if cfg.CONF.register.triggers and not register_all:\n        register_triggers()\n\n    if cfg.CONF.register.sensors and not register_all:\n        register_sensors()\n\n    if cfg.CONF.register.actions and not register_all:\n        register_actions()\n\n    if cfg.CONF.register.rules and not register_all:\n        register_rules()\n\n    if cfg.CONF.register.aliases and not register_all:\n        register_aliases()\n\n    if cfg.CONF.register.policies and not register_all:\n        register_policies()\n\n    if cfg.CONF.register.configs and not register_all:\n        register_configs()\n\n    if cfg.CONF.register.setup_virtualenvs:\n        setup_virtualenvs()\n\n\ndef setup(argv):\n    common_setup(config=config, setup_db=True, register_mq_exchanges=True)\n\n\ndef teardown():\n    common_teardown()\n\n\ndef main(argv):\n    setup(argv)\n    register_content()\n    teardown()\n\n\n# This script registers actions and rules from content-packs.\nif __name__ == '__main__':\n    main(sys.argv[1:])\n", "path": "st2common/st2common/content/bootstrap.py"}]}