Skip to content

Commit

Permalink
Merge pull request #8 from cloudradar-monitoring/DEV-2387
Browse files Browse the repository at this point in the history
Collection of fixes
  • Loading branch information
martinlindhe authored Dec 7, 2021
2 parents fede347 + 4fb998c commit b4afb1f
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 56 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ jobs:
name: Sca
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.42
args: -c .golangci.yml

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Test
run: |
go test -race -coverprofile=profile.cov ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
Expand Down
6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ linters-settings:
exhaustive:
default-signifies-exhaustive: false
funlen:
lines: 120
statements: 120
lines: 150
statements: 150
gci:
local-prefixes: github.com/cloudradar-monitoring/tacoscript
goconst:
Expand All @@ -25,7 +25,7 @@ linters-settings:
- whyNoLint
- wrapperFunc
gocyclo:
min-complexity: 20
min-complexity: 30
goimports:
local-prefixes: github.com/golangci/golangci-lint
golint:
Expand Down
3 changes: 1 addition & 2 deletions apptest/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apptest
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"time"
Expand Down Expand Up @@ -62,7 +61,7 @@ func AssertFileMatchesExpectation(fe *FileExpectation) (isExpectationMatched boo
return true, "", nil
}

fileContentsBytes, err := ioutil.ReadFile(fe.FilePath)
fileContentsBytes, err := os.ReadFile(fe.FilePath)
if err != nil {
return false, "", err
}
Expand Down
2 changes: 1 addition & 1 deletion apptest/fsOSWin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

package apptest

