Skip to content

Commit

Permalink
Reorganizing the way the connection lockfile is created
Browse files Browse the repository at this point in the history
  • Loading branch information
jimi-c committed Sep 3, 2015
1 parent 7034bbe commit b9afbf0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
5 changes: 0 additions & 5 deletions lib/ansible/executor/task_queue_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import os
import socket
import sys
import tempfile

from ansible import constants as C
from ansible.errors import AnsibleError
Expand Down Expand Up @@ -79,10 +78,6 @@ def __init__(self, inventory, variable_manager, loader, display, options, passwo
self._failed_hosts = dict()
self._unreachable_hosts = dict()

# A temporary file (opened pre-fork) used by connection plugins for
# inter-process locking.
self._options.connection_lockfile = tempfile.TemporaryFile()

self._final_q = multiprocessing.Queue()

# create the pool of worker threads, based on the number of forks specified
Expand Down
16 changes: 10 additions & 6 deletions lib/ansible/playbook/play_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import pipes
import random
import re
import tempfile

from ansible import constants as C
from ansible.errors import AnsibleError
Expand Down Expand Up @@ -161,7 +162,6 @@ class PlayContext(Base):
_private_key_file = FieldAttribute(isa='string', default=C.DEFAULT_PRIVATE_KEY_FILE)
_timeout = FieldAttribute(isa='int', default=C.DEFAULT_TIMEOUT)
_shell = FieldAttribute(isa='string')
_connection_lockfd= FieldAttribute(isa='int', default=None)

# privilege escalation fields
_become = FieldAttribute(isa='bool')
Expand Down Expand Up @@ -200,6 +200,10 @@ def __init__(self, play=None, options=None, passwords=None):
self.password = passwords.get('conn_pass','')
self.become_pass = passwords.get('become_pass','')

# A temporary file (opened pre-fork) used by connection
# plugins for inter-process locking.
self.connection_lockf = tempfile.TemporaryFile()

# set options before play to allow play to override them
if options:
self.set_options(options)
Expand Down Expand Up @@ -245,11 +249,6 @@ def set_options(self, options):
if options.connection:
self.connection = options.connection

# The lock file is opened in the parent process, and the workers will
# inherit the open file, so we just need to help them find it.
if options.connection_lockfile:
self.connection_lockfd = options.connection_lockfile.fileno()

self.remote_user = options.remote_user
self.private_key_file = options.private_key_file

Expand Down Expand Up @@ -328,6 +327,11 @@ def set_task_and_variable_override(self, task, variables):

return new_info

def copy(self, exclude_block=False):
new_me = super(PlayContext, self).copy()
new_me.connection_lockf = self.connection_lockf
return new_me

def make_become_cmd(self, cmd, executable=None):
""" helper function to create privilege escalation commands """

Expand Down
14 changes: 7 additions & 7 deletions lib/ansible/plugins/connections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ def check_incorrect_password(self, output):
raise AnsibleError('Incorrect %s password' % self._play_context.become_method)

def lock_connection(self):
f = self._play_context.connection_lockfd
self._display.vvvv('CONNECTION: pid %d waiting for lock on %d' % (os.getpid(), f))
fcntl.lockf(f, fcntl.LOCK_EX)
self._display.vvvv('CONNECTION: pid %d acquired lock on %d' % (os.getpid(), f))
f = self._play_context.connection_lockf
self._display.vvvv('CONNECTION: pid %d waiting for lock on %d' % (os.getpid(), f.fileno()))
fcntl.lockf(f.fileno(), fcntl.LOCK_EX)
self._display.vvvv('CONNECTION: pid %d acquired lock on %d' % (os.getpid(), f.fileno()))

def unlock_connection(self):
f = self._play_context.connection_lockfd
fcntl.lockf(f, fcntl.LOCK_UN)
self._display.vvvv('CONNECTION: pid %d released lock on %d' % (os.getpid(), f))
f = self._play_context.connection_lockf
fcntl.lockf(f.fileno(), fcntl.LOCK_UN)
self._display.vvvv('CONNECTION: pid %d released lock on %d' % (os.getpid(), f.fileno()))

0 comments on commit b9afbf0

Please sign in to comment.