Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
added capability for task single run
Browse files Browse the repository at this point in the history
  • Loading branch information
candysmurf committed Apr 6, 2017
1 parent 7aa0e68 commit ad4f82d
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 194 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ OPTIONS:

```

```sh
$ snaptel task create -h

USAGE:
create [command options] [arguments...]

DESCRIPTION:
Creates a new task in the snap scheduler

OPTIONS:
--task-manifest value, -t value File path for task manifest to use for task creation.
--workflow-manifest value, -w value File path for workflow manifest to use for task creation
--interval value, -i value Interval for the task schedule [ex (simple schedule): 250ms, 1s, 30m (cron schedule): "0 * * * * *"]
--count value The count of runs for the task schedule [defaults to 0 what means no limit, e.g. set to 1 determines a single run task]
--start-date value Start date for the task schedule [defaults to today]
--start-time value Start time for the task schedule [defaults to now]
--stop-date value Stop date for the task schedule [defaults to today]
--stop-time value Start time for the task schedule [defaults to now]
--name value, -n value Optional requirement for giving task names
--duration value, -d value The amount of time to run the task [appends to start or creates a start time before a stop]
--no-start Do not start task on creation [normally started on creation]
--deadline value The deadline for the task to be killed after started if the task runs too long (All tasks default to 5s)
--max-failures value The number of consecutive failures before Snap disables the task

