Commit Graph

58 Commits

Author SHA1 Message Date
Rodolfo Alonso 19eb12bd29 Revert "Implement "kill" method using os.kill()"
This reverts commit 4b21111eb1.

Reason for revert: This method is unstable and prone to timeouts

Change-Id: I6064d60e4d63b085046aace7683d766a79dd22da
2021-03-25 22:05:58 +00:00
Rodolfo Alonso Hernandez 4b21111eb1 Implement "kill" method using os.kill()
Implement the "kill" method (send a signal to a process) using the
Python native library "os".

In functional tests, "RootHelperProcess.kill" method should not fail if
the process does not exist.

Closes-Bug: #1843446
Closes-Bug: #1843418

Change-Id: Iee97a83779dd3e20eb3a223fb8557a94b8f15dc0
2021-03-22 08:58:20 +00:00
Rodolfo Alonso Hernandez ee00bddce7 Remove rootwrap execution (6)
Replace rootwrap execution with privsep context execution.
This series of patches will progressively replace any
rootwrap call.

This patch migrates the "kill_process" method to privsep and
removes the unneeded rootwrap filters.

Change-Id: I48461be8b08cbc21c8af371f551b944343ba37bf
Story: #2007686
Task: #41558
2021-03-05 10:03:22 +00:00
Rodolfo Alonso Hernandez c89c1f53db Remove rootwrap execution (1)
Replace rootwrap execution with privsep context execution.
This series of patches will progressively replace any
rootwrap call.

This patch replaces some "IpNetnsCommand" command execution
methods.

Change-Id: Ic5fdf221a2a2cd0951539b0e040d2a941feee287
Story: #2007686
Task: #41558
2021-02-06 16:22:43 +00:00
Rodolfo Alonso Hernandez a6dbf97242 Deprecate XenAPI support
The configuration options are now marked as deprecated for
removal in X release.

Any related code is deleted. Neutron does not support XenAPI,
same as Nova [1][2].

[1]https://review.opendev.org/#/c/749304/
[2]https://review.opendev.org/#/c/749309/

Change-Id: Ifdb2200a5dac3508fdf8907bdd1f4547dff35341
Story: #2007686
Task: #41269
2020-12-09 20:15:39 +00:00
Zuul 2fba36a88e Merge "Implement "find_child_pids" natively" 2020-11-20 02:27:57 +00:00
Rodolfo Alonso Hernandez b5eadca521 Implement "find_child_pids" natively
Use Python native libraries instead of shell calls.

Closes-Bug: #1900270

Change-Id: I3602cbe33183a8476eb332c85fcf399b211dda07
2020-11-14 17:17:52 +00:00
Rodolfo Alonso Hernandez ad0605f9c3 Add process name to AsyncProcess
Since [1], Neutron sets the name of some processes (Neutron agents).
The "ps" output is modified consequently according to the defined
string:
  "<process name> (<process command>)"

"AsyncProcess" class should use the process name to parse the "ps"
output correctly.

Closes-Bug: #1902678

[1]https://review.opendev.org/#/c/735125/

Change-Id: If33c49c0f3e1e6696f5d2aa4008b287dc3f76c61
2020-11-06 09:17:52 +00:00
Rodolfo Alonso Hernandez b4211c8c54 Implement "find_parent_pid" natively
Use Python native libraries instead of shell calls.

Closes-Bug: #1900268

Change-Id: Iaad9e2b3374b5c81f83c780d306685773af719ed
2020-10-19 10:20:15 +00:00
Brian Haley a2a2301675 Remove usage of six.PY2
Neutron is python 3 only so these can be removed.

Another step in removing all of six usage from neutron.

Change-Id: Ica0913e689bb5b472053661b30f951477d3ec960
2020-05-22 12:59:01 -04:00
Brian Haley 7594bb0627 Remove the dependency on the "mock" package
Now that we are python3 only, we should move to using the built
in version of mock that supports all of our testing needs and
remove the dependency on the "mock" package.

This patch moves all references to "import mock" to
"from unittest import mock". It also cleans up some new line
inconsistency.

