add utils for client side

move common functions there

Change-Id: I51bf2dd4425b5cc385cc0fd5d3734bc71548f740
This commit is contained in:
Kun Huang 2015-11-11 12:37:32 +08:00
parent 89a11930d0
commit 472f4ae4d2
8 changed files with 70 additions and 82 deletions

View File

@ -23,8 +23,8 @@ class TraceEndpoint(object):
def tracer_list(self, ctx): def tracer_list(self, ctx):
# TODO db_api # TODO db_api
# XXX ctx required? # XXX ctx required?
from scalpels.cli.actions.start import agents_map from scalpels.cli.utils import traces_map
return agents_map return traces_map
def start_tracers(self, ctx, tracers): def start_tracers(self, ctx, tracers):
print locals() print locals()

View File

@ -15,7 +15,7 @@ def run(config):
continue continue
loadcall(config) loadcall(config)
def get_creds_from_env(): def _get_creds_from_env():
user = os.environ.get("OS_USERNAME") user = os.environ.get("OS_USERNAME")
pw = os.environ.get("OS_PASSWORD") pw = os.environ.get("OS_PASSWORD")
tenant = os.environ.get("OS_TENANT_NAME") tenant = os.environ.get("OS_TENANT_NAME")
@ -23,7 +23,7 @@ def get_creds_from_env():
return (user, pw, tenant, auth_url) return (user, pw, tenant, auth_url)
def nova_boot_bulk(): def nova_boot_bulk():
creds = get_creds_from_env() creds = _get_creds_from_env()
if None in creds: if None in creds:
raise ValueError("can't find all necessary creds from env: %s" % creds) raise ValueError("can't find all necessary creds from env: %s" % creds)
nova = client.Client(2, *creds) nova = client.Client(2, *creds)

View File

@ -3,54 +3,14 @@
# Author: Kun Huang <academicgareth@gmail.com> # Author: Kun Huang <academicgareth@gmail.com>
from scalpels.cli.api import api as agent_api from scalpels.cli.api import api as agent_api
from prettytable import PrettyTable from scalpels.cli.utils import generate_multiple_result_html
from mako.lookup import TemplateLookup from scalpels.cli.utils import pprint_result
from scalpels import templates
import os
def pprint_result(result):
print "<result %s>" % result["uuid"]
t = PrettyTable(["timestamp", "%s (%s)" % (result["name"], result["unit"])])
for data in result["data"]:
t.add_row([data[0], data[1][:100]])
print t
LOWEST=8
def generate_result_html(result):
if result.rtype == "stream":
tmpl_dir = os.path.dirname(templates.__file__)
lookup = TemplateLookup(directories=[tmpl_dir])
t = lookup.get_template("line-chart.mako")
print t.render(**result.__dict__)
def generate_multiple_result_html(results):
tmpl_dir = os.path.dirname(templates.__file__)
lookup = TemplateLookup(directories=[tmpl_dir])
t = lookup.get_template("multi-line-chart.mako")
d = {"results": results}
print t.render(**d)
def run(config): def run(config):
uuid = config.get("uuid") task = agent_api.try_get_task_from_config(config)
last = config.get("last")
if last and uuid: print "reporting task: <%s>" % task["uuid"]
raise ValueError("can't assign last and uuid togther")
elif not last and not uuid:
task = agent_api.get_latest_task()
elif last:
task = agent_api.get_latest_task()
elif uuid and len(uuid) < LOWEST:
print "at least %d to find a task" % LOWEST
return
else:
# len(uuid) > LOWEST
task = agent_api.get_task(uuid, fuzzy=True)
print "command report: %s" % config
print "task: <%s>" % task["uuid"]
rets = [] rets = []
for ret_uuid in task["results"]: for ret_uuid in task["results"]:
ret = agent_api.get_result(ret_uuid) ret = agent_api.get_result(ret_uuid)

View File

@ -29,19 +29,6 @@ def _parse_agents_from_file(config):
parsed_agents.add(ag["name"]) parsed_agents.add(ag["name"])
return parsed_agents return parsed_agents
# TODO this map should be saved in a config file
# TODO refar to pre/exec/post
agents_map = {
"mysql": "bash %s/mysql-live.sh", #XXX doesn't work now, needs works on interapt pipeline
"rabbit": "python %s/rbt-trace.py",
"rpc": "bash %s/port-input-traffic.sh 5672",
"traffic": "bash %s/device-input-traffic.sh eth0",
"oslolock": "stap %s/oslo-lock.stp", # with sudo, need add current user to stapdev group
"modelsave": "stap %s/model-save.stp", # with sudo, need add current user to stapdev group
"sqlaexec": "stap %s/sqla-exec.stp", # with sudo, need add current user to stapdev group
"rpccount": "stap %s/rpc-count.stp", # with sudo, need add current user to stapdev group
}
def run(config): def run(config):
print "command start: %s" % config print "command start: %s" % config
agents = _parse_agents_from_args(config) agents = _parse_agents_from_args(config)

