/
deployment_backup_cleanup.go
114 lines (90 loc) · 2.84 KB
/
deployment_backup_cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package command
import (
"fmt"
"time"
"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"
)
type DeploymentBackupCleanupCommand struct {
}
func NewDeploymentBackupCleanupCommand() DeploymentBackupCleanupCommand {
return DeploymentBackupCleanupCommand{}
}
func (d DeploymentBackupCleanupCommand) Cli() cli.Command {
return cli.Command{
Name: "backup-cleanup",
Usage: "Cleanup a deployment after a backup was interrupted",
Action: d.Action,
}
}
func (d DeploymentBackupCleanupCommand) Action(c *cli.Context) error {
trapSigint(true)
username, password, target, caCert, debug, deployment, allDeployments := getDeploymentParams(c)
if !allDeployments {
logger := factory.BuildBoshLogger(debug)
cleaner, err := factory.BuildDeploymentBackupCleanuper(
target,
username,
password,
caCert,
logger,
)
if err != nil {
return processError(orchestrator.NewError(err))
}
cleanupErr := cleaner.Cleanup(deployment)
return processError(cleanupErr)
}
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, buffer, logger := createLogger(timestamp, "", deploymentName, debug)
cleaner, factoryError := factory.BuildDeploymentBackupCleanuper(
target,
username,
password,
caCert,
logger,
)
if factoryError != nil {
return orchestrator.NewError(factoryError)
}
printlnWithTimestamp(fmt.Sprintf("Starting cleanup of %s, log file: %s", deploymentName, logFilePath))
err := cleanup(cleaner, deploymentName)
if err != nil {
printlnWithTimestamp(fmt.Sprintf("ERROR: failed to cleanup %s", deploymentName))
fmt.Println(buffer.String())
} else {
printlnWithTimestamp(fmt.Sprintf("Finished cleanup of %s", deploymentName))
}
return err
}
errorHandler := func(deploymentError deployment.AllDeploymentsError) error {
return deploymentError.Process()
}
logger, _ := factory.BuildBoshLoggerWithCustomBuffer(debug)
boshClient, err := factory.BuildBoshClient(target, username, password, caCert, logger)
if err != nil {
return err
}
fmt.Println("Starting cleanup...")
return runForAllDeployments(
cleanupAction,
boshClient,
"could not be cleaned up",
"cleaned up",
errorHandler,
deployment.NewParallelExecutor())
}
func cleanup(cleaner *orchestrator.BackupCleaner, deployment string) orchestrator.Error {
err := cleaner.Cleanup(deployment)
if err != nil {
fmt.Printf("Failed to cleanup deployment '%s'\n", deployment)
return err
}
return nil
}