summaryrefslogtreecommitdiff
path: root/scheduler/drones.py
blob: 14123e71b64056d3b572a528ae8f6be2af95fd59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pickle, os, tempfile
import common
from autotest_lib.scheduler import drone_utility, email_manager
from autotest_lib.client.common_lib import error, global_config


AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER',
                                                 'drone_installation_directory')

class _AbstractDrone(object):
    def __init__(self):
        self._calls = []
        self.hostname = None
        self.enabled = True
        self.max_processes = 0
        self.active_processes = 0


    def shutdown(self):
        pass


    def used_capacity(self):
        if self.max_processes == 0:
            return 1.0
        return float(self.active_processes) / self.max_processes


    def _execute_calls_impl(self, calls):
        raise NotImplementedError


    def _execute_calls(self, calls):
        return_message = self._execute_calls_impl(calls)
        for warning in return_message['warnings']:
            subject = 'Warning from drone %s' % self.hostname
            print subject + '\n' + warning
            email_manager.manager.enqueue_notify_email(subject, warning)
        return return_message['results']


    def call(self, method, *args, **kwargs):
        return self._execute_calls(
            [drone_utility.call(method, *args, **kwargs)])


    def queue_call(self, method, *args, **kwargs):
        self._calls.append(drone_utility.call(method, *args, **kwargs))

    def clear_call_queue(self):
        self._calls = []


    def execute_queued_calls(self):
        if not self._calls:
            return
        self._execute_calls(self._calls)
        self.clear_call_queue()


class _LocalDrone(_AbstractDrone):
    def __init__(self):
        super(_LocalDrone, self).__init__()
        self.hostname = 'localhost'
        self._drone_utility = drone_utility.DroneUtility()


    def _execute_calls_impl(self, calls):
        return self._drone_utility.execute_calls(calls)


    def send_file_to(self, drone, source_path, destination_path,
                     can_fail=False):
        if drone.hostname == self.hostname:
            self.queue_call('copy_file_or_directory', source_path,
                            destination_path)
        else:
            self.queue_call('send_file_to', drone.hostname, source_path,
                            destination_path, can_fail)


class _RemoteDrone(_AbstractDrone):
    _temporary_directory = None

    def __init__(self, hostname):
        super(_RemoteDrone, self).__init__()
        self.hostname = hostname
        self._host = drone_utility.create_host(hostname)
        self._drone_utility_path = os.path.join(AUTOTEST_INSTALL_DIR,
                                                'scheduler',
                                                'drone_utility.py')

        try:
            self._host.run('mkdir -p ' + self._temporary_directory,
                           timeout=10)
        except error.AutoservError:
            pass


    @classmethod
    def set_temporary_directory(cls, temporary_directory):
        cls._temporary_directory = temporary_directory


    def shutdown(self):
        super(_RemoteDrone, self).shutdown()
        self._host.close()


    def _execute_calls_impl(self, calls):
        calls_fd, calls_filename = tempfile.mkstemp(suffix='.pickled_calls')
        calls_file = os.fdopen(calls_fd, 'w+')
        pickle.dump(calls, calls_file)
        calls_file.flush()
        calls_file.seek(0)

        try:
            result = self._host.run('python %s' % self._drone_utility_path,
                                    stdin=calls_file, connect_timeout=300)
        finally:
            calls_file.close()
            os.remove(calls_filename)

        try:
            return pickle.loads(result.stdout)
        except Exception: # pickle.loads can throw all kinds of exceptions
            print 'Invalid response:\n---\n%s\n---' % result.stdout
            raise


    def send_file_to(self, drone, source_path, destination_path,
                     can_fail=False):
        if drone.hostname == self.hostname:
            self.queue_call('copy_file_or_directory', source_path,
                            destination_path)
        elif isinstance(drone, _LocalDrone):
            drone.queue_call('get_file_from', self.hostname, source_path,
                             destination_path)
        else:
            self.queue_call('send_file_to', drone.hostname, source_path,
                            destination_path, can_fail)


def set_temporary_directory(temporary_directory):
    _RemoteDrone.set_temporary_directory(temporary_directory)


def get_drone(hostname):
    """
    Use this factory method to get drone objects.
    """
    if hostname == 'localhost':
        return _LocalDrone()
    return _RemoteDrone(hostname)