summaryrefslogtreecommitdiff
path: root/gear/cmd/geard.py
blob: dff28f8f9f78c78ab55492582b3fc6d79e16d662 (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
# 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 argparse
import daemon
import extras
import gear
import logging
import logging.config
import os
import pbr.version
import signal
import sys

from six.moves import configparser as ConfigParser


pid_file_module = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile'])


class Server(object):
    def __init__(self):
        self.args = None
        self.config = None
        self.gear_server_pid = None

    def parse_arguments(self):
        parser = argparse.ArgumentParser(description="""
Gearman server.

If the statsd python module is available, set STATSD_HOST,
STATSD_PORT, and STATSD_PREFIX environment variables for statsd
support.
""")
        parser.add_argument('-d', dest='nodaemon', action='store_true',
                            help='do not run as a daemon')
        parser.add_argument('-p', dest='port', default=4730,
                            help='port on which to listen')
        parser.add_argument('--log-config', dest='log_config',
                            help='logging config file')
        parser.add_argument('--pidfile', dest='pidfile',
                            default='/var/run/geard/geard.pid',
                            help='PID file')
        parser.add_argument('--ssl-ca', dest='ssl_ca', metavar='PATH',
                            help='path to CA certificate')
        parser.add_argument('--ssl-cert', dest='ssl_cert', metavar='PATH',
                            help='path to SSL public certificate')
        parser.add_argument('--ssl-key', dest='ssl_key', metavar='PATH',
                            help='path to SSL private key')
        parser.add_argument('--acl', dest='acl', metavar='PATH',
                            help='path to ACL file')
        parser.add_argument('--keepalive', dest='keepalive', default=False,
                            action='store_true',
                            help='enable TCP keepalives in socket')
        parser.add_argument('--keepalive-idle', dest='tcp_keepidle', type=int,
                            default=7200, action='store',
                            help='TCP keepalive idle time')
        parser.add_argument('--keepalive-interval', dest='tcp_keepintvl',
                            type=int, default=75, action='store',
                            help='TCP keepalive probe interval')
        parser.add_argument('--keepalive-count', dest='tcp_keepcnt', type=int,
                            default=9, action='store',
                            help='TCP keepalive probes count')
        parser.add_argument('--version', dest='version', action='store_true',
                            help='show version')
        self.args = parser.parse_args()

    def setup_logging(self):
        if self.args.log_config:
            if not os.path.exists(self.args.log_config):
                raise Exception("Unable to read logging config file at %s" %
                                self.args.log_config)
            logging.config.fileConfig(self.args.log_config)
        else:
            if self.args.nodaemon:
                logging.basicConfig(level=logging.DEBUG)
            else:
                logging.basicConfig(level=logging.INFO,
                                    filename="/var/log/geard/geard.log")

    def main(self):
        self.setup_logging()
        statsd_host = os.environ.get('STATSD_HOST')
        statsd_port = int(os.environ.get('STATSD_PORT', 8125))
        statsd_prefix = os.environ.get('STATSD_PREFIX')
        acl = None
        if self.args.acl:
            aclf = ConfigParser.RawConfigParser()
            aclf.read(self.args.acl)
            acl = gear.ACL()
            for section in aclf.sections():
                if aclf.has_option(section, 'register'):
                    register = aclf.get(section, 'register')
                else:
                    register = None
                if aclf.has_option(section, 'invoke'):
                    invoke = aclf.get(section, 'invoke')
                else:
                    invoke = None
                if aclf.has_option(section, 'grant'):
                    grant = aclf.getboolean(section, 'grant')
                else:
                    grant = None
                entry = gear.ACLEntry(section, register, invoke, grant)
                acl.add(entry)
        self.server = gear.Server(self.args.port,
                                  self.args.ssl_key,
                                  self.args.ssl_cert,
                                  self.args.ssl_ca,
                                  statsd_host,
                                  statsd_port,
                                  statsd_prefix,
                                  acl=acl,
                                  keepalive=self.args.keepalive,
                                  tcp_keepidle=self.args.tcp_keepidle,
                                  tcp_keepintvl=self.args.tcp_keepintvl,
                                  tcp_keepcnt=self.args.tcp_keepcnt
                                  )
        signal.pause()


def main():
    server = Server()
    server.parse_arguments()

    if server.args.version:
        vi = pbr.version.VersionInfo('gear')
        print("Gear version: {}".format(vi.version_string()))
        sys.exit(0)

    if server.args.nodaemon:
        server.main()
    else:
        pid = pid_file_module.TimeoutPIDLockFile(server.args.pidfile, 10)
        with daemon.DaemonContext(pidfile=pid):
            server.main()