コード例 #1
0
 def test_ssh_sock(self):
     """Check ssh_sock() function."""
     with mock.patch('tempfile.mkdtemp', return_value='/tmp/foo'):
         # old ssh version uses port
         with mock.patch('git_command.ssh_version', return_value=(6, 6)):
             self.assertTrue(git_command.ssh_sock().endswith('%p'))
         git_command._ssh_sock_path = None
         # new ssh version uses hash
         with mock.patch('git_command.ssh_version', return_value=(6, 7)):
             self.assertTrue(git_command.ssh_sock().endswith('%C'))
         git_command._ssh_sock_path = None
コード例 #2
0
ファイル: test_git_command.py プロジェクト: sambacha/repo
 def test_ssh_sock(self):
     """Check ssh_sock() function."""
     with mock.patch("tempfile.mkdtemp", return_value="/tmp/foo"):
         # old ssh version uses port
         with mock.patch("git_command.ssh_version", return_value=(6, 6)):
             self.assertTrue(git_command.ssh_sock().endswith("%p"))
         git_command._ssh_sock_path = None
         # new ssh version uses hash
         with mock.patch("git_command.ssh_version", return_value=(6, 7)):
             self.assertTrue(git_command.ssh_sock().endswith("%C"))
         git_command._ssh_sock_path = None
コード例 #3
0
ファイル: git_config.py プロジェクト: jiangxin/repo
def close_ssh():
  terminate_ssh_clients()

  for key,p in _ssh_cache.iteritems():
    try:
      os.kill(p.pid, SIGTERM)
      p.wait()
    except OSError:
      pass
  _ssh_cache.clear()

  d = ssh_sock(create=False)
  if d:
    try:
      os.rmdir(os.path.dirname(d))
    except OSError:
      pass
コード例 #4
0
ファイル: git_config.py プロジェクト: kangear/git-repo
def close_ssh():
  global _master_keys_lock

  terminate_ssh_clients()

  for p in _master_processes:
    try:
      os.kill(p.pid, SIGTERM)
      p.wait()
    except OSError:
      pass
  del _master_processes[:]
  _master_keys.clear()

  d = ssh_sock(create=False)
  if d:
    try:
      os.rmdir(os.path.dirname(d))
    except OSError:
      pass

  # We're done with the lock, so we can delete it.
  _master_keys_lock = None
コード例 #5
0
def close_ssh():
    global _master_keys_lock

    terminate_ssh_clients()

    for p in _master_processes:
        try:
            os.kill(p.pid, SIGTERM)
            p.wait()
        except OSError:
            pass
    del _master_processes[:]
    _master_keys.clear()

    d = ssh_sock(create=False)
    if d:
        try:
            os.rmdir(os.path.dirname(d))
        except OSError:
            pass

    # We're done with the lock, so we can delete it.
    _master_keys_lock = None
コード例 #6
0
ファイル: git_config.py プロジェクト: jiangxin/repo
def _open_ssh(host, port=None):
  global _ssh_master

  if port is not None:
    key = '%s:%s' % (host, port)
  else:
    key = host

  if key in _ssh_cache:
    return True

  if not _ssh_master \
  or 'GIT_SSH' in os.environ \
  or sys.platform in ('win32', 'cygwin'):
    # failed earlier, or cygwin ssh can't do this
    #
    return False

  command = ['ssh',
             '-o','ControlPath %s' % ssh_sock(),
             '-M',
             '-N',
             host]

  if port is not None:
    command[3:3] = ['-p',str(port)]

  try:
    Trace(': %s', ' '.join(command))
    p = subprocess.Popen(command)
  except Exception, e:
    _ssh_master = False
    print >>sys.stderr, \
      '\nwarn: cannot enable ssh control master for %s:%s\n%s' \
      % (host,port, str(e))
    return False
