1 # Copyright 2015 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """Class wrapper for controlling an OVS instance.
17 Wraps a pair of ``ovs-vswitchd`` and ``ovsdb-server`` processes.
24 from conf import settings
25 from tools import tasks
27 _OVS_VSWITCHD_BIN = os.path.join(
28 settings.getValue('OVS_DIR'), 'vswitchd', 'ovs-vswitchd')
29 _OVSDB_TOOL_BIN = os.path.join(
30 settings.getValue('OVS_DIR'), 'ovsdb', 'ovsdb-tool')
31 _OVSDB_SERVER_BIN = os.path.join(
32 settings.getValue('OVS_DIR'), 'ovsdb', 'ovsdb-server')
34 _OVS_VAR_DIR = '/usr/local/var/run/openvswitch/'
35 _OVS_ETC_DIR = '/usr/local/etc/openvswitch/'
37 _LOG_FILE_VSWITCHD = os.path.join(
38 settings.getValue('LOG_DIR'), settings.getValue('LOG_FILE_VSWITCHD'))
40 class VSwitchd(tasks.Process):
41 """Class wrapper for controlling an OVS instance.
43 Wraps a pair of ``ovs-vswitchd`` and ``ovsdb-server`` processes.
46 _logfile = _LOG_FILE_VSWITCHD
49 _expect = r'EAL: Master l*core \d+ is ready'
50 _proc_name = 'ovs-vswitchd'
52 def __init__(self, timeout=30, vswitchd_args=None):
53 """Initialise the wrapper with a specific start timeout and extra
56 :param timeout: Timeout to wait for application to start.
57 :param vswitchd_args: Command line parameters for vswitchd.
61 self._logger = logging.getLogger(__name__)
62 self._timeout = timeout
63 vswitchd_args = vswitchd_args or []
65 self._cmd = ['sudo', '-E', _OVS_VSWITCHD_BIN] + vswitchd_args
70 """ Start ``ovsdb-server`` and ``ovs-vswitchd`` instance.
73 :raises: pexpect.EOF, pexpect.TIMEOUT
76 self._start_ovsdb() # this has to be started first
79 super(VSwitchd, self).start()
81 except (pexpect.EOF, pexpect.TIMEOUT) as exc:
86 """Kill ``ovs-vswitchd`` instance if it is alive.
90 self._logger.info('Killing ovs-vswitchd...')
94 super(VSwitchd, self).kill()
98 def _reset_ovsdb(self):
99 """Reset system for 'ovsdb'.
103 self._logger.info('Resetting system after last run...')
105 tasks.run_task(['sudo', 'rm', '-rf', _OVS_VAR_DIR], self._logger)
106 tasks.run_task(['sudo', 'mkdir', '-p', _OVS_VAR_DIR], self._logger)
107 tasks.run_task(['sudo', 'rm', '-rf', _OVS_ETC_DIR], self._logger)
108 tasks.run_task(['sudo', 'mkdir', '-p', _OVS_ETC_DIR], self._logger)
110 tasks.run_task(['sudo', 'rm', '-f',
111 os.path.join(_OVS_ETC_DIR, 'conf.db')],
114 self._logger.info('System reset after last run.')
116 def _start_ovsdb(self):
117 """Start ``ovsdb-server`` instance.
121 tasks.run_task(['sudo', _OVSDB_TOOL_BIN, 'create',
122 os.path.join(_OVS_ETC_DIR, 'conf.db'),
123 os.path.join(settings.getValue('OVS_DIR'), 'vswitchd',
124 'vswitch.ovsschema')],
126 'Creating ovsdb configuration database...')
128 self._ovsdb_pid = tasks.run_background_task(
129 ['sudo', _OVSDB_SERVER_BIN,
130 '--remote=punix:%s' % os.path.join(_OVS_VAR_DIR, 'db.sock'),
131 '--remote=db:Open_vSwitch,Open_vSwitch,manager_options'],
133 'Starting ovsdb-server...')
135 def _kill_ovsdb(self):
136 """Kill ``ovsdb-server`` instance.
141 tasks.run_task(['sudo', 'kill', '-15', str(self._ovsdb_pid)],
142 self._logger, 'Killing ovsdb-server...')