Ejemplo n.º 1
0
 def Stop(cls):
     state = cls.__CurrentState()
     if state is None:
         print("Service %s is not installed." % cls._svc_name_)
         return
     if state == win32service.SERVICE_STOPPED:
         print("Service %s stopped already." % cls._svc_name_)
     else:
         win32serviceutil.StopServiceWithDeps(cls._svc_name_)
         print("Service %s stopped successfully." % cls._svc_name_)
Ejemplo n.º 2
0
def service_manager(action):
    try:
        if action == 'stop':
            win32serviceutil.StopServiceWithDeps(DATADOG_SERVICE)
        elif action == 'start':
            win32serviceutil.StartService(DATADOG_SERVICE)
        elif action == 'restart':
            restartServiceWithDeps(DATADOG_SERVICE)
    except Exception as e:
        warning_popup("Couldn't %s service: \n %s" % (action, str(e)))
Ejemplo n.º 3
0
    def Uninstall(cls):
        state = cls.__CurrentState()
        if state is None:
            print("Service %s is not installed." % cls._svc_name_)
            return

        if state not in [win32service.SERVICE_STOPPED]:
            win32serviceutil.StopServiceWithDeps(cls._svc_name_)

        win32serviceutil.RemoveService(cls._svc_name_)
        print("Service %s uninstalled successfully." % cls._svc_name_)
Ejemplo n.º 4
0
 def Stop(serviceName, waitSecs=30):
   err = 0
   try:
     if waitSecs:
       win32serviceutil.StopServiceWithDeps(serviceName, waitSecs=waitSecs)
     else:
       win32serviceutil.StopService(serviceName)
       if waitSecs:
         win32serviceutil.WaitForServiceStatus(serviceName, win32service.SERVICE_STOPPED, waitSecs)
   except win32service.error, exc:
     print "Error stopping service: %s (%d)" % (exc.strerror, exc.winerror)
     err = exc.winerror
