jadmanski | 093a068 | 2009-10-13 14:55:43 +0000 | [diff] [blame] | 1 | import cPickle, os, tempfile, logging |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2 | import common |
| 3 | from autotest_lib.scheduler import drone_utility, email_manager |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 4 | from autotest_lib.client.common_lib import error, global_config |
| 5 | |
| 6 | |
| 7 | AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER', |
| 8 | 'drone_installation_directory') |
| 9 | |
| 10 | class _AbstractDrone(object): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 11 | """ |
| 12 | Attributes: |
| 13 | * allowed_users: set of usernames allowed to use this drone. if None, |
| 14 | any user can use this drone. |
| 15 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 16 | def __init__(self): |
| 17 | self._calls = [] |
| 18 | self.hostname = None |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 19 | self.enabled = True |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 20 | self.max_processes = 0 |
| 21 | self.active_processes = 0 |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 22 | self.allowed_users = None |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def shutdown(self): |
| 26 | pass |
| 27 | |
| 28 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 29 | def used_capacity(self): |
jamesren | 37b5045 | 2010-03-25 20:38:56 +0000 | [diff] [blame] | 30 | """Gets the capacity used by this drone |
| 31 | |
| 32 | Returns a tuple of (percentage_full, -max_capacity). This is to aid |
| 33 | direct comparisons, so that a 0/10 drone is considered less heavily |
| 34 | loaded than a 0/2 drone. |
| 35 | |
| 36 | This value should never be used directly. It should only be used in |
| 37 | direct comparisons using the basic comparison operators, or using the |
| 38 | cmp() function. |
| 39 | """ |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 40 | if self.max_processes == 0: |
jamesren | 37b5045 | 2010-03-25 20:38:56 +0000 | [diff] [blame] | 41 | return (1.0, 0) |
| 42 | return (float(self.active_processes) / self.max_processes, |
| 43 | -self.max_processes) |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 44 | |
| 45 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 46 | def usable_by(self, user): |
| 47 | if self.allowed_users is None: |
| 48 | return True |
| 49 | return user in self.allowed_users |
| 50 | |
| 51 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 52 | def _execute_calls_impl(self, calls): |
| 53 | raise NotImplementedError |
| 54 | |
| 55 | |
| 56 | def _execute_calls(self, calls): |
| 57 | return_message = self._execute_calls_impl(calls) |
| 58 | for warning in return_message['warnings']: |
| 59 | subject = 'Warning from drone %s' % self.hostname |
showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 60 | logging.warn(subject + '\n' + warning) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 61 | email_manager.manager.enqueue_notify_email(subject, warning) |
| 62 | return return_message['results'] |
| 63 | |
| 64 | |
| 65 | def call(self, method, *args, **kwargs): |
| 66 | return self._execute_calls( |
| 67 | [drone_utility.call(method, *args, **kwargs)]) |
| 68 | |
| 69 | |
| 70 | def queue_call(self, method, *args, **kwargs): |
| 71 | self._calls.append(drone_utility.call(method, *args, **kwargs)) |
| 72 | |
| 73 | def clear_call_queue(self): |
| 74 | self._calls = [] |
| 75 | |
| 76 | |
| 77 | def execute_queued_calls(self): |
| 78 | if not self._calls: |
| 79 | return |
| 80 | self._execute_calls(self._calls) |
| 81 | self.clear_call_queue() |
| 82 | |
| 83 | |
showard | ac5b000 | 2009-10-19 18:34:00 +0000 | [diff] [blame] | 84 | def set_autotest_install_dir(self, path): |
| 85 | pass |
| 86 | |
| 87 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 88 | class _LocalDrone(_AbstractDrone): |
| 89 | def __init__(self): |
| 90 | super(_LocalDrone, self).__init__() |
| 91 | self.hostname = 'localhost' |
| 92 | self._drone_utility = drone_utility.DroneUtility() |
| 93 | |
| 94 | |
| 95 | def _execute_calls_impl(self, calls): |
| 96 | return self._drone_utility.execute_calls(calls) |
| 97 | |
| 98 | |
| 99 | def send_file_to(self, drone, source_path, destination_path, |
| 100 | can_fail=False): |
| 101 | if drone.hostname == self.hostname: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 102 | self.queue_call('copy_file_or_directory', source_path, |
| 103 | destination_path) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 104 | else: |
| 105 | self.queue_call('send_file_to', drone.hostname, source_path, |
| 106 | destination_path, can_fail) |
| 107 | |
| 108 | |
| 109 | class _RemoteDrone(_AbstractDrone): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 110 | def __init__(self, hostname): |
| 111 | super(_RemoteDrone, self).__init__() |
| 112 | self.hostname = hostname |
| 113 | self._host = drone_utility.create_host(hostname) |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame] | 114 | self._autotest_install_dir = AUTOTEST_INSTALL_DIR |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 115 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 116 | |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame] | 117 | def set_autotest_install_dir(self, path): |
| 118 | self._autotest_install_dir = path |
| 119 | |
| 120 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 121 | def shutdown(self): |
| 122 | super(_RemoteDrone, self).shutdown() |
| 123 | self._host.close() |
| 124 | |
| 125 | |
| 126 | def _execute_calls_impl(self, calls): |
jadmanski | 093a068 | 2009-10-13 14:55:43 +0000 | [diff] [blame] | 127 | logging.info("Running drone_utility on %s", self.hostname) |
showard | 202343e | 2009-10-14 16:20:24 +0000 | [diff] [blame] | 128 | drone_utility_path = os.path.join(self._autotest_install_dir, |
| 129 | 'scheduler', 'drone_utility.py') |
| 130 | result = self._host.run('python %s' % drone_utility_path, |
| 131 | stdin=cPickle.dumps(calls), connect_timeout=300) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 132 | |
| 133 | try: |
jadmanski | 093a068 | 2009-10-13 14:55:43 +0000 | [diff] [blame] | 134 | return cPickle.loads(result.stdout) |
| 135 | except Exception: # cPickle.loads can throw all kinds of exceptions |
showard | f098ebd | 2009-06-10 00:13:33 +0000 | [diff] [blame] | 136 | logging.critical('Invalid response:\n---\n%s\n---', result.stdout) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 137 | raise |
| 138 | |
| 139 | |
| 140 | def send_file_to(self, drone, source_path, destination_path, |
| 141 | can_fail=False): |
| 142 | if drone.hostname == self.hostname: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 143 | self.queue_call('copy_file_or_directory', source_path, |
| 144 | destination_path) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 145 | elif isinstance(drone, _LocalDrone): |
| 146 | drone.queue_call('get_file_from', self.hostname, source_path, |
| 147 | destination_path) |
| 148 | else: |
| 149 | self.queue_call('send_file_to', drone.hostname, source_path, |
| 150 | destination_path, can_fail) |
| 151 | |
| 152 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 153 | def get_drone(hostname): |
| 154 | """ |
| 155 | Use this factory method to get drone objects. |
| 156 | """ |
| 157 | if hostname == 'localhost': |
| 158 | return _LocalDrone() |
| 159 | return _RemoteDrone(hostname) |