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

Side by Side Diff: py/utils/ssh_utils.py

Issue 341193004: Add lots of utils, PRESUBMIT.py (Closed) Base URL: https://skia.googlesource.com/common.git@master
Patch Set: Address comments Created 6 years, 6 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
« no previous file with comments | « py/utils/shell_utils.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """ This module contains tools related to ssh used by the buildbot scripts. """
7
8 import atexit
9 import os
10 import re
11 import shell_utils
12 import signal
13
14 def PutSCP(local_path, remote_path, username, host, port, recurse=False,
15 options=None):
16 """ Send a file to the given host over SCP. Assumes that public key
17 authentication is set up between the client and server.
18
19 local_path: path to the file to send on the client
20 remote_path: destination path for the file on the server
21 username: ssh login name
22 host: hostname or ip address of the server
23 port: port on the server to use
24 recurse: boolean indicating whether to transmit everything in a folder
25 options: list of extra options to pass to scp
26 """
27 # TODO(borenet): This will hang for a while if the host does not recognize
28 # the client.
29 cmd = ['scp']
30 if options:
31 cmd.extend(options)
32 if recurse:
33 cmd.append('-r')
34 cmd.extend(
35 ['-P', port, local_path, '%s@%s:%s' % (username, host, remote_path)])
36 shell_utils.run(cmd)
37
38
39 def MultiPutSCP(local_paths, remote_path, username, host, port, options=None):
40 """ Send files to the given host over SCP. Assumes that public key
41 authentication is set up between the client and server.
42
43 local_paths: list of paths of files and directories to send on the client
44 remote_path: destination directory path on the server
45 username: ssh login name
46 host: hostname or ip address of the server
47 port: port on the server to use
48 options: list of extra options to pass to scp
49 """
50 # TODO(borenet): This will hang for a while if the host does not recognize
51 # the client.
52 cmd = ['scp']
53 if options:
54 cmd.extend(options)
55 cmd.extend(['-r', '-P', port])
56 cmd.extend(local_paths)
57 cmd.append('%s@%s:%s' % (username, host, remote_path))
58 shell_utils.run(cmd)
59
60
61 def GetSCP(local_path, remote_path, username, host, port, recurse=False,
62 options=None):
63 """ Retrieve a file from the given host over SCP. Assumes that public key
64 authentication is set up between the client and server.
65
66 local_path: destination path for the file on the client
67 remote_path: path to the file to retrieve on the server
68 username: ssh login name
69 host: hostname or ip address of the server
70 port: port on the server to use
71 recurse: boolean indicating whether to transmit everything in a folder
72 options: list of extra options to pass to scp
73 """
74 # TODO(borenet): This will hang for a while if the host does not recognize
75 # the client.
76 cmd = ['scp']
77 if options:
78 cmd.extend(options)
79 if recurse:
80 cmd.append('-r')
81 cmd.extend(
82 ['-P', port, '%s@%s:%s' % (username, host, remote_path), local_path])
83 shell_utils.run(cmd)
84
85
86 def RunSSHCmd(username, host, port, command, echo=True, options=None):
87 """ Login to the given host and run the given command.
88
89 username: ssh login name
90 host: hostname or ip address of the server
91 port: port on the server to use
92 command: (string) command to run on the server
93 options: list of extra options to pass to ssh
94 """
95 # TODO(borenet): This will hang for a while if the host does not recognize
96 # the client.
97 cmd = ['ssh']
98 if options:
99 cmd.extend(options)
100 cmd.extend(['-p', port, '%s@%s' % (username, host), command])
101 return shell_utils.run(cmd, echo=echo)
102
103
104 def ShellEscape(arg):
105 """ Escape a single argument for passing into a remote shell
106 """
107 arg = re.sub(r'(["\\])', r'\\\1', arg)
108 return '"%s"' % arg if re.search(r'[\' \t\r\n]', arg) else arg
109
110
111 def RunSSH(username, host, port, command, echo=True, options=None):
112 """ Login to the given host and run the given command.
113
114 username: ssh login name
115 host: hostname or ip address of the server
116 port: port on the server to use
117 command: command to run on the server in list format
118 options: list of extra options to pass to ssh
119 """
120 cmd = ' '.join(ShellEscape(arg) for arg in command)
121 return RunSSHCmd(username, host, port, cmd, echo=echo, options=options)
122
123
124 class SshDestination(object):
125 """ Convenience class to remember a host, port, and username.
126 Wraps the other functions in this module.
127 """
128 def __init__(self, host, port, username, options=None):
129 """
130 host - (string) hostname of the target
131 port - (string or int) sshd port on the target
132 username - (string) remote username
133 options - (list of strings) extra options to pass to ssh and scp.
134 """
135 self.host = host
136 self.port = str(port)
137 self.user = username
138 self.options = options
139
140 def Put(self, local_path, remote_path, recurse=False):
141 return PutSCP(local_path, remote_path, self.user, self.host,
142 self.port, recurse=recurse, options=self.options)
143
144 def MultiPut(self, local_paths, remote_path):
145 return MultiPutSCP(local_paths, remote_path, self.user, self.host,
146 self.port, options=self.options)
147
148 def Get(self, local_path, remote_path, recurse=False):
149 return GetSCP(local_path, remote_path, self.user,
150 self.host, self.port, recurse=recurse, options=self.options)
151
152 def RunCmd(self, command, echo=True):
153 return RunSSHCmd(self.user, self.host, self.port, command,
154 echo=echo, options=self.options)
155
156 def Run(self, command, echo=True):
157 return RunSSH(self.user, self.host, self.port, command,
158 echo=echo, options=self.options)
159
160
161 def search_within_string(input_string, pattern):
162 """Search for regular expression in a string.
163
164 input_string: (string) to be searched
165 pattern: (string) to be passed to re.compile, with a symbolic
166 group named 'return'.
167 default: what to return if no match
168
169 Returns a string or None
170 """
171 match = re.search(pattern, input_string)
172 return match.group('return') if match else None
173
174 def SSHAdd(key_file):
175 """ Call ssh-add, and call ssh-agent if necessary.
176 """
177 assert os.path.isfile(key_file)
178 try:
179 shell_utils.run(['ssh-add', key_file],
180 log_in_real_time=False)
181 return
182 except shell_utils.CommandFailedException:
183 ssh_agent_output = shell_utils.run(['ssh-agent', '-s'],
184 log_in_real_time=False)
185 if not ssh_agent_output:
186 raise Exception('ssh-agent did not print anything')
187 ssh_auth_sock = search_within_string(
188 ssh_agent_output, r'SSH_AUTH_SOCK=(?P<return>[^;]*);')
189 ssh_agent_pid = search_within_string(
190 ssh_agent_output, r'SSH_AGENT_PID=(?P<return>[^;]*);')
191 if not (ssh_auth_sock and ssh_agent_pid):
192 raise Exception('ssh-agent did not print meaningful data')
193 os.environ['SSH_AUTH_SOCK'] = ssh_auth_sock
194 os.environ['SSH_AGENT_PID'] = ssh_agent_pid
195 atexit.register(os.kill, int(ssh_agent_pid), signal.SIGTERM)
196 shell_utils.run(['ssh-add', key_file])
OLDNEW
« no previous file with comments | « py/utils/shell_utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698