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
This commit is contained in:
Ihar Hrachyshka 2015-07-18 14:52:53 +02:00
parent dbe7cac34b
commit f53a43fd5e
15 changed files with 53 additions and 42 deletions

View File

@ -26,7 +26,6 @@ from oslo_utils import importutils
from neutron.agent.linux import dhcp from neutron.agent.linux import dhcp
from neutron.agent.linux import external_process from neutron.agent.linux import external_process
from neutron.agent.linux import utils as linux_utils
from neutron.agent.metadata import driver as metadata_driver from neutron.agent.metadata import driver as metadata_driver
from neutron.agent import rpc as agent_rpc from neutron.agent import rpc as agent_rpc
from neutron.common import constants from neutron.common import constants
@ -63,7 +62,7 @@ class DhcpAgent(manager.Manager):
ctx, self.conf.use_namespaces) ctx, self.conf.use_namespaces)
# create dhcp dir to store dhcp info # create dhcp dir to store dhcp info
dhcp_dir = os.path.dirname("/%s/dhcp/" % self.conf.state_path) dhcp_dir = os.path.dirname("/%s/dhcp/" % self.conf.state_path)
linux_utils.ensure_dir(dhcp_dir) utils.ensure_dir(dhcp_dir)
self.dhcp_version = self.dhcp_driver_cls.check_version() self.dhcp_version = self.dhcp_driver_cls.check_version()
self._populate_networks_cache() self._populate_networks_cache()
self._process_monitor = external_process.ProcessMonitor( self._process_monitor = external_process.ProcessMonitor(

View File

@ -22,6 +22,7 @@ import webob
from neutron.agent.linux import keepalived from neutron.agent.linux import keepalived
from neutron.agent.linux import utils as agent_utils from neutron.agent.linux import utils as agent_utils
from neutron.common import utils as common_utils
from neutron.i18n import _LI from neutron.i18n import _LI
from neutron.notifiers import batch_notifier from neutron.notifiers import batch_notifier
@ -157,4 +158,4 @@ class AgentMixin(object):
def _init_ha_conf_path(self): def _init_ha_conf_path(self):
ha_full_path = os.path.dirname("/%s/" % self.conf.ha_confs_path) ha_full_path = os.path.dirname("/%s/" % self.conf.ha_confs_path)
agent_utils.ensure_dir(ha_full_path) common_utils.ensure_dir(ha_full_path)

View File

@ -174,7 +174,7 @@ class DhcpLocalProcess(DhcpBase):
version, plugin) version, plugin)
self.confs_dir = self.get_confs_dir(conf) self.confs_dir = self.get_confs_dir(conf)
self.network_conf_dir = os.path.join(self.confs_dir, network.id) self.network_conf_dir = os.path.join(self.confs_dir, network.id)
utils.ensure_dir(self.network_conf_dir) commonutils.ensure_dir(self.network_conf_dir)
@staticmethod @staticmethod
def get_confs_dir(conf): def get_confs_dir(conf):
@ -199,7 +199,7 @@ class DhcpLocalProcess(DhcpBase):
if self.active: if self.active:
self.restart() self.restart()
elif self._enable_dhcp(): elif self._enable_dhcp():
utils.ensure_dir(self.network_conf_dir) commonutils.ensure_dir(self.network_conf_dir)
interface_name = self.device_manager.setup(self.network) interface_name = self.device_manager.setup(self.network)
self.interface_name = interface_name self.interface_name = interface_name
self.spawn_process() self.spawn_process()

View File

@ -26,6 +26,7 @@ from oslo_utils import fileutils
from neutron.agent.common import config as agent_cfg from neutron.agent.common import config as agent_cfg
from neutron.agent.linux import ip_lib from neutron.agent.linux import ip_lib
from neutron.agent.linux import utils from neutron.agent.linux import utils
from neutron.common import utils as common_utils
from neutron.i18n import _LE from neutron.i18n import _LE
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -78,7 +79,7 @@ class ProcessManager(MonitoredProcess):
self.service_pid_fname = 'pid' self.service_pid_fname = 'pid'
self.service = 'default-service' self.service = 'default-service'
utils.ensure_dir(os.path.dirname(self.get_pid_file_name())) common_utils.ensure_dir(os.path.dirname(self.get_pid_file_name()))
def enable(self, cmd_callback=None, reload_cfg=False): def enable(self, cmd_callback=None, reload_cfg=False):
if not self.active: if not self.active:

View File

@ -23,6 +23,7 @@ from oslo_log import log as logging
from neutron.agent.linux import external_process from neutron.agent.linux import external_process
from neutron.agent.linux import utils from neutron.agent.linux import utils
from neutron.common import exceptions from neutron.common import exceptions
from neutron.common import utils as common_utils
VALID_STATES = ['MASTER', 'BACKUP'] VALID_STATES = ['MASTER', 'BACKUP']
VALID_AUTH_TYPES = ['AH', 'PASS'] VALID_AUTH_TYPES = ['AH', 'PASS']
@ -340,7 +341,7 @@ class KeepalivedManager(object):
def get_full_config_file_path(self, filename, ensure_conf_dir=True): def get_full_config_file_path(self, filename, ensure_conf_dir=True):
conf_dir = self.get_conf_dir() conf_dir = self.get_conf_dir()
if ensure_conf_dir: if ensure_conf_dir:
utils.ensure_dir(conf_dir) common_utils.ensure_dir(conf_dir)
return os.path.join(conf_dir, filename) return os.path.join(conf_dir, filename)
def _output_config_file(self): def _output_config_file(self):

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import errno
import fcntl import fcntl
import glob import glob
import grp import grp
@ -25,6 +24,7 @@ import struct
import tempfile import tempfile
import threading import threading
from debtcollector import removals
import eventlet import eventlet
from eventlet.green import subprocess from eventlet.green import subprocess
from eventlet import greenthread from eventlet import greenthread
@ -189,14 +189,9 @@ def find_child_pids(pid):
return [x.strip() for x in raw_pids.split('\n') if x.strip()] return [x.strip() for x in raw_pids.split('\n') if x.strip()]
def ensure_dir(dir_path): @removals.remove(message='Use neutron.common.utils.ensure_dir instead.')
"""Ensure a directory with 755 permissions mode.""" def ensure_dir(*args, **kwargs):
try: return utils.ensure_dir(*args, **kwargs)
os.makedirs(dir_path, 0o755)
except OSError as e:
# If the directory already existed, don't raise the error.
if e.errno != errno.EEXIST:
raise
def _get_conf_base(cfg_root, uuid, ensure_conf_dir): def _get_conf_base(cfg_root, uuid, ensure_conf_dir):
@ -205,7 +200,7 @@ def _get_conf_base(cfg_root, uuid, ensure_conf_dir):
conf_dir = os.path.abspath(os.path.normpath(cfg_root)) conf_dir = os.path.abspath(os.path.normpath(cfg_root))
conf_base = os.path.join(conf_dir, uuid) conf_base = os.path.join(conf_dir, uuid)
if ensure_conf_dir: if ensure_conf_dir:
ensure_dir(conf_dir) utils.ensure_dir(conf_dir)
return conf_base return conf_base
@ -338,7 +333,7 @@ def ensure_directory_exists_without_file(path):
if not os.path.exists(path): if not os.path.exists(path):
ctxt.reraise = False ctxt.reraise = False
else: else:
ensure_dir(dirname) utils.ensure_dir(dirname)
def is_effective_user(user_id_or_name): def is_effective_user(user_id_or_name):

View File

@ -19,6 +19,7 @@
"""Utilities and helper functions.""" """Utilities and helper functions."""
import datetime import datetime
import errno
import functools import functools
import hashlib import hashlib
import logging as std_logging import logging as std_logging
@ -172,6 +173,16 @@ def find_config_file(options, config_file):
return cfg_file return cfg_file
def ensure_dir(dir_path):
"""Ensure a directory with 755 permissions mode."""
try:
os.makedirs(dir_path, 0o755)
except OSError as e:
# If the directory already existed, don't raise the error.
if e.errno != errno.EEXIST:
raise
def _subprocess_setup(): def _subprocess_setup():
# Python installs a SIGPIPE handler by default. This is usually not what # Python installs a SIGPIPE handler by default. This is usually not what
# non-Python subprocesses expect. # non-Python subprocesses expect.