Fixed an inconsistency in the OVSBridge.deferred() definition
as it needs to also have an *args argument.

Fixed an issue where an l3-agent test was mocking
functools.partial, causing a python3.8 failure.

Unit tests only, removing from tests/base.py affects
functional tests which need additional work.

Change-Id: I40e8a8410840c3774c72ae1a8054574445d66ece
2020-04-28 18:05:37 -04:00
Brian Haley 6842465260 Stop testing python 2
Since it's no longer supported past Train, lets stop
running the tests.

Updated docs and made some pep8 code tweaks as well.

Change-Id: I1c171ab906a3b4c66558163ad26947ebf710a276
2019-10-25 18:50:08 +00:00
Jens Harbott 3330bc01cb Fix race condition when getting cmdline
Even though we check for the existence of the process before, it may
still terminate before we get to read its command line, so catch the
error that can occur here.

Change-Id: I3e89aca8bedfd2912effe2490718223f7d03133e
Closes-Bug: 1844500
2019-09-18 21:32:33 -04:00
Boden R 03ec94ce5e use test tools from neutron-lib
This patch switches the code over to use neutron-lib's test tools module
where appropriate rather than using neutron's.
This includes removing the following functions/classes from neutron and
using them from lib instead:
- get_random_EUI
- get_random_ip_network
- reset_random_seed
- OpenFixture

Change-Id: I0fbfcc7919f1b17b6bb0026fa9b98f157168255e
2019-06-05 06:21:04 -06:00
Slawek Kaplonski dcc3d3c754 Add extra unit test for get_cmdline_from_pid function
In patch [1] get_cmdline_from_pid function was modified to be able
to parse process' cmdline files with arguments separated by space
instead of '\0' char.
This patch adds one extra UT and small refactor which was pointed
in comments to [1] but which I was not able to change there.

[1] https://review.openstack.org/#/c/647605/

Change-Id: Ibd91d0472a686eca79a1126154d9cdf4587c1a19
Related-Bug: #1820870
2019-03-28 14:45:40 +00:00
Slawek Kaplonski cf13b2f0cc Check if process' cmdline is "space separarated"
According to proc man page process arguments in /proc/{pid}/cmdline
should be separated with '\0' char and that char was used in
neutron.agent.linux.utils.get_cmdline_from_pid function.

Recently in fullstack tests it was noticed that sometimes it may
happend that those arguments are separated with space char and this
caused failed test because async_process.AsyncProcess() was not able
to check that process is really active.

This patch adds attempt to split cmdline arguments with space in case
when split with '\0' returns only 1 element.

Change-Id: I35d4c0e2cf56fc3ff15cf307aaf11a8ad8489e1f
Closes-Bug: #1820870
2019-03-25 21:54:52 +01:00
Boden R 68fd13af40 remove neutron.common.exceptions
Today the neutron common exceptions already live in neutron-lib and are
shimmed from neutron. This patch removes the neutron.common.exceptions
module and changes neutron's imports over to use their respective
neutron-lib exception module instead.

NeutronLibImpact

Change-Id: I9704f20eb21da85d2cf024d83338b3d94593671e
2019-02-01 14:35:00 -07:00
Doug Wiegley 8914f8247f Update neutron files for new over-indentation hacking rule (E117)
Change-Id: I594e2d1238f6ffa3c1039624e3b3ed6569485837
2019-01-29 15:36:20 -05:00
Claudiu Belu fee630efaa Fix neutron-openvswitch-agent Windows support
Currently, the neutron-openvswitch-agent does not start on Windows
due to Linux specific imports. This patch addresses this issue.

Also, we're wrapping the object returned by subprocess.Popen using
tpool.Proxy in order to prevent IO operations on the stream
handles from blocking other threads. Currently, the ovs db monitor
blocks the whole process.

Closes-Bug: #1775382