func AssertFileMatchesExpectationOS(filePath string, fe *FileExpectation) (bool, string, error) {
func AssertFileMatchesExpectationOS(filePath string, fe *FileExpectation) (isMatched bool, reason string, err error) {
return true, "", nil
}
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"fmt"
"os"

"github.com/cloudradar-monitoring/tacoscript/applog"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -33,9 +35,17 @@ func initLog() {
applog.Init(Verbose)
}

type errorResult struct {
Error string `yaml:"Error"`
}

func Execute() error {
if err := rootCmd.Execute(); err != nil {
logrus.Debugf("Execute failed: %v", err)

y, _ := yaml.Marshal(errorResult{Error: err.Error()})
fmt.Println(string(y))

os.Exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ConvertToKeyValues(val interface{}, path string) (KeyValues, error) {
key := item.Key.(string)
val := item.Value
res = append(res, KeyValue{
Key: fmt.Sprint(key),
Key: key,
Value: fmt.Sprint(val),
})
}
Expand Down
2 changes: 1 addition & 1 deletion exec/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (sr SystemRunner) createCmd(execContext *Context, tmpFile *os.File) (cmd *e

rawCmds := prelude + strings.Join(execContext.Cmds, newLine)

if _, err = tmpFile.Write([]byte(rawCmds)); err != nil {
if _, err = tmpFile.WriteString(rawCmds); err != nil {
return
}

Expand Down
30 changes: 16 additions & 14 deletions script/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"text/template"

"github.com/cloudradar-monitoring/tacoscript/utils"
Expand All @@ -19,7 +19,7 @@ type FileDataProvider struct {
}

func (fdp FileDataProvider) Read() ([]byte, error) {
return ioutil.ReadFile(fdp.Path)
return os.ReadFile(fdp.Path)
}

type RawDataProvider interface {
Expand Down Expand Up @@ -69,20 +69,22 @@ func (p Builder) BuildScripts() (tasks.Scripts, error) {
Tasks: []tasks.Task{},
}
index := 0
steps := rawTask.Value.(yaml.MapSlice)
for _, step := range steps {
taskTypeID := step.Key.(string)

index++
task, e := p.TaskBuilder.Build(taskTypeID, fmt.Sprintf("%s.%s[%d]", scriptID, taskTypeID, index), step.Value)
if e != nil {
return tasks.Scripts{}, e
if steps, ok := rawTask.Value.(yaml.MapSlice); ok {
for _, step := range steps {
taskTypeID := step.Key.(string)

index++
task, e := p.TaskBuilder.Build(taskTypeID, fmt.Sprintf("%s.%s[%d]", scriptID, taskTypeID, index), step.Value)
if e != nil {
return tasks.Scripts{}, e
}

errs.Add(task.Validate())
script.Tasks = append(script.Tasks, task)
}

errs.Add(task.Validate())
script.Tasks = append(script.Tasks, task)
} else {
errs.Add(fmt.Errorf("script failed to run. input YAML is malformed"))
}

scripts = append(scripts, script)
}
err = ValidateScripts(scripts)
Expand Down
4 changes: 2 additions & 2 deletions script/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package script
import (
"context"
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/cloudradar-monitoring/tacoscript/utils"
Expand Down Expand Up @@ -65,7 +65,7 @@ func (tm *TaskBuilderTaskMock) GetPath() string {

func (rdpm RawDataProviderMock) Read() ([]byte, error) {
if rdpm.FileName != "" {
return ioutil.ReadFile("yaml" + string(os.PathSeparator) + rdpm.FileName)
return os.ReadFile(filepath.Join("yaml", rdpm.FileName))
}

return []byte(rdpm.DataToReturn), rdpm.ErrToReturn
Expand Down
2 changes: 1 addition & 1 deletion script/scriptResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type taskResult struct {
Started onlyTime `yaml:"Started"`
Duration time.Duration `yaml:"Duration"`

Changes map[string]string `yaml:"Changes,omitempty"` // map for custom key-val data depending on type
Changes map[string]interface{} `yaml:"Changes,omitempty"` // map for custom key-val data depending on type
}

type scriptSummary struct {
Expand Down
32 changes: 26 additions & 6 deletions script/scriptRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,25 @@ func (r Runner) Run(ctx context.Context, scripts tasks.Scripts, globalAbortOnErr

tasksRun++

changeMap := make(map[string]string)
name := ""
comment := ""
changeMap := make(map[string]interface{})

if cmdRunTask, ok := task.(*tasks.CmdRunTask); ok {
name = strings.Join(cmdRunTask.GetNames(), "; ")

if !res.IsSkipped {
changeMap["pid"] = fmt.Sprintf("%d", res.Pid)
comment = `Command "` + name + `" run`
changeMap["pid"] = res.Pid
if runErr, ok := res.Err.(exec.RunError); ok {
changeMap["retcode"] = fmt.Sprintf("%d", runErr.ExitCode)
changeMap["retcode"] = runErr.ExitCode
}

changeMap["stderr"] = strings.TrimSpace(strings.ReplaceAll(res.StdErr, "\r\n", "\n"))
changeMap["stdout"] = strings.TrimSpace(strings.ReplaceAll(res.StdOut, "\r\n", "\n"))

if exec.IsPowerShell(cmdRunTask.Shell) {
changeMap["stdout"] = powershellUnquote(changeMap["stdout"])
changeMap["stdout"] = powershellUnquote(changeMap["stdout"].(string))
}
changes++
}
Expand All @@ -82,6 +87,9 @@ func (r Runner) Run(ctx context.Context, scripts tasks.Scripts, globalAbortOnErr
aborted = total - tasksRun
}
}
if pkgTask, ok := task.(*tasks.PkgTask); ok {
name = pkgTask.NamedTask.Name
}

if len(res.Changes) > 0 {
for k, v := range res.Changes {
Expand All @@ -93,12 +101,24 @@ func (r Runner) Run(ctx context.Context, scripts tasks.Scripts, globalAbortOnErr
if res.Err != nil {
errString = res.Err.Error()
}

if managedTask, ok := task.(*tasks.FileManagedTask); ok {
name = managedTask.Name
if errString == "" {
if managedTask.Updated {
comment = "File " + name + " updated"
} else {
comment = "All files in creates exists"
}
}
}

result.Results = append(result.Results, taskResult{
ID: script.ID,
Function: task.GetName(),
Name: res.Name,
Name: name,
Result: res.Succeeded(),
Comment: res.Comment,
Comment: comment,
Started: onlyTime(taskStart),
Duration: res.Duration,
Changes: changeMap,
Expand Down
36 changes: 29 additions & 7 deletions tasks/fileManagedTask.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"database/sql"
"fmt"
"io/fs"
"os"
"time"

Expand Down Expand Up @@ -140,6 +141,9 @@ type FileManagedTask struct {
Creates []string
OnlyIf []string
Require []string

// was managed file updated?
Updated bool
}

func (crt *FileManagedTask) GetName() string {
Expand Down Expand Up @@ -199,7 +203,9 @@ type FileManagedTaskExecutor struct {

func (fmte *FileManagedTaskExecutor) Execute(ctx context.Context, task Task) ExecutionResult {
logrus.Debugf("will trigger '%s' task", task.GetPath())
execRes := ExecutionResult{}
execRes := ExecutionResult{
Changes: make(map[string]string),
}

fileManagedTask, ok := task.(*FileManagedTask)
if !ok {
Expand All @@ -218,7 +224,7 @@ func (fmte *FileManagedTaskExecutor) Execute(ctx context.Context, task Task) Exe
Path: fileManagedTask.Path,
}
logrus.Debugf("will check if the task '%s' should be executed", task.GetPath())
skipReason, err := fmte.shouldBeExecuted(execCtx, fileManagedTask)
skipReason, err := fmte.shouldBeExecuted(execCtx, fileManagedTask, &execRes)
if err != nil {
execRes.Err = err
return execRes
Expand Down Expand Up @@ -257,6 +263,16 @@ func (fmte *FileManagedTaskExecutor) Execute(ctx context.Context, task Task) Exe
execRes.Err = err
return execRes
}
fileManagedTask.Updated = true

var info fs.FileInfo
info, err = fmte.FsManager.Stat(fileManagedTask.Name)
if err != nil {
execRes.Err = err
return execRes
}

execRes.Changes["length"] = fmt.Sprintf("%d bytes written", info.Size())
}
err = fmte.applyFileAttributesToTarget(fileManagedTask)
if err != nil {
Expand Down Expand Up @@ -301,7 +317,7 @@ func (fmte *FileManagedTaskExecutor) checkOnlyIfs(ctx *exec2.Context, fileManage
if err != nil {
runErr, isRunErr := err.(exec2.RunError)
if isRunErr {
logrus.Debugf("will skip %s since onlyif condition has failed: %v", fileManagedTask, runErr)
logrus.Debugf("will skip %s since onlyif condition has failed: %v", fileManagedTask.String(), runErr)
return false, nil
}

Expand All @@ -314,6 +330,7 @@ func (fmte *FileManagedTaskExecutor) checkOnlyIfs(ctx *exec2.Context, fileManage
func (fmte *FileManagedTaskExecutor) shouldBeExecuted(
ctx *exec2.Context,
fileManagedTask *FileManagedTask,
execRes *ExecutionResult,
) (skipReason string, err error) {
isExists, fileName, err := fmte.checkMissingFileCondition(fileManagedTask)
if err != nil {
Expand Down Expand Up @@ -352,15 +369,15 @@ func (fmte *FileManagedTaskExecutor) shouldBeExecuted(
return onlyIfConditionFailedReason, nil
}

skipReasonForContents, err := fmte.shouldSkipForContentExpectation(fileManagedTask)
skipReasonForContents, err := fmte.shouldSkipForContentExpectation(fileManagedTask, execRes)
if err != nil {
return "", err
}
if skipReasonForContents != "" {
return skipReasonForContents, nil
}

logrus.Debugf("all execution conditions are met, will continue %s", fileManagedTask)
logrus.Debugf("all execution conditions are met, will continue %s", fileManagedTask.String())
return "", nil
}

Expand Down Expand Up @@ -571,7 +588,10 @@ func (fmte *FileManagedTaskExecutor) copyContentToTarget(fileManagedTask *FileMa
return err
}

func (fmte *FileManagedTaskExecutor) shouldSkipForContentExpectation(fileManagedTask *FileManagedTask) (skipReason string, err error) {
func (fmte *FileManagedTaskExecutor) shouldSkipForContentExpectation(
fileManagedTask *FileManagedTask,
execRes *ExecutionResult,
) (skipReason string, err error) {
if !fileManagedTask.Contents.Valid {
logrus.Debug("contents section is missing, won't check the content")
return "", nil
Expand Down Expand Up @@ -607,7 +627,9 @@ func (fmte *FileManagedTaskExecutor) shouldSkipForContentExpectation(fileManaged
logrus.WithFields(
logrus.Fields{
"multiline": contentDiff,
}).Infof(`file '%s' differs from the expected content field, will copy diff to file`, fileManagedTask.Name)
}).Debugf(`file '%s' differs from the expected content field, will copy diff to file`, fileManagedTask.Name)

execRes.Changes["diff"] = contentDiff

return "", nil
}
Expand Down
Loading

0 comments on commit b4afb1f

Please sign in to comment.