Skip to content

Commit 91db934

Browse files
authored
Merge pull request #106 from flownative/feature/cluster-flag
Allow to specify cluster for resource download/upload
2 parents 6087373 + 5fb20b1 commit 91db934

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

cmd/beach/cmd/helpers.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
asset "github.com/flownative/localbeach/assets"
3434
)
3535

36+
var instanceIdentifier, projectNamespace, clusterIdentifier string
37+
3638
func copyFileFromAssets(src, dst string) (int64, error) {
3739
source, err := asset.Assets.Open(src)
3840
if err != nil {
@@ -90,12 +92,18 @@ func getRelativePersistentResourcePathByHash(hash string) string {
9092
}
9193
}
9294

93-
func retrieveCloudStorageCredentials(instanceIdentifier string, projectNamespace string) (err error, bucketName string, privateKey []byte) {
95+
func retrieveCloudStorageCredentials(instanceIdentifier string, projectNamespace string, clusterIdentifier string) (err error, bucketName string, privateKey []byte) {
9496
log.Info("Retrieving cloud storage access data from instance")
9597

9698
internalHost := "beach@" + instanceIdentifier + "." + projectNamespace
99+
jumpHost := ""
100+
if clusterIdentifier != "" {
101+
jumpHost = "beach@ssh." + clusterIdentifier + ".flownative.cloud"
102+
} else {
103+
jumpHost = "beach@ssh.flownative.cloud"
104+
}
97105
output, err := exec.RunCommand("ssh", []string{
98-
"-J", "beach@ssh.flownative.cloud", internalHost,
106+
"-J", jumpHost, internalHost,
99107
"/bin/bash", "-c", "env | grep BEACH_GOOGLE_CLOUD_STORAGE_",
100108
})
101109
if err != nil {

cmd/beach/cmd/resource-download.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"path/filepath"
3030
)
3131

32+
var sourceBucketName, targetResourcesPath string
33+
3234
// resourceDownloadCmd represents the resource-download command
3335
var resourceDownloadCmd = &cobra.Command{
3436
Use: "resource-download",
@@ -59,8 +61,9 @@ Notes:
5961
func init() {
6062
resourceDownloadCmd.Flags().StringVar(&instanceIdentifier, "instance", "", "instance identifier of the Beach instance to download from, eg. 'instance-123abc45-def6-7890-abcd-1234567890ab'")
6163
resourceDownloadCmd.Flags().StringVar(&projectNamespace, "namespace", "", "The project namespace of the Beach instance to download from, eg. 'beach-project-123abc45-def6-7890-abcd-1234567890ab'")
62-
resourceDownloadCmd.Flags().StringVar(&bucketName, "bucket", "", "name of the bucket to download resources from")
63-
resourceDownloadCmd.Flags().StringVar(&resourcesPath, "resources-path", "", "custom path where to store the downloaded resources, e.g. 'Data/Persistent/Protected'")
64+
resourceDownloadCmd.Flags().StringVar(&clusterIdentifier, "cluster", "", "The cluster identifier of the Beach instance to download from, eg. 'h9acc4'")
65+
resourceDownloadCmd.Flags().StringVar(&sourceBucketName, "bucket", "", "name of the bucket to download resources from")
66+
resourceDownloadCmd.Flags().StringVar(&targetResourcesPath, "resources-path", "", "custom path where to store the downloaded resources, e.g. 'Data/Persistent/Protected'")
6467
_ = resourceDownloadCmd.MarkFlagRequired("instance")
6568
_ = resourceDownloadCmd.MarkFlagRequired("namespace")
6669
rootCmd.AddCommand(resourceDownloadCmd)
@@ -73,24 +76,24 @@ func handleResourceDownloadRun(cmd *cobra.Command, args []string) {
7376
return
7477
}
7578

76-
if resourcesPath == "" {
77-
resourcesPath = sandbox.ProjectDataPersistentResourcesPath
79+
if targetResourcesPath == "" {
80+
targetResourcesPath = sandbox.ProjectDataPersistentResourcesPath
7881
}
7982

80-
_, err = os.Stat(resourcesPath)
83+
_, err = os.Stat(targetResourcesPath)
8184
if err != nil {
82-
log.Fatal(fmt.Sprintf("The path %v does not exist", resourcesPath))
85+
log.Fatal(fmt.Sprintf("The path %v does not exist", targetResourcesPath))
8386
return
8487
}
8588

86-
err, bucketNameFromCredentials, privateKeyDecoded := retrieveCloudStorageCredentials(instanceIdentifier, projectNamespace)
89+
err, bucketNameFromCredentials, privateKeyDecoded := retrieveCloudStorageCredentials(instanceIdentifier, projectNamespace, clusterIdentifier)
8790
if err != nil {
8891
log.Fatal(err)
8992
return
9093
}
9194

92-
if bucketName == "" {
93-
bucketName = bucketNameFromCredentials
95+
if sourceBucketName == "" {
96+
sourceBucketName = bucketNameFromCredentials
9497
}
9598

9699
ctx := context.Background()
@@ -100,9 +103,9 @@ func handleResourceDownloadRun(cmd *cobra.Command, args []string) {
100103
return
101104
}
102105

103-
log.Info(fmt.Sprintf("Downloading resources from bucket %v to local directory %v ...", bucketName, resourcesPath))
106+
log.Info(fmt.Sprintf("Downloading resources from bucket %v to local directory %v ...", sourceBucketName, targetResourcesPath))
104107

105-
bucket := client.Bucket(bucketName)
108+
bucket := client.Bucket(sourceBucketName)
106109
it := bucket.Objects(ctx, nil)
107110
for {
108111
attributes, err := it.Next()
@@ -113,7 +116,7 @@ func handleResourceDownloadRun(cmd *cobra.Command, args []string) {
113116
log.Error(err)
114117
} else {
115118
source := bucket.Object(attributes.Name)
116-
targetPathAndFilename := filepath.Join(resourcesPath, getRelativePersistentResourcePathByHash(attributes.Name), filepath.Base(attributes.Name))
119+
targetPathAndFilename := filepath.Join(targetResourcesPath, getRelativePersistentResourcePathByHash(attributes.Name), filepath.Base(attributes.Name))
117120

118121
err = os.MkdirAll(filepath.Dir(targetPathAndFilename), 0755)
119122
if err != nil {

cmd/beach/cmd/resource-upload.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"google.golang.org/api/option"
3030
)
3131

32-
var instanceIdentifier, projectNamespace, bucketName, resourcesPath, resumeWithFile string
32+
var targetBucketName, sourceResourcesPath, resumeWithFile string
3333
var force bool
3434

3535
// resourceUploadCmd represents the resource-upload command
@@ -62,6 +62,7 @@ Notes:
6262
func init() {
6363
resourceUploadCmd.Flags().StringVar(&instanceIdentifier, "instance", "", "instance identifier of the Beach instance to upload to, eg. 'instance-123abc45-def6-7890-abcd-1234567890ab'")
6464
resourceUploadCmd.Flags().StringVar(&projectNamespace, "namespace", "", "The project namespace of the Beach instance to upload to, eg. 'beach-project-123abc45-def6-7890-abcd-1234567890ab'")
65+
resourceUploadCmd.Flags().StringVar(&clusterIdentifier, "cluster", "", "The cluster identifier of the Beach instance to upload to, eg. 'h9acc4'")
6566
resourceUploadCmd.Flags().BoolVar(&force, "force", false, "Force uploading resources which already exist in the target bucket")
6667
resourceUploadCmd.Flags().StringVar(&resumeWithFile, "resume-with-file", "", "If specified, resume uploading resources starting with the given filename, eg. '12dcde4c13142942288c5a973caf0fa720ed2794'")
6768
_ = resourceUploadCmd.MarkFlagRequired("instance")
@@ -75,23 +76,23 @@ func handleResourceUploadRun(cmd *cobra.Command, args []string) {
7576
log.Fatal("Could not activate sandbox: ", err)
7677
return
7778
}
78-
if resourcesPath == "" {
79-
resourcesPath = sandbox.ProjectDataPersistentResourcesPath
79+
if sourceResourcesPath == "" {
80+
sourceResourcesPath = sandbox.ProjectDataPersistentResourcesPath
8081
}
81-
_, err = os.Stat(resourcesPath)
82+
_, err = os.Stat(sourceResourcesPath)
8283
if err != nil {
83-
log.Fatal("The path %v does not exist", resourcesPath)
84+
log.Fatal("The path %v does not exist", sourceResourcesPath)
8485
return
8586
}
8687

87-
err, bucketNameFromCredentials, privateKeyDecoded := retrieveCloudStorageCredentials(instanceIdentifier, projectNamespace)
88+
err, bucketNameFromCredentials, privateKeyDecoded := retrieveCloudStorageCredentials(instanceIdentifier, projectNamespace, clusterIdentifier)
8889
if err != nil {
8990
log.Fatal(err)
9091
return
9192
}
9293

93-
if bucketName == "" {
94-
bucketName = bucketNameFromCredentials
94+
if targetBucketName == "" {
95+
targetBucketName = bucketNameFromCredentials
9596
}
9697

9798
ctx := context.Background()
@@ -101,10 +102,10 @@ func handleResourceUploadRun(cmd *cobra.Command, args []string) {
101102
return
102103
}
103104

104-
log.Info(fmt.Sprintf("Uploading resources from local directory %v to bucket %v...", resourcesPath, bucketName))
105+
log.Info(fmt.Sprintf("Uploading resources from local directory %v to bucket %v...", sourceResourcesPath, targetBucketName))
105106

106107
var fileList []string
107-
err = filepath.Walk(resourcesPath, func(path string, f os.FileInfo, err error) error {
108+
err = filepath.Walk(sourceResourcesPath, func(path string, f os.FileInfo, err error) error {
108109
if !f.IsDir() {
109110
fileList = append(fileList, path)
110111
}
@@ -115,7 +116,7 @@ func handleResourceUploadRun(cmd *cobra.Command, args []string) {
115116
return
116117
}
117118

118-
bucket := client.Bucket(bucketName)
119+
bucket := client.Bucket(targetBucketName)
119120
for _, pathAndFilename := range fileList {
120121
filename := filepath.Base(pathAndFilename)
121122

0 commit comments

Comments
 (0)