replace oslo.concurrency with fasteners for file locks (Oracle-Bug: 22117282)

- oslo concurrency brings in too much new dependencies for a patch release, fasteners
is a much smaller change.
- also change utest inventory clear so it doesn't cause an
ownership change of the inventory file.
This commit is contained in:
Steve Noyes 2015-11-02 10:46:39 -05:00
parent 24c4c6c5d7
commit 9651fa3e1d
3 changed files with 41 additions and 35 deletions

View File

@ -11,16 +11,14 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import logging
import os
import pexpect
import pwd
import tempfile
import yaml
from oslo_concurrency import lockutils
# pool of semaphores for intra-process locks
semaphores = lockutils.Semaphores()
from fasteners import InterProcessLock
def get_kolla_home():
@ -43,6 +41,14 @@ def get_kolla_log_dir():
return '/var/log/kolla/'
def get_admin_uids():
"""get uid and gid of admin user"""
user_info = pwd.getpwnam(get_admin_user())
uid = user_info.pw_uid
gid = user_info.pw_gid
return uid, gid
def get_kolla_log_file_size():
return os.environ.get('KOLLA_LOG_FILE_SIZE', 500000)
@ -182,37 +188,39 @@ def sync_read_file(path, mode='r'):
return file data
"""
_lock(path)
with open(path, mode) as data_file:
data = data_file.read()
lock = get_lock(path)
try:
lock.acquire(blocking=True, timeout=120)
with open(path, mode) as data_file:
data = data_file.read()
except Exception as e:
raise e
finally:
lock.release()
return data
def sync_write_file(path, data, mode='w'):
"""synchronously write file"""
_lock(path)
with open(path, mode) as data_file:
data_file.write(data)
lock = get_lock(path)
try:
lock.acquire(blocking=True, timeout=120)
with open(path, mode) as data_file:
data_file.write(data)
except Exception as e:
raise e
finally:
lock.release()
@contextlib.contextmanager
def _lock(lock_path, delay=0.01):
"""Context based lock
This function yields an InterProcessLock instance.
:param lock_path: The path in which to store external lock files. For
external locking to work properly, this must be the same for all
references to the lock.
:param delay: Delay between acquisition attempts (in seconds).
"""
name = lock_path.replace('/', '_')
int_lock = lockutils.internal_lock(name, semaphores)
with int_lock:
ext_lock = lockutils.external_lock(name, '', lock_path)
ext_lock.acquire(delay=delay)
try:
yield ext_lock
finally:
ext_lock.release()
def get_lock(path):
fname = 'kollacli_' + os.path.basename(path) + '.lock'
lock_path = os.path.join(tempfile.gettempdir(), fname)
if not os.path.isfile(lock_path):
# lock doesn't exit. create it and set owned by kolla group
with open(lock_path, 'a') as _:
pass
_, gid = get_admin_uids()
os.chown(lock_path, -1, gid)
lock = InterProcessLock(lock_path)
return lock

View File

@ -4,7 +4,7 @@ cliff>=1.13.0 # Apache-2.0
cliff-tablib>=1.1
docker-py>=1.3.1
jsonpickle>=0.9
oslo.concurrency>=2.8.0
fasteners>=0.13.0
oslo.i18n>=1.3.0 # Apache-2.0
paramiko>=1.15
pbr>=0.10

View File

@ -96,8 +96,6 @@ class KollaCliTest(testtools.TestCase):
return (retval, msg)
def _init_file(self, filepath):
if os.path.exists(filepath):
os.remove(filepath)
with open(filepath, 'w'):
pass