From 3d68a51eac894f5df9296a45634a20a407feb7b4 Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Sun, 9 Oct 2016 07:29:40 -0700 Subject: [PATCH] neutron-lib: use replace_file from neutron lib Make use of the file utility replace_file from neutron-lib. The utility replace_file in neutron is marked as deprecated. Change-Id: I7e3ed10a22012be9511e43e4dc3bf73076b2954e --- neutron/agent/linux/dhcp.py | 11 +++--- neutron/agent/linux/dibbler.py | 6 ++-- neutron/agent/linux/keepalived.py | 4 +-- neutron/agent/linux/ra.py | 4 +-- neutron/cmd/pd_notify.py | 6 ++-- neutron/common/utils.py | 20 +++-------- neutron/tests/functional/common/test_utils.py | 36 ------------------- neutron/tests/unit/agent/l3/test_agent.py | 2 +- .../unit/agent/l3/test_dvr_local_router.py | 2 +- neutron/tests/unit/agent/linux/test_dhcp.py | 4 +-- 10 files changed, 24 insertions(+), 71 deletions(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index c63586272da..78dfed41d0d 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -23,6 +23,7 @@ import time import netaddr from neutron_lib import constants from neutron_lib import exceptions +from neutron_lib.utils import file as file_utils from oslo_config import cfg from oslo_log import log as logging import oslo_messaging @@ -272,7 +273,7 @@ class DhcpLocalProcess(DhcpBase): @interface_name.setter def interface_name(self, value): interface_file_path = self.get_conf_file_name('interface') - common_utils.replace_file(interface_file_path, value) + file_utils.replace_file(interface_file_path, value) @property def active(self): @@ -638,7 +639,7 @@ class Dnsmasq(DhcpLocalProcess): buf.write('%s %s %s * *\n' % (timestamp, port.mac_address, ip_address)) contents = buf.getvalue() - common_utils.replace_file(filename, contents) + file_utils.replace_file(filename, contents) LOG.debug('Done building initial lease file %s with contents:\n%s', filename, contents) return filename @@ -708,7 +709,7 @@ class Dnsmasq(DhcpLocalProcess): buf.write('%s,%s,%s\n' % (port.mac_address, name, ip_address)) - common_utils.replace_file(filename, buf.getvalue()) + file_utils.replace_file(filename, buf.getvalue()) LOG.debug('Done building host file %s', filename) return filename @@ -847,7 +848,7 @@ class Dnsmasq(DhcpLocalProcess): if alloc: buf.write('%s\t%s %s\n' % (alloc.ip_address, fqdn, hostname)) addn_hosts = self.get_conf_file_name('addn_hosts') - common_utils.replace_file(addn_hosts, buf.getvalue()) + file_utils.replace_file(addn_hosts, buf.getvalue()) return addn_hosts def _output_opts_file(self): @@ -856,7 +857,7 @@ class Dnsmasq(DhcpLocalProcess): options += self._generate_opts_per_port(subnet_index_map) name = self.get_conf_file_name('opts') - common_utils.replace_file(name, '\n'.join(options)) + file_utils.replace_file(name, '\n'.join(options)) return name def _generate_opts_per_subnet(self): diff --git a/neutron/agent/linux/dibbler.py b/neutron/agent/linux/dibbler.py index 14fc6b6acad..71f34999613 100644 --- a/neutron/agent/linux/dibbler.py +++ b/neutron/agent/linux/dibbler.py @@ -17,6 +17,7 @@ import os import shutil import jinja2 +from neutron_lib.utils import file as file_utils from oslo_config import cfg from oslo_log import log as logging import six @@ -26,7 +27,6 @@ from neutron.agent.linux import pd from neutron.agent.linux import pd_driver from neutron.agent.linux import utils from neutron.common import constants -from neutron.common import utils as common_utils LOG = logging.getLogger(__name__) @@ -84,7 +84,7 @@ class PDDibbler(pd_driver.PDDriverBase): buf.write('%s' % SCRIPT_TEMPLATE.render( prefix_path=self.prefix_path, l3_agent_pid=os.getpid())) - common_utils.replace_file(script_path, buf.getvalue()) + file_utils.replace_file(script_path, buf.getvalue()) os.chmod(script_path, 0o744) dibbler_conf = utils.get_conf_file_name(dcwa, 'client', 'conf', False) @@ -96,7 +96,7 @@ class PDDibbler(pd_driver.PDDriverBase): interface_name='"%s"' % ex_gw_ifname, bind_address='%s' % lla)) - common_utils.replace_file(dibbler_conf, buf.getvalue()) + file_utils.replace_file(dibbler_conf, buf.getvalue()) return dcwa def _spawn_dibbler(self, pmon, router_ns, dibbler_conf): diff --git a/neutron/agent/linux/keepalived.py b/neutron/agent/linux/keepalived.py index e00bf5f321c..0ed3e8d2791 100644 --- a/neutron/agent/linux/keepalived.py +++ b/neutron/agent/linux/keepalived.py @@ -18,6 +18,7 @@ import os import netaddr from neutron_lib import exceptions +from neutron_lib.utils import file as file_utils from oslo_config import cfg from oslo_log import log as logging from oslo_utils import fileutils @@ -25,7 +26,6 @@ from oslo_utils import fileutils from neutron._i18n import _, _LE from neutron.agent.linux import external_process from neutron.common import constants -from neutron.common import utils as common_utils VALID_STATES = ['MASTER', 'BACKUP'] VALID_AUTH_TYPES = ['AH', 'PASS'] @@ -369,7 +369,7 @@ class KeepalivedManager(object): def _output_config_file(self): config_str = self.config.get_config_str() config_path = self.get_full_config_file_path('keepalived.conf') - common_utils.replace_file(config_path, config_str) + file_utils.replace_file(config_path, config_str) return config_path diff --git a/neutron/agent/linux/ra.py b/neutron/agent/linux/ra.py index 4c8c745d27e..27a70e5c964 100644 --- a/neutron/agent/linux/ra.py +++ b/neutron/agent/linux/ra.py @@ -18,6 +18,7 @@ from itertools import chain as iter_chain import jinja2 import netaddr from neutron_lib import constants +from neutron_lib.utils import file as file_utils from oslo_config import cfg from oslo_log import log as logging import six @@ -26,7 +27,6 @@ from neutron._i18n import _ from neutron.agent.linux import external_process from neutron.agent.linux import utils from neutron.common import constants as n_const -from neutron.common import utils as common_utils RADVD_SERVICE_NAME = 'radvd' @@ -139,7 +139,7 @@ class DaemonMonitor(object): max_rtr_adv_interval=self._agent_conf.max_rtr_adv_interval, network_mtu=int(network_mtu))) - common_utils.replace_file(radvd_conf, buf.getvalue()) + file_utils.replace_file(radvd_conf, buf.getvalue()) return radvd_conf def _get_radvd_process_manager(self, callback=None): diff --git a/neutron/cmd/pd_notify.py b/neutron/cmd/pd_notify.py index 9dff494a48e..27481e2680b 100644 --- a/neutron/cmd/pd_notify.py +++ b/neutron/cmd/pd_notify.py @@ -17,7 +17,7 @@ import os import signal import sys -from neutron.common import utils +from neutron_lib.utils import file as file_utils def main(): @@ -32,7 +32,7 @@ def main(): prefix = os.getenv('PREFIX1', "::") if operation == "add" or operation == "update": - utils.replace_file(prefix_fname, "%s/64" % prefix) + file_utils.replace_file(prefix_fname, "%s/64" % prefix) elif operation == "delete": - utils.replace_file(prefix_fname, "::/64") + file_utils.replace_file(prefix_fname, "::/64") os.kill(int(agent_pid), signal.SIGUSR1) diff --git a/neutron/common/utils.py b/neutron/common/utils.py index 9cf5d03ba0a..c1549dae29e 100644 --- a/neutron/common/utils.py +++ b/neutron/common/utils.py @@ -26,7 +26,6 @@ import os.path import random import signal import sys -import tempfile import time import uuid @@ -35,6 +34,7 @@ import eventlet from eventlet.green import subprocess import netaddr from neutron_lib import constants as n_const +from neutron_lib.utils import file as file_utils from neutron_lib.utils import helpers from neutron_lib.utils import host from neutron_lib.utils import net @@ -340,22 +340,10 @@ def round_val(val): rounding=decimal.ROUND_HALF_UP)) +@removals.remove( + message="Use replace_file from neutron_lib.utils") def replace_file(file_name, data, file_mode=0o644): - """Replaces the contents of file_name with data in a safe manner. - - First write to a temp file and then rename. Since POSIX renames are - atomic, the file is unlikely to be corrupted by competing writes. - - We create the tempfile on the same device to ensure that it can be renamed. - """ - - base_dir = os.path.dirname(os.path.abspath(file_name)) - with tempfile.NamedTemporaryFile('w+', - dir=base_dir, - delete=False) as tmp_file: - tmp_file.write(data) - os.chmod(tmp_file.name, file_mode) - os.rename(tmp_file.name, file_name) + file_utils.replace_file(file_name, data, file_mode=file_mode) def load_class_by_alias_or_classname(namespace, name): diff --git a/neutron/tests/functional/common/test_utils.py b/neutron/tests/functional/common/test_utils.py index 3a72ca33fd8..bbbb37bf006 100644 --- a/neutron/tests/functional/common/test_utils.py +++ b/neutron/tests/functional/common/test_utils.py @@ -11,48 +11,12 @@ # under the License. import eventlet -import os.path -import stat import testtools from neutron.common import utils from neutron.tests import base -class TestReplaceFile(base.BaseTestCase): - def setUp(self): - super(TestReplaceFile, self).setUp() - temp_dir = self.get_default_temp_dir().path - self.file_name = os.path.join(temp_dir, "new_file") - self.data = "data to copy" - - def _verify_result(self, file_mode): - self.assertTrue(os.path.exists(self.file_name)) - with open(self.file_name) as f: - content = f.read() - self.assertEqual(self.data, content) - mode = os.stat(self.file_name).st_mode - self.assertEqual(file_mode, stat.S_IMODE(mode)) - - def test_replace_file_default_mode(self): - file_mode = 0o644 - utils.replace_file(self.file_name, self.data) - self._verify_result(file_mode) - - def test_replace_file_custom_mode(self): - file_mode = 0o722 - utils.replace_file(self.file_name, self.data, file_mode) - self._verify_result(file_mode) - - def test_replace_file_custom_mode_twice(self): - file_mode = 0o722 - utils.replace_file(self.file_name, self.data, file_mode) - self.data = "new data to copy" - file_mode = 0o777 - utils.replace_file(self.file_name, self.data, file_mode) - self._verify_result(file_mode) - - class TestWaitUntilTrue(base.BaseTestCase): def test_wait_until_true_predicate_succeeds(self): utils.wait_until_true(lambda: True) diff --git a/neutron/tests/unit/agent/l3/test_agent.py b/neutron/tests/unit/agent/l3/test_agent.py index e2e8d2387ff..bd44759d3b8 100644 --- a/neutron/tests/unit/agent/l3/test_agent.py +++ b/neutron/tests/unit/agent/l3/test_agent.py @@ -102,7 +102,7 @@ class BasicRouterOperationsFramework(base.BaseTestCase): self.utils_exec = self.utils_exec_p.start() self.utils_replace_file_p = mock.patch( - 'neutron.common.utils.replace_file') + 'neutron_lib.utils.file.replace_file') self.utils_replace_file = self.utils_replace_file_p.start() self.external_process_p = mock.patch( diff --git a/neutron/tests/unit/agent/l3/test_dvr_local_router.py b/neutron/tests/unit/agent/l3/test_dvr_local_router.py index 61e75569a2f..df649c44be4 100644 --- a/neutron/tests/unit/agent/l3/test_dvr_local_router.py +++ b/neutron/tests/unit/agent/l3/test_dvr_local_router.py @@ -76,7 +76,7 @@ class TestDvrRouterOperations(base.BaseTestCase): self.utils_exec = self.utils_exec_p.start() self.utils_replace_file_p = mock.patch( - 'neutron.common.utils.replace_file') + 'neutron_lib.utils.file.replace_file') self.utils_replace_file = self.utils_replace_file_p.start() self.external_process_p = mock.patch( diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index bdeafedfde6..48c7d4c6e75 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -876,7 +876,7 @@ class TestBase(TestConfBase): self.config_parse(self.conf) self.conf.set_override('state_path', '') - self.replace_p = mock.patch('neutron.common.utils.replace_file') + self.replace_p = mock.patch('neutron_lib.utils.file.replace_file') self.execute_p = mock.patch('neutron.agent.common.utils.execute') mock.patch('neutron.agent.linux.utils.execute').start() self.safe = self.replace_p.start() @@ -1036,7 +1036,7 @@ class TestDhcpLocalProcess(TestBase): self.assertEqual(lp.interface_name, 'tap0') def test_set_interface_name(self): - with mock.patch('neutron.common.utils.replace_file') as replace: + with mock.patch('neutron_lib.utils.file.replace_file') as replace: lp = LocalChild(self.conf, FakeDualNetwork()) with mock.patch.object(lp, 'get_conf_file_name') as conf_file: conf_file.return_value = '/interface'