Skip to content

Commit

Permalink
Removing the auto upload of support bundle code (#378)
Browse files Browse the repository at this point in the history
* Commenting out the auto upload of support bundle

* Commenting out unused variables

* Remove support bundle upload to s3

* Remove the s3 upload unit test

* Remove some unused vars
  • Loading branch information
vedantjoshi84 authored Jul 25, 2024
1 parent 445e2cc commit 82c0f0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 143 deletions.
84 changes: 16 additions & 68 deletions cmd/supportBundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
"github.com/platform9/pf9ctl/pkg/test_utils"
)

//Errors returned by the functions
var ErrHostIP = errors.New("Host IP not found")
var ErrRemove = errors.New("Unable to remove bundle")
var ErrUpload = errors.New("Unable to upload supportBundle to S3")
var ErrPartialBundle = errors.New("Failed to generate complete supportBundle, generated partial bundle")
// Errors returned by the functions
var ErrHostIP = errors.New("host IP not found")
var ErrRemove = errors.New("unable to remove bundle")
var ErrUpload = errors.New("unable to upload supportBundle to S3")
var ErrPartialBundle = errors.New("failed to generate complete supportBundle, generated partial bundle")

//HostIP test case
// HostIP test case
func TestHostIP(t *testing.T) {
type want struct {
host string
err error
// host string
err error
}

type args struct {
Expand Down Expand Up @@ -71,21 +71,21 @@ func TestHostIP(t *testing.T) {

}

//GenSupportBundle test case(Only for Local Host)
// GenSupportBundle test case(Only for Local Host)
func TestGenSupportBundle(t *testing.T) {

//isRemote := false

timestamp := time.Now()
type want struct {
targetfile string
err error
// targetfile string
err error
}

type args struct {
exec cmdexec.Executor
timestamp time.Time
isRemote bool
exec cmdexec.Executor
// timestamp time.Time
// isRemote bool
}

cases := map[string]struct {
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestGenSupportBundle(t *testing.T) {

}

//GenTargetFilename test case
// GenTargetFilename test case
func TestGenTargetFilename(t *testing.T) {

type want struct {
Expand Down Expand Up @@ -169,59 +169,7 @@ func TestGenTargetFilename(t *testing.T) {

}

//S3Upload test case
func TestS3Upload(t *testing.T) {
type want struct {
err error
}

type args struct {
exec cmdexec.Executor
}

cases := map[string]struct {
args
want
}{
//Success case.The S3Upload function returns nil error on successful execution
"CheckPass": {
args: args{
exec: &cmdexec.MockExecutor{
MockRun: func(name string, args ...string) error {
return nil
},
},
},
want: want{
err: nil,
},
},
//Failure case.The S3Upload function returns ErrUpload error on execution
"CheckFail": {
args: args{
exec: &cmdexec.MockExecutor{
MockRun: func(name string, args ...string) error {
return ErrUpload
},
},
},
want: want{
err: ErrUpload,
},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {

err1 := supportBundle.S3Upload(tc.exec)
test_utils.Equals(t, tc.want.err, err1)
})
}

}

//RemoveBundle test case
// RemoveBundle test case
func TestRemoveBundle(t *testing.T) {
type want struct {
err error
Expand Down
89 changes: 14 additions & 75 deletions pkg/supportBundle/supportBundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/platform9/pf9ctl/pkg/client"
"github.com/platform9/pf9ctl/pkg/cmdexec"
"github.com/platform9/pf9ctl/pkg/color"
"github.com/platform9/pf9ctl/pkg/keystone"
"github.com/platform9/pf9ctl/pkg/objects"
"github.com/platform9/pf9ctl/pkg/pmk"
"github.com/platform9/pf9ctl/pkg/util"
Expand All @@ -33,11 +32,11 @@ var (
hostOS string

//Errors returned from the functions
ErrHostIP = fmt.Errorf("Host IP not found")
ErrRemove = fmt.Errorf("Unable to remove bundle")
ErrGenBundle = fmt.Errorf("Unable to generate supportBundle in remote host")
ErrUpload = fmt.Errorf("Unable to upload supportBundle to S3")
ErrPartialBundle = fmt.Errorf("Failed to generate complete supportBundle, generated partial bundle")
ErrHostIP = fmt.Errorf("host IP not found")
ErrRemove = fmt.Errorf("unable to remove bundle")
ErrGenBundle = fmt.Errorf("unable to generate supportBundle in remote host")
ErrUpload = fmt.Errorf("unable to upload supportBundle to S3")
ErrPartialBundle = fmt.Errorf("failed to generate complete supportBundle, generated partial bundle")

//Timestamp used for generating targetfile
Timestamp = time.Now()
Expand All @@ -54,7 +53,7 @@ func HostOS(exec cmdexec.Executor) {
// To get the Host IP address
func HostIP(exec cmdexec.Executor) (string, error) {
zap.S().Debug("Fetching HostIP")
host, err := exec.RunWithStdout("bash", "-c", fmt.Sprintf("hostname -I"))
host, err := exec.RunWithStdout("bash", "-c", "hostname -I")
if err != nil {
zap.S().Error("Host IP Not found", err)
return host, ErrHostIP
Expand All @@ -64,10 +63,10 @@ func HostIP(exec cmdexec.Executor) (string, error) {
return host, nil
}

// To upload pf9ctl log bundle to S3 bucket
// Generate support bundle and not upload
func SupportBundleUpload(ctx objects.Config, allClients client.Client, isRemote bool) error {

zap.S().Debugf("Received a call to upload pf9ctl supportBundle to %s bucket.\n", S3_BUCKET_NAME)
zap.S().Debugf("Received a call to generate pf9ctl supportBundle\n")
HostOS(allClients.Executor)
fileloc, err = GenSupportBundle(allClients.Executor, Timestamp, isRemote)
if err != nil && err != ErrPartialBundle {
Expand All @@ -78,70 +77,10 @@ func SupportBundleUpload(ctx objects.Config, allClients client.Client, isRemote
zap.S().Debugf(color.Red("x ")+"Failed to generate supportBundle\n", err.Error())
}

// To get the HostIP
hostIP, err := HostIP(allClients.Executor)
if err != nil {
zap.S().Debug("Unable to fetch Host IP")
}

//To remove extra spaces and lines after the IP
hostIP = strings.TrimSpace(strings.Trim(hostIP, "\n"))

// Fetch the keystone token.
// This is used as a reference to the segment event.
auth, err := allClients.Keystone.GetAuth(
ctx.Username,
ctx.Password,
ctx.Tenant,
ctx.MfaToken,
)
if err != nil {
zap.S().Debug("Unable to locate keystone credentials: %s\n", err.Error())
return fmt.Errorf("Unable to locate keystone credentials: %s\n", err.Error())
}

// To Fetch FQDN
FQDN, err := keystone.FetchRegionFQDN(ctx.Fqdn, ctx.Region, auth)
if err != nil {
zap.S().Debug("unable to fetch fqdn: %w")
return fmt.Errorf("unable to fetch fqdn: %w", err)
}
//To fetch FQDN from config if region given is invalid
if FQDN == "" {
FQDN = ctx.Fqdn
FQDN = strings.Replace(FQDN, "https://", "", 1)
}

// S3 location to upload the file
S3_Location = S3_Loc + "/" + FQDN + "/" + hostIP + "/"

// To upload the pf9cli log bundle to S3 bucket
errUpload := S3Upload(allClients.Executor)
if errUpload != nil {
zap.S().Debugf("Failed to upload pf9ctl supportBundle to %s bucket!! ", S3_BUCKET_NAME, errUpload)

if err := allClients.Segment.SendEvent("supportBundle upload Failed", auth, "Failed", ""); err != nil {
zap.S().Debugf("Unable to send Segment event for supportBundle. Error: %s", err.Error())
}

} else {
zap.S().Debugf("Succesfully uploaded pf9ctl supportBundle to %s bucket at %s location \n",
S3_BUCKET_NAME, S3_Location)
if err := allClients.Segment.SendEvent("supportBundle upload Success", auth, "Success", ""); err != nil {
zap.S().Debugf("Unable to send Segment event for supportBundle. Error: %s", err.Error())
}
}

// Remove the supportbundle after uploading to S3
errremove := RemoveBundle(allClients.Executor)
if errremove != nil {
zap.S().Debug("Error removing generated bundle", errremove)
}

return nil
}

//To generate the targetfile name including the hostname and the timestamp
// To generate the targetfile name including the hostname and the timestamp
func GenTargetFilename(timestamp time.Time, hostname string) string {

//timestamp format for the archive file(Note:UTC Time is taken)
Expand All @@ -157,7 +96,7 @@ func GenTargetFilename(timestamp time.Time, hostname string) string {
return targetfile
}

//To upload supportBundle to the S3 location
// To upload supportBundle to the S3 location
func S3Upload(exec cmdexec.Executor) error {
errUpload := exec.Run("bash", "-c", fmt.Sprintf("curl -T %s -H %s %s", fileloc,
S3_ACL, S3_Location))
Expand All @@ -167,7 +106,7 @@ func S3Upload(exec cmdexec.Executor) error {
return nil
}

//To remove the supportBundle
// To remove the supportBundle
func RemoveBundle(exec cmdexec.Executor) error {
errremove := exec.Run("bash", "-c", fmt.Sprintf("rm -rf %s", fileloc))
if errremove != nil {
Expand All @@ -189,8 +128,8 @@ func statPaths(exec cmdexec.Executor, paths ...string) bool {
return couldStatAll
}

//This function is used to generate the support bundles.
//It copies all the log files specified into a directory and archives that given directory.
// This function is used to generate the support bundles.
// It copies all the log files specified into a directory and archives that given directory.
func GenSupportBundle(exec cmdexec.Executor, timestamp time.Time, isRemote bool) (string, error) {

//Check whether the source directories exist in remote node.
Expand All @@ -215,7 +154,7 @@ func GenSupportBundle(exec cmdexec.Executor, timestamp time.Time, isRemote bool)
statPaths(exec, util.DmesgLog, msgfile, lockfile)

// To fetch the hostname of remote node
hostname, err := exec.RunWithStdout("bash", "-c", fmt.Sprintf("hostname"))
hostname, err := exec.RunWithStdout("bash", "-c", "hostname")
if err != nil {
zap.S().Debug("Failed to fetch hostname ", err)
}
Expand Down

0 comments on commit 82c0f0c

Please sign in to comment.