Coverage for watcher/applier/sync.py: 54%

38 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-17 12:22 +0000

1# -*- encoding: utf-8 -*- 

2# Copyright (c) 2017 ZTE 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# 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 

13# implied. 

14# See the License for the specific language governing permissions and 

15# limitations under the License. 

16# 

17 

18from oslo_config import cfg 

19from oslo_log import log 

20 

21from watcher.applier.loading import default 

22from watcher.common import context 

23from watcher.common import exception 

24from watcher import objects 

25 

26 

27CONF = cfg.CONF 

28LOG = log.getLogger(__name__) 

29 

30 

31class Syncer(object): 

32 """Syncs all available actions with the Watcher DB""" 

33 

34 def sync(self): 

35 ctx = context.make_context() 

36 action_loader = default.DefaultActionLoader() 

37 available_actions = action_loader.list_available() 

38 for action_type in available_actions.keys(): 

39 load_action = action_loader.load(action_type) 

40 load_description = load_action.get_description() 

41 try: 

42 action_desc = objects.ActionDescription.get_by_type( 

43 ctx, action_type) 

44 if action_desc.description != load_description: 

45 action_desc.description = load_description 

46 action_desc.save() 

47 except exception.ActionDescriptionNotFound: 

48 obj_action_desc = objects.ActionDescription(ctx) 

49 obj_action_desc.action_type = action_type 

50 obj_action_desc.description = load_description 

51 obj_action_desc.create() 

52 self._cancel_ongoing_actionplans(ctx) 

53 

54 def _cancel_ongoing_actionplans(self, context): 

55 actions_plans = objects.ActionPlan.list( 

56 context, 

57 filters={'state': objects.action_plan.State.ONGOING, 

58 'hostname': CONF.host}, 

59 eager=True) 

60 for ap in actions_plans: 

61 ap.state = objects.action_plan.State.CANCELLED 

62 ap.save() 

63 filters = {'action_plan_uuid': ap.uuid, 

64 'state__in': (objects.action.State.PENDING, 

65 objects.action.State.ONGOING)} 

66 actions = objects.Action.list(context, filters=filters, eager=True) 

67 for a in actions: 

68 a.state = objects.action.State.CANCELLED 

69 a.save() 

70 LOG.info("Action Plan %(uuid)s along with appropriate Actions " 

71 "has been cancelled because it was in %(state)s state " 

72 "when Applier had been stopped on %(hostname)s host.", 

73 {'uuid': ap.uuid, 

74 'state': objects.action_plan.State.ONGOING, 

75 'hostname': ap.hostname})