```
Example Usage
-------------
Expand Down Expand Up @@ -225,10 +251,11 @@ and then in another terminal:
12. unload the plugins
13. create a task using task manifest
14. create a task using workflow
15. list tasks
16. watch a task
17. export a task
18. stop a task
15. create a single run task
16. list tasks
17. watch a task
18. export a task
19. stop a task
```sh
$ snaptel plugin load /opt/snap/plugins/snap-plugin-collector-mock1
Expand All @@ -246,6 +273,7 @@ $ snaptel plugin config get -t <plugin_type> -n <plugin_name> -v <plugin_version
$ snaptel plugin unload collector <plugin_name> <plugin_version>
$ snaptel task create -t mock-file.json
$ snaptel task create -w workflow.json -i 1s
$ snaptel task create -t mock-file.yml --count 1
$ snaptel task list
$ snaptel task watch <task_id>
$ snaptel task export <task_id>
Expand Down
14 changes: 2 additions & 12 deletions snaptel/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ var (
{
Name: "create",
Description: "Creates a new task in the snap scheduler",
Usage: "There are two ways to create a task.\n\t1) Use a task manifest with [--task-manifest]\n\t2) Provide a workflow manifest and schedule details.\n\n\t* Note: Start and stop date/time are optional.\n",
Usage: "There are two ways to create a task.\n\t1) Use a task manifest with [--task-manifest]\n\t2) Provide a workflow manifest and schedule details.\n\n\t* Note: Start, stop date/time, and count are optional.\n\t* Using `task create -h` to see options.\n",
Action: createTask,
Flags: []cli.Flag{
flTaskManifest,
flWorkfowManifest,
flTaskSchedInterval,
flTaskSchedCount,
flTaskSchedStartDate,
flTaskSchedStartTime,
flTaskSchedStopDate,
Expand Down Expand Up @@ -120,17 +121,6 @@ var (
flRunning,
},
},
{
Name: "swap",
Usage: "swap <load_plugin_path> <unload_plugin_type>:<unload_plugin_name>:<unload_plugin_version> or swap <load_plugin_path> -t <unload_plugin_type> -n <unload_plugin_name> -v <unload_plugin_version>",
Action: swapPlugins,
Flags: []cli.Flag{
flPluginAsc,
flPluginType,
flPluginName,
flPluginVersion,
},
},
{
Name: "config",
Subcommands: []cli.Command{
Expand Down
79 changes: 40 additions & 39 deletions snaptel/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"fmt"

snapClient "github.com/intelsdi-x/snap-client-go/client"
"github.com/intelsdi-x/snap-client-go/client/snap"
"github.com/intelsdi-x/snap-client-go/client/plugins"
"github.com/intelsdi-x/snap-client-go/client/tasks"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -66,44 +67,44 @@ func GetFirstChar(s string) string {

func getErrorDetail(err error, ctx *cli.Context) error {
switch err.(type) {
case *snap.GetMetricsNotFound:
return newUsageError(fmt.Sprintf("Error getting metrics: %v", err.(*snap.GetMetricsNotFound).Payload.Message), ctx)
case *snap.GetMetricsInternalServerError:
return newUsageError(fmt.Sprintf("Error getting metrics: %v", err.(*snap.GetMetricsInternalServerError).Payload.Message), ctx)
case *snap.LoadPluginBadRequest:
return newUsageError(fmt.Sprintf("Error loading plugin: %v", err.(*snap.LoadPluginBadRequest).Payload.Message), ctx)
case *snap.LoadPluginConflict:
return newUsageError(fmt.Sprintf("Error loading plugin: %v", err.(*snap.LoadPluginConflict).Payload.Message), ctx)
case *snap.LoadPluginInternalServerError:
return newUsageError(fmt.Sprintf("Error loading plugin: %v", err.(*snap.LoadPluginInternalServerError).Payload.Message), ctx)
case *snap.LoadPluginUnsupportedMediaType:
return newUsageError(fmt.Sprintf("\nError loading plugin: %v", err.(*snap.LoadPluginUnsupportedMediaType).Payload.Message), ctx)
case *snap.UnloadPluginBadRequest:
return newUsageError(fmt.Sprintf("Error unloading plugin: %v", err.(*snap.UnloadPluginBadRequest).Payload.Message), ctx)
case *snap.UnloadPluginConflict:
return fmt.Errorf("Error unloading plugin: %v", err.(*snap.UnloadPluginConflict).Payload.Message)
case *snap.UnloadPluginInternalServerError:
return newUsageError(fmt.Sprintf("Error unloading plugin: %v", err.(*snap.UnloadPluginInternalServerError).Payload.Message), ctx)
case *snap.UnloadPluginNotFound:
return newUsageError(fmt.Sprintf("Error unloading plugin: %v", err.(*snap.UnloadPluginNotFound).Payload.Message), ctx)
case *snap.GetPluginBadRequest:
return newUsageError(fmt.Sprintf("Error getting plugin: %v", err.(*snap.GetPluginBadRequest).Payload.Message), ctx)
case *snap.GetPluginInternalServerError:
return newUsageError(fmt.Sprintf("Error getting plugin: %v", err.(*snap.GetPluginInternalServerError).Payload.Message), ctx)
case *snap.GetPluginNotFound:
return newUsageError(fmt.Sprintf("Error getting plugin: %v", err.(*snap.GetPluginNotFound).Payload.Message), ctx)
case *snap.GetPluginConfigItemBadRequest:
return newUsageError(fmt.Sprintf("Error getting plugin config item: %v", err.(*snap.GetPluginConfigItemBadRequest).Payload.Message), ctx)
case *snap.GetTaskNotFound:
return newUsageError(fmt.Sprintf("Error getting task: %v", err.(*snap.GetTaskNotFound).Payload.Message), ctx)
case *snap.AddTaskInternalServerError:
return newUsageError(fmt.Sprintf("Error creating task: %v", err.(*snap.AddTaskInternalServerError).Payload.Message), ctx)
case *snap.UpdateTaskStateBadRequest:
return newUsageError(fmt.Sprintf("Error updating task: %v", err.(*snap.UpdateTaskStateBadRequest).Payload.Message), ctx)
case *snap.UpdateTaskStateConflict:
return newUsageError(fmt.Sprintf("Error updating task: %v", err.(*snap.UpdateTaskStateConflict).Payload.Message), ctx)
case *snap.UpdateTaskStateInternalServerError:
return newUsageError(fmt.Sprintf("Error updating task: %v", err.(*snap.UpdateTaskStateInternalServerError).Payload.Message), ctx)
case *plugins.GetMetricsNotFound:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.GetMetricsNotFound).Payload.Message), ctx)
case *plugins.GetMetricsInternalServerError:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.GetMetricsInternalServerError).Payload.Message), ctx)
case *plugins.LoadPluginBadRequest:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.LoadPluginBadRequest).Payload.Message), ctx)
case *plugins.LoadPluginConflict:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.LoadPluginConflict).Payload.Message), ctx)
case *plugins.LoadPluginInternalServerError:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.LoadPluginInternalServerError).Payload.Message), ctx)
case *plugins.LoadPluginUnsupportedMediaType:
return newUsageError(fmt.Sprintf("\n%v", err.(*plugins.LoadPluginUnsupportedMediaType).Payload.Message), ctx)
case *plugins.UnloadPluginBadRequest:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.UnloadPluginBadRequest).Payload.Message), ctx)
case *plugins.UnloadPluginConflict:
return fmt.Errorf("%v", err.(*plugins.UnloadPluginConflict).Payload.Message)
case *plugins.UnloadPluginInternalServerError:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.UnloadPluginInternalServerError).Payload.Message), ctx)
case *plugins.UnloadPluginNotFound:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.UnloadPluginNotFound).Payload.Message), ctx)
case *plugins.GetPluginBadRequest:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.GetPluginBadRequest).Payload.Message), ctx)
case *plugins.GetPluginInternalServerError:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.GetPluginInternalServerError).Payload.Message), ctx)
case *plugins.GetPluginNotFound:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.GetPluginNotFound).Payload.Message), ctx)
case *plugins.GetPluginConfigItemBadRequest:
return newUsageError(fmt.Sprintf("%v", err.(*plugins.GetPluginConfigItemBadRequest).Payload.Message), ctx)
case *tasks.GetTaskNotFound:
return newUsageError(fmt.Sprintf("%v", err.(*tasks.GetTaskNotFound).Payload.Message), ctx)
case *tasks.AddTaskInternalServerError:
return newUsageError(fmt.Sprintf("%v", err.(*tasks.AddTaskInternalServerError).Payload.Message), ctx)
case *tasks.UpdateTaskStateBadRequest:
return newUsageError(fmt.Sprintf("%v", err.(*tasks.UpdateTaskStateBadRequest).Payload.Message), ctx)
case *tasks.UpdateTaskStateConflict:
return newUsageError(fmt.Sprintf("%v", err.(*tasks.UpdateTaskStateConflict).Payload.Message), ctx)
case *tasks.UpdateTaskStateInternalServerError:
return newUsageError(fmt.Sprintf("%v", err.(*tasks.UpdateTaskStateInternalServerError).Payload.Message), ctx)
default:
return newUsageError(fmt.Sprintf("Error: %v", err), ctx)
}
Expand Down
6 changes: 3 additions & 3 deletions snaptel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strconv"
"text/tabwriter"