View File

@ -25,6 +25,7 @@ from oslo_utils import timeutils
from neutron.agent.linux import async_process from neutron.agent.linux import async_process
from neutron.agent.linux import utils from neutron.agent.linux import utils
from neutron.common import utils as common_utils
from neutron.tests import base from neutron.tests import base
from neutron.tests.common import net_helpers from neutron.tests.common import net_helpers
from neutron.tests.fullstack import config_fixtures from neutron.tests.fullstack import config_fixtures
@ -51,7 +52,7 @@ class ProcessFixture(fixtures.Fixture):
def start(self): def start(self):
fmt = self.process_name + "--%Y-%m-%d--%H%M%S.log" fmt = self.process_name + "--%Y-%m-%d--%H%M%S.log"
log_dir = os.path.join(DEFAULT_LOG_DIR, self.test_name) log_dir = os.path.join(DEFAULT_LOG_DIR, self.test_name)
utils.ensure_dir(log_dir) common_utils.ensure_dir(log_dir)
cmd = [spawn.find_executable(self.exec_name), cmd = [spawn.find_executable(self.exec_name),
'--log-dir', log_dir, '--log-dir', log_dir,

View File

@ -79,8 +79,7 @@ class BasicRouterOperationsFramework(base.BaseTestCase):
'neutron.agent.linux.ip_lib.device_exists') 'neutron.agent.linux.ip_lib.device_exists')
self.device_exists = self.device_exists_p.start() self.device_exists = self.device_exists_p.start()
self.ensure_dir = mock.patch('neutron.agent.linux.utils' self.ensure_dir = mock.patch('neutron.common.utils.ensure_dir').start()
'.ensure_dir').start()
mock.patch('neutron.agent.linux.keepalived.KeepalivedManager' mock.patch('neutron.agent.linux.keepalived.KeepalivedManager'
'.get_full_config_file_path').start() '.get_full_config_file_path').start()

View File

@ -65,8 +65,7 @@ class TestDvrRouterOperations(base.BaseTestCase):
'neutron.agent.linux.ip_lib.device_exists') 'neutron.agent.linux.ip_lib.device_exists')
self.device_exists = self.device_exists_p.start() self.device_exists = self.device_exists_p.start()
self.ensure_dir = mock.patch('neutron.agent.linux.utils' self.ensure_dir = mock.patch('neutron.common.utils.ensure_dir').start()
'.ensure_dir').start()
mock.patch('neutron.agent.linux.keepalived.KeepalivedManager' mock.patch('neutron.agent.linux.keepalived.KeepalivedManager'
'.get_full_config_file_path').start() '.get_full_config_file_path').start()

View File

@ -24,9 +24,9 @@ from neutron.agent.common import config
from neutron.agent.dhcp import config as dhcp_config from neutron.agent.dhcp import config as dhcp_config
from neutron.agent.linux import dhcp from neutron.agent.linux import dhcp
from neutron.agent.linux import external_process from neutron.agent.linux import external_process
from neutron.agent.linux import utils
from neutron.common import config as base_config from neutron.common import config as base_config
from neutron.common import constants from neutron.common import constants
from neutron.common import utils
from neutron.extensions import extra_dhcp_opt as edo_ext from neutron.extensions import extra_dhcp_opt as edo_ext
from neutron.tests import base from neutron.tests import base

View File

