Skip to content

Commit

Permalink
Refactor content registration into their own files
Browse files Browse the repository at this point in the history
  * Add: Create bootstrap folder in components for registration.
  * Fix: Deleted st2api model.
  • Loading branch information
Lakshmi Kannan committed Sep 9, 2014
1 parent fb9bfab commit a0e45ad
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 86 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import glob
import json

from oslo.config import cfg

from st2api.service import triggers as TriggerService
from st2common import log as logging
from st2common.exceptions.db import StackStormDBObjectNotFoundError
from st2common.models.api.action import RunnerTypeAPI
from st2common.models.api.reactor import RuleAPI, TriggerAPI
from st2common.persistence.action import (RunnerType, Action)
from st2common.persistence.reactor import Rule
from st2common.models.db.action import ActionDB
from st2common.persistence.action import RunnerType
from st2common.util.action_db import get_runnertype_by_name
from st2common.util import reference


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -172,71 +162,3 @@ def register_runner_types():
LOG.exception('Unable to register runner type %s.', runnertype['name'])

LOG.info('End : register default RunnerTypes.')


def register_actions():
actions = glob.glob(cfg.CONF.actions.modules_path + '/*.json')
for action in actions:
LOG.debug('Loading action from %s.', action)
with open(action, 'r') as fd:
try:
content = json.load(fd)
except ValueError:
LOG.exception('Unable to load action from %s.', action)
continue
try:
model = Action.get_by_name(str(content['name']))
except ValueError:
model = ActionDB()
model.name = content['name']
model.description = content['description']
model.enabled = content['enabled']
model.entry_point = content['entry_point']
model.parameters = content.get('parameters', {})
model.required_parameters = content.get('required_parameters', [])
try:
runner_type = get_runnertype_by_name(str(content['runner_type']))
model.runner_type = {'name': runner_type.name}
except StackStormDBObjectNotFoundError:
LOG.exception('Failed to register action %s as runner %s was not found',
model.name, str(content['runner_type']))
continue
try:
model = Action.add_or_update(model)
LOG.audit('Action created. Action %s from %s.', model, action)
except Exception:
LOG.exception('Failed to create action %s.', model.name)


def register_rules():
rules = glob.glob(cfg.CONF.rules.rules_path + '/*.json')
for rule in rules:
LOG.debug('Loading rule from %s.', rule)
with open(rule, 'r') as fd:
try:
content = json.load(fd)
except ValueError:
LOG.exception('Unable to load rule from %s.', rule)
continue
rule_api = RuleAPI(**content)
trigger_api = TriggerAPI(**rule_api.trigger)

rule_db = RuleAPI.to_model(rule_api)
trigger_db = TriggerService.create_trigger_db(trigger_api)

try:
rule_db.id = Rule.get_by_name(rule_api.name).id
except ValueError:
LOG.info('Rule %s not found. Creating new one.', rule)

rule_db.trigger = reference.get_ref_from_model(trigger_db)

try:
rule_db = Rule.add_or_update(rule_db)
LOG.audit('Rule updated. Rule %s from %s.', rule_db, rule)
except Exception:
LOG.exception('Failed to create rule %s.', rule_api.name)


def init_model():
pass
Empty file.
Empty file.
46 changes: 46 additions & 0 deletions st2actions/st2actions/bootstrap/registrar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import glob
import json

from oslo.config import cfg

from st2common import log as logging
from st2common.exceptions.db import StackStormDBObjectNotFoundError
from st2common.persistence.action import Action
from st2common.models.db.action import ActionDB
from st2common.util.action_db import get_runnertype_by_name

LOG = logging.getLogger(__name__)