Co-Authored-By: Lucian Petrut <lpetrut@cloudbasesolutions.com>
Change-Id: I8bbc9d1f8332e5644a6071f599a7c6a66bef7928
2018-07-18 14:41:21 +03:00
Brian Haley d1efeeb433 Fix UnixDomainHttpProtocol class to support all eventlet versions
It was recently decided to uncap eventlet:
http://lists.openstack.org/pipermail/openstack-dev/2018-April/129096.html
So eventlet is now capped at 0.20 not by global requirements,
it is capped in upper-constraints, because currently not every
openstack project is able to work with a newer eventlet version,
mostly because of the caps in projects requirements.txt.
According to global-requirements, last allowed version of
eventlet is 0.22.1:
https://git.openstack.org/cgit/openstack/requirements/tree/global-requirements.txt

In an effort to support both eventlet<0.22 and eventlet>=0.22,
change the code to try and determine the correct number of
arguments to use in the call to initialize the parent class.

Change-Id: Ibe3dc8af6cf9f8bb4f8eababb7f4276e4db3f1f9
Closes-bug: #1777640
2018-07-10 15:20:11 -04:00
Jenkins 19a069d99f Merge "Pass parameters when create eventlet.wsgi server" 2017-04-17 05:35:54 +00:00
Jenkins 2d073a3dd4 Merge "Print useful error on rootwrap daemon failure" 2017-04-07 22:07:25 +00:00
Brian Haley 0476ad641d Print useful error on rootwrap daemon failure
If the rootwrap daemon fails to execute a command, it
generates a cryptic message:

Unserializable message: ('#ERROR', ValueError('I/O operation on closed file',))

We should at least log the command that we were trying
to run, which will help users figure out why it failed.

Change-Id: I2c94e5a226630432028351f8287868f4fe5d2fa1
Closes-bug: #1677742
2017-04-04 16:59:52 -04:00
Huan Xie bc23e29423 Use os-xenapi for neutron when XenServer as hypervisor
We have made os-xenapi repository to deal with XenServer Dom0
specific functions, this patch is to change neutron to use
os-xenapi when XenServer is hypervisor and move the building
RPM scripts into os-xenapi repo

Depends-On: I8a31c81d9475387fe4ed7030b70b26098e588771

Change-Id: Ia958c366189386b1b5abbadbb4d74950aaa23bb2
2017-03-30 18:33:37 +00:00
Duan Jiong 5fd6b4c768 Pass parameters when create eventlet.wsgi server
Otherwise, config option wsgi_log_format in neutron.conf
will not work

Closes-Bug: #1672615
Change-Id: I43a3b6d99f940b5276aabaccad15d1050023baa2
2017-03-17 14:14:25 +08:00
Daniel Alvarez 320b789401 Remove dependency on strings for kill_process
utils.kill_process() parsed the error string after the kill command
which can lead to internationalization issues. We shouldn't relay upon
different translations so this follow-up patch removes this dependency
by checking if the process is still running after the kill when a
ProcessExecutionError exception occurs.

Before, this was achieved by comparing against "No such process" string

Change-Id: I22bd63992d1029f99fea401f07167383f8ff7dd0
2017-03-15 04:28:01 +00:00
Anindita Das 13080f0237 Remove deprecated method get_interface_mac
Removing the deprecated method get_interface_mac and from
neutron/agent/linux/utils.py and the associated test in
neutron/tests/unit/agent/linux/test_utils.py. This is scheduled
for removal in Pike.
This was deprecated in I1695d7e46efe5245eb581bd40d5420250a3bad89.

Change-Id: I6b84563c2631a3e47826320f03fa1fdfe44cf2a9
2017-03-13 05:55:07 +00:00
Jenkins 86d47bad2d Merge "XenAPI: Support daemon mode for rootwrap" 2017-01-21 02:18:20 +00:00
Oleg Bondarev 7953e9886d Fix empty string check for python 3
It's '' in py2 and b'' in py3.
See bug for traceback.

