Factor out entry point helper and apply to marconi.cmd.server
Change-Id: I3be826366948348430ebd286527ead5fec6413b9
This commit is contained in:
parent
8bf10fe4df
commit
b1849c1d65
@ -14,13 +14,12 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import atexit
|
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
import termios
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from marconi import bootstrap
|
from marconi import bootstrap
|
||||||
|
from marconi.common import cli
|
||||||
from marconi.common import config
|
from marconi.common import config
|
||||||
from marconi.openstack.common import log as logging
|
from marconi.openstack.common import log as logging
|
||||||
|
|
||||||
@ -28,34 +27,7 @@ PROJECT_CFG = config.project('marconi')
|
|||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def _fail(returncode, ex):
|
@cli.runnable
|
||||||
"""Handles terminal errors.
|
|
||||||
|
|
||||||
:param returncode: process return code to pass to sys.exit
|
|
||||||
:param ex: the error that occurred
|
|
||||||
"""
|
|
||||||
|
|
||||||
LOG.exception(ex)
|
|
||||||
sys.stderr.write('ERROR: %s\n' % ex)
|
|
||||||
sys.exit(returncode)
|
|
||||||
|
|
||||||
|
|
||||||
def _enable_echo(enable):
|
|
||||||
"""Enables or disables terminal echo.
|
|
||||||
|
|
||||||
:param enable: pass True to enable echo, False to disable
|
|
||||||
"""
|
|
||||||
|
|
||||||
fd = sys.stdin.fileno()
|
|
||||||
new = termios.tcgetattr(fd)
|
|
||||||
if enable:
|
|
||||||
new[3] |= termios.ECHO
|
|
||||||
else:
|
|
||||||
new[3] &= ~termios.ECHO
|
|
||||||
|
|
||||||
termios.tcsetattr(fd, termios.TCSANOW, new)
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
"""Entry point to start marconi-gc.
|
"""Entry point to start marconi-gc.
|
||||||
|
|
||||||
@ -66,13 +38,7 @@ def run():
|
|||||||
or interrupted.
|
or interrupted.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
atexit.register(_enable_echo, True)
|
|
||||||
_enable_echo(False)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logging.setup('marconi')
|
|
||||||
PROJECT_CFG.load(args=sys.argv[1:])
|
|
||||||
|
|
||||||
info = _('Starting marconi-gc')
|
info = _('Starting marconi-gc')
|
||||||
print(info + _('. Use CTRL+C to exit...\n'))
|
print(info + _('. Use CTRL+C to exit...\n'))
|
||||||
LOG.info(info)
|
LOG.info(info)
|
||||||
@ -95,9 +61,3 @@ def run():
|
|||||||
|
|
||||||
LOG.exception(ex)
|
LOG.exception(ex)
|
||||||
print('')
|
print('')
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
LOG.info('Terminating marconi-gc')
|
|
||||||
|
|
||||||
except Exception as ex:
|
|
||||||
_fail(1, ex)
|
|
||||||
|
@ -16,18 +16,10 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from marconi import bootstrap
|
from marconi import bootstrap
|
||||||
|
from marconi.common import cli
|
||||||
|
|
||||||
|
|
||||||
def fail(returncode, e):
|
@cli.runnable
|
||||||
sys.stderr.write('ERROR: %s\n' % e)
|
|
||||||
sys.exit(returncode)
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
try:
|
server = bootstrap.Bootstrap(cli_args=sys.argv[1:])
|
||||||
server = bootstrap.Bootstrap(cli_args=sys.argv[1:])
|
server.run()
|
||||||
server.run()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
fail(1, '... terminating marconi')
|
|
||||||
except RuntimeError as e:
|
|
||||||
fail(1, e)
|
|
||||||
|
77
marconi/common/cli.py
Normal file
77
marconi/common/cli.py
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
# Copyright (c) 2013 Rackspace Hosting, Inc.
|
||||||
|
#
|
||||||
|
# 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 atexit
|
||||||
|
import functools
|
||||||
|
import sys
|
||||||
|
import termios
|
||||||
|
|
||||||
|
from marconi.common import config
|
||||||
|
from marconi.openstack.common import log as logging
|
||||||
|
|
||||||
|
PROJECT_CFG = config.project('marconi')
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _fail(returncode, ex):
|
||||||
|
"""Handles terminal errors.
|
||||||
|
|
||||||
|
:param returncode: process return code to pass to sys.exit
|
||||||
|
:param ex: the error that occurred
|
||||||
|
"""
|
||||||
|
|
||||||
|
LOG.exception(ex)
|
||||||
|
sys.exit(returncode)
|
||||||
|
|
||||||
|
|
||||||
|
def _enable_echo(enable):
|
||||||
|
"""Enables or disables terminal echo.
|
||||||
|
|
||||||
|
:param enable: pass True to enable echo, False to disable
|
||||||
|
"""
|
||||||
|
|
||||||
|
fd = sys.stdin.fileno()
|
||||||
|
new_attr = termios.tcgetattr(fd)
|
||||||
|
if enable:
|
||||||
|
new_attr[3] |= termios.ECHO
|
||||||
|
else:
|
||||||
|
new_attr[3] &= ~termios.ECHO
|
||||||
|
|
||||||
|
termios.tcsetattr(fd, termios.TCSANOW, new_attr)
|
||||||
|
|
||||||
|
|
||||||
|
def runnable(func):
|
||||||
|
"""Entry point wrapper.
|
||||||
|
|
||||||
|
Note: This call blocks until the process is killed
|
||||||
|
or interrupted.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@functools.wraps(func)
|
||||||
|
def _wrapper():
|
||||||
|
atexit.register(_enable_echo, True)
|
||||||
|
_enable_echo(False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
logging.setup('marconi')
|
||||||
|
PROJECT_CFG.load(args=sys.argv[1:])
|
||||||
|
func()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
LOG.info('Terminating')
|
||||||
|
except Exception as ex:
|
||||||
|
_fail(1, ex)
|
||||||
|
|
||||||
|
return _wrapper
|
Loading…
Reference in New Issue
Block a user