def link_mountpoint(self, repo):
        '''
        Ensure that the mountpoint is present in the correct location and
        points at the correct path
        '''
        lcachelink = salt.utils.path.join(repo.linkdir, repo._mountpoint)
        lcachedest = salt.utils.path.join(repo.cachedir, repo.root()).rstrip(os.sep)
        wipe_linkdir = False
        create_link = False
        try:
            with repo.gen_lock(lock_type='mountpoint', timeout=10):
                walk_results = list(os.walk(repo.linkdir, followlinks=False))
                if walk_results != repo.linkdir_walk:
                    log.debug(
                        'Results of walking %s differ from expected results',
                        repo.linkdir
                    )
                    log.debug('Walk results: %s', walk_results)
                    log.debug('Expected results: %s', repo.linkdir_walk)
                    wipe_linkdir = True
                else:
                    if not all(not salt.utils.path.islink(x[0])
                               and os.path.isdir(x[0])
                               for x in walk_results[:-1]):
                        log.debug(
                            'Linkdir parents of %s are not all directories',
                            lcachelink
                        )
                        wipe_linkdir = True
                    elif not salt.utils.path.islink(lcachelink):
                        wipe_linkdir = True
                    else:
                        try:
                            ldest = salt.utils.path.readlink(lcachelink)
                        except Exception:
                            log.debug(
                                'Failed to read destination of %s', lcachelink
                            )
                            wipe_linkdir = True
                        else:
                            if ldest != lcachedest:
                                log.debug(
                                    'Destination of %s (%s) does not match '
                                    'the expected value (%s)',
                                    lcachelink, ldest, lcachedest
                                )
                                # Since we know that the parent dirs of the
                                # link are set up properly, all we need to do
                                # is remove the symlink and let it be created
                                # below.
                                try:
                                    if salt.utils.platform.is_windows() \
                                            and not ldest.startswith('\\\\') \
                                            and os.path.isdir(ldest):
                                        # On Windows, symlinks to directories
                                        # must be removed as if they were
                                        # themselves directories.
                                        shutil.rmtree(lcachelink)
                                    else:
                                        os.remove(lcachelink)
                                except Exception as exc:
                                    log.exception(
                                        'Failed to remove existing git_pillar '
                                        'mountpoint link %s: %s',
                                        lcachelink, exc.__str__()
                                    )
                                wipe_linkdir = False
                                create_link = True

                if wipe_linkdir:
                    # Wiping implies that we need to create the link
                    create_link = True
                    try:
                        shutil.rmtree(repo.linkdir)
                    except OSError:
                        pass
                    try:
                        ldirname = os.path.dirname(lcachelink)
                        os.makedirs(ldirname)
                        log.debug('Successfully made linkdir parent %s', ldirname)
                    except OSError as exc:
                        log.error(
                            'Failed to os.makedirs() linkdir parent %s: %s',
                            ldirname, exc.__str__()
                        )
                        return False

                if create_link:
                    try:
                        os.symlink(lcachedest, lcachelink)
                        log.debug(
                            'Successfully linked %s to cachedir %s',
                            lcachelink, lcachedest
                        )
                        return True
                    except OSError as exc:
                        log.error(
                            'Failed to create symlink to %s at path %s: %s',
                            lcachedest, lcachelink, exc.__str__()
                        )
                        return False
        except GitLockError:
            log.error(
                'Timed out setting mountpoint lock for %s remote \'%s\'. If '
                'this error persists, it may be because an earlier %s '
                'checkout was interrupted. The lock can be cleared by running '
                '\'salt-run cache.clear_git_lock %s type=mountpoint\', or by '
                'manually removing %s.',
                self.role, repo.id, self.role, self.role,
                repo._get_lock_file(lock_type='mountpoint')
            )
            return False
        return True
def link_mountpoint ( self , repo ) : lcachelink = salt . utils . path . join ( repo . linkdir , repo . _mountpoint ) lcachedest = salt . utils . path . join ( repo . cachedir , repo . root ( ) ) . rstrip ( os . sep ) wipe_linkdir = False create_link = False try : with repo . gen_lock ( lock_type = 'mountpoint' , timeout = 10 ) : walk_results = list ( os . walk ( repo . linkdir , followlinks = False ) ) if walk_results != repo . linkdir_walk : log . debug ( 'Results of walking %s differ from expected results' , repo . linkdir ) log . debug ( 'Walk results: %s' , walk_results ) log . debug ( 'Expected results: %s' , repo . linkdir_walk ) wipe_linkdir = True else : if not all ( not salt . utils . path . islink ( x [ 0 ] ) and os . path . isdir ( x [ 0 ] ) for x in walk_results [ : - 1 ] ) : log . debug ( 'Linkdir parents of %s are not all directories' , lcachelink ) wipe_linkdir = True elif not salt . utils . path . islink ( lcachelink ) : wipe_linkdir = True else : try : ldest = salt . utils . path . readlink ( lcachelink ) except Exception : log . debug ( 'Failed to read destination of %s' , lcachelink ) wipe_linkdir = True else : if ldest != lcachedest : log . debug ( 'Destination of %s (%s) does not match ' 'the expected value (%s)' , lcachelink , ldest , lcachedest ) try : if salt . utils . platform . is_windows ( ) and not ldest . startswith ( '\\\\' ) and os . path . isdir ( ldest ) : shutil . rmtree ( lcachelink ) else : os . remove ( lcachelink ) except Exception as exc : log . exception ( 'Failed to remove existing git_pillar ' 'mountpoint link %s: %s' , lcachelink , exc . __str__ ( ) ) wipe_linkdir = False create_link = True if wipe_linkdir : create_link = True try : shutil . rmtree ( repo . linkdir ) except OSError : pass try : ldirname = os . path . dirname ( lcachelink ) os . makedirs ( ldirname ) log . debug ( 'Successfully made linkdir parent %s' , ldirname ) except OSError as exc : log . error ( 'Failed to os.makedirs() linkdir parent %s: %s' , ldirname , exc . __str__ ( ) ) return False if create_link : try : os . symlink ( lcachedest , lcachelink ) log . debug ( 'Successfully linked %s to cachedir %s' , lcachelink , lcachedest ) return True except OSError as exc : log . error ( 'Failed to create symlink to %s at path %s: %s' , lcachedest , lcachelink , exc . __str__ ( ) ) return False except GitLockError : log . error ( 'Timed out setting mountpoint lock for %s remote \'%s\'. If ' 'this error persists, it may be because an earlier %s ' 'checkout was interrupted. The lock can be cleared by running ' '\'salt-run cache.clear_git_lock %s type=mountpoint\', or by ' 'manually removing %s.' , self . role , repo . id , self . role , self . role , repo . _get_lock_file ( lock_type = 'mountpoint' ) ) return False return True
Ensure that the mountpoint is present in the correct location and
        points at the correct path