Closes-Bug: #1657476
Change-Id: Ic2c32669bf238b702e13e81e15dd079d538a6abc
2017-01-20 10:03:20 +04:00
Jianghua Wang 8047da17db XenAPI: Support daemon mode for rootwrap
For Neutron's compute agent in a XenServer's compute node, the commands
actually need run in Dom0. Currently XenServer only supports rootwrap
for that purpose by invoking a script which invokes XenAPI to execute
commands in dom0. There are much performance overhead due to it requires
parsing on the script and the configuration file every time running
commands.

This change is to support daemon mode with which each agent service will
call XenAPI directly to execute commands in dom0. And it will keep the
single XenAPI session.

DocImpact: Need update the following configuration.

file: /etc/neutron/plugins/ml2/openvswitch_agent.ini
[agent]
root_helper_daemon = xenapi_root_helper
[xenapi]
connection_url = http://169.254.0.1
connection_username = root
connection_password = xenroot

Closes-Bug: #1585510
Change-Id: I684034359fe0571bc92dbcf342a9821553b1da35
2017-01-19 07:33:43 +00:00
Daniel Alvarez 1d38f30555 Kill processes when cleaning up namespaces
This patch will kill processes that are listening on any port/UNIX
socket within the namespace to be cleaned up. To kill them it will
issue a SIGTERM to them (or to their parents if they were forked) and,
if they don't die after a few seconds, a SIGKILL to them and all their
children.

This is intended for those cases when there's no specific cleanup and
serves as a fallback method.

Change-Id: I4195f633ef4a1788496d1293846f19eef89416aa
Partial-Bug: #1403455
2016-12-20 10:52:41 +00:00
John Schwarz 3c1bf8697b Don't depend on translated strings for error check
Currently, execute() may raise an exception that contains a *translated*
string that starts with 'Exit code: %(returncode)d...' if the returncode
of a process was not 0. find_child_pids() will then check if the
raised exception contains 'Exit code: 1' (to check if the returncode is
1), but in non-English locales this will fail as the 2 strings are not
encoded the same.

This patch adds a new ProcessExecutionError (which inherits from
RuntimeError, so as to not change all the code that currently depends on
execute() returning RuntimeError) which now accepts a returncode. This
can be changed explicitly without depending on the error message.

Later patches can move ProcessExecutionError to neutron-lib, if this is
needed - this patch intends to write the smallest piece of code that can
be backported.

Closes-Bug: #1638273
Change-Id: I85d3bec13e852918eb13e73c1367c70e1f4d34b1
2016-11-01 14:39:09 +02:00
Henry Gessau eb1efc7ace Account for Py2/Py3 differences in fcntl.ioctl return value
Closes-Bug: #1630439

Change-Id: Icc7bc9372d87dfd6cc15a2b472e38250479ac4ec
2016-10-06 14:59:18 -04:00
venkata anil 70ea188f5d New option for num_threads for state change server
Currently max number of client connections(i.e greenlets spawned at
a time) opened at any time by the WSGI server is set to 100 with
wsgi_default_pool_size[1].

This configuration may be fine for neutron api server. But with
wsgi_default_pool_size(=100) requests, state change server
is creating heavy cpu load on agent.
So this server(which run on agents) need lesser value i.e
can be configured to half the number of cpu on agent

We use "ha_keepalived_state_change_server_threads" config option
to configure number of threads in state change server instead of
wsgi_default_pool_size.

[1] https://review.openstack.org/#/c/278007/

DocImpact: Add new config option -
ha_keepalived_state_change_server_threads, to configure number
of threads in state change server.

Closes-Bug: #1581580
Change-Id: I822ea3844792a7731fd24419b7e90e5aef141993
2016-09-23 17:07:12 +00:00
Victor Stinner 6355173e4c Reuse to_utf8() and jsonutils.dump_as_bytes()
Reactor code:

* Reuse oslo_utils.encodeutils.to_utf8() instead of existing
  isinstance(text, six.text_type) test
* Replace jsonutils.dumps(obj).encode('utf-8') with
  jsonutils.dump_as_bytes(obj).
* Other minor bytes/Unicode changes

