cwctl.py
changeset 5380 a4e7e87d315f
parent 5323 329b4f6d18b4
child 5423 e15abfdcce38
equal deleted inserted replaced
5379:c082dea0731b 5380:a4e7e87d315f
   118         else:
   118         else:
   119             askconfirm = False
   119             askconfirm = False
   120         self.run_args(args, askconfirm)
   120         self.run_args(args, askconfirm)
   121 
   121 
   122     def run_args(self, args, askconfirm):
   122     def run_args(self, args, askconfirm):
       
   123         status = 0
   123         for appid in args:
   124         for appid in args:
   124             if askconfirm:
   125             if askconfirm:
   125                 print '*'*72
   126                 print '*'*72
   126                 if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
   127                 if not ASK.confirm('%s instance %r ?' % (self.name, appid)):
   127                     continue
   128                     continue
   128             self.run_arg(appid)
   129             status = max(status, self.run_arg(appid))
       
   130         sys.exit(status)
   129 
   131 
   130     def run_arg(self, appid):
   132     def run_arg(self, appid):
   131         cmdmeth = getattr(self, '%s_instance' % self.name)
   133         cmdmeth = getattr(self, '%s_instance' % self.name)
   132         try:
   134         try:
   133             cmdmeth(appid)
   135             status = cmdmeth(appid)
   134         except (KeyboardInterrupt, SystemExit):
   136         except (KeyboardInterrupt, SystemExit):
   135             print >> sys.stderr, '%s aborted' % self.name
   137             print >> sys.stderr, '%s aborted' % self.name
   136             sys.exit(2) # specific error code
   138             return 2 # specific error code
   137         except (ExecutionError, ConfigurationError), ex:
   139         except (ExecutionError, ConfigurationError), ex:
   138             print >> sys.stderr, 'instance %s not %s: %s' % (
   140             print >> sys.stderr, 'instance %s not %s: %s' % (
   139                 appid, self.actionverb, ex)
   141                 appid, self.actionverb, ex)
       
   142             status = 4
   140         except Exception, ex:
   143         except Exception, ex:
   141             import traceback
   144             import traceback
   142             traceback.print_exc()
   145             traceback.print_exc()
   143             print >> sys.stderr, 'instance %s not %s: %s' % (
   146             print >> sys.stderr, 'instance %s not %s: %s' % (
   144                 appid, self.actionverb, ex)
   147                 appid, self.actionverb, ex)
   145 
   148             status = 8
       
   149         return status
   146 
   150 
   147 class InstanceCommandFork(InstanceCommand):
   151 class InstanceCommandFork(InstanceCommand):
   148     """Same as `InstanceCommand`, but command is forked in a new environment
   152     """Same as `InstanceCommand`, but command is forked in a new environment
   149     for each argument
   153     for each argument
   150     """
   154     """
   592     options = ()
   596     options = ()
   593 
   597 
   594     @staticmethod
   598     @staticmethod
   595     def status_instance(appid):
   599     def status_instance(appid):
   596         """print running status information for an instance"""
   600         """print running status information for an instance"""
       
   601         status = 0
   597         for mode in cwcfg.possible_configurations(appid):
   602         for mode in cwcfg.possible_configurations(appid):
   598             config = cwcfg.config_for(appid, mode)
   603             config = cwcfg.config_for(appid, mode)
   599             print '[%s-%s]' % (appid, mode),
   604             print '[%s-%s]' % (appid, mode),
   600             try:
   605             try:
   601                 pidf = config['pid-file']
   606                 pidf = config['pid-file']
   602             except KeyError:
   607             except KeyError:
   603                 print 'buggy instance, pid file not specified'
   608                 print 'buggy instance, pid file not specified'
   604                 continue
   609                 continue
   605             if not exists(pidf):
   610             if not exists(pidf):
   606                 print "doesn't seem to be running"
   611                 print "doesn't seem to be running"
       
   612                 status = 1
   607                 continue
   613                 continue
   608             pid = int(open(pidf).read().strip())
   614             pid = int(open(pidf).read().strip())
   609             # trick to guess whether or not the process is running
   615             # trick to guess whether or not the process is running
   610             try:
   616             try:
   611                 getpgid(pid)
   617                 getpgid(pid)
   612             except OSError:
   618             except OSError:
   613                 print "should be running with pid %s but the process can not be found" % pid
   619                 print "should be running with pid %s but the process can not be found" % pid
       
   620                 status = 1
   614                 continue
   621                 continue
   615             print "running with pid %s" % (pid)
   622             print "running with pid %s" % (pid)
   616 
   623         return status
   617 
   624 
   618 class UpgradeInstanceCommand(InstanceCommandFork):
   625 class UpgradeInstanceCommand(InstanceCommandFork):
   619     """Upgrade an instance after cubicweb and/or component(s) upgrade.
   626     """Upgrade an instance after cubicweb and/or component(s) upgrade.
   620 
   627 
   621     For repository update, you will be prompted for a login / password to use
   628     For repository update, you will be prompted for a login / password to use