| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | # Copyright (C) 2007 Insecure.Com LLC. |
|---|
| 5 | # |
|---|
| 6 | # Author: Guilherme Polo <ggpolo@gmail.com> |
|---|
| 7 | # |
|---|
| 8 | # This program is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU General Public License as published by |
|---|
| 10 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This program is distributed in the hope that it will be useful, |
|---|
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | # GNU General Public License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU General Public License |
|---|
| 19 | # along with this program; if not, write to the Free Software |
|---|
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | |
|---|
| 22 | """ |
|---|
| 23 | Core Scheduler Controller |
|---|
| 24 | """ |
|---|
| 25 | |
|---|
| 26 | # If you want to start Scheduler at system startup, please, especify here |
|---|
| 27 | # umit config dir |
|---|
| 28 | # CONFIG_DIR = /home/myuser/.umit |
|---|
| 29 | CONFIG_DIR = '' |
|---|
| 30 | |
|---|
| 31 | import os |
|---|
| 32 | import sys |
|---|
| 33 | import signal |
|---|
| 34 | import subprocess |
|---|
| 35 | |
|---|
| 36 | from umitCore.I18N import _ |
|---|
| 37 | from umitCore.UmitLogging import log |
|---|
| 38 | from umitCore.Paths import Path |
|---|
| 39 | from umitCore.Utils import check_process |
|---|
| 40 | import umitCore.Scheduler as Scheduler |
|---|
| 41 | |
|---|
| 42 | HOME_CONF = None |
|---|
| 43 | RUNNING_FILE = None |
|---|
| 44 | |
|---|
| 45 | def setup_homedir(usethis): |
|---|
| 46 | """ |
|---|
| 47 | Setting umit home directory. |
|---|
| 48 | """ |
|---|
| 49 | Path.set_umit_conf(usethis, True) |
|---|
| 50 | |
|---|
| 51 | global HOME_CONF, RUNNING_FILE |
|---|
| 52 | |
|---|
| 53 | HOME_CONF = os.path.split(Path.get_umit_conf())[0] |
|---|
| 54 | RUNNING_FILE = os.path.join(HOME_CONF, 'schedrunning') |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | def usage(): |
|---|
| 58 | """ |
|---|
| 59 | Show help |
|---|
| 60 | """ |
|---|
| 61 | print _("Use: %s start|stop|cleanup <config_dir>" % (__file__, )) |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | def kill(pid): |
|---|
| 65 | """ |
|---|
| 66 | Kill a process. |
|---|
| 67 | """ |
|---|
| 68 | if sys.platform == "win32": |
|---|
| 69 | import win32gui |
|---|
| 70 | import win32api |
|---|
| 71 | import win32con |
|---|
| 72 | |
|---|
| 73 | handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid) |
|---|
| 74 | win32gui.SendMessage(handle, win32con.WM_CLOSE, 0, 0 ) # testing |
|---|
| 75 | win32api.TerminateProcess(handle, -1) |
|---|
| 76 | win32api.CloseHandle(handle) |
|---|
| 77 | |
|---|
| 78 | log.debug(">>> Process (%d) stopped." % pid) |
|---|
| 79 | else: |
|---|
| 80 | try: |
|---|
| 81 | os.kill(pid, signal.SIGTERM) |
|---|
| 82 | log.debug(">>> Process (%d) stopped." % pid) |
|---|
| 83 | except OSError: |
|---|
| 84 | log.debug(">>> Process already stopped, or this user didn't \ |
|---|
| 85 | start it.") |
|---|
| 86 | return -1 |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | def cleanup(): |
|---|
| 90 | """ |
|---|
| 91 | Do necessary cleanup. |
|---|
| 92 | """ |
|---|
| 93 | if os.path.isfile(RUNNING_FILE): # control file exists |
|---|
| 94 | pid = int(open(RUNNING_FILE, 'r').read()) |
|---|
| 95 | |
|---|
| 96 | log.debug(">>> Cleanup needed..") |
|---|
| 97 | log.debug(">>> Checking if Scheduler process with pid %d is still \ |
|---|
| 98 | running" % pid) |
|---|
| 99 | |
|---|
| 100 | if check_process(pid): |
|---|
| 101 | # process is running |
|---|
| 102 | log.debug(">>> Scheduler running, stopping it..") |
|---|
| 103 | ret = kill(pid) |
|---|
| 104 | if ret != -1: |
|---|
| 105 | os.remove(RUNNING_FILE) |
|---|
| 106 | |
|---|
| 107 | else: |
|---|
| 108 | # process is not running |
|---|
| 109 | log.debug(">>> Scheduler is stopped already, removing control \ |
|---|
| 110 | file..") |
|---|
| 111 | os.remove(RUNNING_FILE) |
|---|
| 112 | |
|---|
| 113 | log.debug(">>> Cleanup finished") |
|---|
| 114 | |
|---|
| 115 | else: |
|---|
| 116 | # control file is not present |
|---|
| 117 | log.debug(">>> No cleanup needed, you can start the Scheduler.") |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | def start(): |
|---|
| 121 | """ |
|---|
| 122 | Start scheduler |
|---|
| 123 | """ |
|---|
| 124 | if os.path.isfile(RUNNING_FILE): # control file exists |
|---|
| 125 | cleanup() |
|---|
| 126 | |
|---|
| 127 | process = subprocess.Popen([sys.executable, Scheduler.__file__, |
|---|
| 128 | sys.path[0], 'start', HOME_CONF]) |
|---|
| 129 | |
|---|
| 130 | pid = process.pid |
|---|
| 131 | |
|---|
| 132 | log.debug(">>> Starting Scheduler..") |
|---|
| 133 | log.debug(">>> Writing pid (%d) to %s" % (pid, RUNNING_FILE)) |
|---|
| 134 | |
|---|
| 135 | f_run = open(RUNNING_FILE, 'w') |
|---|
| 136 | f_run.write("%d" % pid) |
|---|
| 137 | f_run.close() |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | def stop(): |
|---|
| 141 | """ |
|---|
| 142 | Stop Scheduler process |
|---|
| 143 | """ |
|---|
| 144 | if not os.path.isfile(RUNNING_FILE): # control file doesn't exist |
|---|
| 145 | log.critical(">>> Control file for Scheduler is not available, so, \n\ |
|---|
| 146 | Scheduler shouldn't be running. Try starting it, or run this in cleanup mode.") |
|---|
| 147 | sys.exit(0) |
|---|
| 148 | |
|---|
| 149 | log.debug(">>> Stoping Scheduler") |
|---|
| 150 | |
|---|
| 151 | cleanup() |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | if __name__ == "__main__": |
|---|
| 155 | if len(sys.argv) < 2 or len(sys.argv) > 3: |
|---|
| 156 | usage() |
|---|
| 157 | sys.exit(0) |
|---|
| 158 | |
|---|
| 159 | # try to set especified config dir |
|---|
| 160 | try: |
|---|
| 161 | setup_homedir(sys.argv[2]) |
|---|
| 162 | except IndexError: # no path especified |
|---|
| 163 | setup_homedir(os.path.join(os.path.expanduser("~"), '.umit')) |
|---|
| 164 | |
|---|
| 165 | cmds = {"start": start, |
|---|
| 166 | "stop": stop, |
|---|
| 167 | "cleanup": cleanup |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | try: |
|---|
| 171 | cmds[sys.argv[1]]() |
|---|
| 172 | except KeyError, e: |
|---|
| 173 | print "Invalid command especified: %s" % e |
|---|
| 174 | usage() |
|---|
| 175 | |
|---|