Use standard Python command line parser (#6)

This commit is contained in:
Frédéric Guillot 2016-04-21 14:20:34 -04:00 committed by Maxime Belanger
parent b013e96aff
commit 0280cf911e
8 changed files with 88 additions and 79 deletions

View File

@ -23,6 +23,35 @@ Requirements
- MongoDB
- Python 2.7
Command line usage
------------------
Usage:
```bash
usage: almanach [-h] [--logging LOGGING] {api,collector} config_file
```
Start the API daemon:
```bash
almanach api /path/to/almanach.cfg
```
Start the collector:
```bash
almanach collector /path/to/almanach.cfg
```
Custom logging configuration:
```bash
almanach collector /path/to/almanach.cfg --logging /path/to/logging.cfg
```
The syntax of the logging configuration file is available in the official [Python documentation](https://docs.python.org/2/library/logging.config.html).
Database entities
-----------------

View File

@ -12,14 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from flask import Flask
from gunicorn.app.base import Application
from almanach import config
from almanach.adapters import api_route_v1 as api_route
from almanach import log_bootstrap
from almanach.adapters.database_adapter import DatabaseAdapter
from almanach.core.controller import Controller
@ -30,24 +26,12 @@ class AlmanachApi(Application):
super(AlmanachApi, self).__init__()
def init(self, parser, opts, args):
log_bootstrap.configure()
config.read(args)
self._controller = Controller(DatabaseAdapter())
def load(self):
logging.info("starting flask worker")
api_route.controller = self._controller
app = Flask("almanach")
app.register_blueprint(api_route.api)
return app
def run():
almanach_api = AlmanachApi()
almanach_api.run()
if __name__ == "__main__":
run()

44
almanach/cli.py Normal file
View File

@ -0,0 +1,44 @@
# Copyright 2016 Internap.
#
# 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 argparse
import logging
from almanach import config
from almanach.api import AlmanachApi
from almanach.collector import AlmanachCollector
def run():
parser = argparse.ArgumentParser()
parser.add_argument("service", help="Service to execute: 'api' or 'collector'", choices=["api", "collector"])
parser.add_argument("config_file", help="Config file path")
parser.add_argument("--logging", help="Logger configuration")
args = parser.parse_args()
config.read(args.config_file)
if args.service == "api":
almanach_api = AlmanachApi()
almanach_api.run()
else:
almanach_collector = AlmanachCollector()
almanach_collector.run()
if args.logging:
logging.config.fileConfig(args.logging, disable_existing_loggers=False)
if __name__ == "__main__":
run()

View File

@ -13,11 +13,9 @@
# limitations under the License.
import logging
import sys
from kombu import Connection
from almanach import log_bootstrap
from almanach import config
from almanach.adapters.bus_adapter import BusAdapter
from almanach.adapters.database_adapter import DatabaseAdapter
@ -28,24 +26,11 @@ from almanach.core.controller import Controller
class AlmanachCollector(object):
def __init__(self):
log_bootstrap.configure()
config.read(sys.argv)
self._controller = Controller(DatabaseAdapter())
_connection = Connection(config.rabbitmq_url(), heartbeat=540)
retry_adapter = RetryAdapter(_connection)
self._busAdapter = BusAdapter(self._controller, _connection, retry_adapter)
def run(self):
logging.info("starting bus adapter")
logging.info("Listening for incoming events")
self._busAdapter.run()
logging.info("shutting down")
def run():
almanach_collector = AlmanachCollector()
almanach_collector.run()
if __name__ == "__main__":
run()

View File

@ -13,26 +13,18 @@
# limitations under the License.
import ConfigParser
import pkg_resources
import os.path as Path
import os.path as os_path
from almanach.common.almanach_exception import AlmanachException
configuration = ConfigParser.RawConfigParser()
def read(args=[], config_file="resources/config/almanach.cfg"):
filename = pkg_resources.resource_filename("almanach", config_file)
def read(filename):
if not os_path.isfile(filename):
raise AlmanachException("Config file '{0}' not found".format(filename))
for param in args:
if param.startswith("config_file="):
filename = param.split("=")[-1]
break
if not Path.isfile(filename):
raise AlmanachException("config file '{0}' not found".format(filename))
print "loading configuration file {0}".format(filename)
print("Loading configuration file {0}".format(filename))
configuration.read(filename)
@ -112,7 +104,7 @@ def rabbitmq_time_to_live():
def _read_file(filename):
file = open(filename, "r")
content = file.read()
file.close()
f = open(filename, "r")
content = f.read()
f.close()
return content

View File

@ -1,26 +0,0 @@
# Copyright 2016 Internap.
#
# 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 logging
import pkg_resources
def get_config_file():
logging_conf = pkg_resources.resource_filename("almanach", "resources/config/logging.cfg")
return logging_conf
def configure():
logging_conf_file = get_config_file()
logging.config.fileConfig(logging_conf_file, disable_existing_loggers=False)

View File

@ -24,8 +24,7 @@ packages =
[entry_points]
console_scripts =
almanach_collector = almanach.collector:run
almanach_api = almanach.api:run
almanach = almanach.cli:run
[nosetests]
no-path-adjustment = 1

View File

@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pkg_resources
import unittest
from datetime import datetime
import mongomock
from datetime import datetime
from flexmock import flexmock, flexmock_teardown
from hamcrest import assert_that, contains_inanyorder
from pymongo import MongoClient
from almanach.adapters.database_adapter import DatabaseAdapter
from almanach.common.volume_type_not_found_exception import VolumeTypeNotFoundException
from almanach.common.almanach_exception import AlmanachException
@ -29,7 +31,7 @@ from tests.builder import a, instance, volume, volume_type
class DatabaseAdapterTest(unittest.TestCase):
def setUp(self):
config.read(config_file="resources/config/test.cfg")
config.read(pkg_resources.resource_filename("almanach", "resources/config/test.cfg"))
mongo_connection = mongomock.Connection()
self.adapter = DatabaseAdapter()