From 372d26f6fbf4125fe0ebc5d89ea1f31973965cfd Mon Sep 17 00:00:00 2001 From: Glen Rodgers Date: Wed, 12 Dec 2018 11:15:26 +0000 Subject: [PATCH] Extracted the all deps logic for creating logger [#161951838] Signed-off-by: Mehul Modha --- cli/command/all_deployments.go | 16 ++++++++++ cli/command/deployment_backup.go | 30 ++++++++----------- cli/command/deployment_backup_cleanup.go | 18 +++-------- factory/deployment_backup_cleanuper.go | 9 ++++-- factory/deployment_backuper.go | 10 ++++++- integration/deployment/backup_cleanup_test.go | 2 +- integration/deployment/backup_test.go | 2 +- 7 files changed, 50 insertions(+), 37 deletions(-) diff --git a/cli/command/all_deployments.go b/cli/command/all_deployments.go index ae26ccdfa..baa7c4350 100644 --- a/cli/command/all_deployments.go +++ b/cli/command/all_deployments.go @@ -1,12 +1,19 @@ package command import ( + "bytes" "errors" "fmt" + "io" + "os" + "path/filepath" "strings" + "github.com/cloudfoundry/bosh-utils/logger" + "github.com/cloudfoundry-incubator/bosh-backup-and-restore/bosh" "github.com/cloudfoundry-incubator/bosh-backup-and-restore/executor/deployment" + "github.com/cloudfoundry-incubator/bosh-backup-and-restore/factory" "github.com/cloudfoundry-incubator/bosh-backup-and-restore/orchestrator" "github.com/urfave/cli" ) @@ -134,6 +141,15 @@ func NewDeploymentExecutable(action ActionFunc, name string) DeploymentExecutabl } } +func createLogger(timestamp string, artifactPath string, deploymentName string, debug bool) (string, *bytes.Buffer, logger.Logger) { + logFilePath := filepath.Join(artifactPath, fmt.Sprintf("%s_%s.log", deploymentName, timestamp)) + logFile, _ := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, defaultLogfilePermissions) + buffer := new(bytes.Buffer) + multiWriter := io.MultiWriter(buffer, logFile) + logger := factory.BuildBoshLoggerWithCustomWriter(multiWriter, debug) + return logFilePath, buffer, logger +} + func (d DeploymentExecutable) Execute() deployment.DeploymentError { err := d.action(d.name) return deployment.DeploymentError{Deployment: d.name, Errs: err} diff --git a/cli/command/deployment_backup.go b/cli/command/deployment_backup.go index 25eefe503..3a2cfbaea 100644 --- a/cli/command/deployment_backup.go +++ b/cli/command/deployment_backup.go @@ -1,11 +1,7 @@ package command import ( - "bytes" "fmt" - "io" - "os" - "path/filepath" "time" "github.com/cloudfoundry-incubator/bosh-backup-and-restore/executor/deployment" @@ -58,22 +54,23 @@ func (d DeploymentBackupCommand) Action(c *cli.Context) error { func backupAll(target, username, password, caCert, artifactPath string, withManifest, debug bool) error { backupAction := func(deploymentName string) orchestrator.Error { - timeStamp := time.Now().UTC().Format(artifactTimeStampFormat) - - logfilePath := filepath.Join(artifactPath, fmt.Sprintf("%s_%s.log", deploymentName, timeStamp)) - logFile, _ := os.OpenFile(logfilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, defaultLogfilePermissions) - - buffer := new(bytes.Buffer) - multiWriter := io.MultiWriter(buffer, logFile) - - logger := factory.BuildBoshLoggerWithCustomWriter(multiWriter, debug) - - backuper, factoryErr := factory.BuildDeploymentBackuper(target, username, password, caCert, withManifest, logger, timeStamp) + timestamp := time.Now().UTC().Format(artifactTimeStampFormat) + logFilePath, buffer, logger := createLogger(timestamp, artifactPath, deploymentName, debug) + + backuper, factoryErr := factory.BuildDeploymentBackuper( + target, + username, + password, + caCert, + withManifest, + logger, + timestamp, + ) if factoryErr != nil { return orchestrator.NewError(factoryErr) } - printlnWithTimestamp(fmt.Sprintf("Starting backup of %s, log file: %s", deploymentName, logfilePath)) + printlnWithTimestamp(fmt.Sprintf("Starting backup of %s, log file: %s", deploymentName, logFilePath)) err := backuper.Backup(deploymentName, artifactPath) if err != nil { @@ -108,7 +105,6 @@ func backupAll(target, username, password, caCert, artifactPath string, withMani errorHandler, deployment.NewParallelExecutor()) } - func backupSingleDeployment(deployment, target, username, password, caCert, artifactPath string, withManifest, debug bool) error { logger := factory.BuildBoshLogger(debug) timeStamp := time.Now().UTC().Format(artifactTimeStampFormat) diff --git a/cli/command/deployment_backup_cleanup.go b/cli/command/deployment_backup_cleanup.go index e4f475e35..efe31ca71 100644 --- a/cli/command/deployment_backup_cleanup.go +++ b/cli/command/deployment_backup_cleanup.go @@ -1,10 +1,7 @@ package command import ( - "bytes" "fmt" - "io" - "os" "time" "github.com/cloudfoundry-incubator/bosh-backup-and-restore/executor/deployment" @@ -53,19 +50,12 @@ func (d DeploymentBackupCleanupCommand) Action(c *cli.Context) error { } return cleanupAllDeployments(target, username, password, caCert, debug) - } + func cleanupAllDeployments(target, username, password, caCert string, debug bool) error { cleanupAction := func(deploymentName string) orchestrator.Error { - timeStamp := time.Now().UTC().Format(artifactTimeStampFormat) - - logFilePath := fmt.Sprintf("%s_%s.log", deploymentName, timeStamp) - logFile, _ := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, defaultLogfilePermissions) - - buffer := new(bytes.Buffer) - multiWriter := io.MultiWriter(buffer, logFile) - - logger := factory.BuildBoshLoggerWithCustomWriter(multiWriter, debug) + timestamp := time.Now().UTC().Format(artifactTimeStampFormat) + logFilePath, buffer, logger := createLogger(timestamp, "", deploymentName, debug) cleaner, factoryError := factory.BuildDeploymentBackupCleanuper( target, @@ -83,7 +73,7 @@ func cleanupAllDeployments(target, username, password, caCert string, debug bool err := cleanup(cleaner, deploymentName) if err != nil { - printlnWithTimestamp(fmt.Sprintf("ERROR: failed cleanup of %s", deploymentName)) + printlnWithTimestamp(fmt.Sprintf("ERROR: failed to cleanup %s", deploymentName)) fmt.Println(buffer.String()) } else { printlnWithTimestamp(fmt.Sprintf("Finished cleanup of %s", deploymentName)) diff --git a/factory/deployment_backup_cleanuper.go b/factory/deployment_backup_cleanuper.go index 6f25ec06e..d137bb08e 100644 --- a/factory/deployment_backup_cleanuper.go +++ b/factory/deployment_backup_cleanuper.go @@ -22,7 +22,10 @@ func BuildDeploymentBackupCleanuper( return nil, err } - return orchestrator.NewBackupCleaner(logger, - bosh.NewDeploymentManager(boshClient, logger, false), orderer.NewKahnBackupLockOrderer(), - executor.NewParallelExecutor()), nil + return orchestrator.NewBackupCleaner( + logger, + bosh.NewDeploymentManager(boshClient, logger, false), + orderer.NewKahnBackupLockOrderer(), + executor.NewParallelExecutor(), + ), nil } diff --git a/factory/deployment_backuper.go b/factory/deployment_backuper.go index 7aa3fad99..aa3f5e517 100644 --- a/factory/deployment_backuper.go +++ b/factory/deployment_backuper.go @@ -11,7 +11,15 @@ import ( boshlog "github.com/cloudfoundry/bosh-utils/logger" ) -func BuildDeploymentBackuper(target, username, password, caCert string, withManifest bool, logger boshlog.Logger, timestamp string) (*orchestrator.Backuper, error) { +func BuildDeploymentBackuper( + target, + username, + password, + caCert string, + withManifest bool, + logger boshlog.Logger, + timestamp string, +) (*orchestrator.Backuper, error) { boshClient, err := BuildBoshClient(target, username, password, caCert, logger) if err != nil { return nil, err diff --git a/integration/deployment/backup_cleanup_test.go b/integration/deployment/backup_cleanup_test.go index 3bb88c97a..46091f79c 100644 --- a/integration/deployment/backup_cleanup_test.go +++ b/integration/deployment/backup_cleanup_test.go @@ -328,7 +328,7 @@ instance_groups: It("reports that the one deployment failed to clean up with the correct error message", func() { Eventually(session.ExitCode()).Should(Equal(1)) Expect(session.Out).To(gbytes.Say("Failed to cleanup deployment '" + deployment1 + "'")) - Expect(session.Out).To(gbytes.Say(fmt.Sprintf("ERROR: failed cleanup of %s", deployment1))) + Expect(session.Out).To(gbytes.Say(fmt.Sprintf("ERROR: failed to cleanup %s", deployment1))) Expect(session.Out).To(gbytes.Say("INFO - Looking for scripts")) Expect(session.Out).To(gbytes.Say("INFO - redis-dedicated-node/fake-uuid/redis/post-backup-unlock")) diff --git a/integration/deployment/backup_test.go b/integration/deployment/backup_test.go index 41c826801..ee95d4a42 100644 --- a/integration/deployment/backup_test.go +++ b/integration/deployment/backup_test.go @@ -1214,7 +1214,7 @@ backup_name: name_2 }) }) - Context("When deployment does not exist", func() { + Context("When deployment doezs not exist", func() { BeforeEach(func() { deploymentName = "my-non-existent-deployment" director.VerifyAndMock(