@ -16,7 +16,7 @@ import mock
import os.path import os.path
from neutron.agent.linux import external_process as ep from neutron.agent.linux import external_process as ep
from neutron.agent.linux import utils from neutron.common import utils as common_utils
from neutron.tests import base from neutron.tests import base
@ -105,7 +105,7 @@ class TestProcessManager(base.BaseTestCase):
self.delete_if_exists = mock.patch( self.delete_if_exists = mock.patch(
'oslo_utils.fileutils.delete_if_exists').start() 'oslo_utils.fileutils.delete_if_exists').start()
self.ensure_dir = mock.patch.object( self.ensure_dir = mock.patch.object(
utils, 'ensure_dir').start() common_utils, 'ensure_dir').start()
self.conf = mock.Mock() self.conf = mock.Mock()
self.conf.external_pids = '/var/path' self.conf.external_pids = '/var/path'

View File

@ -12,9 +12,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import errno
import mock
import socket import socket
import mock
import testtools import testtools
from neutron.agent.linux import utils from neutron.agent.linux import utils
@ -282,18 +282,6 @@ class TestBaseOSUtils(base.BaseTestCase):
getegid.assert_called_once_with() getegid.assert_called_once_with()
getgrgid.assert_called_once_with(self.EGID) getgrgid.assert_called_once_with(self.EGID)
@mock.patch('os.makedirs')
def test_ensure_dir_no_fail_if_exists(self, makedirs):
error = OSError()
error.errno = errno.EEXIST
makedirs.side_effect = error
utils.ensure_dir("/etc/create/concurrently")
@mock.patch('os.makedirs')
def test_ensure_dir_calls_makedirs(self, makedirs):
utils.ensure_dir("/etc/create/directory")
makedirs.assert_called_once_with("/etc/create/directory", 0o755)
class TestUnixDomainHttpConnection(base.BaseTestCase): class TestUnixDomainHttpConnection(base.BaseTestCase):
def test_connect(self): def test_connect(self):

View File

@ -524,7 +524,7 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase):
self.cfg.CONF.metadata_backlog = 128 self.cfg.CONF.metadata_backlog = 128
self.cfg.CONF.metadata_proxy_socket_mode = config.USER_MODE self.cfg.CONF.metadata_proxy_socket_mode = config.USER_MODE
@mock.patch.object(agent_utils, 'ensure_dir') @mock.patch.object(utils, 'ensure_dir')
def test_init_doesnot_exists(self, ensure_dir): def test_init_doesnot_exists(self, ensure_dir):
agent.UnixDomainMetadataProxy(mock.Mock()) agent.UnixDomainMetadataProxy(mock.Mock())
ensure_dir.assert_called_once_with('/the') ensure_dir.assert_called_once_with('/the')
@ -561,7 +561,7 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase):
@mock.patch.object(agent, 'MetadataProxyHandler') @mock.patch.object(agent, 'MetadataProxyHandler')
@mock.patch.object(agent_utils, 'UnixDomainWSGIServer') @mock.patch.object(agent_utils, 'UnixDomainWSGIServer')
@mock.patch.object(agent_utils, 'ensure_dir') @mock.patch.object(utils, 'ensure_dir')
def test_run(self, ensure_dir, server, handler): def test_run(self, ensure_dir, server, handler):
p = agent.UnixDomainMetadataProxy(self.cfg.CONF) p = agent.UnixDomainMetadataProxy(self.cfg.CONF)
p.run() p.run()

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import errno
import eventlet import eventlet
import mock import mock
import netaddr import netaddr
@ -663,3 +665,17 @@ class TestDelayedStringRenderer(base.BaseTestCase):
LOG.logger.setLevel(logging.logging.DEBUG) LOG.logger.setLevel(logging.logging.DEBUG)
LOG.debug("Hello %s", delayed) LOG.debug("Hello %s", delayed)
self.assertTrue(my_func.called) self.assertTrue(my_func.called)
class TestEnsureDir(base.BaseTestCase):
@mock.patch('os.makedirs')
def test_ensure_dir_no_fail_if_exists(self, makedirs):
error = OSError()
error.errno = errno.EEXIST
makedirs.side_effect = error
utils.ensure_dir("/etc/create/concurrently")
@mock.patch('os.makedirs')
def test_ensure_dir_calls_makedirs(self, makedirs):
utils.ensure_dir("/etc/create/directory")
makedirs.assert_called_once_with("/etc/create/directory", 0o755)