"github.com/intelsdi-x/snap-client-go/client/snap"
"github.com/intelsdi-x/snap-client-go/client/plugins"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -62,12 +62,12 @@ func getConfig(ctx *cli.Context) error {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
defer w.Flush()

params := snap.NewGetPluginConfigItemParams()
params := plugins.NewGetPluginConfigItemParams()
params.SetPtype(ptyp)
params.SetPname(pname)
params.SetPversion(int64(pver))

resp, err := client.Snap.GetPluginConfigItem(params)
resp, err := client.Plugins.GetPluginConfigItem(params)
if err != nil {
return getErrorDetail(err, ctx)
}
Expand Down
5 changes: 5 additions & 0 deletions snaptel/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ var (
Usage: "Interval for the task schedule [ex (simple schedule): 250ms, 1s, 30m (cron schedule): \"0 * * * * *\"]",
}

flTaskSchedCount = cli.StringFlag{
Name: "count",
Usage: "The count of runs for the task schedule [defaults to 0 what means no limit, e.g. set to 1 determines a single run task]",
}

flTaskSchedStartTime = cli.StringFlag{
Name: "start-time",
Usage: "Start time for the task schedule [defaults to now]",
Expand Down
73 changes: 40 additions & 33 deletions snaptel/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,19 @@ import (
"text/tabwriter"
"time"

"github.com/intelsdi-x/snap-client-go/client/snap"
"github.com/intelsdi-x/snap-client-go/client/plugins"
"github.com/intelsdi-x/snap-client-go/models"
"github.com/urfave/cli"
)

func listMetrics(ctx *cli.Context) error {
verbose := ctx.Bool("verbose")

params := snap.NewGetMetricsParams()
resp, err := client.Snap.GetMetrics(params)
metrics, err := queryMetrics(ctx)
if err != nil {
return getErrorDetail(err, ctx)
}
if len(resp.Payload.Metrics) == 0 {
fmt.Println("No metrics found. Have you loaded any collectors yet?")
return nil
return err
}

/*
NAMESPACE VERSION
/intel/mock/foo 1,2
Expand All @@ -60,7 +56,7 @@ func listMetrics(ctx *cli.Context) error {
// /intel/mock/[host]/baz 2 mock unit mock description

printFields(w, false, 0, "NAMESPACE", "VERSION", "UNIT", "DESCRIPTION")
for _, mt := range resp.Payload.Metrics {
for _, mt := range metrics {
namespace := getNamespace(mt)
printFields(w, false, 0, namespace, mt.Version, mt.Unit, mt.Description)
}
Expand All @@ -70,7 +66,7 @@ func listMetrics(ctx *cli.Context) error {

// groups the same namespace of different versions.
metsByVer := make(map[string][]string)
for _, mt := range resp.Payload.Metrics {
for _, mt := range metrics {
metsByVer[*mt.Namespace] = append(metsByVer[*mt.Namespace], strconv.Itoa(int(mt.Version)))
}
//make list in alphabetical order
Expand Down Expand Up @@ -144,31 +140,16 @@ func getMetric(ctx *cli.Context) error {
if !ctx.IsSet("metric-namespace") {
return newUsageError("Error: Must provide metric namespace", ctx)
}
ns := ctx.String("metric-namespace")
ver := ctx.Int("metric-version")

params := snap.NewGetMetricsParams()
params.SetNs(&ns)
ver64 := int64(ver)
params.SetVer(&ver64)

resp, err := client.Snap.GetMetrics(params)
metrics, err := queryMetrics(ctx)
if err != nil {
return getErrorDetail(err, ctx)
}

metrics := resp.Payload.Metrics
len := len(metrics)
if len > 0 {
// Multiple metrics
for i, m := range metrics {
err := printMetric(m, i)
if err != nil {
return err
}
return err
}

for i, m := range metrics {
err := printMetric(m, i)
if err != nil {
return err
}
} else {
return fmt.Errorf("Error getting metrics: No metric found the giving namespace %s, version %d", ns, ver)
}
return nil
}
Expand All @@ -185,3 +166,29 @@ func getNamespace(mt *models.Metric) string {
}
return *ns
}

func queryMetrics(ctx *cli.Context) ([]*models.Metric, error) {
ns := ctx.String("metric-namespace")
ver := ctx.Int("metric-version")
params := plugins.NewGetMetricsParams()

if strings.Trim(ns, " ") != "" {
params.SetNs(&ns)
}
if ver > 0 {
ver64 := int64(ver)
params.SetVer(&ver64)
}

resp, err := client.Plugins.GetMetrics(params)
if err != nil {
return nil, getErrorDetail(err, ctx)
}

if (len(ns) > 0 || ver > 0) && len(resp.Payload.Metrics) == 0 {
return nil, fmt.Errorf("No metric found the giving namespace %s, version %d", ns, ver)
} else if len(resp.Payload.Metrics) == 0 {
return nil, fmt.Errorf("No metrics found. Have you loaded any collectors yet?")
}
return resp.Payload.Metrics, nil
}
Loading

0 comments on commit ad4f82d

Please sign in to comment.