summaryrefslogtreecommitdiff
path: root/neutron/debug/commands.py
blob: 464966d253a6e7f11c38a1376fd4f3805cd6205f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright 2012,  Nachi Ueno,  NTT MCL,  Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License,  Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing,  software
#    distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from cliff import lister
from neutronclient.common import utils
from neutronclient.neutron import v2_0 as client
from neutronclient.neutron.v2_0 import port

from neutron.openstack.common.gettextutils import _LI
from neutron.openstack.common import log as logging


class ProbeCommand(client.NeutronCommand):
    log = logging.getLogger(__name__ + '.ProbeCommand')

    def get_debug_agent(self):
        return self.app.debug_agent

    def run(self, parsed_args):
        self.log.debug('run(%s)', parsed_args)
        self.log.info(_('Unimplemented commands'))


class CreateProbe(ProbeCommand):
    """Create probe port and interface, then plug it in."""

    log = logging.getLogger(__name__ + '.CreateProbe')

    def get_parser(self, prog_name):
        parser = super(CreateProbe, self).get_parser(prog_name)
        parser.add_argument(
            'id', metavar='network_id',
            help=_('ID of network to probe'))
        parser.add_argument(
            '--device-owner',
            default='network', choices=['network', 'compute'],
            help=_('Owner type of the device: network/compute'))
        return parser

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        debug_agent = self.get_debug_agent()
        probe_port = debug_agent.create_probe(parsed_args.id,
                                              parsed_args.device_owner)
        self.log.info(_('Probe created : %s '), probe_port.id)


class DeleteProbe(ProbeCommand):
    """Delete probe - delete port then uplug."""

    log = logging.getLogger(__name__ + '.DeleteProbe')

    def get_parser(self, prog_name):
        parser = super(DeleteProbe, self).get_parser(prog_name)
        parser.add_argument(
            'id', metavar='port_id',
            help=_('ID of probe port to delete'))
        return parser

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        debug_agent = self.get_debug_agent()
        debug_agent.delete_probe(parsed_args.id)
        self.log.info(_('Probe %s deleted'), parsed_args.id)


class ListProbe(client.NeutronCommand, lister.Lister):
    """List probes."""

    log = logging.getLogger(__name__ + '.ListProbe')
    _formatters = {'fixed_ips': port._format_fixed_ips, }

    def get_debug_agent(self):
        return self.app.debug_agent

    def get_data(self, parsed_args):

        debug_agent = self.get_debug_agent()
        info = debug_agent.list_probes()
        columns = len(info) > 0 and sorted(info[0].keys()) or []
        return (columns, (utils.get_item_properties(
            s, columns, formatters=self._formatters, )
            for s in info), )


class ClearProbe(ProbeCommand):
    """Clear All probes."""

    log = logging.getLogger(__name__ + '.ClearProbe')

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        debug_agent = self.get_debug_agent()
        cleared_probes_count = debug_agent.clear_probes()
        self.log.info(_LI('%d probe(s) deleted') % cleared_probes_count)


class ExecProbe(ProbeCommand):
    """Exec commands on the namespace of the probe."""

    log = logging.getLogger(__name__ + '.ExecProbe')

    def get_parser(self, prog_name):
        parser = super(ExecProbe, self).get_parser(prog_name)
        parser.add_argument(
            'id', metavar='port_id',
            help=_('ID of probe port to execute command'))
        parser.add_argument(
            'command', metavar='command',
            nargs='?',
            default=None,
            help=_('Command to execute'))
        return parser

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        debug_agent = self.get_debug_agent()
        result = debug_agent.exec_command(parsed_args.id, parsed_args.command)
        self.app.stdout.write(result + '\n')


class PingAll(ProbeCommand):
    """Ping all fixed_ip."""

    log = logging.getLogger(__name__ + '.ExecProbe')

    def get_parser(self, prog_name):
        parser = super(PingAll, self).get_parser(prog_name)
        parser.add_argument(
            '--timeout', metavar='<timeout>',
            default=10,
            help=_('Ping timeout'))
        parser.add_argument(
            '--id', metavar='network_id',
            default=None,
            help=_('ID of network'))
        return parser

    def run(self, parsed_args):
        self.log.debug('run(%s)' % parsed_args)
        debug_agent = self.get_debug_agent()
        result = debug_agent.ping_all(parsed_args.id,
                                      timeout=parsed_args.timeout)
        self.app.stdout.write(result + '\n')