Skip to content

Commit

Permalink
Extracted the all deps logic for creating logger
Browse files Browse the repository at this point in the history
[#161951838]

Signed-off-by: Mehul Modha <[email protected]>
  • Loading branch information
gmrodgers authored and Mehul Modha committed Dec 12, 2018
1 parent ec47350 commit 372d26f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 37 deletions.
16 changes: 16 additions & 0 deletions cli/command/all_deployments.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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}
Expand Down
30 changes: 13 additions & 17 deletions cli/command/deployment_backup.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package command

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"time"

"github.com/cloudfoundry-incubator/bosh-backup-and-restore/executor/deployment"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 4 additions & 14 deletions cli/command/deployment_backup_cleanup.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package command

import (
"bytes"
"fmt"
"io"
"os"
"time"

"github.com/cloudfoundry-incubator/bosh-backup-and-restore/executor/deployment"
Expand Down Expand Up @@ -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,
Expand All @@ -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))
Expand Down
9 changes: 6 additions & 3 deletions factory/deployment_backup_cleanuper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 9 additions & 1 deletion factory/deployment_backuper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion integration/deployment/backup_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion integration/deployment/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 372d26f

Please sign in to comment.