View File

@ -2,29 +2,11 @@
#-*- coding:utf-8 -*- #-*- coding:utf-8 -*-
# Author: Kun Huang <academicgareth@gmail.com> # Author: Kun Huang <academicgareth@gmail.com>
from scalpels.db import api as db_api
from scalpels.cli.api import api as agent_api from scalpels.cli.api import api as agent_api
LOWEST=8
def get_last_task():
last_task = db_api.task_get_last()
return last_task
def run(config): def run(config):
uuid = config.get("uuid") task = agent_api.try_get_task_from_config(config)
last = config.get("last")
if last and uuid: print "stopping task: <%s>" % task["uuid"]
raise ValueError("can't assign last and uuid togther")
elif not last and not uuid:
task = agent_api.get_latest_task()
elif last:
task = agent_api.get_latest_task()
elif uuid and len(uuid) < LOWEST:
print "at least %d to find a task" % LOWEST
return
else:
# len(uuid) > LOWEST
task = agent_api.get_task(uuid, fuzzy=True)
agent_api.stop_task(task["uuid"]) agent_api.stop_task(task["uuid"])

View File

@ -4,6 +4,7 @@
from scalpels.cli.rpcapi import rpcapi from scalpels.cli.rpcapi import rpcapi
from scalpels.cli.utils import UUID_LOWEST_SUPPORT
class API(object): class API(object):
def __init__(self): def __init__(self):
@ -19,6 +20,8 @@ class API(object):
rpcapi.stop_task(uuid=uuid) rpcapi.stop_task(uuid=uuid)
def get_task(self, uuid, fuzzy=False): def get_task(self, uuid, fuzzy=False):
if fuzzy and len(uuid) < UUID_LOWEST_SUPPORT:
raise ValueError("fuzzy uuid query must get %s length" % UUID_LOWEST_SUPPORT)
return rpcapi.get_task(uuid=uuid, fuzzy=fuzzy) return rpcapi.get_task(uuid=uuid, fuzzy=fuzzy)
def get_latest_task(self): def get_latest_task(self):
@ -30,4 +33,15 @@ class API(object):
def get_all_results(self): def get_all_results(self):
return rpcapi.get_all_results() return rpcapi.get_all_results()
def try_get_task_from_config(self, config):
uuid = config.get("uuid")
last = config.get("last")
if last and uuid:
raise ValueError("can't assign last and uuid togther")
elif uuid:
return self.get_task(uuid, fuzzy=True)
else: # no matter whether last is set
return self.get_latest_task()
api = API() api = API()

45
scalpels/cli/utils.py Normal file
View File

@ -0,0 +1,45 @@
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author: Kun Huang <academicgareth@gmail.com>
from mako.lookup import TemplateLookup
from prettytable import PrettyTable
from scalpels import templates
import os
UUID_LOWEST_SUPPORT = 8
# TODO this map should be saved in a config file
# TODO refar to pre/exec/post
tracers_map = {
"mysql": "bash %s/mysql-live.sh", #XXX doesn't work now, needs works on interapt pipeline
"rabbit": "python %s/rbt-trace.py",
"rpc": "bash %s/port-input-traffic.sh 5672",
"traffic": "bash %s/device-input-traffic.sh eth0",
"oslolock": "stap %s/oslo-lock.stp", # with sudo, need add current user to stapdev group
"modelsave": "stap %s/model-save.stp", # with sudo, need add current user to stapdev group
"sqlaexec": "stap %s/sqla-exec.stp", # with sudo, need add current user to stapdev group
"rpccount": "stap %s/rpc-count.stp", # with sudo, need add current user to stapdev group
}
def generate_result_html(result):
if result.rtype == "stream":
tmpl_dir = os.path.dirname(templates.__file__)
lookup = TemplateLookup(directories=[tmpl_dir])
t = lookup.get_template("line-chart.mako")
print t.render(**result.__dict__)
def generate_multiple_result_html(results):
tmpl_dir = os.path.dirname(templates.__file__)
lookup = TemplateLookup(directories=[tmpl_dir])
t = lookup.get_template("multi-line-chart.mako")
d = {"results": results}
print t.render(**d)
def pprint_result(result):
print "<result %s>" % result["uuid"]
t = PrettyTable(["timestamp", "%s (%s)" % (result["name"], result["unit"])])
for data in result["data"]:
t.add_row([data[0], data[1][:100]])
print t

View File

@ -18,7 +18,7 @@ python <path-to-dir>/agent.py <uuid> mysql
def read_from_ag(ag): def read_from_ag(ag):
# wrong impl. here, need read from config or db instead # wrong impl. here, need read from config or db instead
from scalpels.cli.actions.start import agents_map from scalpels.cli.utils import tracers_map as agents_map
data_dir = db_api.setup_config_get()["data_dir"].rstrip("/") data_dir = db_api.setup_config_get()["data_dir"].rstrip("/")
return agents_map.get(ag) % data_dir return agents_map.get(ag) % data_dir