1 # Copyright 2015 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.
5 """Defines the task controller library."""
17 #pylint: disable=relative-import
23 ISOLATE_PY
= os
.path
.join(common_lib
.SWARMING_DIR
, 'isolate.py')
24 SWARMING_PY
= os
.path
.join(common_lib
.SWARMING_DIR
, 'swarming.py')
27 class Error(Exception):
31 class ConnectionTimeoutError(Error
):
35 class TaskController(object):
36 """Provisions, configures, and controls a task machine.
38 This class is an abstraction of a physical task machine. It provides an
39 end to end API for controlling a task machine. Operations on the task machine
40 are performed using the instance's "rpc" property. A simple end to end
41 scenario is as follows:
43 task = TaskController(...)
45 task.WaitForConnection()
46 proc = task.rpc.subprocess.Popen(['ls'])
47 print task.rpc.subprocess.GetStdout(proc)
54 def __init__(self
, isolated_hash
, dimensions
, priority
=100,
55 idle_timeout_secs
=common_lib
.DEFAULT_TIMEOUT_SECS
,
56 connection_timeout_secs
=common_lib
.DEFAULT_TIMEOUT_SECS
,
57 verbosity
='ERROR', name
=None, run_id
=None):
58 assert isinstance(dimensions
, dict)
59 type(self
)._tasks
.append(self
)
60 type(self
)._task
_count
+= 1
61 self
.verbosity
= verbosity
62 self
._name
= name
or 'Task%d' % type(self
)._task
_count
63 self
._priority
= priority
64 self
._isolated
_hash
= isolated_hash
65 self
._idle
_timeout
_secs
= idle_timeout_secs
66 self
._dimensions
= dimensions
67 self
._connect
_event
= threading
.Event()
68 self
._connected
= False
69 self
._ip
_address
= None
70 self
._otp
= self
._CreateOTP
()
72 self
._output
_dir
= None
74 run_id
= run_id
or datetime
.datetime
.now().strftime('%Y-%m-%d-%H-%M-%S')
75 self
._task
_name
= '%s/%s/%s' % (
76 os
.path
.splitext(sys
.argv
[0])[0], self
._name
, run_id
)
78 parser
= argparse
.ArgumentParser()
79 parser
.add_argument('--isolate-server')
80 parser
.add_argument('--swarming-server')
81 parser
.add_argument('--task-connection-timeout-secs',
82 default
=common_lib
.DEFAULT_TIMEOUT_SECS
)
83 args
, _
= parser
.parse_known_args()
85 self
._isolate
_server
= args
.isolate_server
86 self
._swarming
_server
= args
.swarming_server
87 self
._connection
_timeout
_secs
= (connection_timeout_secs
or
88 args
.task_connection_timeout_secs
)
100 return self
._connected
103 def connect_event(self
):
104 return self
._connect
_event
112 return self
._verbosity
115 def verbosity(self
, level
):
116 """Sets the verbosity level as a string.
118 Either a string ('INFO', 'DEBUG', etc) or a logging level (logging.INFO,
119 logging.DEBUG, etc) is allowed.
121 assert isinstance(level
, (str, int))
122 if isinstance(level
, int):
123 level
= logging
.getLevelName(level
)
124 self
._verbosity
= level
#pylint: disable=attribute-defined-outside-init
127 def output_dir(self
):
128 if not self
._output
_dir
:
129 self
._output
_dir
= self
.rpc
.GetOutputDir()
130 return self
._output
_dir
133 def ReleaseAllTasks(cls
):
134 for task
in cls
._tasks
:
137 def Process(self
, cmd
, *args
, **kwargs
):
138 return process
.ControllerProcessWrapper(self
.rpc
, cmd
, *args
, **kwargs
)
140 def _CreateOTP(self
):
141 """Creates the OTP."""
142 controller_name
= socket
.gethostname()
143 test_name
= os
.path
.basename(sys
.argv
[0])
144 creation_time
= datetime
.datetime
.utcnow()
145 otp
= 'task:%s controller:%s test:%s creation:%s' % (
146 self
._name
, controller_name
, test_name
, creation_time
)
150 """Creates the task machine."""
151 logging
.info('Creating %s', self
.name
)
152 self
._connect
_event
.clear()
153 self
._ExecuteSwarming
()
155 def WaitForConnection(self
):
156 """Waits for the task machine to connect.
159 ConnectionTimeoutError if the task doesn't connect in time.
161 logging
.info('Waiting for %s to connect with a timeout of %d seconds',
162 self
._name
, self
._connection
_timeout
_secs
)
163 self
._connect
_event
.wait(self
._connection
_timeout
_secs
)
164 if not self
._connect
_event
.is_set():
165 raise ConnectionTimeoutError('%s failed to connect' % self
.name
)
168 """Quits the task's RPC server so it can release the machine."""
169 if self
._rpc
is not None and self
._connected
:
170 logging
.info('Copying output-dir files to controller')
171 self
.RetrieveOutputFiles()
172 logging
.info('Releasing %s', self
._name
)
175 except (socket
.error
, jsonrpclib
.Fault
):
176 logging
.error('Unable to connect to %s to call Quit', self
.name
)
178 self
._connected
= False
180 def _ExecuteSwarming(self
):
181 """Executes swarming.py."""
187 '--priority', str(self
._priority
),
188 '--task-name', self
._task
_name
,
191 if self
._isolate
_server
:
192 cmd
.extend(['--isolate-server', self
._isolate
_server
])
193 if self
._swarming
_server
:
194 cmd
.extend(['--swarming', self
._swarming
_server
])
195 for key
, value
in self
._dimensions
.iteritems():
196 cmd
.extend(['--dimension', key
, value
])
200 '--controller', common_lib
.MY_IP
,
202 '--verbosity', self
._verbosity
,
203 '--idle-timeout', str(self
._idle
_timeout
_secs
),
204 '--output-dir', '${ISOLATED_OUTDIR}'
207 self
._ExecuteProcess
(cmd
)
209 def _ExecuteProcess(self
, cmd
):
210 """Executes a process, waits for it to complete, and checks for success."""
211 logging
.debug('Running %s', ' '.join(cmd
))
212 p
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
213 _
, stderr
= p
.communicate()
214 if p
.returncode
!= 0:
217 def OnConnect(self
, ip_address
):
218 """Receives task ip address on connection."""
219 self
._ip
_address
= ip_address
220 self
._connected
= True
221 self
._rpc
= rpc_server
.RpcServer
.Connect(self
._ip
_address
)
222 logging
.info('%s connected from %s', self
._name
, ip_address
)
223 self
._connect
_event
.set()
225 def RetrieveOutputFiles(self
):
226 """Retrieves all files in the output-dir."""
227 files
= self
.rpc
.ListDir(self
.output_dir
)
229 remote_path
= self
.rpc
.PathJoin(self
.output_dir
, fname
)
230 local_name
= os
.path
.join(common_lib
.GetOutputDir(),
231 '%s.%s' % (self
.name
, fname
))
232 contents
= self
.rpc
.ReadFile(remote_path
)
233 with
open(local_name
, 'wb+') as fh
: