• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import subprocess
7
8from telemetry.internal import forwarders
9from telemetry.internal.forwarders import do_nothing_forwarder
10
11import py_utils
12
13
14class CrOsForwarderFactory(forwarders.ForwarderFactory):
15
16  def __init__(self, cri):
17    super(CrOsForwarderFactory, self).__init__()
18    self._cri = cri
19
20  # pylint: disable=arguments-differ
21  def Create(self, port_pair, use_remote_port_forwarding=True):
22    if self._cri.local:
23      return do_nothing_forwarder.DoNothingForwarder(port_pair)
24    return CrOsSshForwarder(self._cri, use_remote_port_forwarding, port_pair)
25
26
27class CrOsSshForwarder(forwarders.Forwarder):
28
29  def __init__(self, cri, use_remote_port_forwarding, port_pair):
30    super(CrOsSshForwarder, self).__init__(port_pair)
31    self._cri = cri
32    self._proc = None
33    forwarding_args = self._ForwardingArgs(
34        use_remote_port_forwarding, self.host_ip, port_pair)
35    self._proc = subprocess.Popen(
36        self._cri.FormSSHCommandLine(['sleep', '999999999'], forwarding_args),
37        stdout=subprocess.PIPE,
38        stderr=subprocess.PIPE,
39        stdin=subprocess.PIPE,
40        shell=False)
41    py_utils.WaitFor(
42        lambda: self._cri.IsHTTPServerRunningOnPort(self.host_port), 60)
43    logging.debug('Server started on %s:%d', self.host_ip, self.host_port)
44
45  # pylint: disable=unused-argument
46  @staticmethod
47  def _ForwardingArgs(use_remote_port_forwarding, host_ip, port_pair):
48    if use_remote_port_forwarding:
49      arg_format = '-R{remote_port}:{host_ip}:{local_port}'
50    else:
51      arg_format = '-L{local_port}:{host_ip}:{remote_port}'
52    return [arg_format.format(host_ip=host_ip,
53                              local_port=port_pair.local_port,
54                              remote_port=port_pair.remote_port)]
55
56  @property
57  def host_port(self):
58    return self._port_pair.remote_port
59
60  def Close(self):
61    if self._proc:
62      self._proc.kill()
63      self._proc = None
64    super(CrOsSshForwarder, self).Close()
65