Merge "Use argparse to parse command line options"

This commit is contained in:
Zuul 2018-04-12 10:23:36 +00:00 committed by Gerrit Code Review
commit a8b61f9961
4 changed files with 61 additions and 49 deletions

View File

@ -1,7 +1,21 @@
#!/usr/bin/env python
# Copyright (c) 2015-2016 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 storlets.agent.daemon.server import main
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
sys.exit(main())

View File

@ -1,7 +1,21 @@
#!/usr/bin/env python
# Copyright (c) 2015-2016 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 storlets.agent.daemon_factory.server import main
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
sys.exit(main())

View File

@ -12,6 +12,7 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import errno
import os
import pwd
@ -234,34 +235,25 @@ class StorletDaemon(SBusServer):
self._wait_all_child_processes()
def usage():
"""
Print the expected command line arguments.
"""
print("storlets-daemon <storlet_name> <sbus_path> <log_level> "
"<pool_size> <cotnainer id>")
def main(argv):
def main():
"""
The entry point of daemon_factory process
:param argv: parameters given from command line
"""
if (len(argv) != 5):
usage()
return EXIT_FAILURE
storlet_name = argv[0]
sbus_path = argv[1]
log_level = argv[2]
pool_size = argv[3]
container_id = argv[4]
parser = argparse.ArgumentParser(
description='Daemon process to execute storlet applications')
parser.add_argument('storlet_name', help='storlet name')
parser.add_argument('sbus_path', help='the path to unix domain socket')
parser.add_argument('log_level', help='log level')
parser.add_argument('pool_size', type=int,
help='the maximun thread numbers used swapns for '
'one storlet application')
parser.add_argument('container_id', help='container id')
opts = parser.parse_args()
# Initialize logger
logger = get_logger("storlets-daemon", log_level, container_id)
logger = get_logger("storlets-daemon", opts.log_level, opts.container_id)
logger.debug("Storlet Daemon started")
SBus.start_logger("DEBUG", container_id=container_id)
SBus.start_logger("DEBUG", container_id=opts.container_id)
# Impersonate the swift user
pw = pwd.getpwnam('swift')
@ -270,9 +262,10 @@ def main(argv):
# create an instance of storlet daemon
try:
daemon = StorletDaemon(storlet_name, sbus_path, logger, pool_size)
except Exception as e:
logger.error(e.message)
daemon = StorletDaemon(opts.storlet_name, opts.sbus_path,
logger, opts.pool_size)
except Exception as err:
logger.error(err.message)
return EXIT_FAILURE
# Start the main loop

View File

@ -12,6 +12,7 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import errno
import os
import pwd
@ -23,8 +24,8 @@ from storlets.sbus import SBus
from storlets.sbus.datagram import FDMetadata, SBusServiceDatagram
from storlets.sbus.command import SBUS_CMD_HALT, SBUS_CMD_PING
from storlets.sbus.file_description import SBUS_FD_SERVICE_OUT
from storlets.agent.common.server import command_handler, EXIT_FAILURE, \
CommandSuccess, CommandFailure, SBusServer
from storlets.agent.common.server import command_handler, CommandSuccess, \
CommandFailure, SBusServer
from storlets.agent.common.utils import get_logger
@ -502,31 +503,21 @@ class StorletDaemonFactory(SBusServer):
pass
def usage():
"""
Print the expected command line arguments.
"""
print("storlets-daemon-factory <path> <log level> <container_id>")
def main(argv):
def main():
"""
The entry point of daemon_factory process
:param argv: parameters given from command line
"""
if (len(argv) != 3):
usage()
return EXIT_FAILURE
sbus_path = argv[0]
log_level = argv[1]
container_id = argv[2]
parser = argparse.ArgumentParser(
description='Factory process to manage storlet daemons')
parser.add_argument('sbus_path', help='the path to unix domain socket')
parser.add_argument('log_level', help='log level')
parser.add_argument('container_id', help='container id')
opts = parser.parse_args()
# Initialize logger
logger = get_logger("daemon-factory", log_level, container_id)
logger = get_logger("daemon-factory", opts.log_level, opts.container_id)
logger.debug("Daemon factory started")
SBus.start_logger("DEBUG", container_id=container_id)
SBus.start_logger("DEBUG", container_id=opts.container_id)
# Impersonate the swift user
pw = pwd.getpwnam('swift')
@ -534,7 +525,7 @@ def main(argv):
os.setresuid(pw.pw_uid, pw.pw_uid, pw.pw_uid)
# create an instance of daemon_factory
factory = StorletDaemonFactory(sbus_path, logger, container_id)
factory = StorletDaemonFactory(opts.sbus_path, logger, opts.container_id)
# Start the main loop
return factory.main_loop()