def _expand_clones(settings, partition, name, inventory, inventory_lock, new_refs, moved_refs, deleted_refs): bare_path = _compose_bare_repository_path(settings, name, partition) if new_refs: logging.debug("Processing new refs of %s/%s (%s)..." % \ (partition, name, new_refs)) for refname in new_refs: clone_path = _compose_clone_repository_path(settings, name, partition, refname) logging.info("Populating new ref '%s'" % clone_path) try: if ref_is_commit(settings, refname): commit_id = refname.replace(settings.HASHPREFIX, '') logging.debug("Will create a clone pointing to '%s'" % commit_id) git.clone(clone_path, "%s" % bare_path, shared=True) git.reset(clone_path, commit_id, hard=True) else: git.clone(clone_path, "%s" % bare_path, branch=refname) # Needs reset so the proxy notices about the change on the mutable # http://docs.python.org/2.7/library/multiprocessing.html#managers # Locking on the assignment is guarateed by the library, but # additional locking is needed as A = A + 1 is a critical section. if inventory_lock: inventory_lock.acquire() inventory[name] += [refname] if inventory_lock: inventory_lock.release() except JensGitError, error: if os.path.isdir(clone_path): shutil.rmtree(clone_path) logging.error("Unable to create clone '%s' (%s)" % \ (clone_path, error))
def _refresh_environments(settings): logging.debug("Refreshing environment metadata...") path = settings.ENV_METADATADIR try: git.fetch(path) git.reset(path, "origin/master", hard=True) except JensGitError, error: raise JensError("Couldn't refresh environments metadata (%s)" % error)
def _refresh_repositories(settings): logging.debug("Refreshing repositories metadata...") path = settings.REPO_METADATADIR try: git.fetch(path) git.reset(path, "origin/master", hard=True) except JensGitError, error: raise JensError("Couldn't refresh repositories metadata (%s)" % error)
if moved_refs: logging.debug("Processing moved refs of %s/%s (%s)..." % \ (partition, name, moved_refs)) for refname in moved_refs: clone_path = _compose_clone_repository_path(settings, name, partition, refname) logging.info("Updating ref '%s'" % clone_path) try: # If this fails, the bare would have the correct HEADs # but the clone will be out of date and won't ever be # updated until a new commit arrives to the bare. # Reason: a lock file left behind because Git was killed # mid-flight. git.fetch(clone_path) git.reset(clone_path, "origin/%s" % refname, hard=True) except JensGitError, error: logging.error("Unable to refresh clone '%s' (%s)" % \ (clone_path, error)) if deleted_refs: logging.debug("Processing deleted refs of %s/%s (%s)..." % \ (partition, name, deleted_refs)) for refname in deleted_refs: clone_path = _compose_clone_repository_path(settings, name, partition, refname) logging.info("Removing %s" % clone_path) try: if os.path.isdir(clone_path): shutil.rmtree(clone_path) if refname in inventory[name]:
try: git.fetch(path) try: metadata = open(settings.REPO_METADATA, 'r') except IOError, error: raise JensError("Could not open '%s' to put a lock on it" % \ settings.REPO_METADATA) # jens-gitlab-producer collaborates with jens-update asynchronously # so have to make sure that exclusive access to the file when writing # is guaranteed. Of course, the reader will have to implement the same # protocol on the other end. try: logging.info("Trying to acquire a lock to refresh the metadata...") fcntl.flock(metadata, fcntl.LOCK_EX) logging.debug("Lock acquired") except IOError, error: metadata.close() raise JensError("Could not lock '%s'" % settings.REPO_METADATA) git.reset(path, "origin/master", hard=True) try: logging.debug( "Trying to release the lock used to refresh the metadata...") fcntl.flock(metadata, fcntl.LOCK_UN) logging.debug("Lock released") except IOError, error: raise JensError("Could not unlock '%s'" % settings.REPO_METADATA) finally: metadata.close() except JensGitError, error: raise JensError("Couldn't refresh repositories metadata (%s)" % error)
path = settings.REPO_METADATADIR try: git.fetch(path) try: metadata = open(settings.REPO_METADATA, 'r') except IOError, error: raise JensError("Could not open '%s' to put a lock on it" % \ settings.REPO_METADATA) # jens-gitlab-producer collaborates with jens-update asynchronously # so have to make sure that exclusive access to the file when writing # is guaranteed. Of course, the reader will have to implement the same # protocol on the other end. try: logging.info("Trying to acquire a lock to refresh the metadata...") fcntl.flock(metadata, fcntl.LOCK_EX) logging.debug("Lock acquired") except IOError, error: metadata.close() raise JensError("Could not lock '%s'" % settings.REPO_METADATA) git.reset(path, "origin/master", hard=True) try: logging.debug("Trying to release the lock used to refresh the metadata...") fcntl.flock(metadata, fcntl.LOCK_UN) logging.debug("Lock released") except IOError, error: raise JensError("Could not unlock '%s'" % settings.REPO_METADATA) finally: metadata.close() except JensGitError, error: raise JensError("Couldn't refresh repositories metadata (%s)" % error)