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
|
||||
# limitations under the License.
|
||||
|
||||
import atexit
|
||||
import random
|
||||
import sys
|
||||
import termios
|
||||
import time
|
||||
|
||||
from marconi import bootstrap
|
||||
from marconi.common import cli
|
||||
from marconi.common import config
|
||||
from marconi.openstack.common import log as logging
|
||||
|
||||
@ -28,34 +27,7 @@ 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.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)
|
||||
|
||||
|
||||
@cli.runnable
|
||||
def run():
|
||||
"""Entry point to start marconi-gc.
|
||||
|
||||
@ -66,13 +38,7 @@ def run():
|
||||
or interrupted.
|
||||
"""
|
||||
|
||||
atexit.register(_enable_echo, True)
|
||||
_enable_echo(False)
|
||||
|
||||
try:
|
||||
logging.setup('marconi')
|
||||
PROJECT_CFG.load(args=sys.argv[1:])
|
||||
|
||||
info = _('Starting marconi-gc')
|
||||
print(info + _('. Use CTRL+C to exit...\n'))
|
||||
LOG.info(info)
|
||||
@ -95,9 +61,3 @@ def run():
|
||||
|
||||
LOG.exception(ex)
|
||||
print('')
|
||||
|
||||
except KeyboardInterrupt:
|
||||
LOG.info('Terminating marconi-gc')
|
||||
|
||||
except Exception as ex:
|
||||
_fail(1, ex)
|
||||
|
@ -16,18 +16,10 @@
|
||||
import sys
|
||||
|
||||
from marconi import bootstrap
|
||||
from marconi.common import cli
|
||||
|
||||
|
||||
def fail(returncode, e):
|
||||
sys.stderr.write('ERROR: %s\n' % e)
|
||||
sys.exit(returncode)
|
||||
|
||||
|
||||
@cli.runnable
|
||||
def run():
|
||||
try:
|
||||
server = bootstrap.Bootstrap(cli_args=sys.argv[1:])
|
||||
server.run()
|
||||
except KeyboardInterrupt:
|
||||
fail(1, '... terminating marconi')
|
||||
except RuntimeError as e:
|
||||
fail(1, e)
|
||||
server = bootstrap.Bootstrap(cli_args=sys.argv[1:])
|
||||
server.run()
|
||||
|
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