| 1 |
# Copyright 2016 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 |
|
| 5 |
|
| 6 |
from recipe_engine import recipe_api |
| 7 |
|
| 8 |
from . import default |
| 9 |
|
| 10 |
|
| 11 |
"""SSH flavor, used for running code on a remote device via SSH. |
| 12 |
|
| 13 |
Must be subclassed to set self.device_dirs. The default implementation assumes |
| 14 |
a Linux-based device. |
| 15 |
""" |
| 16 |
|
| 17 |
|
| 18 |
class SSHFlavor(default.DefaultFlavor): |
| 19 |
|
| 20 |
def __init__(self, m, app_name): |
| 21 |
super(SSHFlavor, self).__init__(m, app_name) |
| 22 |
self._did_ssh_setup = False |
| 23 |
self._ssh_args = [] |
| 24 |
self._user_ip = 'root@variable_chromeos_device_hostname' |
| 25 |
|
| 26 |
def _ssh_setup(self): |
| 27 |
if self._did_ssh_setup: |
| 28 |
return |
| 29 |
|
| 30 |
tmp = self.m.path.mkdtemp('chromite') |
| 31 |
with self.m.context(cwd=tmp): |
| 32 |
chromite_url = 'https://chromium.googlesource.com/chromiumos/chromite.git' |
| 33 |
self.m.step('clone chromite', ['git', 'clone', chromite_url]) |
| 34 |
testing_rsa = tmp.joinpath('chromite', 'ssh_keys', 'testing_rsa') |
| 35 |
self.m.step('chmod 600 testing_rsa', ['chmod', '600', testing_rsa]) |
| 36 |
self._ssh_args = [ |
| 37 |
'-oConnectTimeout=30', '-oConnectionAttempts=4', |
| 38 |
'-oNumberOfPasswordPrompts=0', '-oProtocol=2', '-oServerAliveInterval=15', |
| 39 |
'-oServerAliveCountMax=8', '-oStrictHostKeyChecking=no', |
| 40 |
'-oUserKnownHostsFile=/dev/null', '-oIdentitiesOnly=yes', |
| 41 |
'-i', testing_rsa |
| 42 |
] |
| 43 |
|
| 44 |
self._did_ssh_setup = True |
| 45 |
|
| 46 |
@property |
| 47 |
def user_ip(self): |
| 48 |
return self._user_ip |
| 49 |
|
| 50 |
def ssh(self, title, *cmd, **kwargs): |
| 51 |
self._ssh_setup() |
| 52 |
|
| 53 |
if 'infra_step' not in kwargs: |
| 54 |
kwargs['infra_step'] = True |
| 55 |
|
| 56 |
ssh_cmd = ['ssh', '-oConnectTimeout=15', '-oBatchMode=yes', '-t', '-t'] + \ |
| 57 |
self._ssh_args + [self.user_ip] + list(cmd) |
| 58 |
|
| 59 |
return self._run(title, ssh_cmd, **kwargs) |
| 60 |
|
| 61 |
def ensure_device_dir(self, path): |
| 62 |
self.ssh('mkdir %s' % path, 'mkdir', '-p', path) |
| 63 |
|
| 64 |
def install(self): |
| 65 |
self.ensure_device_dir(self.device_dirs.resource_dir) |
| 66 |
if self.app_name: |
| 67 |
self.create_clean_device_dir(self.device_dirs.bin_dir) |
| 68 |
host_path = self.host_dirs.bin_dir.joinpath(self.app_name) |
| 69 |
device_path = self.device_path_join(self.device_dirs.bin_dir, self.app_name) |
| 70 |
self.copy_file_to_device(host_path, device_path) |
| 71 |
self.ssh('make %s executable' % self.app_name, 'chmod', '+x', device_path) |
| 72 |
|
| 73 |
def create_clean_device_dir(self, path): |
| 74 |
# use -f to silently return if path doesn't exist |
| 75 |
self.ssh('rm %s' % path, 'rm', '-rf', path) |
| 76 |
self.ensure_device_dir(path) |
| 77 |
|
| 78 |
def read_file_on_device(self, path, **kwargs): |
| 79 |
rv = self.ssh('read %s' % path, |
| 80 |
'cat', path, stdout=self.m.raw_io.output(), |
| 81 |
**kwargs) |
| 82 |
return rv.stdout.decode('utf-8').rstrip() if rv and rv.stdout else None |
| 83 |
|
| 84 |
def remove_file_on_device(self, path): |
| 85 |
# use -f to silently return if path doesn't exist |
| 86 |
self.ssh('rm %s' % path, 'rm', '-f', path) |
| 87 |
|
| 88 |
def scp_device_path(self, device_path): |
| 89 |
return '%s:%s' % (self.user_ip, device_path) |
| 90 |
|
| 91 |
def copy_file_to_device(self, host_path, device_path): |
| 92 |
device_path = self.scp_device_path(device_path) |
| 93 |
self._run('scp %s %s' % (host_path, device_path), |
| 94 |
['scp'] + self._ssh_args + [host_path, device_path], infra_step=True) |
| 95 |
|
| 96 |
# TODO(benjaminwagner): implement with rsync |
| 97 |
#def copy_directory_contents_to_device(self, host_path, device_path): |
| 98 |
|
| 99 |
# TODO(benjaminwagner): implement with rsync |
| 100 |
#def copy_directory_contents_to_host(self, device_path, host_path): |
| 101 |
|
| 102 |
def step(self, name, cmd, **kwargs): |
| 103 |
# Run cmd (installed above) |
| 104 |
cmd[0] = self.device_path_join(self.device_dirs.bin_dir, cmd[0]) |
| 105 |
self.ssh(str(name), *cmd) |