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."""
18 #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
()
73 run_id
= run_id
or datetime
.datetime
.now().strftime('%Y-%m-%d-%H-%M-%S')
74 self
._task
_name
= '%s/%s/%s' % (
75 os
.path
.splitext(sys
.argv
[0])[0], self
._name
, run_id
)
77 parser
= argparse
.ArgumentParser()
78 parser
.add_argument('--isolate-server')
79 parser
.add_argument('--swarming-server')
80 parser
.add_argument('--task-connection-timeout-secs',
81 default
=common_lib
.DEFAULT_TIMEOUT_SECS
)
82 args
, _
= parser
.parse_known_args()
84 self
._isolate
_server
= args
.isolate_server
85 self
._swarming
_server
= args
.swarming_server
86 self
._connection
_timeout
_secs
= (connection_timeout_secs
or
87 args
.task_connection_timeout_secs
)
99 return self
._connected
102 def connect_event(self
):
103 return self
._connect
_event
111 return self
._verbosity
114 def verbosity(self
, level
):
115 """Sets the verbosity level as a string.
117 Either a string ('INFO', 'DEBUG', etc) or a logging level (logging.INFO,
118 logging.DEBUG, etc) is allowed.
120 assert isinstance(level
, (str, int))
121 if isinstance(level
, int):
122 level
= logging
.getLevelName(level
)
123 self
._verbosity
= level
#pylint: disable=attribute-defined-outside-init
126 def ReleaseAllTasks(cls
):
127 for task
in cls
._tasks
:
130 def Process(self
, cmd
, verbose
=False, detached
=False, cwd
=None):
131 return process
.ControllerProcessWrapper(
132 self
.rpc
, cmd
, verbose
, detached
, cwd
)
134 def _CreateOTP(self
):
135 """Creates the OTP."""
136 controller_name
= socket
.gethostname()
137 test_name
= os
.path
.basename(sys
.argv
[0])
138 creation_time
= datetime
.datetime
.utcnow()
139 otp
= 'task:%s controller:%s test:%s creation:%s' % (
140 self
._name
, controller_name
, test_name
, creation_time
)
144 """Creates the task machine."""
145 logging
.info('Creating %s', self
.name
)
146 self
._connect
_event
.clear()
147 self
._ExecuteSwarming
()
149 def WaitForConnection(self
):
150 """Waits for the task machine to connect.
153 ConnectionTimeoutError if the task doesn't connect in time.
155 logging
.info('Waiting for %s to connect with a timeout of %d seconds',
156 self
._name
, self
._connection
_timeout
_secs
)
157 self
._connect
_event
.wait(self
._connection
_timeout
_secs
)
158 if not self
._connect
_event
.is_set():
159 raise ConnectionTimeoutError('%s failed to connect' % self
.name
)
162 """Quits the task's RPC server so it can release the machine."""
163 if self
._rpc
is not None and self
._connected
:
164 logging
.info('Releasing %s', self
._name
)
167 except (socket
.error
, xmlrpclib
.Fault
):
168 logging
.error('Unable to connect to %s to call Quit', self
.name
)
170 self
._connected
= False
172 def _ExecuteSwarming(self
):
173 """Executes swarming.py."""
179 '--priority', str(self
._priority
),
180 '--task-name', self
._task
_name
,
183 if self
._isolate
_server
:
184 cmd
.extend(['--isolate-server', self
._isolate
_server
])
185 if self
._swarming
_server
:
186 cmd
.extend(['--swarming', self
._swarming
_server
])
187 for key
, value
in self
._dimensions
.iteritems():
188 cmd
.extend(['--dimension', key
, value
])
192 '--controller', common_lib
.MY_IP
,
194 '--verbosity', self
._verbosity
,
195 '--idle-timeout', str(self
._idle
_timeout
_secs
),
198 self
._ExecuteProcess
(cmd
)
200 def _ExecuteProcess(self
, cmd
):
201 """Executes a process, waits for it to complete, and checks for success."""
202 logging
.debug('Running %s', ' '.join(cmd
))
203 p
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
204 _
, stderr
= p
.communicate()
205 if p
.returncode
!= 0:
208 def OnConnect(self
, ip_address
):
209 """Receives task ip address on connection."""
210 self
._ip
_address
= ip_address
211 self
._connected
= True
212 self
._rpc
= ssl_util
.SslRpcServer
.Connect(self
._ip
_address
)
213 logging
.info('%s connected from %s', self
._name
, ip_address
)
214 self
._connect
_event
.set()