Debian Codegraph @

heatclient/osc/v1/stack_failures.py

raw symbols SCIP
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13 #
14
15 import collections
16
17 from osc_lib.command import command
18
19 from heatclient._i18n import _
20 from heatclient.common import format_utils
21 from heatclient import exc
22
23
24 class ListStackFailures(command.Command):
25 """Show information about failed stack resources."""
26
27 def take_action(self, parsed_args):
28 self.heat_client = self.app.client_manager.orchestration
29 failures = self._build_failed_resources(parsed_args.stack)
30 deployment_failures = self._build_software_deployments(failures)
31 self._print_failures(failures, deployment_failures,
32 long=parsed_args.long)
33
34 def get_parser(self, prog_name):
35 parser = super().get_parser(prog_name)
36 parser.add_argument(
37 'stack',
38 metavar='<stack>',
39 help=_('Stack to display (name or ID)'),
40 )
41 parser.add_argument(
42 '--long',
43 action='store_true',
44 default=False,
45 help=_('Show full deployment logs in output'),
46 )
47 return parser
48
49 def _build_failed_resources(self, stack):
50 """List information about FAILED stack resources.
51
52 Failed resources are added by recursing from the top level stack into
53 failed nested stack resources. A failed nested stack resource is only
54 added to the failed list if it contains no failed resources.
55 """
56 s = self.heat_client.stacks.get(stack)
57 if s.status != 'FAILED':
58 return []
59 resources = self.heat_client.resources.list(s.id)
60 failures = collections.OrderedDict()
61 self._append_failed_resources(failures, resources, [s.stack_name])
62 return failures
63
64 def _append_failed_resources(self, failures, resources, resource_path):
65 """Recursively build list of failed resources."""
66 appended = False
67 for rsc in resources:
68 if not rsc.resource_status.endswith('FAILED'):
69 continue
70 # determine if this resources is a nested stack
71 links_rel = list([link['rel'] for link in rsc.links])
72 is_nested = 'nested' in links_rel
73 nested_appended = False
74 next_resource_path = list(resource_path)
75 next_resource_path.append(rsc.resource_name)
76 if is_nested:
77 try:
78 nested_resources = self.heat_client.resources.list(
79 rsc.physical_resource_id)
80 nested_appended = self._append_failed_resources(
81 failures, nested_resources, next_resource_path)
82 except exc.HTTPNotFound:
83 # there is a failed resource but no stack
84 pass
85 if not nested_appended:
86 failures['.'.join(next_resource_path)] = rsc
87 appended = True
88 return appended
89
90 def _build_software_deployments(self, resources):
91 """Build a dict of software deployments from the supplied resources.
92
93 The key is the deployment ID.
94 """
95 df = {}
96 if not resources:
97 return df
98 for r in resources.values():
99 if r.resource_type not in ('OS::Heat::StructuredDeployment',
100 'OS::Heat::SoftwareDeployment'):
101 continue
102 try:
103 sd = self.heat_client.software_deployments.get(
104 deployment_id=r.physical_resource_id)
105 df[r.physical_resource_id] = sd
106 except exc.HTTPNotFound:
107 pass
108 return df
109
110 def _print_failures(self, failures, deployment_failures, long=False):
111 """Print failed resources.
112
113 If the resource is a deployment resource, look up the deployment and
114 print deploy_stdout and deploy_stderr.
115 """
116 out = self.app.stdout
117
118 if not failures:
119 return
120 for k, f in failures.items():
121 out.write('%s:\n' % k)
122 out.write(' resource_type: %s\n' % f.resource_type)
123 out.write(' physical_resource_id: %s\n' %
124 f.physical_resource_id)
125 out.write(' status: %s\n' % f.resource_status)
126 reason = format_utils.indent_and_truncate(
127 f.resource_status_reason,
128 spaces=4,
129 truncate=not long,
130 truncate_prefix='...\n')
131 out.write(' status_reason: |\n%s\n' % reason)
132 df = deployment_failures.get(f.physical_resource_id)
133 if df:
134 for output in ('deploy_stdout', 'deploy_stderr'):
135 format_utils.print_software_deployment_output(
136 data=df.output_values, name=output, long=long, out=out)