Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: telemetry/telemetry/internal/forwarders/cros_forwarder.py

Issue 2982743002: webrtc: Fix missing port forwarding on CrOS (Closed)
Patch Set: webrtc: Fix missing port forwarding on CrOS and android Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import logging 5 import logging
6 import re
6 import subprocess 7 import subprocess
8 import tempfile
7 9
8 from telemetry.internal import forwarders 10 from telemetry.internal import forwarders
9 from telemetry.internal.forwarders import do_nothing_forwarder 11 from telemetry.internal.forwarders import do_nothing_forwarder
10 12
11 import py_utils 13 import py_utils
12 14
13 15
14 class CrOsForwarderFactory(forwarders.ForwarderFactory): 16 class CrOsForwarderFactory(forwarders.ForwarderFactory):
15 17
16 def __init__(self, cri): 18 def __init__(self, cri):
17 super(CrOsForwarderFactory, self).__init__() 19 super(CrOsForwarderFactory, self).__init__()
18 self._cri = cri 20 self._cri = cri
19 21
20 # pylint: disable=arguments-differ 22 # pylint: disable=arguments-differ
21 def Create(self, port_pair, use_remote_port_forwarding=True): 23 def Create(self, port_pair, use_remote_port_forwarding=True):
22 if self._cri.local: 24 if self._cri.local:
23 return do_nothing_forwarder.DoNothingForwarder(port_pair) 25 return do_nothing_forwarder.DoNothingForwarder(port_pair)
24 return CrOsSshForwarder(self._cri, use_remote_port_forwarding, port_pair) 26 return CrOsSshForwarder(self._cri, use_remote_port_forwarding, port_pair)
25 27
26 28
27 class CrOsSshForwarder(forwarders.Forwarder): 29 class CrOsSshForwarder(forwarders.Forwarder):
28 30
29 def __init__(self, cri, use_remote_port_forwarding, port_pair): 31 def __init__(self, cri, use_remote_port_forwarding, port_pair):
30 super(CrOsSshForwarder, self).__init__(port_pair) 32 super(CrOsSshForwarder, self).__init__(port_pair)
31 self._cri = cri 33 self._cri = cri
32 self._proc = None 34 self._proc = None
35 self._remote_port = None
33 forwarding_args = self._ForwardingArgs( 36 forwarding_args = self._ForwardingArgs(
34 use_remote_port_forwarding, self.host_ip, port_pair) 37 use_remote_port_forwarding, self.host_ip, port_pair)
38 err_file = tempfile.NamedTemporaryFile()
35 self._proc = subprocess.Popen( 39 self._proc = subprocess.Popen(
36 self._cri.FormSSHCommandLine(['sleep', '999999999'], forwarding_args), 40 self._cri.FormSSHCommandLine(['-NT'], forwarding_args,
41 port_forward=use_remote_port_forwarding),
37 stdout=subprocess.PIPE, 42 stdout=subprocess.PIPE,
38 stderr=subprocess.PIPE, 43 stderr=err_file,
39 stdin=subprocess.PIPE, 44 stdin=subprocess.PIPE,
40 shell=False) 45 shell=False)
46 def _get_remote_port(err_file):
47 # When we specify the remote port '0' in ssh remote port forwarding,
48 # the remote ssh server should return the port it binds to in stderr.
49 # e.g. 'Allocated port 42360 for remote forward to localhost:12345',
50 # the port 42360 is the port created remotely and the traffic to the
51 # port will be relayed to localhost port 12345.
52 line = err_file.readline()
53 tokens = re.search(r'port (\d+) for remote forward to', line)
54 if tokens:
55 self._remote_port = int(tokens.group(1))
56 return tokens
57
58 if use_remote_port_forwarding and port_pair.remote_port == 0:
59 with open(err_file.name, 'r') as err_file_reader:
60 py_utils.WaitFor(lambda: _get_remote_port(err_file_reader), 60)
61
41 py_utils.WaitFor( 62 py_utils.WaitFor(
42 lambda: self._cri.IsHTTPServerRunningOnPort(self.host_port), 60) 63 lambda: self._cri.IsHTTPServerRunningOnPort(self.host_port), 60)
64 err_file.close()
43 logging.debug('Server started on %s:%d', self.host_ip, self.host_port) 65 logging.debug('Server started on %s:%d', self.host_ip, self.host_port)
44 66
45 # pylint: disable=unused-argument 67 # pylint: disable=unused-argument
46 @staticmethod 68 @staticmethod
47 def _ForwardingArgs(use_remote_port_forwarding, host_ip, port_pair): 69 def _ForwardingArgs(use_remote_port_forwarding, host_ip, port_pair):
48 if use_remote_port_forwarding: 70 if use_remote_port_forwarding:
49 arg_format = '-R{remote_port}:{host_ip}:{local_port}' 71 arg_format = '-R{remote_port}:{host_ip}:{local_port}'
50 else: 72 else:
51 arg_format = '-L{local_port}:{host_ip}:{remote_port}' 73 arg_format = '-L{local_port}:{host_ip}:{remote_port}'
52 return [arg_format.format(host_ip=host_ip, 74 return [arg_format.format(host_ip=host_ip,
53 local_port=port_pair.local_port, 75 local_port=port_pair.local_port,
54 remote_port=port_pair.remote_port)] 76 remote_port=port_pair.remote_port)]
55 77
56 @property 78 @property
57 def host_port(self): 79 def host_port(self):
80 # Return remote port if it is resolved remotely.
81 if self._remote_port:
82 return self._remote_port
58 return self._port_pair.remote_port 83 return self._port_pair.remote_port
59 84
60 def Close(self): 85 def Close(self):
61 if self._proc: 86 if self._proc:
62 self._proc.kill() 87 self._proc.kill()
63 self._proc = None 88 self._proc = None
64 super(CrOsSshForwarder, self).Close() 89 super(CrOsSshForwarder, self).Close()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698