Ejemplo n.º 5
0
def _main():
    servicename = 'salt-minion'
    try:
        status = win32serviceutil.QueryServiceStatus(servicename)
    except win32service.error as details:
        if details[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
            instart(MinionService, servicename, 'Salt Minion')
            sys.exit(os.EX_OK)
    if status[1] == win32service.SERVICE_RUNNING:
        win32serviceutil.StopServiceWithDeps(servicename)
        win32serviceutil.StartService(servicename)
    else:
        win32serviceutil.StartService(servicename)
Ejemplo n.º 6
0
 def setup(cls, cmd, user=None, password=None, startup='manual', cwd=None, wait=0):
     from gramex.config import app_log
     name, service_name = cls._svc_display_name_, cls._svc_name_
     port = getattr(cls, '_svc_port_', None)
     if cwd is None:
         cwd = os.getcwd()
     info = (name, cwd, 'port %s' % port if port is not None else '')
     service_class = win32serviceutil.GetServiceClassString(cls)
     startup = cls.startup_map[startup]
     running = win32service.SERVICE_RUNNING
     if cmd[0] == 'install':
         win32serviceutil.InstallService(
             service_class, service_name, displayName=name, description=cls._svc_description_,
             startType=startup, userName=user, password=password)
         win32serviceutil.SetServiceCustomOption(cls._svc_name_, 'cwd', cwd)
         app_log.info('Installed service. %s will run from %s %s' % info)
     elif cmd[0] in {'update', 'change'}:
         win32serviceutil.ChangeServiceConfig(
             service_class, service_name, displayName=name, description=cls._svc_description_,
             startType=startup, userName=user, password=password)
         win32serviceutil.SetServiceCustomOption(cls._svc_name_, 'cwd', cwd)
         app_log.info('Updated service. %s will run from %s %s' % info)
     elif cmd[0] in {'remove', 'uninstall'}:
         try:
             win32serviceutil.StopService(service_name)
         except pywintypes.error as e:
             if e.args[0] != winerror.ERROR_SERVICE_NOT_ACTIVE:
                 raise
         win32serviceutil.RemoveService(service_name)
         app_log.info('Removed service. %s ran from %s %s' % info)
     elif cmd[0] == 'start':
         win32serviceutil.StartService(service_name, cmd[1:])
         if wait:
             win32serviceutil.WaitForServiceStatus(service_name, running, wait)
         app_log.info('Started service %s at %s %s' % info)
     elif cmd[0] == 'stop':
         if wait:
             win32serviceutil.StopServiceWithDeps(service_name, waitSecs=wait)
         else:
             win32serviceutil.StopService(service_name)
         app_log.info('Stopped service %s at %s %s' % info)
     elif cmd[0]:
         app_log.error('Unknown command: %s' % cmd[0])
Ejemplo n.º 7
0
def daemonize():
    '''
    Daemonize a process
    '''
    if 'os' in os.environ:
        if os.environ['os'].startswith('Windows'):
            import ctypes
            if ctypes.windll.shell32.IsUserAnAdmin() == 0:
                import win32api
                executablepath = sys.executable
                pypath = executablepath.split('\\')
                win32api.ShellExecute(
                    0,
                    'runas',
                    executablepath, 
                    os.path.join(pypath[0], os.sep, pypath[1], 'Lib\\site-packages\\salt\\utils\\saltminionservice.py'),
                    os.path.join(pypath[0], os.sep, pypath[1]),
                    0 )
                sys.exit(0)
            else:
                import saltminionservice
                import win32serviceutil
                import win32service
                import winerror
                servicename = 'salt-minion'
                try:
                    status = win32serviceutil.QueryServiceStatus(servicename)
                except win32service.error, details:
                    if details[0]==winerror.ERROR_SERVICE_DOES_NOT_EXIST:
                        saltminionservice.instart(saltminionservice.MinionService, servicename, 'Salt Minion')
                        sys.exit(0)
                if status[1] == win32service.SERVICE_RUNNING:
                    win32serviceutil.StopServiceWithDeps(servicename)
                    win32serviceutil.StartService(servicename)
                else:
                    win32serviceutil.StartService(servicename)
                sys.exit(0)
Ejemplo n.º 8
0
def svcStop(svc_name, machine=None):
    status = win32serviceutil.StopServiceWithDeps(svc_name, machine, 30)
    while status == STOPPING:
        time.sleep(1)
        status = svcStatus(svc_name, machine)
    return status
Ejemplo n.º 9
0
import sys

class MinionService(Service):
    def start(self):
        self.runflag=True
        self.log("Starting the Salt Minion")
        minion = salt.Minion()
        minion.start()
        while self.runflag:
            pass
            #self.sleep(10)
            #self.log("I'm alive ...")
    def stop(self):
        self.runflag=False
        self.log("Shutting down the Salt Minion")

if __name__ == '__main__':
    servicename = 'salt-minion'
    try:
        status = win32serviceutil.QueryServiceStatus(servicename)
    except win32service.error as details:
        if details[0]==winerror.ERROR_SERVICE_DOES_NOT_EXIST:
            instart(MinionService, servicename, 'Salt Minion')
            sys.exit(0)
    if status[1] == win32service.SERVICE_RUNNING:
        win32serviceutil.StopServiceWithDeps(servicename)
        win32serviceutil.StartService(servicename)
    else:
        win32serviceutil.StartService(servicename)

Ejemplo n.º 10
0
def daemonize():
    '''
    Daemonize a process
    '''
    if 'os' in os.environ:
        if os.environ['os'].startswith('Windows'):
            import ctypes
            if ctypes.windll.shell32.IsUserAnAdmin() == 0:
                import win32api
                executablepath = sys.executable
                pypath = executablepath.split('\\')
                win32api.ShellExecute(
                    0, 'runas', executablepath,
                    os.path.join(
                        pypath[0], os.sep, pypath[1],
                        'Lib\\site-packages\\salt\\utils\\saltminionservice.py'
                    ), os.path.join(pypath[0], os.sep, pypath[1]), 0)
                sys.exit(0)
            else:
                import saltminionservice
                import win32serviceutil
                import win32service
                import winerror
                servicename = 'salt-minion'
                try:
                    status = win32serviceutil.QueryServiceStatus(servicename)
                except win32service.error as details:
                    if details[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
                        saltminionservice.instart(
                            saltminionservice.MinionService, servicename,
                            'Salt Minion')
                        sys.exit(0)
                if status[1] == win32service.SERVICE_RUNNING:
                    win32serviceutil.StopServiceWithDeps(servicename)
                    win32serviceutil.StartService(servicename)
                else:
                    win32serviceutil.StartService(servicename)
                sys.exit(0)
    try:
        pid = os.fork()
        if pid > 0:
            # exit first parent
            sys.exit(0)
    except OSError as exc:
        msg = 'fork #1 failed: {0} ({1})'.format(exc.errno, exc.strerror)
        log.error(msg)
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/")
    os.setsid()
    os.umask(18)

    # do second fork
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError as exc:
        msg = 'fork #2 failed: {0} ({1})'
        log.error(msg.format(exc.errno, exc.strerror))
        sys.exit(1)

    dev_null = open('/dev/null', 'rw')
    os.dup2(dev_null.fileno(), sys.stdin.fileno())
    os.dup2(dev_null.fileno(), sys.stdout.fileno())
    os.dup2(dev_null.fileno(), sys.stderr.fileno())
Ejemplo n.º 11
0
 def _stop_service(self):
     # StopServiceWithDeps checks the status of each service as it
     # stops it, which is exactly what we want here.
     win32serviceutil.StopServiceWithDeps(service_name)