예제 #1
0
파일: pusher.py 프로젝트: jojax/duckspush
def start_push_project():
    parser = OptionParser(
        usage="usage: %prog [options] <project_name> <api_key>",
        version="%prog 1.0")
    parser.add_option("-d", "--dashboard",
                      action="store",
                      dest="dashboard",
                      help="limit widgets collected to this dashboard",)
    parser.add_option("-l", "--limit",
                      action="store",
                      type="int",
                      dest="limit",
                      help="Limit number of collected widgets",)
    parser.add_option("-c", "--config",
                      action="store",
                      type="str",
                      dest="config",
                      help="Location of configuration file",
                      default="duckspush_settings.yaml")
    ## Todo
    # parser.add_option("-p", "--datasources-path",
    #                   action="store",
    #                   type="string",
    #                   dest="datasources_module",
    #                   help="Path of module containing datasources",)
    (options, args) = parser.parse_args()

    if len(args) != 2:
        parser.error("wrong number of arguments")
        print parser.usage

    duckspush_settings_path = options.config
    project_name = args[0]
    try:
        with open(duckspush_settings_path) as f:
            settings_data = f.read()
        proj = yaml.load(settings_data)["projects"].get(project_name)
    except IOError:
        pass
    else:
        if proj:
            sys.exit("A project already exist under this name.")

    api_key = args[1]
    dashboard_api_cli = api.get_api_cli(api_key, "dashboard")
    # Trying to get custom widgets for this api key
    try:
        widgets_data = dashboard_api_cli.read_widgets()["data"]
    except HTTPError, e:
        if e.response.status_code == 401:
            sys.exit("Your api key %s is incorrect.Please set a correct one"
                     % api_key)
예제 #2
0
파일: pusher.py 프로젝트: jojax/duckspush
def run_pusher():
    parser = OptionParser(usage="usage: %prog [options] <project_name>",
                          version="%prog 1.0")
    parser.add_option("-p", "--push-interval",
                      action="store",
                      dest="push_interval",
                      type="int",
                      default=30,
                      help="Push data interval => sec")
    parser.add_option("-t", "--collectors_timeout",
                      action="store",
                      dest="timeout",
                      type="int",
                      default=10,
                      help="Timeout for data collection",)
    parser.add_option("-l", "--log-level",
                      action="store",
                      dest="log_level",
                      type="str",
                      default="INFO",
                      help="Timeout for data collection",)
    parser.add_option("-c", "--config",
                      action="store",
                      type="str",
                      dest="config",
                      help="Location of configuration file",
                      default="duckspush_settings.yaml")

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("wrong number of arguments")
        print parser.usage

    formatter = logging.Formatter(
        '[%(name)s|%(asctime)s|%(levelname)s]:%(message)s')
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)

    # settings loggers
    for l in loggers:
        try:
            l.setLevel(getattr(logging, options.log_level.upper()))
        except AttributeError:
            sys.exit("Unknow log_level: %s.Please set a correct one"
                     % options.log_level)
        l.addHandler(handler)

    duckspush_settings_path = options.config
    project_name = args[0]
    with open(duckspush_settings_path) as f:
        settings_data = f.read()
    try:
        proj_settings = yaml.load(settings_data)["projects"][project_name]
    except KeyError:
        sys.exit("Project %s does not exists" % project_name)

    sys.path.append(proj_settings.get("path"))
    pusher = DucksboardPusher(proj_settings)
    pusher.push_api_cli = api.get_api_cli(proj_settings.get("api_key"),
                                          "push")
    try:
        pusher.run(push_interval=options.push_interval,
                   collectors_timeout=options.timeout)
        gevent.signal(signal.SIGQUIT, gevent.shutdown)
    except KeyboardInterrupt:
        pusher_logger.info("Stopping pusher")