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
This commit is contained in:
parent
29669c04dd
commit
3d68a51eac
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -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(
|
||||
|
@ -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(
|
||||
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user