41 from six.moves.urllib.parse import urlparse |
41 from six.moves.urllib.parse import urlparse |
42 |
42 |
43 from logilab.common.clcommands import CommandLine |
43 from logilab.common.clcommands import CommandLine |
44 from logilab.common.shellutils import ASK |
44 from logilab.common.shellutils import ASK |
45 from logilab.common.configuration import merge_options |
45 from logilab.common.configuration import merge_options |
|
46 from logilab.common.deprecation import deprecated |
46 |
47 |
47 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage |
48 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage |
48 from cubicweb.utils import support_args |
49 from cubicweb.utils import support_args |
49 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CWDEV, CONFIGURATIONS |
50 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CWDEV, CONFIGURATIONS |
50 from cubicweb.toolsutils import Command, rm, create_dir, underline_title |
51 from cubicweb.toolsutils import Command, rm, create_dir, underline_title |
101 } |
102 } |
102 ), |
103 ), |
103 ) |
104 ) |
104 actionverb = None |
105 actionverb = None |
105 |
106 |
|
107 @deprecated('[3.22] startorder is not used any more') |
106 def ordered_instances(self): |
108 def ordered_instances(self): |
107 """return instances in the order in which they should be started, |
109 """return list of known instances |
108 considering $REGISTRY_DIR/startorder file if it exists (useful when |
|
109 some instances depends on another as external source). |
|
110 |
|
111 Instance used by another one should appears first in the file (one |
|
112 instance per line) |
|
113 """ |
110 """ |
114 regdir = cwcfg.instances_dir() |
111 regdir = cwcfg.instances_dir() |
115 _allinstances = list_instances(regdir) |
112 return list_instances(regdir) |
116 if isfile(join(regdir, 'startorder')): |
|
117 allinstances = [] |
|
118 for line in open(join(regdir, 'startorder')): |
|
119 line = line.strip() |
|
120 if line and not line.startswith('#'): |
|
121 try: |
|
122 _allinstances.remove(line) |
|
123 allinstances.append(line) |
|
124 except ValueError: |
|
125 print('ERROR: startorder file contains unexistant ' |
|
126 'instance %s' % line) |
|
127 allinstances += _allinstances |
|
128 else: |
|
129 allinstances = _allinstances |
|
130 return allinstances |
|
131 |
113 |
132 def run(self, args): |
114 def run(self, args): |
133 """run the <command>_method on each argument (a list of instance |
115 """run the <command>_method on each argument (a list of instance |
134 identifiers) |
116 identifiers) |
135 """ |
117 """ |
136 if not args: |
118 if not args: |
137 args = self.ordered_instances() |
119 args = list_instances(cwcfg.instances_dir()) |
138 try: |
120 try: |
139 askconfirm = not self.config.force |
121 askconfirm = not self.config.force |
140 except AttributeError: |
122 except AttributeError: |
141 # no force option |
123 # no force option |
142 askconfirm = False |
124 askconfirm = False |
570 given, stop them all. |
552 given, stop them all. |
571 """ |
553 """ |
572 name = 'stop' |
554 name = 'stop' |
573 actionverb = 'stopped' |
555 actionverb = 'stopped' |
574 |
556 |
575 def ordered_instances(self): |
|
576 instances = super(StopInstanceCommand, self).ordered_instances() |
|
577 instances.reverse() |
|
578 return instances |
|
579 |
|
580 def stop_instance(self, appid): |
557 def stop_instance(self, appid): |
581 """stop the instance's server""" |
558 """stop the instance's server""" |
582 config = cwcfg.config_for(appid) |
559 config = cwcfg.config_for(appid) |
583 helper = self.config_helper(config, cmdname='stop') |
560 helper = self.config_helper(config, cmdname='stop') |
584 helper.poststop() # do this anyway |
561 helper.poststop() # do this anyway |
618 identifiers of the instances to restart. If no instance is |
595 identifiers of the instances to restart. If no instance is |
619 given, restart them all. |
596 given, restart them all. |
620 """ |
597 """ |
621 name = 'restart' |
598 name = 'restart' |
622 actionverb = 'restarted' |
599 actionverb = 'restarted' |
623 |
|
624 def run_args(self, args, askconfirm): |
|
625 regdir = cwcfg.instances_dir() |
|
626 if not isfile(join(regdir, 'startorder')) or len(args) <= 1: |
|
627 # no specific startorder |
|
628 super(RestartInstanceCommand, self).run_args(args, askconfirm) |
|
629 return |
|
630 print ('some specific start order is specified, will first stop all ' |
|
631 'instances then restart them.') |
|
632 # get instances in startorder |
|
633 for appid in args: |
|
634 if askconfirm: |
|
635 print('*'*72) |
|
636 if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
|
637 continue |
|
638 StopInstanceCommand(self.logger).stop_instance(appid) |
|
639 forkcmd = [w for w in sys.argv if not w in args] |
|
640 forkcmd[1] = 'start' |
|
641 forkcmd = ' '.join(forkcmd) |
|
642 for appid in reversed(args): |
|
643 status = system('%s %s' % (forkcmd, appid)) |
|
644 if status: |
|
645 sys.exit(status) |
|
646 |
600 |
647 def restart_instance(self, appid): |
601 def restart_instance(self, appid): |
648 StopInstanceCommand(self.logger).stop_instance(appid) |
602 StopInstanceCommand(self.logger).stop_instance(appid) |
649 self.start_instance(appid) |
603 self.start_instance(appid) |
650 |
604 |