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

Commit

Permalink
Fix bugs and adjust behaviours (issue #14)
Browse files Browse the repository at this point in the history
- continuous report run: send the the payload (received chunk)
- manage json Marshal conversion
- manage and propagate received exit_code
- polling, send result always (not only success case)

Closes #14
  • Loading branch information
pcantera committed Dec 12, 2017
1 parent ee9daa4 commit 47b9aba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
3 changes: 1 addition & 2 deletions api/polling/polling_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ func (p *PollingService) UpdateCommand(pollingCommandVector *map[string]interfac
func (p *PollingService) ReportBootstrapLog(PollingContinuousReportVector *map[string]interface{}) (command *types.PollingContinuousReport, status int, err error) {
log.Debug("ReportBootstrapLog")

payload := make(map[string]interface{})
data, status, err := p.concertoService.Post("/command_polling/bootstrap_logs", &payload)
data, status, err := p.concertoService.Post("/command_polling/bootstrap_logs", PollingContinuousReportVector)
if err != nil {
return nil, status, err
}
Expand Down
18 changes: 11 additions & 7 deletions cmdpolling/continuousreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"time"
"os"
"encoding/json"

log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
Expand Down Expand Up @@ -44,12 +46,12 @@ func cmdContinuousReportRun(c *cli.Context) error {
command := types.PollingContinuousReport{
Stdout: chunk,
}
commandIn, err := utils.ItemConvertParamsWithTagAsID(command)
if err != nil {
return fmt.Errorf("error parsing payload %v", err)
}

_, statusCode, err := pollingSvc.ReportBootstrapLog(commandIn)
var commandIn map[string]interface{}
inRec, _ := json.Marshal(command)
json.Unmarshal(inRec, &commandIn)

_, statusCode, err := pollingSvc.ReportBootstrapLog(&commandIn)
switch {
// 0<100 error cases??
case statusCode == 0:
Expand All @@ -69,11 +71,13 @@ func cmdContinuousReportRun(c *cli.Context) error {
return nil
}

if err := utils.RunContinuousReportRun(fn, cmdArg, thresholds); err != nil {
exitCode, err := utils.RunContinuousReportRun(fn, cmdArg, thresholds)
if err != nil {
formatter.PrintFatal("cannot process continuous report command", err)
}

log.Info("completed")
log.Info("completed: ", exitCode)
os.Exit(exitCode)
return nil
}

Expand Down
31 changes: 14 additions & 17 deletions cmdpolling/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/signal"
"syscall"
"time"
"encoding/json"

log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
Expand Down Expand Up @@ -132,26 +133,22 @@ func processingCommandRoutine(pollingSvc *polling.PollingService, formatter form
command.Stdout = ""
}

// 3. If command successfully executed, then status is propagated to IMCO
if command.ExitCode == 0 {
log.Debug("Reporting command execution status")
commandIn, err := utils.ItemConvertParamsWithTagAsID(*command)
if err != nil {
formatter.PrintError("Couldn't send polling command report data; error parsing payload", err)
}
// 3. then status is propagated to IMCO
log.Debug("Reporting command execution status")

_, status, err := pollingSvc.UpdateCommand(commandIn, command.Id)
if err != nil {
formatter.PrintError("Couldn't send polling command report data", err)
}
var commandIn map[string]interface{}
inRec, _ := json.Marshal(command)
json.Unmarshal(inRec, &commandIn)

if status == 200 {
log.Debug("Command execution results successfully reported")
} else {
log.Error("Cannot report the command execution results")
}
_, status, err := pollingSvc.UpdateCommand(&commandIn, command.Id)
if err != nil {
formatter.PrintError("Couldn't send polling command report data", err)
}

if status == 200 {
log.Debug("Command execution results successfully reported")
} else {
log.Error("Cannot run the retrieved command")
log.Error("Cannot report the command execution results")
}
} else {
log.Error("Cannot retrieve the next command")
Expand Down
12 changes: 0 additions & 12 deletions utils/cli_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,6 @@ func ItemConvertParams(item interface{}) (*map[string]interface{}, error) {
return &v, nil
}

// ItemConvertParamsWithTagAsID converts param items into map of interface with json tags
func ItemConvertParamsWithTagAsID(item interface{}) (*map[string]interface{}, error) {
it := reflect.ValueOf(item)
nf := it.NumField()
v := make(map[string]interface{})

for i := 0; i < nf; i++ {
v[it.Type().Field(i).Tag.Get("json")] = fmt.Sprintf("%s", it.Field(i).Interface())
}
return &v, nil
}

// JSONParam parses parameter as json structure
func JSONParam(param string) (interface{}, error) {
var p interface{}
Expand Down
14 changes: 9 additions & 5 deletions utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func RunCmd(command string) (output string, exitCode int, startedAt time.Time, f
return
}

func RunContinuousReportRun(fn func(chunk string) error, cmdArg string, thresholds Thresholds) error {
func RunContinuousReportRun(fn func(chunk string) error, cmdArg string, thresholds Thresholds) (int, error) {
log.Debug("RunContinuousReportRun")

// command thresholds flags
Expand Down Expand Up @@ -193,13 +193,13 @@ func RunContinuousReportRun(fn func(chunk string) error, cmdArg string, threshol
// Gets the pipe command
stdout, err := newCmd.StdoutPipe()
if err != nil {
return fmt.Errorf("cannot get pipe command %v", err)
return 1, fmt.Errorf("cannot get pipe command %v", err)
}
log.Info("==> Executing: ", strings.Join(newCmd.Args, " "))

// Start command asynchronously
if err = newCmd.Start(); err != nil {
return fmt.Errorf("cannot start the specified command %v", err)
return 1, fmt.Errorf("cannot start the specified command %v", err)
}

chunk := ""
Expand Down Expand Up @@ -232,8 +232,12 @@ func RunContinuousReportRun(fn func(chunk string) error, cmdArg string, threshol
if len(chunk) > 0 {
log.Debug("Sending the last pending chunk")
if err := fn(chunk); err != nil {
return err
log.Error("Cannot send the last chunk", err.Error())
}
}
return nil

err = newCmd.Wait()
exitCode := extractExitCode(err)

return exitCode, nil
}

0 comments on commit 47b9aba

Please sign in to comment.