Permalink
Browse files

pyw's discovery

  • Loading branch information...
1 parent dd8e37a commit c0a1044a85503046b5b1830c492c680aa092da9b @pengyuwei committed Jun 12, 2012
View
@@ -6,9 +6,9 @@ import sys
import zmq
+from nova import utils
from nova import flags
from nova import log as logging
-from nova import utils
from dough import api
from dough import context as dough_context
@@ -25,6 +25,7 @@ if __name__ == '__main__':
# Socket to receive messages on
handler = zmq_context.socket(zmq.REP)
handler.bind("tcp://%s:%s" % (FLAGS.api_listen, FLAGS.api_listen_port))
+ print "listen:", FLAGS.api_listen, FLAGS.api_listen_port
poller = zmq.Poller()
poller.register(handler, zmq.POLLIN)
@@ -40,8 +41,13 @@ if __name__ == '__main__':
method = msg_body['method']
args = msg_body['args']
context = dough_context.get_context(**args)
+ print "-"*60
+ print "\033[0;31m" + method + "\033[0m:"
+ print args
method_func = getattr(api, method)
response = method_func(context, **args)
+ print "response:"
+ print response
except Exception, e:
print traceback.format_exc()
cli_msg['code'] = 500
View
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+#
+# Copyright 2012 Sina Corporation
+# All Rights Reserved.
+# Author: YuWei Peng <pengyuwei@gmail.com>
+#
+# 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 sys
+import time
+import ConfigParser
+import json
+import zmq
+from collections import OrderedDict
+from dough.client.dough_client import *
+
+def show_usage():
+ print "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 d8462edac32e4d688ae20d67a51d529f 2012-01-01T00:00:00 2012-07-07T00:00:00 24"
+
+def show_result(data):
+ print data
+
+def main():
+ if len(sys.argv) == 2:
+ show_usage()
+ return
+ if len(sys.argv) == 3:
+ if sys.argv[2] in ['--help', "-h", "?"]:
+ show_usage()
+ return
+
+ data = None
+ client = DoughClient()
+
+ if len(sys.argv) > 6:
+ tenant_id = sys.argv[3]
+ time_from = sys.argv[4]
+ time_to = sys.argv[5]
+ period = sys.argv[6]
+ if sys.argv[2] == '-d':
+ data = client.query_report(tenant_id, time_from, time_to, period)
+ else:
+ pass
+
+ show_result(data)
+ 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)
+ else:
+ pass
+ show_result(data)
+ else:
+ pass
+
+if __name__ == '__main__':
+ main()
View
@@ -0,0 +1,134 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+#
+# Copyright 2012 Sina Corporation
+# All Rights Reserved.
+# Author: YuWei Peng <pengyuwei@gmail.com>
+#
+# 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 datetime
+import sys
+import time
+import traceback
+
+#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.billing import api
+from dough import context as dough_context
+from dough import db
+from dough import api
+from nova.openstack.common import cfg
+#from dough import exception
+
+utils.default_flagfile(filename='/etc/dough/dough.conf')
+FLAGS = flags.FLAGS
+#logging.setup()
+
+manager_opts = [
+ cfg.StrOpt('resource_name',
+ short='r',
+ default='name1',
+ help='resource_name.'),
+ cfg.StrOpt('purchase',
+ short='p',
+ default='name1',
+ help='purchase.'),
+ cfg.StrOpt('help',
+ short='?',
+ default='',
+ help='help'),
+ ]
+
+FLAGS.register_cli_opts(manager_opts)
+flags.FLAGS(sys.argv)
+
+def show_usage():
+ print "dough-mansger -n <resource_name>"
+
+def get_subs(context, param):
+ print "filter: resource_name=", param
+ try:
+ sub = db.subscription_get_byname(context, resource_name=param)
+ subscription_id = sub['id']
+ status = sub['status']
+ tenant_id = sub['project_id']
+ resource_uuid = sub['resource_uuid']
+ created_at = sub['created_at']
+ updated_at = sub['updated_at']
+ expires_at = sub['expires_at']
+ order_unit = sub['product']['order_unit']
+ order_size = sub['product']['order_size']
+ price = sub['product']['price']
+ currency = sub['product']['currency']
+
+ region_name = sub['product']['region']['name']
+ item_name = sub['product']['item']['name']
+ pay_type = sub['product']['payment_type']
+ interval_unit = pay_type['interval_unit']
+ interval_size = pay_type['interval_size']
+ is_prepaid = pay_type['is_prepaid']
+ print '-'*60
+ print "%24s : %s" % ("subscription_id", str(subscription_id))
+ print "%24s : %s" % ("tenant_id", str(tenant_id))
+ print "%24s : %s" % ("expires_at", str(expires_at))
+ print "%24s : %s" % ("price", str(price))
+ print "%24s : %s" % ("region_name", str(region_name))
+ print "%24s : %s" % ("item_name", str(item_name))
+# print "%24s : %s" % ("pay_type", str(pay_type))
+ print "%24s : %s" % ("created_at", str(created_at))
+ except Exception, e:
+ print Exception, e
+
+def query_report(tenant_id, timestamp_from, timestamp_to):
+ datetime_from = iso8601.parse_date(timestamp_from)
+ datetime_to = iso8601.parse_date(timestamp_to)
+ context = dough_context.get_context(tenant_id=self.tenant_id)
+ data = api.query_report(self.context,
+ timestamp_from,
+ timestamp_to)
+ return data
+
+"""
+select id, subscription_id, created_at, quantity from purchases where subscription_id =151 and created_at>"2012-06-03 00:00:00";
+"""
+
+def main():
+ baselen = 2
+ context = dough_context.get_admin_context()
+ if len(sys.argv) == baselen:
+ get_subs(context, sys.argv)
+ return
+ if len(sys.argv) == baselen:
+ if sys.argv[baselen-1] in ['--help', "-h", "?"]:
+ show_usage()
+ return
+
+ if len(sys.argv) == baselen + 2:
+ param = sys.argv[baselen + 1]
+ if sys.argv[baselen][:2] == '-r':
+ get_subs(context, param)
+ elif len(sys.argv) == baselen + 4:
+ tenant_id = sys.argv[baselen + 1]
+ timestamp_from = sys.argv[baselen + 2]
+ timestamp_to = sys.argv[baselen + 3]
+ print query_report(tenant_id, timestamp_from, timestamp_to)
+
+
+if __name__ == '__main__':
+ main()
Oops, something went wrong.

0 comments on commit c0a1044

Please sign in to comment.