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 = settings.getValue('OVS_VAR_DIR')
35 _OVS_ETC_DIR = settings.getValue('OVS_ETC_DIR')
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
47 _ovsdb_pidfile_path = os.path.join(settings.getValue('LOG_DIR'), "ovsdb_pidfile.pid")
48 _proc_name = 'ovs-vswitchd'
50 def __init__(self, timeout=30, vswitchd_args=None, expected_cmd=None):
51 """Initialise the wrapper with a specific start timeout and extra
54 :param timeout: Timeout to wait for application to start.
55 :param vswitchd_args: Command line parameters for vswitchd.
59 self._logger = logging.getLogger(__name__)
60 self._timeout = timeout
61 self._expect = expected_cmd
62 vswitchd_args = vswitchd_args or []
63 self._cmd = ['sudo', '-E', _OVS_VSWITCHD_BIN] + vswitchd_args
68 """ Start ``ovsdb-server`` and ``ovs-vswitchd`` instance.
71 :raises: pexpect.EOF, pexpect.TIMEOUT
75 self._start_ovsdb() # this has to be started first
78 super(VSwitchd, self).start()
80 except (pexpect.EOF, pexpect.TIMEOUT) as exc:
81 logging.error("Exception during VSwitch start.")
85 def kill(self, signal='-15', sleep=2):
86 """Kill ``ovs-vswitchd`` instance if it is alive.
90 self._logger.info('Killing ovs-vswitchd...')
94 super(VSwitchd, self).kill(signal, sleep)
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 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',
132 '--pidfile=' + self._ovsdb_pidfile_path, '--overwrite-pidfile'],
134 'Starting ovsdb-server...')
136 def _kill_ovsdb(self):
137 """Kill ``ovsdb-server`` instance.
141 with open(self._ovsdb_pidfile_path, "r") as pidfile:
142 ovsdb_pid = pidfile.read().strip()
144 self._logger.info("Killing ovsdb with pid: " + ovsdb_pid)
147 tasks.run_task(['sudo', 'kill', '-15', str(ovsdb_pid)],
148 self._logger, 'Killing ovsdb-server...')
151 def get_db_sock_path():
152 """Method returns location of db.sock file
154 :returns: path to db.sock file.
156 return os.path.join(_OVS_VAR_DIR, 'db.sock')