swift/bin/swift-init
Lisak, Peter 7b7c6c5249 swift-init return codes
Currently, swift-init returns zero if can't locate config on start.
Because of this problem, it is not possible to distinguish if managed
to start server.

Due to legacy two new complementary options are added. Default is context
dependent.
--strict returns non-zero if some config is missing (default mode
if explicitly named server)
--non-strict returns zero even if some config is missing (default mode
if alias is used)

As a side effect:
If some of demanded servers already running it does not try to start
unstarted and also returns non-zero (in strict mode). That is still sufficient
for the goal of patch.

For future improvements LSB status codes should be considered.

DocImpact
Change-Id: I7750abd4a94875b46f83f4aeee8509388d543c2b
2015-11-16 07:07:11 +01:00

93 lines
3.7 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright (c) 2010-2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from optparse import OptionParser
from swift.common.manager import Manager, UnknownCommandError, \
KILL_WAIT, RUN_DIR
USAGE = """%prog <server>[.config] [<server>[.config] ...] <command> [options]
Commands:
""" + '\n'.join(["%16s: %s" % x for x in Manager.list_commands()])
def main():
parser = OptionParser(USAGE)
parser.add_option('-v', '--verbose', action="store_true",
default=False, help="display verbose output")
parser.add_option('-w', '--no-wait', action="store_false", dest="wait",
default=True, help="won't wait for server to start "
"before returning")
parser.add_option('-o', '--once', action="store_true",
default=False, help="only run one pass of daemon")
# this is a negative option, default is options.daemon = True
parser.add_option('-n', '--no-daemon', action="store_false", dest="daemon",
default=True, help="start server interactively")
parser.add_option('-g', '--graceful', action="store_true",
default=False, help="send SIGHUP to supporting servers")
parser.add_option('-c', '--config-num', metavar="N", type="int",
dest="number", default=0,
help="send command to the Nth server only")
parser.add_option('-k', '--kill-wait', metavar="N", type="int",
dest="kill_wait", default=KILL_WAIT,
help="wait N seconds for processes to die (default 15)")
parser.add_option('-r', '--run-dir', type="str",
dest="run_dir", default=RUN_DIR,
help="alternative directory to store running pid files "
"default: %s" % RUN_DIR)
# Changing behaviour if missing config
parser.add_option('--strict', dest='strict', action='store_true',
help="Return non-zero status code if some config is "
"missing. Default mode if server is explicitly "
"named.")
# a negative option for strict
parser.add_option('--non-strict', dest='strict', action='store_false',
help="Return zero status code even if some config is "
"missing. Default mode if server is one of aliases "
"`all`, `main` or `rest`.")
options, args = parser.parse_args()
if len(args) < 2:
parser.print_help()
print 'ERROR: specify server(s) and command'
return 1
command = args[-1]
servers = args[:-1]
# this is just a silly swap for me cause I always try to "start main"
commands = dict(Manager.list_commands()).keys()
if command not in commands and servers[0] in commands:
servers.append(command)
command = servers.pop(0)
manager = Manager(servers, run_dir=options.run_dir)
try:
status = manager.run_command(command, **options.__dict__)
except UnknownCommandError:
parser.print_help()
print 'ERROR: unknown command, %s' % command
status = 1
return 1 if status else 0
if __name__ == "__main__":
sys.exit(main())