This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
snapshot.go
132 lines (110 loc) · 3.99 KB
/
snapshot.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cmd
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/SUSE/cf-plugin-backup/models"
"github.com/SUSE/cf-plugin-backup/util"
termuiprogressbar "github.com/SUSE/termui/termprogressbar"
)
// snapshotCmd represents the snapshot command
var snapshotCmd = &cobra.Command{
Use: "backup-snapshot",
Short: "Create a new CloudFoundry backup snapshot",
Long: `Create a new CloudFoundry backup snapshot to a local file.
`,
Run: func(cmd *cobra.Command, args []string) {
var currentIndex int
orgQuotas, err := util.GetOrgQuotaDefinitions(&util.CliConnectionCCApi{CliConnection: CliConnection})
util.FreakOut(err)
log.Println("org quota definitions done")
spaceQuotas, err := util.GetSpaceQuotaDefinitions(&util.CliConnectionCCApi{CliConnection: CliConnection})
util.FreakOut(err)
log.Println("space quota definitions done")
backupResources, err := util.GetOrgsResourcesRecurively(&util.CliConnectionCCApi{CliConnection: CliConnection})
util.FreakOut(err)
log.Println("orgs done")
sharedDomains, err := util.GetSharedDomains(&util.CliConnectionCCApi{CliConnection: CliConnection})
util.FreakOut(err)
log.Println("shared domains done")
securityGroups, err := util.GetSecurityGroups(&util.CliConnectionCCApi{CliConnection: CliConnection})
util.FreakOut(err)
log.Println("groups done")
featureFlags, err := util.GetFeatureFlags(&util.CliConnectionCCApi{CliConnection: CliConnection})
util.FreakOut(err)
log.Println("feature flags done")
backupJSON, err := util.CreateBackupJSON(models.BackupModel{
OrgQuotas: orgQuotas,
SpaceQuotas: spaceQuotas,
Organizations: backupResources,
SharedDomains: sharedDomains,
SecurityGroups: securityGroups,
FeatureFlags: featureFlags,
})
util.FreakOut(err)
err = ioutil.WriteFile(backupFile, []byte(backupJSON), 0644)
util.FreakOut(err)
// Save app bits
packager := &util.CFPackager{
Cli: CliConnection,
Writer: new(util.CFFileWriter),
Reader: new(util.CFFileReader),
}
appBits := util.NewCFDroplet(CliConnection, packager)
backupModel := models.BackupModel{}
err = json.Unmarshal([]byte(backupJSON), &backupModel)
util.FreakOut(err)
err = os.Mkdir(filepath.Join(backupDir, backupAppBitsDir), 0755)
if err != nil && !os.IsExist(err) {
util.FreakOut(err)
}
var appsToBackup []*models.ResourceModel
resources := util.RestoreOrgResourceModels(backupModel.Organizations)
if resources != nil {
for _, org := range *resources {
if org.Entity["spaces"] != nil {
for _, space := range *(org.Entity["spaces"].(*[]*models.ResourceModel)) {
if space.Entity["apps"] != nil {
for _, app := range *(space.Entity["apps"].(*[]*models.ResourceModel)) {
if dockerImg, hit := app.Entity["docker_image"]; !hit || dockerImg == nil {
appsToBackup = append(appsToBackup, app)
}
}
}
}
}
}
}
if len(appsToBackup) > 0 {
log.Printf("Saving bits for %d apps", len(appsToBackup))
termuiPGBar := termuiprogressbar.NewProgressBar(len(appsToBackup), true)
termuiPGBar.Start()
for _, app := range appsToBackup {
appGUID := app.Metadata["guid"].(string)
if currentIndex < len(appsToBackup) {
termuiPGBar.Increment()
currentIndex++
}
appZipPath := filepath.Join(backupDir, backupAppBitsDir, appGUID+".zip")
err := appBits.SaveDroplet(appGUID, appZipPath)
if err != nil {
log.Printf("Could not save bits for %v: %v", appGUID, err)
}
}
termuiPGBar.FinishPrint("App bits saved")
}
},
}
func init() {
RootCmd.AddCommand(snapshotCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// snapshotCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// snapshotCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}