Change-Id: I03b8eff0fd70ab65ac66d6f3221e8ced0a56db17
2016-04-11 12:25:53 +02:00
Assaf Muller fd93e19f2a Change get_root_helper_child_pid to stop when it finds cmd
get_root_helper_child_pid recursively finds the child of pid,
until it can no longer find a child. However, the intention is
not to find the deepest child, but to strip away root helpers.
For example 'sudo neutron-rootwrap x' is supposed to find the
pid of x. However, in cases 'x' spawned quick lived children of
its own (For example: ip / brctl / ovs invocations),
get_root_helper_child_pid returned those pids if called in
the wrong time.

Change-Id: I582aa5c931c8bfe57f49df6899445698270bb33e
Closes-Bug: #1558819
2016-04-04 12:15:33 -04:00
Dariusz Smigiel (dasm) 25fdc2a9f9 Remove deprecated method from agent utils file
Commit I26b0a4d6105420a2c242b81a4cd58e0adef4cbec marked method
replace_file as redundant. Functionality was moved to
neutron.common.utils:replace_file

Related-Bug: #1504477
Change-Id: I77f907bee20bf921d4127502c1ce8156425e158a
2016-03-23 16:16:09 +00:00
lzklibj 3491cbc0c5 Unify assertEqual for empty usages
Update previous assertEqual(observed, *empty) usages to
assertEqual(*empty*, observed).

This patch aslo update hacking check for assertEqual with
empty types.

Change-Id: I981277618f92254a5beb9d3308a317d8c14e125c
2016-01-07 10:06:35 +00:00
Ihar Hrachyshka e30d8cead1 Make sure we return unicode strings for process output
Process output is supposed to be represented with lines, so we should
put Python strings in the queue (not bytes). Just in case, we do it only
for Python 3 environment.

To fix that, we reuse code from utils.execute() linux/windows
implementations.

This fixes the TestAsyncProcess.test_async_process_respawns functional
test for Python 3 environment.

Related-Bug: #1515118
Change-Id: I9efec2290003add44909aab33a0026372a580016
2015-11-20 16:49:51 +01:00
Cedric Brandily 4e4706e0f5 Ensure to decode bytes or fail
The commit 048316e981 introduces the
pattern:

  if isinstance(line, bytes):
    try:
      line = line.decode(encoding='utf-8')
    except UnicodeError:
      pass
  # concat line with a string

which is not working in PY3K if an UnicodeError is raised because line
is (silently) not decoded and concatened to a string.

This change ensures to return a text object or to raise an error.

Closes-Bug: #1503415
Blueprint: neutron-python3
Change-Id: I16b8013f33aa3efad65be8040d3210120e047bbd
2015-11-04 07:03:12 +01:00
Lajos Katona 599977e20b Fix locale problem in execute()
Change from new format string to old style formatting.

Change-Id: Ib39de7169416c2cc053d4aa909075c68cd2d7f0b
Closes-bug: #1449897
2015-08-31 12:08:09 +02:00
ajmiller ee374e7a5f Add optional file permission argument to replace_file()
The replace_file() utility function currently sets the mode of all files
it creates to 0o644.  This is not appropriate for all files.  This patch
adds an optional "file_mode" argument to the function.

Change-Id: I9744abde10b95fadef6e74c55332d041e5372071
Partial-Bug: 1488320
2015-08-28 08:13:25 -07:00
fumihiko kakuma 048316e981 Python 3: encode or decode i/o data of Popen.communicate()
In Python 3, input and output for Popen.communicate() is bytes type.
Therefore, encode input data and decode return data for Popen.communicate().

Change-Id: I70f009e3366f0eeda5790652ea14f3627b934664
Blueprint: neutron-python3
Closes-Bug: #1479159
2015-08-16 14:42:48 +09:00
Ihar Hrachyshka f53a43fd5e ensure_dir: move under neutron.common.utils
There is nothing Linux or agent specific in the function. I need to use
it outside agent code in one of depending patches, hence moving it into
better location while leaving the previous symbol in place, with
deprecation warning, for backwards compatibility.

