Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / agent / linux / ip_monitor.py
1 # Copyright 2015 Red Hat, 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 oslo_log import log as logging
17 from oslo_utils import excutils
18
19 from neutron._i18n import _LE
20 from neutron.agent.linux import async_process
21 from neutron.agent.linux import ip_lib
22
23 LOG = logging.getLogger(__name__)
24
25
26 class IPMonitorEvent(object):
27     def __init__(self, line, added, interface, cidr):
28         self.line = line
29         self.added = added
30         self.interface = interface
31         self.cidr = cidr
32
33     def __str__(self):
34         return self.line
35
36     @classmethod
37     def from_text(cls, line):
38         route = line.split()
39
40         try:
41             first_word = route[0]
42         except IndexError:
43             with excutils.save_and_reraise_exception():
44                 LOG.error(_LE('Unable to parse route "%s"'), line)
45
46         added = (first_word != 'Deleted')
47         if not added:
48             route = route[1:]
49
50         try:
51             interface = ip_lib.remove_interface_suffix(route[1])
52             cidr = route[3]
53         except IndexError:
54             with excutils.save_and_reraise_exception():
55                 LOG.error(_LE('Unable to parse route "%s"'), line)
56
57         return cls(line, added, interface, cidr)
58
59
60 class IPMonitor(async_process.AsyncProcess):
61     """Wrapper over `ip monitor address`.
62
63     To monitor and react indefinitely:
64         m = IPMonitor(namespace='tmp', root_as_root=True)
65         m.start()
66         for iterable in m:
67             event = IPMonitorEvent.from_text(iterable)
68             print(event, event.added, event.interface, event.cidr)
69     """
70
71     def __init__(self,
72                  namespace=None,
73                  run_as_root=True,
74                  respawn_interval=None):
75         super(IPMonitor, self).__init__(['ip', '-o', 'monitor', 'address'],
76                                         run_as_root=run_as_root,
77                                         respawn_interval=respawn_interval,
78                                         namespace=namespace)
79
80     def __iter__(self):
81         return self.iter_stdout(block=True)
82
83     def start(self):
84         super(IPMonitor, self).start(block=True)
85
86     def stop(self):
87         super(IPMonitor, self).stop(block=True)