/
resource-upload.go
160 lines (138 loc) · 5.18 KB
/
resource-upload.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright © 2019 - 2020 Robert Lemke / Flownative GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"cloud.google.com/go/storage"
"github.com/flownative/localbeach/pkg/beachsandbox"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/api/option"
)
var instanceIdentifier, projectNamespace, bucketName, resourcesPath, resumeWithFile string
var force bool
// resourceUploadCmd represents the resource-upload command
var resourceUploadCmd = &cobra.Command{
Use: "resource-upload",
Short: "Upload resources (assets) from a local Flow or Neos installation to Beach",
Long: `resource-upload
This command uploads Flow resources from a local Flow or Neos project to a Beach instance.
Resource data (that is, the actual files containing binary data, like images or documents)
will be uploaded from the Data/Persistent/Resources directory. It is your responsibility
to make sure that the database content is matching this data.
The Google Cloud Storage bucket name will be determined automatically through the environment
variables set in the given instance. You can override the bucket name by specifying the --bucket
parameter.
Be aware that Neos and Flow keep track of existing resources by a database table. If
resources are not registered in there, Flow does not know about them.
Notes:
- existing data in the Beach instance will be left unchanged
- older instances may use a namespace called "beach"
`,
Args: cobra.ExactArgs(0),
Run: handleResourceUploadRun,
}
func init() {
resourceUploadCmd.Flags().StringVar(&instanceIdentifier, "instance", "", "instance identifier of the Beach instance to upload to, eg. 'instance-123abc45-def6-7890-abcd-1234567890ab'")
resourceUploadCmd.Flags().StringVar(&projectNamespace, "namespace", "", "The project namespace of the Beach instance to upload to, eg. 'beach-project-123abc45-def6-7890-abcd-1234567890ab'")
resourceUploadCmd.Flags().BoolVar(&force, "force", false, "Force uploading resources which already exist in the target bucket")
resourceUploadCmd.Flags().StringVar(&resumeWithFile, "resume-with-file", "", "If specified, resume uploading resources starting with the given filename, eg. '12dcde4c13142942288c5a973caf0fa720ed2794'")
_ = resourceUploadCmd.MarkFlagRequired("instance")
_ = resourceUploadCmd.MarkFlagRequired("namespace")
rootCmd.AddCommand(resourceUploadCmd)
}
func handleResourceUploadRun(cmd *cobra.Command, args []string) {
sandbox, err := beachsandbox.GetActiveSandbox()
if err != nil {
log.Fatal("Could not activate sandbox: ", err)
return
}
if resourcesPath == "" {
resourcesPath = sandbox.ProjectDataPersistentResourcesPath
}
_, err = os.Stat(resourcesPath)
if err != nil {
log.Fatal("The path %v does not exist", resourcesPath)
return
}
err, bucketNameFromCredentials, privateKeyDecoded := retrieveCloudStorageCredentials(instanceIdentifier, projectNamespace)
if err != nil {
log.Fatal(err)
return
}
if bucketName == "" {
bucketName = bucketNameFromCredentials
}
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithCredentialsJSON(privateKeyDecoded))
if err != nil {
log.Fatal(fmt.Sprintf("Failed to initialize cloud storage client: %v", err))
return
}
log.Info(fmt.Sprintf("Uploading resources from local directory %v to bucket %v...", resourcesPath, bucketName))
var fileList []string
err = filepath.Walk(resourcesPath, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
fileList = append(fileList, path)
}
return nil
})
if err != nil {
log.Fatal(fmt.Sprintf("Failed creating list of files to upload: %v", err))
return
}
bucket := client.Bucket(bucketName)
for _, pathAndFilename := range fileList {
filename := filepath.Base(pathAndFilename)
if resumeWithFile != "" && filename < resumeWithFile {
log.Debug("Skipped " + filename)
continue
}
_, err = bucket.Object(filename).Attrs(ctx)
if err == storage.ErrObjectNotExist || force == true {
source, err := os.Open(pathAndFilename)
if err != nil {
log.Fatal(err)
return
}
destination := bucket.Object(filename).NewWriter(ctx)
if _, err = io.Copy(destination, source); err != nil {
source.Close()
log.Fatal(err)
return
}
if err := destination.Close(); err != nil {
source.Close()
log.Fatal(err)
return
}
if err = source.Close(); err != nil {
log.Error(err)
} else {
log.Debug("Uploaded " + filename)
}
} else if err == nil {
log.Debug("Skipped " + filename + " (already exists)")
} else {
log.Error(err)
}
}
log.Info("Done")
return
}