Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / debug / commands.py
1 # Copyright 2012,  Nachi Ueno,  NTT MCL,  Inc.
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License,  Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    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,  WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 from cliff import lister
17 from neutronclient.common import utils
18 from neutronclient.neutron import v2_0 as client
19 from neutronclient.neutron.v2_0 import port
20
21 from neutron._i18n import _, _LI
22
23
24 class ProbeCommand(client.NeutronCommand):
25
26     def get_debug_agent(self):
27         return self.app.debug_agent
28
29
30 class CreateProbe(ProbeCommand):
31     """Create probe port and interface, then plug it in."""
32
33     def get_parser(self, prog_name):
34         parser = super(CreateProbe, self).get_parser(prog_name)
35         parser.add_argument(
36             'id', metavar='network_id',
37             help=_('ID of network to probe'))
38         parser.add_argument(
39             '--device-owner',
40             default='network', choices=['network', 'compute'],
41             help=_('Owner type of the device: network/compute'))
42         return parser
43
44     def take_action(self, parsed_args):
45         debug_agent = self.get_debug_agent()
46         probe_port = debug_agent.create_probe(parsed_args.id,
47                                               parsed_args.device_owner)
48         self.log.info(_('Probe created : %s '), probe_port.id)
49
50
51 class DeleteProbe(ProbeCommand):
52     """Delete probe - delete port then uplug."""
53
54     def get_parser(self, prog_name):
55         parser = super(DeleteProbe, self).get_parser(prog_name)
56         parser.add_argument(
57             'id', metavar='port_id',
58             help=_('ID of probe port to delete'))
59         return parser
60
61     def take_action(self, parsed_args):
62         debug_agent = self.get_debug_agent()
63         debug_agent.delete_probe(parsed_args.id)
64         self.log.info(_('Probe %s deleted'), parsed_args.id)
65
66
67 class ListProbe(ProbeCommand, lister.Lister):
68     """List probes."""
69
70     _formatters = {'fixed_ips': port._format_fixed_ips, }
71
72     def take_action(self, parsed_args):
73         debug_agent = self.get_debug_agent()
74         info = debug_agent.list_probes()
75         columns = sorted(info[0].keys()) if info else []
76         return (columns, (utils.get_item_properties(
77             s, columns, formatters=self._formatters, )
78             for s in info), )
79
80
81 class ClearProbe(ProbeCommand):
82     """Clear All probes."""
83
84     def take_action(self, parsed_args):
85         debug_agent = self.get_debug_agent()
86         cleared_probes_count = debug_agent.clear_probes()
87         self.log.info(_LI('%d probe(s) deleted'), cleared_probes_count)
88
89
90 class ExecProbe(ProbeCommand):
91     """Exec commands on the namespace of the probe."""
92
93     def get_parser(self, prog_name):
94         parser = super(ExecProbe, self).get_parser(prog_name)
95         parser.add_argument(
96             'id', metavar='port_id',
97             help=_('ID of probe port to execute command'))
98         parser.add_argument(
99             'command', metavar='command',
100             nargs='?',
101             default=None,
102             help=_('Command to execute'))
103         return parser
104
105     def take_action(self, parsed_args):
106         debug_agent = self.get_debug_agent()
107         result = debug_agent.exec_command(parsed_args.id, parsed_args.command)
108         self.app.stdout.write(result + '\n')
109
110
111 class PingAll(ProbeCommand):
112     """Ping all fixed_ip."""
113
114     def get_parser(self, prog_name):
115         parser = super(PingAll, self).get_parser(prog_name)
116         parser.add_argument(
117             '--timeout', metavar='<timeout>',
118             default=10,
119             help=_('Ping timeout'))
120         parser.add_argument(
121             '--id', metavar='network_id',
122             default=None,
123             help=_('ID of network'))
124         return parser
125
126     def take_action(self, parsed_args):
127         debug_agent = self.get_debug_agent()
128         result = debug_agent.ping_all(parsed_args.id,
129                                       timeout=parsed_args.timeout)
130         self.app.stdout.write(result + '\n')