Change-Id: I252356a72f3c742e57c1b6127275030f0994a221
2015-07-21 16:33:09 +02:00
Brian Haley 1c29fab7cb Change ensure_dir to not check directory exists first
I224be69168ede8a496a5f7d59b04b722f4de7192 added an EEXIST
check, so no need to check if the directory is already
there, just try and create it.

Change-Id: Iba51fc8263bf59326489319d0dd3f69af00a8eeb
2015-06-11 00:11:06 -04:00
Carl Baldwin 75c3a8dee4 Enhance utils.ensure_dir to be resilient to concurrent workers
In rare cases, concurrent workers may attempt to ensure a directory
exists.  One may successfully create the directory while the other
gets an oserror that it already exists.  This patch detects the
problem and returns successfully in both cases.

Change-Id: I224be69168ede8a496a5f7d59b04b722f4de7192
2015-06-03 16:56:23 +00:00
armando-migliaccio 31631e82bb Fix intermittent UT failures in test_utils
Change eba4c2941e introduced these tests. However they are not that useful as they
simply mimick the code, without really ensuring that the behavior is expected, so
they provide negative value ([1]), plus, they fail randomly.

This patch removes them in favor of a more useful functional check.

[1] http://googletesting.blogspot.com/2015/01/testing-on-toilet-change-detector-tests.html

Closes-bug: #1441347

Change-Id: I8a321995295deef7f6d30be303486be491e2771f
2015-04-07 16:40:47 -07:00
Cedric Brandily 80bea7a386 Allow metadata proxy running with nobody user/group
Currently metadata proxy cannot run with nobody user/group as metadata
proxy requires to connect to metadata_proxy_socket when queried.

This change allows to run metadata proxy with nobody user/group by
allowing to choose the metadata_proxy_socket mode with the new option
metadata_proxy_socket_mode (4 choices) in order to adapt socket
permissions to metadata proxy user/group.

This change refactors also where options are defined to enable
metadata_proxy_user/group options in the metadata agent.

In practice:
* if metadata_proxy_user is agent effective user or root, then:
  * metadata proxy is allowed to use rootwrap (unsecure)
  * set metadata_proxy_socket_mode = user (0o644)
* else if metadata_proxy_group is agent effective group, then:
  * metadata proxy is not allowed to use rootwrap (secure)
  * set metadata_proxy_socket_mode = group (0o664)
  * set metadata_proxy_log_watch = false
* else:
  * metadata proxy has lowest permissions (securest) but metadata proxy
    socket can be opened by everyone
  * set metadata_proxy_socket_mode = all (0o666)
  * set metadata_proxy_log_watch = false

An alternative is to set metadata_proxy_socket_mode = deduce, in such
case metadata agent uses previous rules to choose the correct mode.

DocImpact
Closes-Bug: #1427228
Change-Id: I235a0cc4f0cbd55ae4ec1570daf2ebbb6a72441d
2015-04-06 18:31:37 +02:00
Jenkins 3f45031d68 Merge "Allow metadata proxy to log with nobody user/group" 2015-04-02 11:39:27 +00:00
Cedric Brandily fbc2278414 Allow metadata proxy to log with nobody user/group
Currently metadata proxy cannot run with nobody user/group as
metadata proxy (as other services) uses WatchedFileHandler handler to
log to file which does not support permissions drop (the process must
be able to r/w after permissions drop to "watch" the file).

This change allows to enable/disable log watch in metadata proxies with
the new option metadata_proxy_log_watch. It should be disabled when
metadata_proxy_user/group is not allowed to read/write metadata proxy
log files. Option default value is deduced from metadata_proxy_user:

* True if metadata_proxy_user is agent effective user id/name,
* False otherwise.

When log watch is disabled and logrotate is enabled on metadata proxy
logging files, 'copytruncate' logrotate option must be used otherwise
metadata proxy logs will be lost after the first log rotation.

DocImpact
Change-Id: I40a7bd82a2c60d9198312fdb52e3010c60db3511
Partial-Bug: #1427228
2015-04-01 22:41:07 +02:00