Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 1 | #pylint: disable-msg=C0111 |
| 2 | |
Prashanth B | cf731e3 | 2014-08-10 18:03:57 -0700 | [diff] [blame^] | 3 | import cPickle |
| 4 | import logging |
| 5 | import os |
| 6 | import tempfile |
| 7 | import time |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 8 | import common |
| 9 | from autotest_lib.scheduler import drone_utility, email_manager |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 10 | from autotest_lib.client.bin import local_host |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import error, global_config, utils |
Prashanth B | f78a6fb | 2014-06-10 16:09:40 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib.cros.graphite import stats |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER', |
| 16 | 'drone_installation_directory') |
| 17 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 18 | class DroneUnreachable(Exception): |
| 19 | """The drone is non-sshable.""" |
| 20 | pass |
| 21 | |
| 22 | |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 23 | class _BaseAbstractDrone(object): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 24 | """ |
| 25 | Attributes: |
| 26 | * allowed_users: set of usernames allowed to use this drone. if None, |
| 27 | any user can use this drone. |
| 28 | """ |
Prashanth B | cf731e3 | 2014-08-10 18:03:57 -0700 | [diff] [blame^] | 29 | def __init__(self, timestamp_remote_calls=True): |
| 30 | """Instantiate an abstract drone. |
| 31 | |
| 32 | @param timestamp_remote_calls: If true, drone_utility is invoked with |
| 33 | the --call_time option and the current time. Currently this is only |
| 34 | used for testing. |
| 35 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 36 | self._calls = [] |
| 37 | self.hostname = None |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 38 | self.enabled = True |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 39 | self.max_processes = 0 |
| 40 | self.active_processes = 0 |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 41 | self.allowed_users = None |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 42 | self._autotest_install_dir = AUTOTEST_INSTALL_DIR |
| 43 | self._host = None |
Prashanth B | cf731e3 | 2014-08-10 18:03:57 -0700 | [diff] [blame^] | 44 | self.timestamp_remote_calls = timestamp_remote_calls |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def shutdown(self): |
| 48 | pass |
| 49 | |
| 50 | |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 51 | @property |
| 52 | def _drone_utility_path(self): |
| 53 | return os.path.join(self._autotest_install_dir, |
| 54 | 'scheduler', 'drone_utility.py') |
| 55 | |
| 56 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 57 | def used_capacity(self): |
jamesren | 37b5045 | 2010-03-25 20:38:56 +0000 | [diff] [blame] | 58 | """Gets the capacity used by this drone |
| 59 | |
| 60 | Returns a tuple of (percentage_full, -max_capacity). This is to aid |
| 61 | direct comparisons, so that a 0/10 drone is considered less heavily |
| 62 | loaded than a 0/2 drone. |
| 63 | |
| 64 | This value should never be used directly. It should only be used in |
| 65 | direct comparisons using the basic comparison operators, or using the |
| 66 | cmp() function. |
| 67 | """ |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 68 | if self.max_processes == 0: |
jamesren | 37b5045 | 2010-03-25 20:38:56 +0000 | [diff] [blame] | 69 | return (1.0, 0) |
| 70 | return (float(self.active_processes) / self.max_processes, |
| 71 | -self.max_processes) |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 72 | |
| 73 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 74 | def usable_by(self, user): |
| 75 | if self.allowed_users is None: |
| 76 | return True |
| 77 | return user in self.allowed_users |
| 78 | |
| 79 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 80 | def _execute_calls_impl(self, calls): |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 81 | if not self._host: |
| 82 | raise ValueError('Drone cannot execute calls without a host.') |
Prashanth B | cf731e3 | 2014-08-10 18:03:57 -0700 | [diff] [blame^] | 83 | drone_utility_cmd = self._drone_utility_path |
| 84 | if self.timestamp_remote_calls: |
| 85 | drone_utility_cmd = '%s --call_time %s' % ( |
| 86 | drone_utility_cmd, time.time()) |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 87 | logging.info("Running drone_utility on %s", self.hostname) |
Prashanth B | cf731e3 | 2014-08-10 18:03:57 -0700 | [diff] [blame^] | 88 | result = self._host.run('python %s' % drone_utility_cmd, |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 89 | stdin=cPickle.dumps(calls), stdout_tee=None, |
| 90 | connect_timeout=300) |
| 91 | try: |
| 92 | return cPickle.loads(result.stdout) |
| 93 | except Exception: # cPickle.loads can throw all kinds of exceptions |
| 94 | logging.critical('Invalid response:\n---\n%s\n---', result.stdout) |
| 95 | raise |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def _execute_calls(self, calls): |
Prashanth B | f78a6fb | 2014-06-10 16:09:40 -0700 | [diff] [blame] | 99 | stats.Gauge('drone_execute_call_count').send( |
| 100 | self.hostname.replace('.', '_'), len(calls)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 101 | return_message = self._execute_calls_impl(calls) |
| 102 | for warning in return_message['warnings']: |
| 103 | subject = 'Warning from drone %s' % self.hostname |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 104 | logging.warning(subject + '\n' + warning) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 105 | email_manager.manager.enqueue_notify_email(subject, warning) |
| 106 | return return_message['results'] |
| 107 | |
| 108 | |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 109 | def get_calls(self): |
| 110 | """Returns the calls queued against this drone. |
| 111 | |
| 112 | @return: A list of calls queued against the drone. |
| 113 | """ |
| 114 | return self._calls |
| 115 | |
| 116 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 117 | def call(self, method, *args, **kwargs): |
| 118 | return self._execute_calls( |
| 119 | [drone_utility.call(method, *args, **kwargs)]) |
| 120 | |
| 121 | |
| 122 | def queue_call(self, method, *args, **kwargs): |
| 123 | self._calls.append(drone_utility.call(method, *args, **kwargs)) |
| 124 | |
| 125 | def clear_call_queue(self): |
| 126 | self._calls = [] |
| 127 | |
| 128 | |
| 129 | def execute_queued_calls(self): |
| 130 | if not self._calls: |
| 131 | return |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 132 | results = self._execute_calls(self._calls) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 133 | self.clear_call_queue() |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 134 | return results |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 135 | |
| 136 | |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 137 | def set_autotest_install_dir(self, path): |
| 138 | pass |
| 139 | |
| 140 | |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 141 | SiteDrone = utils.import_site_class( |
| 142 | __file__, 'autotest_lib.scheduler.site_drones', |
| 143 | '_SiteAbstractDrone', _BaseAbstractDrone) |
| 144 | |
| 145 | |
| 146 | class _AbstractDrone(SiteDrone): |
| 147 | pass |
| 148 | |
| 149 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 150 | class _LocalDrone(_AbstractDrone): |
Prashanth B | cf731e3 | 2014-08-10 18:03:57 -0700 | [diff] [blame^] | 151 | def __init__(self, timestamp_remote_calls=True): |
| 152 | super(_LocalDrone, self).__init__( |
| 153 | timestamp_remote_calls=timestamp_remote_calls) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 154 | self.hostname = 'localhost' |
Prashanth B | 340fd1e | 2014-06-22 12:44:10 -0700 | [diff] [blame] | 155 | self._host = local_host.LocalHost() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 156 | self._drone_utility = drone_utility.DroneUtility() |
| 157 | |
| 158 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 159 | def send_file_to(self, drone, source_path, destination_path, |
| 160 | can_fail=False): |
| 161 | if drone.hostname == self.hostname: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 162 | self.queue_call('copy_file_or_directory', source_path, |
| 163 | destination_path) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 164 | else: |
| 165 | self.queue_call('send_file_to', drone.hostname, source_path, |
| 166 | destination_path, can_fail) |
| 167 | |
| 168 | |
| 169 | class _RemoteDrone(_AbstractDrone): |
Prashanth B | cf731e3 | 2014-08-10 18:03:57 -0700 | [diff] [blame^] | 170 | def __init__(self, hostname, timestamp_remote_calls=True): |
| 171 | super(_RemoteDrone, self).__init__( |
| 172 | timestamp_remote_calls=timestamp_remote_calls) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 173 | self.hostname = hostname |
| 174 | self._host = drone_utility.create_host(hostname) |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 175 | if not self._host.is_up(): |
| 176 | logging.error('Drone %s is unpingable, kicking out', hostname) |
| 177 | raise DroneUnreachable |
Eric Li | d656d56 | 2011-04-20 11:48:29 -0700 | [diff] [blame] | 178 | |
| 179 | |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame] | 180 | def set_autotest_install_dir(self, path): |
| 181 | self._autotest_install_dir = path |
| 182 | |
| 183 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 184 | def shutdown(self): |
| 185 | super(_RemoteDrone, self).shutdown() |
| 186 | self._host.close() |
| 187 | |
| 188 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 189 | def send_file_to(self, drone, source_path, destination_path, |
| 190 | can_fail=False): |
| 191 | if drone.hostname == self.hostname: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 192 | self.queue_call('copy_file_or_directory', source_path, |
| 193 | destination_path) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 194 | elif isinstance(drone, _LocalDrone): |
| 195 | drone.queue_call('get_file_from', self.hostname, source_path, |
| 196 | destination_path) |
| 197 | else: |
| 198 | self.queue_call('send_file_to', drone.hostname, source_path, |
| 199 | destination_path, can_fail) |
| 200 | |
| 201 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 202 | def get_drone(hostname): |
| 203 | """ |
| 204 | Use this factory method to get drone objects. |
| 205 | """ |
| 206 | if hostname == 'localhost': |
| 207 | return _LocalDrone() |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 208 | try: |
| 209 | return _RemoteDrone(hostname) |
| 210 | except DroneUnreachable: |
| 211 | return None |