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

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

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

Powered by Google App Engine
This is Rietveld 408576698