Skip to content

Commit 3caebc8

Browse files
committed
Fixup API code and add missing code from Ceilometer
1 parent 9423f17 commit 3caebc8

5 files changed

Lines changed: 124 additions & 18 deletions

File tree

billingstack/api/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
from billingstack.openstack.common import cfg
2020

2121
API_SERVICE_OPTS = [
22-
cfg.IntOpt('billing_api_port',
23-
default=9001,
24-
help='The port for the billing API server',
25-
),
22+
cfg.IntOpt('api_port', default=9091,
23+
help='The port for the billing API server'),
24+
cfg.IntOpt('api_listen', default='0.0.0.0', help='Bind to address')
2625
]
2726

28-
cfg.CONF.register_opts(API_SERVICE_OPTS)
27+
cfg.CONF.register_opts(API_SERVICE_OPTS, 'service:api')

billingstack/api/app.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,33 @@
1616
# License for the specific language governing permissions and limitations
1717
# under the License.
1818

19+
from pecan import configuration
1920
from pecan import make_app
2021

2122
from billingstack.api import config as api_config
23+
from billingstack.api import hooks
2224

23-
def setup_app(pecan_config=None):
2425

25-
return make_app(
26+
def get_pecan_config():
27+
# Set up the pecan configuration
28+
filename = api_config.__file__.replace('.pyc', '.py')
29+
return configuration.conf_from_file(filename)
30+
31+
32+
def setup_app(pecan_config=None, extra_hooks=None):
33+
34+
app_hooks = [hooks.ConfigHook(),
35+
hooks.DBHook()]
36+
37+
if extra_hooks:
38+
app_hooks.extend(extra_hooks)
39+
40+
if not pecan_config:
41+
pecan_config = get_pecan_config()
42+
43+
configuration.set_config(dict(pecan_config), overwrite=True)
44+
45+
app = make_app(
2646
pecan_config.app.root,
2747
static_root=pecan_config.app.static_root,
2848
template_path=pecan_config.app.template_path,
@@ -33,4 +53,6 @@ def setup_app(pecan_config=None):
3353
pecan_config.app,
3454
'guess_content_type_from_ext',
3555
True),
36-
)
56+
)
57+
58+
return app

billingstack/api/hooks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pecan import hooks
2+
3+
from billingstack.openstack.common import cfg
4+
from billingstack import storage
5+
6+
7+
class ConfigHook(hooks.PecanHook):
8+
"""Attach the configuration object to the request
9+
so controllers can get to it.
10+
"""
11+
12+
def before(self, state):
13+
state.request.cfg = cfg.CONF
14+
15+
16+
class DBHook(hooks.PecanHook):
17+
18+
def before(self, state):
19+
storage_engine = storage.get_engine(state.request.cfg)
20+
state.request.storage_engine = storage_engine
21+
state.request.storage_conn = storage_engine.get_connection(
22+
state.request.cfg)
23+
24+
# def after(self, state):
25+
# print 'method:', state.request.method
26+
# print 'response:', state.response.status

billingstack/service.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
#
4+
# Copyright © 2012 eNovance <licensing@enovance.com>
5+
#
6+
# Author: Julien Danjou <julien@danjou.info>
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9+
# not use this file except in compliance with the License. You may obtain
10+
# a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17+
# License for the specific language governing permissions and limitations
18+
# under the License.
19+
20+
import os
21+
import socket
22+
23+
from billingstack.openstack.common import cfg
24+
from billingstack.openstack.common import rpc
25+
from billingstack.openstack.common import context
26+
from billingstack.openstack.common import log
27+
from billingstack.openstack.common.rpc import service as rpc_service
28+
29+
30+
cfg.CONF.register_opts([
31+
cfg.IntOpt('periodic_interval',
32+
default=600,
33+
help='seconds between running periodic tasks'),
34+
cfg.StrOpt('host',
35+
default=socket.getfqdn(),
36+
help='Name of this node. This can be an opaque identifier. '
37+
'It is not necessarily a hostname, FQDN, or IP address. '
38+
'However, the node name must be valid within '
39+
'an AMQP key, and if using ZeroMQ, a valid '
40+
'hostname, FQDN, or IP address'),
41+
])
42+
43+
44+
class PeriodicService(rpc_service.Service):
45+
46+
def start(self):
47+
super(PeriodicService, self).start()
48+
admin_context = context.RequestContext('admin', 'admin', is_admin=True)
49+
self.tg.add_timer(cfg.CONF.periodic_interval,
50+
self.manager.periodic_tasks,
51+
context=admin_context)
52+
53+
54+
def _sanitize_cmd_line(argv):
55+
"""Remove non-nova CLI options from argv."""
56+
cli_opt_names = ['--%s' % o.name for o in CLI_OPTIONS]
57+
return [a for a in argv if a in cli_opt_names]
58+
59+
60+
def prepare_service(argv=[]):
61+
rpc.set_defaults(control_exchange='billingstack')
62+
cfg.CONF(argv[1:], project='billingstack')
63+
log.setup('billingstack')

bin/billingstack-api

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import os
2222
import sys
2323
from wsgiref import simple_server
2424

25-
from ceilometer.api import app
26-
from ceilometer import service
27-
from ceilometer.openstack.common import cfg
28-
from ceilometer.openstack.common import log as logging
25+
from billingstack.api import app
26+
from billingstack import service
27+
from billingstack.openstack.common import cfg
28+
from billingstack.openstack.common import log as logging
2929

3030

3131
if __name__ == '__main__':
@@ -37,16 +37,12 @@ if __name__ == '__main__':
3737
root = app.setup_app()
3838

3939
# Create the WSGI server and start it
40-
host, port = '0.0.0.0', int(cfg.CONF.metering_api_port)
40+
host, port = cfg.CONF['service:api'].api_listen, int(cfg.CONF['service:api'].api_port)
4141
srv = simple_server.make_server(host, port, root)
4242

4343
print 'Starting server in PID %s' % os.getpid()
4444

45-
if host == '0.0.0.0':
46-
print 'serving on 0.0.0.0:%s, view at http://127.0.0.1:%s' % \
47-
(port, port)
48-
else:
49-
print "serving on http://%s:%s" % (host, port)
45+
print "serving on http://%s:%s" % (host, port)
5046

5147
try:
5248
srv.serve_forever()

0 commit comments

Comments
 (0)