summaryrefslogtreecommitdiff
path: root/zuul/cmd/scheduler.py
blob: 7748a80a7def50c41d3dca6b2bdbc59c2362c4bf (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
# Copyright 2012 Hewlett-Packard Development Company, L.P.
# Copyright 2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import logging
import os
import sys
import signal

import zuul.cmd
from zuul.lib.config import get_default
from zuul.lib.statsd import get_statsd_config
import zuul.scheduler

# No zuul imports here because they pull in paramiko which must not be
# imported until after the daemonization.
# https://github.com/paramiko/paramiko/issues/59
# Similar situation with gear and statsd.


class Scheduler(zuul.cmd.ZuulDaemonApp):
    app_name = 'scheduler'
    app_description = 'The main zuul process.'

    def __init__(self):
        super(Scheduler, self).__init__()
        self.gear_server_pid = None

    def createParser(self):
        parser = super(Scheduler, self).createParser()
        parser.add_argument('command',
                            choices=zuul.scheduler.COMMANDS,
                            nargs='?')
        return parser

    def parseArguments(self, args=None):
        super(Scheduler, self).parseArguments()
        if self.args.command:
            self.args.nodaemon = True

    def reconfigure_handler(self, signum, frame):
        signal.signal(signal.SIGHUP, signal.SIG_IGN)
        self.log.debug("Reconfiguration triggered")
        self.readConfig()
        self.setup_logging('scheduler', 'log_config')
        try:
            self.sched.reconfigure(self.config)
        except Exception:
            self.log.exception("Reconfiguration failed:")
        signal.signal(signal.SIGHUP, self.reconfigure_handler)

    def exit_handler(self, signum, frame):
        self.sched.exit()
        self.sched.join()
        self.stop_gear_server()
        sys.exit(0)

    def start_gear_server(self):
        pipe_read, pipe_write = os.pipe()
        child_pid = os.fork()
        if child_pid == 0:
            os.close(pipe_write)
            self.setup_logging('gearman_server', 'log_config')
            import zuul.lib.gearserver

            (statsd_host, statsd_port, statsd_prefix) = get_statsd_config(
                self.config)
            if statsd_prefix:
                statsd_prefix += '.zuul.geard'
            else:
                statsd_prefix = 'zuul.geard'

            host = get_default(self.config, 'gearman_server', 'listen_address')
            port = int(get_default(self.config, 'gearman_server', 'port',
                                   4730))
            ssl_key = get_default(self.config, 'gearman_server', 'ssl_key')
            ssl_cert = get_default(self.config, 'gearman_server', 'ssl_cert')
            ssl_ca = get_default(self.config, 'gearman_server', 'ssl_ca')
            zuul.lib.gearserver.GearServer(port,
                                           ssl_key=ssl_key,
                                           ssl_cert=ssl_cert,
                                           ssl_ca=ssl_ca,
                                           host=host,
                                           statsd_host=statsd_host,
                                           statsd_port=statsd_port,
                                           statsd_prefix=statsd_prefix)

            # Keep running until the parent dies:
            pipe_read = os.fdopen(pipe_read)
            pipe_read.read()
            os._exit(0)
        else:
            os.close(pipe_read)
            self.gear_server_pid = child_pid
            self.gear_pipe_write = pipe_write

    def stop_gear_server(self):
        if self.gear_server_pid:
            os.kill(self.gear_server_pid, signal.SIGKILL)

    def run(self):
        # See comment at top of file about zuul imports
        import zuul.scheduler
        if self.args.command in zuul.scheduler.COMMANDS:
            self.send_command(self.args.command)
            sys.exit(0)
        # See comment at top of file about zuul imports
        import zuul.executor.client
        import zuul.merger.client
        import zuul.nodepool
        import zuul.webapp
        import zuul.zk

        if (self.config.has_option('gearman_server', 'start') and
            self.config.getboolean('gearman_server', 'start')):
            self.start_gear_server()

        self.setup_logging('scheduler', 'log_config')
        self.log = logging.getLogger("zuul.Scheduler")

        self.sched = zuul.scheduler.Scheduler(self.config)

        gearman = zuul.executor.client.ExecutorClient(self.config, self.sched)
        merger = zuul.merger.client.MergeClient(self.config, self.sched)
        nodepool = zuul.nodepool.Nodepool(self.sched)

        zookeeper = zuul.zk.ZooKeeper()
        zookeeper_hosts = get_default(self.config, 'zookeeper',
                                      'hosts', '127.0.0.1:2181')
        zookeeper_timeout = float(get_default(self.config, 'zookeeper',
                                              'session_timeout', 10.0))

        zookeeper.connect(zookeeper_hosts, timeout=zookeeper_timeout)

        cache_expiry = get_default(self.config, 'webapp', 'status_expiry', 1)
        listen_address = get_default(self.config, 'webapp', 'listen_address',
                                     '0.0.0.0')
        port = get_default(self.config, 'webapp', 'port', 8001)

        webapp = zuul.webapp.WebApp(
            self.sched, port=port, cache_expiry=cache_expiry,
            listen_address=listen_address)

        self.configure_connections()
        self.sched.setExecutor(gearman)
        self.sched.setMerger(merger)
        self.sched.setNodepool(nodepool)
        self.sched.setZooKeeper(zookeeper)

        self.log.info('Starting scheduler')
        try:
            self.sched.start()
            self.sched.registerConnections(self.connections, webapp)
            self.sched.reconfigure(self.config)
            self.sched.resume()
        except Exception:
            self.log.exception("Error starting Zuul:")
            # TODO(jeblair): If we had all threads marked as daemon,
            # we might be able to have a nicer way of exiting here.
            sys.exit(1)
        self.log.info('Starting Webapp')
        webapp.start()

        signal.signal(signal.SIGHUP, self.reconfigure_handler)

        if self.args.nodaemon:
            signal.signal(signal.SIGTERM, self.exit_handler)
            while True:
                try:
                    signal.pause()
                except KeyboardInterrupt:
                    print("Ctrl + C: asking scheduler to exit nicely...\n")
                    self.exit_handler(signal.SIGINT, None)
        else:
            self.sched.join()


def main():
    Scheduler().main()


if __name__ == "__main__":
    main()