コード例 #7
0
ファイル: git_config.py プロジェクト: kangear/git-repo
def _open_ssh(host, port=None):
  global _ssh_master

  # Acquire the lock.  This is needed to prevent opening multiple masters for
  # the same host when we're running "repo sync -jN" (for N > 1) _and_ the
  # manifest <remote fetch="ssh://xyz"> specifies a different host from the
  # one that was passed to repo init.
  _master_keys_lock.acquire()
  try:

    # Check to see whether we already think that the master is running; if we
    # think it's already running, return right away.
    if port is not None:
      key = '%s:%s' % (host, port)
    else:
      key = host

    if key in _master_keys:
      return True

    if not _ssh_master \
    or 'GIT_SSH' in os.environ \
    or sys.platform in ('win32', 'cygwin'):
      # failed earlier, or cygwin ssh can't do this
      #
      return False

    # We will make two calls to ssh; this is the common part of both calls.
    command_base = ['ssh',
                     '-o','ControlPath %s' % ssh_sock(),
                     host]
    if port is not None:
      command_base[1:1] = ['-p', str(port)]

    # Since the key wasn't in _master_keys, we think that master isn't running.
    # ...but before actually starting a master, we'll double-check.  This can
    # be important because we can't tell that that '*****@*****.**' is the same
    # as 'myhost.com' where "User git" is setup in the user's ~/.ssh/config file.
    check_command = command_base + ['-O','check']
    try:
      Trace(': %s', ' '.join(check_command))
      check_process = subprocess.Popen(check_command,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
      check_process.communicate() # read output, but ignore it...
      isnt_running = check_process.wait()

      if not isnt_running:
        # Our double-check found that the master _was_ infact running.  Add to
        # the list of keys.
        _master_keys.add(key)
        return True
    except Exception:
      # Ignore excpetions.  We we will fall back to the normal command and print
      # to the log there.
      pass

    command = command_base[:1] + \
              ['-M', '-N'] + \
              command_base[1:]
    try:
      Trace(': %s', ' '.join(command))
      p = subprocess.Popen(command)
    except Exception as e:
      _ssh_master = False
      print('\nwarn: cannot enable ssh control master for %s:%s\n%s'
             % (host,port, str(e)), file=sys.stderr)
      return False

    _master_processes.append(p)
    _master_keys.add(key)
    time.sleep(1)
    return True
  finally:
    _master_keys_lock.release()
コード例 #8
0
def _open_ssh(host, port=None):
    global _ssh_master

    # Acquire the lock.  This is needed to prevent opening multiple masters for
    # the same host when we're running "repo sync -jN" (for N > 1) _and_ the
    # manifest <remote fetch="ssh://xyz"> specifies a different host from the
    # one that was passed to repo init.
    _master_keys_lock.acquire()
    try:

        # Check to see whether we already think that the master is running; if we
        # think it's already running, return right away.
        if port is not None:
            key = '%s:%s' % (host, port)
        else:
            key = host

        if key in _master_keys:
            return True

        if not _ssh_master \
        or 'GIT_SSH' in os.environ \
        or sys.platform in ('win32', 'cygwin'):
            # failed earlier, or cygwin ssh can't do this
            #
            return False

        # We will make two calls to ssh; this is the common part of both calls.
        command_base = [
            'ssh', '-o',
            'ControlPath %s' % ssh_sock(), '-o', 'StrictHostKeyChecking=no',
            host
        ]
        if port is not None:
            command_base[1:1] = ['-p', str(port)]

        # Since the key wasn't in _master_keys, we think that master isn't running.
        # ...but before actually starting a master, we'll double-check.  This can
        # be important because we can't tell that that '*****@*****.**' is the same
        # as 'myhost.com' where "User git" is setup in the user's ~/.ssh/config file.
        check_command = command_base + ['-O', 'check']
        try:
            Trace(': %s', ' '.join(check_command))
            check_process = subprocess.Popen(check_command,
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.PIPE)
            check_process.communicate()  # read output, but ignore it...
            isnt_running = check_process.wait()

            if not isnt_running:
                # Our double-check found that the master _was_ infact running.  Add to
                # the list of keys.
                _master_keys.add(key)
                return True
        except Exception:
            # Ignore excpetions.  We we will fall back to the normal command and print
            # to the log there.
            pass

        command = command_base[:1] + \
                  ['-M', '-N'] + \
                  command_base[1:]
        try:
            Trace(': %s', ' '.join(command))
            p = subprocess.Popen(command)
        except Exception as e:
            _ssh_master = False
            print('\nwarn: cannot enable ssh control master for %s:%s\n%s' %
                  (host, port, str(e)),
                  file=sys.stderr)
            return False

        _master_processes.append(p)
        _master_keys.add(key)
        time.sleep(1)
        return True
    finally:
        _master_keys_lock.release()