def register_actions():
actions = glob.glob(cfg.CONF.actions.modules_path + '/*.json')
for action in actions:
LOG.debug('Loading action from %s.', action)
with open(action, 'r') as fd:
try:
content = json.load(fd)
except ValueError:
LOG.exception('Unable to load action from %s.', action)
continue
try:
model = Action.get_by_name(str(content['name']))
except ValueError:
model = ActionDB()
model.name = content['name']
model.description = content['description']
model.enabled = content['enabled']
model.entry_point = content['entry_point']
model.parameters = content.get('parameters', {})
model.required_parameters = content.get('required_parameters', [])
try:
runner_type = get_runnertype_by_name(str(content['runner_type']))
model.runner_type = {'name': runner_type.name}
except StackStormDBObjectNotFoundError:
LOG.exception('Failed to register action %s as runner %s was not found',
model.name, str(content['runner_type']))
continue
try:
model = Action.add_or_update(model)
LOG.audit('Action created. Action %s from %s.', model, action)
except Exception:
LOG.exception('Failed to create action %s.', model.name)
Empty file added st2actions/tests/__init__.py
Empty file.
10 changes: 6 additions & 4 deletions st2api/st2api/cmd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from st2common import log as logging
from st2common.models.db import db_setup
from st2common.models.db import db_teardown
import st2actions.bootstrap.registrar as actions_registrar
import st2actionrunner.bootstrap.registrar as runner_registrar
import st2reactor.bootstrap.registrar as rules_registrar
from st2api import config
from st2api import app
from st2api import model


eventlet.monkey_patch(
Expand Down Expand Up @@ -53,19 +55,19 @@ def __setup():
# 5. register runnertypes and actions. The order is important because actions require action
# types to be present in the system.
try:
model.register_runner_types()
runner_registrar.register_runner_types()
except Exception as e:
LOG.warning('Failed to register action types: %s', e, exc_info=True)
LOG.warning('Not registering stock actions.')
else:
try:
model.register_actions()
actions_registrar.register_actions()
except Exception as e:
LOG.warning('Failed to register actions: %s', e, exc_info=True)

# 6. register rules
try:
model.register_rules()
rules_registrar.register_rules()
except Exception as e:
LOG.warning('Failed to register rules: %s', e, exc_info=True)

Expand Down
5 changes: 2 additions & 3 deletions st2api/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import tests.config
from pecan.testing import load_test_app
from oslo.config import cfg

from st2api import model
import st2actionrunner.bootstrap.registrar as runner_registrar
from st2tests import DbTestCase


Expand All @@ -26,7 +25,7 @@ def setUpClass(cls):

# TODO(manas) : register action types here for now. RunnerType registration can be moved
# to posting to /runnertypes but that implies implementing POST.
model.register_runner_types()
runner_registrar.register_runner_types()

cls.app = load_test_app(config=cfg_dict)

Expand Down
Empty file.
42 changes: 42 additions & 0 deletions st2reactor/st2reactor/bootstrap/registrar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import glob
import json

from oslo.config import cfg

from st2api.service import triggers as TriggerService
from st2common import log as logging
from st2common.models.api.reactor import RuleAPI, TriggerAPI
from st2common.persistence.reactor import Rule
from st2common.util import reference

LOG = logging.getLogger(__name__)


def register_rules():
rules = glob.glob(cfg.CONF.rules.rules_path + '/*.json')
for rule in rules:
LOG.debug('Loading rule from %s.', rule)
with open(rule, 'r') as fd:
try:
content = json.load(fd)
except ValueError:
LOG.exception('Unable to load rule from %s.', rule)
continue
rule_api = RuleAPI(**content)
trigger_api = TriggerAPI(**rule_api.trigger)

rule_db = RuleAPI.to_model(rule_api)
trigger_db = TriggerService.create_trigger_db(trigger_api)

try:
rule_db.id = Rule.get_by_name(rule_api.name).id
except ValueError:
LOG.info('Rule %s not found. Creating new one.', rule)

rule_db.trigger = reference.get_ref_from_model(trigger_db)

try:
rule_db = Rule.add_or_update(rule_db)
LOG.audit('Rule updated. Rule %s from %s.', rule_db, rule)
except Exception:
LOG.exception('Failed to create rule %s.', rule_api.name)

0 comments on commit a0e45ad

Please sign in to comment.