Permalink
Browse files

new_lb_protocol/add log

  • Loading branch information...
1 parent cec479f commit e4f9eb931c48a5ea70417e68b50f2c3091d52cae @pengyuwei committed Jul 9, 2012
Showing with 128 additions and 61 deletions.
  1. +38 −18 bin/dough-client
  2. +16 −7 bin/dough-farmer
  3. +14 −6 dough/billing/api.py
  4. +23 −9 dough/billing/driver/load_balancer.py
  5. +27 −10 dough/client/dough_client.py
  6. +10 −11 dough/db/sqlalchemy/api.py
View
@@ -18,10 +18,6 @@
# under the License.
import sys
-#import time
-#import ConfigParser
-#import json
-#import zmq
from collections import OrderedDict
from dough.client.dough_client import DoughClient
@@ -31,13 +27,18 @@ def show_usage():
print "\tdough_client"
print "\tdough_client <param> <tenant_id> <time_from> [time_to] [period_hours]"
print "param:"
- print "\t -m : query_monthly_report"
- print "\t -d : query_report"
- print "./dough-client -d 1adfb274fee24dbd96ea89b57d110fc5 2012-06-01T00:00:00 2012-07-01T00:00:00 days network resource_name"
- print "\t -u : unsubscribe_item"
- print "./dough-client -u user_id, tenant_id, region, resource_uuid, item, timestamp"
- print "\t -s : subscribe_item"
- print "./dough-client -s user_id tenant_id resource_uuid resource_name region item item_type payment_type timestamp"
+ print "\t -m : query_monthly_report"
+ print "\t -d : query_report"
+ print "\t -u : unsubscribe_item"
+ print "\t -s : subscribe_item"
+ print "\t -l : get_load_balancers"
+ print "example:"
+ print "\t./dough-client -s user_id tenant_id resource_uuid resource_name region item item_type payment_type timestamp"
+ print "\t./dough-client -u user_id, tenant_id, region, resource_uuid, item, timestamp"
+ print "\t./dough-client -d 1adfb274fee24dbd96ea89b57d110fc5 2012-06-01T00:00:00 2012-07-01T00:00:00 days network resource_name"
+ print "\tget_all: ./dough-client -l user_id tenant_id"
+ print "\tget_by_uuid: ./dough-client -l user_id tenant_id load_balancer_uuid"
+ print "\tisrunning?: ./dough-client -l load_balancer_uuid"
def show_result(data):
@@ -121,15 +122,34 @@ def main():
show_report(data)
elif len(sys.argv) > 6:
return
- elif len(sys.argv) > 5:
- tenant_id = sys.argv[3]
- time_from = sys.argv[4]
- time_to = sys.argv[5]
- if sys.argv[2] == '-m':
- data = client.query_monthly_report(tenant_id, time_from, time_to)
+ elif len(sys.argv) >= 3:
+ if sys.argv[2] == '-l':
+ if len(sys.argv) == 5:
+ user_id = sys.argv[3]
+ tenant_id = sys.argv[4]
+ data = client.load_balancer_get_all(user_id, tenant_id)
+ print data
+ elif len(sys.argv) == 6:
+ user_id = sys.argv[3]
+ tenant_id = sys.argv[4]
+ lb_id = sys.argv[5]
+ data = client.load_balancer_get(user_id, tenant_id, lb_id)
+ print data
+ elif len(sys.argv) == 4:
+ lb_id = sys.argv[3]
+ data = client.load_balancer_is_running(lb_id)
+ print data
+ else:
+ pass
+ elif sys.argv[2] == '-m':
+ if len(sys.argv) == 6:
+ tenant_id = sys.argv[3]
+ time_from = sys.argv[4]
+ time_to = sys.argv[5]
+ data = client.query_monthly_report(tenant_id, time_from, time_to)
+ show_result(data)
else:
pass
- show_result(data)
else:
pass
View
@@ -1,22 +1,23 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
-import datetime
+#import datetime
import sys
import time
-import traceback
+#import traceback
-from dateutil.relativedelta import relativedelta
-import zmq
+#from dateutil.relativedelta import relativedelta
+#import zmq
from nova import flags
from nova import log as logging
from nova import utils
-from dough import billing
+#from dough import billing
from dough import context as dough_context
from dough import db
-from dough import exception
+#from dough import exception
+from kanyun.common.app import App
utils.default_flagfile(filename='/etc/dough/dough.conf')
flags.FLAGS(sys.argv)
@@ -26,10 +27,13 @@ FLAGS = flags.FLAGS
if __name__ == '__main__':
from dough.billing import api
+ app = App(conf="dough/dough.conf", name="farmer")
context = dough_context.get_admin_context()
+ context.app = app
while True:
current_time = utils.utcnow()
- print "-" * 50, str(current_time)
+ print "-" * 30, str(current_time)
+ app.info("------------ farming ----------")
subscriptions = list()
_subscriptions = db.subscription_get_all(context)
for sub in _subscriptions:
@@ -64,9 +68,14 @@ if __name__ == '__main__':
created_at, updated_at, expires_at, order_unit,
order_size, price, currency, region_name, item_name,
interval_unit, interval_size, is_prepaid) in subscriptions:
+ print "-" * 20
+ print "farming:subid=", subscription_id, "resid=", resource_uuid, "tid=", tenant_id
+ print "item_name=", item_name
+
action = getattr(api, status)
action(context, subscription_id, tenant_id, resource_uuid,
created_at, updated_at, expires_at, order_unit,
order_size, price, currency, region_name,
item_name, interval_unit, interval_size, is_prepaid)
+
time.sleep(600)
View
@@ -17,7 +17,6 @@
# under the License.
from dateutil.relativedelta import relativedelta
-
from nova import utils
from dough import db
@@ -26,8 +25,9 @@
def creating(context, subscription_id, tenant_id, resource_uuid,
created_at, updated_at, expires_at,
- order_unit, order_size, price, currency, region_name,
+ order_unit, order_size, price, currency, region_name,
item_name, interval_unit, interval_size, is_prepaid):
+ app = context.app
conn = driver.get_connection(item_name)
if not conn.is_running(resource_uuid):
if created_at + relativedelta(minutes=10) < utils.utcnow():
@@ -44,6 +44,7 @@ def creating(context, subscription_id, tenant_id, resource_uuid,
expires_at, order_size)
print "creating", tenant_id, subscription_id, \
quantity, order_size, "\033[1;33m", price, "\033[0m"
+ app.info("creating %s:subid=%s,tid=%s,price=%s" % (item_name, subscription_id, tenant_id, str(price)))
charge(context, tenant_id, subscription_id, quantity,
order_size, price)
db.subscription_extend(context, subscription_id,
@@ -52,8 +53,9 @@ def creating(context, subscription_id, tenant_id, resource_uuid,
def deleting(context, subscription_id, tenant_id, resource_uuid,
created_at, updated_at, expires_at,
- order_unit, order_size, price, currency, region_name,
+ order_unit, order_size, price, currency, region_name,
item_name, interval_unit, interval_size, is_prepaid):
+ app = context.app
conn = driver.get_connection(item_name)
if not conn.is_terminated(resource_uuid):
if updated_at + relativedelta(minutes=10) < utils.utcnow():
@@ -71,14 +73,16 @@ def deleting(context, subscription_id, tenant_id, resource_uuid,
expires_at, order_size)
print "deleting", tenant_id, subscription_id, \
quantity, order_size, "\033[1;33m", price, "\033[0m"
+ app.info("deleting %s:subid=%s,tid=%s,price=%s" % (item_name, subscription_id, tenant_id, str(price)))
charge(context, tenant_id, subscription_id, quantity,
order_size, price)
def verified(context, subscription_id, tenant_id, resource_uuid,
created_at, updated_at, expires_at,
- order_unit, order_size, price, currency, region_name,
+ order_unit, order_size, price, currency, region_name,
item_name, interval_unit, interval_size, is_prepaid):
+ app = context.app
conn = driver.get_connection(item_name)
if not conn.is_running(resource_uuid):
# FIXME(lzyeval): raise Exception()
@@ -91,23 +95,27 @@ def verified(context, subscription_id, tenant_id, resource_uuid,
expires_at, order_size)
print "verified", tenant_id, subscription_id, \
quantity, order_size, "\033[1;33m", price, "\033[0m"
+ app.info("verified %s:subid=%s,tid=%s,price=%s" % (item_name, subscription_id, tenant_id, str(price)))
charge(context, tenant_id, subscription_id, quantity, order_size, price)
db.subscription_extend(context, subscription_id,
expires_at + relativedelta(**interval_info))
-def error(*args, **kwargs):
+def error(context, *args, **kwargs):
# TODO(lzyeval): report
+ print "error", args, kwargs
return
def charge(context, tenant_id, subscription_id, quantity, order_size, price):
if not quantity:
return
- line_total = price * quantity / order_size
+ line_total = price * quantity / order_size
values = {
'subscription_id': subscription_id,
'quantity': quantity,
'line_total': line_total,
}
+ print "purchase_create, subid=", subscription_id, values
+ context.app.info("purchase_create:subid=%s, line_total=%s" % (subscription_id, str(line_total)))
db.purchase_create(context, values)
@@ -16,7 +16,7 @@
# License for the specific language governing permissions and limitations
# under the License.
-import zmq
+import zmq
from nova import flags
from nova import utils
@@ -32,6 +32,7 @@ def __init__(self, protocol="tcp", host="localhost", port="80"):
context = zmq.Context()
self.handler = context.socket(zmq.REQ)
self.handler.connect(url)
+ print "connect", url
def __del__(self):
self.handler.close()
@@ -44,22 +45,35 @@ def send(self, msg_body):
r_msg_type, r_msg_uuid, r_msg_body = self.handler.recv_multipart()
assert (all([x == y for x, y in zip([msg_type, msg_uuid],
[r_msg_type, r_msg_uuid])]))
- result = utils.loads(r_msg_body)['msg']
+ #result = utils.loads(r_msg_body)['msg']
+ #if result['code'] == 500:
+ result = utils.loads(r_msg_body)
if result['code'] == 500:
- raise Exception()
+ return None
else:
- return result['load_balancer_ids']
-
+ #return result['load_balancer_ids']
+ return result['data']
+print "connect load_balancer:", FLAGS.demux_host, FLAGS.demux_port
DEMUX_CLIENT = Client(host=FLAGS.demux_host, port=FLAGS.demux_port)
def is_running(load_balancer_uuid):
# TODO(lzyeval): handle error
- load_balancers = DEMUX_CLIENT.send({'cmd': 'read_load_balancer_id_all',
- 'msg': {'user_name': 'foo',
- 'tenant': 'bar',}})
- return load_balancer_uuid in load_balancers
+ #load_balancers = DEMUX_CLIENT.send({'cmd': 'read_load_balancer_id_all',
+ # 'msg': {'user_name': 'foo',
+ # 'tenant': 'bar'}})
+ tenant_id = None
+ user_id = None
+ load_balancer = DEMUX_CLIENT.send({'method': 'get_load_balancer',
+ 'args': {'load_balancer_uuid': load_balancer_uuid,
+ 'tenant_id': tenant_id,
+ 'user_id': user_id}})
+ print load_balancer_uuid, "load_balancer is_running:", load_balancer
+ if load_balancer is None:
+ return False
+ return 'state' in load_balancer \
+ and load_balancer['state'] == "active"
def is_terminated(load_balancer_uuid):
@@ -18,14 +18,8 @@
# under the License.
import sys
-#import time
-#from md5 import md5
import json
import zmq
-#import urllib2
-#import datetime
-#import base64
-#from collections import OrderedDict
from nova import utils
from nova import flags
@@ -35,17 +29,14 @@
utils.default_flagfile(filename='/etc/dough/dough.conf')
logging.setup()
+
api_opts = [
cfg.StrOpt('api_host',
default='127.0.0.1',
help='IP address of dough API.'),
cfg.IntOpt('api_port',
default=8783,
help='Port of dough api.'),
-# cfg.StrOpt('monthly_report',
-# short='m',
-# default='name1',
-# help='monthly_report.'),
]
cli_opts = [
@@ -61,13 +52,19 @@
short='u',
default='default1',
help='unsubscribe_item.'),
+ cfg.StrOpt('load_balancer',
+ short='l',
+ default='default1',
+ help='load_balancer.'),
]
FLAGS = flags.FLAGS
FLAGS.register_cli_opts(cli_opts)
FLAGS.register_opts(api_opts)
flags.FLAGS(sys.argv)
+from dough.billing.driver import load_balancer
+
STANDARD_PROTOCOL = {
'method': 'query_report',
'args': {
@@ -149,3 +146,23 @@ def unsubscribe_item(self, user_id, tenant_id, region, resource_uuid, item, time
data = self.invoke(request)
return data
+
+ def load_balancer_get_all(self, user_id, tenant_id):
+ data = load_balancer.DEMUX_CLIENT.send({'method': 'get_all_load_balancers',
+ 'args': {'user_id': user_id,
+ 'tenant_id': tenant_id}})
+
+ return data
+
+ def load_balancer_get(self, user_id, tenant_id, lb_id):
+ data = load_balancer.DEMUX_CLIENT.send({'method': 'get_load_balancer',
+ 'args': {'user_id': user_id,
+ 'tenant_id': tenant_id,
+ 'load_balancer_uuid': lb_id,
+ }})
+
+ return data
+
+ def load_balancer_is_running(self, uuid):
+ ret = load_balancer.is_running(uuid)
+ return ret
Oops, something went wrong.

0 comments on commit e4f9eb9

Please sign in to comment.