• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6from recipe_engine import recipe_api
7
8import default
9
10
11"""SSH flavor, used for running code on a remote device via SSH.
12
13Must be subclassed to set self.device_dirs. The default implementation assumes
14a Linux-based device.
15"""
16
17
18class SSHFlavor(default.DefaultFlavor):
19
20  def __init__(self, m):
21    super(SSHFlavor, self).__init__(m)
22    self._user_ip = ''
23
24  @property
25  def user_ip(self):
26    if not self._user_ip:
27      path = self.m.path.expanduser('~/ssh_machine.json')
28      ssh_info = self.m.file.read_json('read ssh_machine.json', path,
29                                       test_data={'user_ip':'foo@127.0.0.1'})
30      self._user_ip = ssh_info.get(u'user_ip')
31    return self._user_ip
32
33  def ssh(self, title, *cmd, **kwargs):
34    if 'infra_step' not in kwargs:
35      kwargs['infra_step'] = True
36
37    ssh_cmd = ['ssh', '-oConnectTimeout=15', '-oBatchMode=yes',
38               '-t', '-t', self.user_ip] + list(cmd)
39
40    return self._run(title, ssh_cmd, **kwargs)
41
42  def ensure_device_dir(self, path):
43    self.ssh('mkdir %s' % path, 'mkdir', '-p', path)
44
45  def install(self):
46    self.ensure_device_dir(self.device_dirs.resource_dir)
47    self.create_clean_device_dir(self.device_dirs.bin_dir)
48
49  def create_clean_device_dir(self, path):
50    # use -f to silently return if path doesn't exist
51    self.ssh('rm %s' % path, 'rm', '-rf', path)
52    self.ensure_device_dir(path)
53
54  def read_file_on_device(self, path, **kwargs):
55    rv = self.ssh('read %s' % path,
56                   'cat', path, stdout=self.m.raw_io.output(),
57                   **kwargs)
58    return rv.stdout.rstrip() if rv and rv.stdout else None
59
60  def remove_file_on_device(self, path):
61    # use -f to silently return if path doesn't exist
62    self.ssh('rm %s' % path, 'rm', '-f', path)
63
64  def scp_device_path(self, device_path):
65    return '%s:%s' % (self.user_ip, device_path)
66
67  def copy_file_to_device(self, host_path, device_path):
68    device_path = self.scp_device_path(device_path)
69    self._run('scp %s %s' % (host_path, device_path),
70              ['scp', host_path, device_path], infra_step=True)
71
72  # TODO(benjaminwagner): implement with rsync
73  #def copy_directory_contents_to_device(self, host_path, device_path):
74
75  # TODO(benjaminwagner): implement with rsync
76  #def copy_directory_contents_to_host(self, device_path, host_path):
77
78  def step(self, name, cmd, **kwargs):
79    # Push and run cmd
80    host_path = self.host_dirs.bin_dir.join(cmd[0])
81    cmd[0] = self.device_path_join(self.device_dirs.bin_dir, cmd[0])
82    self.copy_file_to_device(host_path, cmd[0])
83
84    self.ssh('make %s executable' % name, 'chmod', '+x', cmd[0])
85    self.ssh(str(name), *cmd)
86