Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
Signed-off-by: SudhanshuBawane <[email protected]>
  • Loading branch information
SudhanshuBawane committed Jan 25, 2024
1 parent 90be109 commit 6fe811a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
3 changes: 0 additions & 3 deletions agent/check_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,6 @@ func (a *Agent) executeCheck(ctx context.Context, request *corev2.CheckRequest,
var err error
assets, err = asset.GetAll(ctx, a.assetGetter, checkAssets)

//sudhanshu/5009
//check if asset.db exits if not then re-create it also delete the SHA associated with it and re-create it.
logger.Println("======================info=====================\n", assets)
if err != nil {
a.sendFailure(event, fmt.Errorf("error getting assets for check: %s", err))
return
Expand Down
32 changes: 8 additions & 24 deletions asset/boltdb_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run
// has proceeded to Update below will block here.
if err := b.db.View(func(tx *bolt.Tx) error {
// If the key exists, the bucket should already exist.
logger.Println("======================info=====================\n", assetBucketName)

bucket := tx.Bucket(assetBucketName)
logger.Println("======================info=====================\n", bucket)

if bucket == nil {
return nil
}
Expand All @@ -141,24 +141,20 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run

return nil
}); err != nil {
logger.Println("=====SUDHANSHU Get call return 1 =======", err)
return nil, err
}

// Check to see if the view was successful.
if localAsset != nil {
localAsset.Name = asset.Name
localAsset.SHA512 = asset.Sha512
logger.Println("======localAsset value========\n", localAsset)
return localAsset, nil
}

if err := b.db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(assetBucketName)

if err != nil {
logger.Println("=====SUDHANSHU CreateBucketIfNotExists call return 2 =======", err)
logger.Println(bucket, "========info========/n")
return err
}

Expand All @@ -178,8 +174,7 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run
tmpFile, err := b.fetchWithDuration(ctx, asset)

if err != nil {
logger.Println("=====SUDHANSHU fetch With Duration call return 3 =======", err)
logger.Println(bucket, "========info========/n", tmpFile)

return err
}
defer tmpFile.Close()
Expand All @@ -188,13 +183,9 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run
// verify
if err := b.verifier.Verify(tmpFile, asset.Sha512); err != nil {

logger.Println("=====SUDHANSHU File size verifier call return 4 =======", err)
logger.Println(bucket, "========info========")
// Attempt to retrieve the size of the downloaded asset
var size uint64
if fileInfo, err := tmpFile.Stat(); err == nil {
logger.Println("=====SUDHANSHU tmpFile stat call return 5 =======", err)
logger.Println(bucket, "========info========")
size = uint64(fileInfo.Size())
}

Expand All @@ -208,8 +199,6 @@ func (b *boltDBAssetManager) Get(ctx context.Context, asset *corev2.Asset) (*Run
assetPath, err := b.expandWithDuration(tmpFile, asset)

if err != nil {
logger.Println("=====SUDHANSHU expand with path call return 5 =======", err, assetPath, tmpFile)
logger.Println(bucket, "========info========")
return err
}

Expand Down Expand Up @@ -261,20 +250,15 @@ func (b *boltDBAssetManager) expandWithDuration(tmpFile *os.File, asset *corev2.
Observe(v * float64(1000))
}))
defer timer.ObserveDuration()
assetSHA := asset.Sha512
//fullPath := "/home/raiden/Desktop/sensu/agent/cache/" + assetSHA

//cfg := agent.NewConfig()
//viper.SetDefault(FlagCacheDir, path.SystemCacheDir("sensu-agent"))
assetSHA := asset.Sha512
CacheDir := viper.GetString(FlagCacheDir)
fullPath := filepath.Join(CacheDir, assetSHA)
errorSHA := os.RemoveAll(fullPath)
logger.Println()

//logger.Println("=====cache dir =========", sensupath.UserCacheDir("sensuctl"))
logger.Println("======= cache dir=========", CacheDir, fullPath)
logger.Println("========== SHA Error is =====================", errorSHA)
logger.Println()
if err := CleanUp(fullPath); err != nil { //fix for git issue 5009
fmt.Errorf("error cleaning up the SHA dir: %s", err)

Check failure on line 259 in asset/boltdb_manager.go

View workflow job for this annotation

GitHub Actions / staticcheck (project)

Errorf doesn't have side effects and its return value is ignored (SA4017)
}

assetPath = filepath.Join(b.localStorage, asset.Sha512)
return assetPath, b.expander.Expand(tmpFile, assetPath)
}
15 changes: 13 additions & 2 deletions asset/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package asset
import (
"errors"
"fmt"
"io"

archiver "github.com/mholt/archiver/v3"
"io"
"os"

filetype "gopkg.in/h2non/filetype.v1"
filetype_types "gopkg.in/h2non/filetype.v1/types"
Expand Down Expand Up @@ -37,6 +37,17 @@ type namer interface {
Name() string
}

// Sudhanshu - CleanUp the SHA for the git issue 5009 fix. Making sure that the asset.db after creation gets updated properly.

func CleanUp(fullPath string) error {
errorSHA := os.RemoveAll(fullPath)
if errorSHA != nil {
return errorSHA
}
return nil

}

// Expand an archive to a target directory.
func (a *archiveExpander) Expand(archive io.ReadSeeker, targetDirectory string) error {
// detect the type of archive the asset is
Expand Down
34 changes: 34 additions & 0 deletions asset/expander_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
package asset

import (
v2 "github.com/sensu/core/v2"
"os"
"path/filepath"
"testing"

"github.com/sensu/sensu-go/testing/testutil"
)

var asset *v2.Asset

Check failure on line 12 in asset/expander_test.go

View workflow job for this annotation

GitHub Actions / staticcheck (project)

var asset is unused (U1000)

// sudhanshu- Git issue 5009

func TestCleanUp(t *testing.T) {
t.Parallel()

// Create a temporary directory for testing
tmpDir := t.TempDir()

// Define the SHA and file name
SHAName := "shaAsset.tar"
SHAFilePath := filepath.Join(tmpDir, SHAName)

// Create a dummy file inside the temporary directory
SHAFile, err := os.Create(SHAFilePath)
if err != nil {
t.Fatalf("Failed to create dummy file: %v", err)
}
SHAFile.Close()

// Call CleanUp with the SHA of the dummy file and the temporary directory
err = CleanUp(SHAFilePath)
if err != nil {
t.Errorf("CleanUp returned an error: %v", err)
}

_, err = os.Stat(SHAFilePath)
if !os.IsNotExist(err) {
t.Errorf("CleanUp did not remove the dummy file as expected")
}
}

func TestExpandValidTar(t *testing.T) {
t.Parallel()
assetPath := getFixturePath("rubby-on-rails.tar")
Expand Down
8 changes: 3 additions & 5 deletions asset/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (r RuntimeAssetSet) Scripts() (map[string]io.ReadCloser, error) {
scripts := make(map[string]io.ReadCloser)
for _, asset := range r {
err := filepath.Walk(asset.LibDir(), func(path string, info os.FileInfo, err error) error {
logger.Println("======================info=====================\n", err, path, asset.LibDir())
if strings.HasSuffix(path, ".js") {
f, err := os.Open(path)
if err != nil {
Expand All @@ -51,16 +50,15 @@ func (r RuntimeAssetSet) Scripts() (map[string]io.ReadCloser, error) {
// GetAll gets a list of assets with the provided getter.
func GetAll(ctx context.Context, getter Getter, assets []types.Asset) (RuntimeAssetSet, error) {
runtimeAssets := make([]*RuntimeAsset, 0, len(assets))
logger.Println("======================info===================== 1", runtimeAssets)

for _, asset := range assets {
runtimeAsset, err := getter.Get(ctx, &asset)
logger.Println("======================info===================== 2: error", runtimeAsset, err)

if err != nil {
logger.Println("======================info===================== 3 : error", err)

return nil, err
}
if runtimeAsset != nil {
logger.Println("======================info===================== 4 : error", runtimeAsset, err)
runtimeAssets = append(runtimeAssets, runtimeAsset)
}
}
Expand Down

0 comments on commit 6fe811a

Please sign in to comment.