summaryrefslogtreecommitdiff
path: root/scripts/scriptmgr.py
blob: 0eef052934ad69e390f21d6068cd3e308051939a (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#! /usr/bin/env python

"""Bulk start/stop of scripts.

Reads a bunch of config files and maps them to scripts, then handles those.

Config template:

    [scriptmgr]
    job_name = scriptmgr_cphdb5
    config_list = ~/random/conf/*.ini
    logfile = ~/log/%(job_name)s.log
    pidfile = ~/pid/%(job_name)s.pid
    #use_skylog = 1

    # defaults for services
    [DEFAULT]
    cwd = ~/
    args = -v

    # service descriptions

    [cube_dispatcher]
    script = cube_dispatcher.py

    [table_dispatcher]
    script = table_dispatcher.py

    [bulk_loader]
    script = bulk_loader.py

    [londiste]
    script = londiste.py
    args = replay

    [pgqadm]
    script = pgqadm.py
    args = ticker

    # services to be ignored

    [log_checker]
    disabled = 1
"""

import sys, os, signal, glob, ConfigParser, time

import pkgloader
pkgloader.require('skytools', '3.0')

import skytools

command_usage = """
%prog [options] INI CMD [subcmd args]

commands:
  start [-a | jobname ..]    start a job
  stop [-a | jobname ..]     stop a job
  restart [-a | jobname ..]  restart job(s)
  reload [-a | jobname ..]   send reload signal
  status
"""

def job_sort_cmp(j1, j2):
    d1 = j1['service'] + j1['job_name']
    d2 = j2['service'] + j2['job_name']
    if d1 < d2: return -1
    elif d1 > d2: return 1
    else: return 0

class ScriptMgr(skytools.DBScript):
    __doc__ = __doc__
    svc_list = []
    svc_map = {}
    config_list = []
    job_map = {}
    job_list = []
    def init_optparse(self, p = None):
        p = skytools.DBScript.init_optparse(self, p)
        p.add_option("-a", "--all", action="store_true", help="apply command to all jobs")
        p.set_usage(command_usage.strip())
        return p

    def load_jobs(self):
        self.svc_list = []
        self.svc_map = {}
        self.config_list = []

        # load services
        svc_list = self.cf.sections()
        svc_list.remove(self.service_name)
        for svc_name in svc_list:
            cf = self.cf.clone(svc_name)
            disabled = cf.getboolean('disabled', 0)
            defscript = None
            if disabled:
                defscript = '/disabled'
            svc = {
                'service': svc_name,
                'script': cf.getfile('script', defscript),
                'cwd': cf.getfile('cwd'),
                'disabled': cf.getboolean('disabled', 0),
                'args': cf.get('args', ''),
            }
            self.svc_list.append(svc)
            self.svc_map[svc_name] = svc

        # generate config list
        for tmp in self.cf.getlist('config_list'):
            tmp = os.path.expanduser(tmp)
            tmp = os.path.expandvars(tmp)
            for fn in glob.glob(tmp):
                self.config_list.append(fn)

        # read jobs
        for fn in self.config_list:
            raw = ConfigParser.SafeConfigParser({'job_name':'?', 'service_name':'?'})
            raw.read(fn)

            # skip its own config
            if raw.has_section(self.service_name):
                continue

            got = 0
            for sect in raw.sections():
                if sect in self.svc_map:
                    got = 1
                    self.add_job(fn, sect)
            if not got:
                self.log.warning('Cannot find service for %s' % fn)

    def add_job(self, cf_file, service_name):
        svc = self.svc_map[service_name]
        cf = skytools.Config(service_name, cf_file)
        disabled = svc['disabled']
        if not disabled:
            disabled = cf.getboolean('disabled', 0)
        job = {
            'disabled': disabled,
            'config': cf_file,
            'cwd': svc['cwd'],
            'script': svc['script'],
            'args': svc['args'],
            'service': svc['service'],
            'job_name': cf.get('job_name'),
            'pidfile': cf.getfile('pidfile', ''),
        }
        self.job_list.append(job)
        self.job_map[job['job_name']] = job

    def cmd_status(self):
        for job in self.job_list:
            os.chdir(job['cwd'])
            cf = skytools.Config(job['service'], job['config'])
            pidfile = cf.getfile('pidfile', '')
            name = job['job_name']
            svc = job['service']
            if job['disabled']:
                name += "  (disabled)"

            if not pidfile:
                print(" pidfile? [%s] %s" % (svc, name))
            elif os.path.isfile(pidfile):
                print(" OK       [%s] %s" % (svc, name))
            else:
                print(" STOPPED  [%s] %s" % (svc, name))

    def cmd_info(self):
        for job in self.job_list:
            print(job)

    def cmd_start(self, job_name):
        if job_name not in self.job_map:
            self.log.error('Unknown job: '+job_name)
            return 1
        job = self.job_map[job_name]
        if job['disabled']:
            self.log.info("Skipping %s" % job_name)
            return 0
        self.log.info('Starting %s' % job_name)
        os.chdir(job['cwd'])
        pidfile = job['pidfile']
        if not pidfile:
            self.log.warning("No pidfile for %s cannot launch")
            return 0
        if os.path.isfile(pidfile):
            if skytools.signal_pidfile(pidfile, 0):
                self.log.warning("Script %s seems running" % job_name)
                return 0
            else:
                self.log.info("Ignoring stale pidfile for %s" % job_name)
        cmd = "%(script)s %(config)s %(args)s -d" % job
        res = os.system(cmd)
        self.log.debug(res)
        if res != 0:
            self.log.error('startup failed: %s' % job_name)
            return 1
        else:
            return 0

    def cmd_stop(self, job_name):
        if job_name not in self.job_map:
            self.log.error('Unknown job: '+job_name)
            return
        job = self.job_map[job_name]
        if job['disabled']:
            self.log.info("Skipping %s" % job_name)
            return
        self.log.info('Stopping %s' % job_name)
        self.signal_job(job, signal.SIGINT)

    def cmd_reload(self, job_name):
        if job_name not in self.job_map:
            self.log.error('Unknown job: '+job_name)
            return
        job = self.job_map[job_name]
        if job['disabled']:
            self.log.info("Skipping %s" % job_name)
            return
        self.log.info('Reloading %s' % job_name)
        self.signal_job(job, signal.SIGHUP)

    def signal_job(self, job, sig):
        os.chdir(job['cwd'])
        pidfile = job['pidfile']
        if not pidfile:
            self.log.warning("No pidfile for %s (%s)" % (job['job_name'], job['config']))
            return
        if os.path.isfile(pidfile):
            pid = int(open(pidfile).read())
            try:
                os.kill(pid, sig)
            except Exception, det:
                self.log.warning("Signaling %s failed: %s" % (job['job_name'], str(det)))
        else:
            self.log.warning("Job %s not running" % job['job_name'])

    def work(self):
        self.set_single_loop(1)
        self.job_list = []
        self.job_map = {}
        self.load_jobs()

        if len(self.args) < 2:
            print("need command")
            sys.exit(1)

        jobs = self.args[2:]
        if len(jobs) == 0 and self.options.all:
            for job in self.job_list:
                jobs.append(job['job_name'])

        self.job_list.sort(job_sort_cmp)

        cmd = self.args[1]
        if cmd == "status":
            self.cmd_status()
            return
        elif cmd == "info":
            self.cmd_info()
            return

        if len(jobs) == 0:
            print("no jobs given?")
            sys.exit(1)

        if cmd == "start":
            err = 0
            for n in jobs:
                err += self.cmd_start(n)
            if err > 0:
                self.log.error('some scripts failed')
                sys.exit(1)
        elif cmd == "stop":
            for n in jobs:
                self.cmd_stop(n)
        elif cmd == "restart":
            for n in jobs:
                self.cmd_stop(n)
                time.sleep(2)
                self.cmd_start(n)
        elif cmd == "reload":
            for n in jobs:
                self.cmd_reload(n)
        else:
            print("unknown command: " + cmd)
            sys.exit(1)

if __name__ == '__main__':
    script = ScriptMgr('scriptmgr', sys.argv[1:])
    script.start()