-
Notifications
You must be signed in to change notification settings - Fork 784
/
edit_storage.go
233 lines (201 loc) · 7.81 KB
/
edit_storage.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package cmd
import (
"context"
"fmt"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/cloud/buckets"
"github.com/jenkins-x/jx/pkg/cloud/gke"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gocloud.dev/blob"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"io"
"net/url"
"time"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
)
var (
editStorageLong = templates.LongDesc(`
Configures the storage location for a set of pipeline output data for your team
Per team you can specify a Git repository URL to store artifacts inside per classification or you can use a HTTP URL.
If you don't specify any specific storage for a classifier it will try the classifier 'default'.If there is still no configuration then it will default to the git repository for a project.'
`)
editStorageExample = templates.Examples(`
# Be prompted what classification to edit
jx edit storage
# Configure the git/http URLs of where to store logs
jx edit storage -c logs
# Configure the git URL of where to store logs (defaults to gh-pages branch)
jx edit storage -c logs --git-url https://github.com/myorg/mylogs.git'
# Configure the git URL and branch of where to store logs
jx edit storage -c logs --git-url https://github.com/myorg/mylogs.git' --git-branch cheese
# Configure the git URL of where all storage goes to by default unless a specific classifier has a config
jx edit storage -c default --git-url https://github.com/myorg/mylogs.git'
`)
)
// EditStorageOptions the options for the create spring command
type EditStorageOptions struct {
CreateOptions
StorageLocation jenkinsv1.StorageLocation
Bucket string
BucketKind string
GKEProjectID string
GKEZone string
}
// NewCmdEditStorage creates a command object for the "create" command
func NewCmdEditStorage(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &EditStorageOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "storage",
Short: "Configures the storage location for a set of pipeline output data for your team",
Aliases: []string{"store"},
Long: editStorageLong,
Example: editStorageExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
cmd.Flags().StringVarP(&options.StorageLocation.Classifier, "classifier", "c", "", "A name which classifies this type of file. Example values: "+kube.ClassificationValues)
cmd.Flags().StringVarP(&options.StorageLocation.BucketURL, "bucket-url", "", "", "Specify the go-cloud URL of the bucket to use")
cmd.Flags().StringVarP(&options.StorageLocation.GitURL, "git-url", "", "", "Specify the Git URL to populate in a gh-pages branch")
cmd.Flags().StringVarP(&options.StorageLocation.GitBranch, "git-branch", "", "gh-pages", "The branch to use to store files in the git branch")
cmd.Flags().StringVarP(&options.Bucket, "bucket", "", "", "Specify the name of the bucket to use")
cmd.Flags().StringVarP(&options.BucketKind, "bucket-kind", "", "", "The kind of bucket to use like 'gs, s3, azure' etc")
cmd.Flags().StringVarP(&options.GKEProjectID, "gke-project-id", "", "", "Google Project ID to use for a new bucket")
cmd.Flags().StringVarP(&options.GKEZone, "gke-zone", "", "", "The zone (e.g. us-central1-a) where the new bucket will be created")
return cmd
}
// Run implements the command
func (o *EditStorageOptions) Run() error {
settings, err := o.TeamSettings()
if err != nil {
return err
}
classifier := o.StorageLocation.Classifier
if classifier == "" && ! o.BatchMode {
o.StorageLocation.Classifier, err = util.PickName(kube.Classifications, "Pick the content classification name", "The name is used as a key to store content in different locations", o.In, o.Out, o.Err)
if err != nil {
return errors.Wrapf(err, "failed to pick the classification name")
}
}
if classifier == "" {
return util.MissingOption("classifier")
}
currentLocation := settings.StorageLocationOrDefault(classifier)
if o.StorageLocation.BucketURL == "" && o.StorageLocation.GitURL == "" {
if o.Bucket != "" {
o.StorageLocation.BucketURL, err = buckets.CreateBucketURL(o.Bucket, o.BucketKind, settings)
if err != nil {
return errors.Wrapf(err, "failed to create the bucket URL for %s", o.Bucket)
}
ctx, _ := context.WithTimeout(context.Background(), time.Second * 20)
bucket, err := blob.Open(ctx, o.StorageLocation.BucketURL)
if err != nil {
return errors.Wrapf(err, "failed to open the bucket for %s", o.StorageLocation.BucketURL)
}
// lets check if the bucket exists
iter := bucket.List(nil)
obj, err := iter.Next(ctx)
if err != nil {
if err == io.EOF {
log.Infof("bucket %s is empty\n", o.StorageLocation.BucketURL)
} else {
log.Infof("The bucket %s does not exist yet so lets create it...\n", util.ColorInfo(o.StorageLocation.BucketURL))
err = o.createBucket(o.StorageLocation.BucketURL, bucket)
if err != nil {
return errors.Wrapf(err, "failed to create the bucket for %s", o.StorageLocation.BucketURL)
}
}
} else {
log.Infof("Found item in bucket %s for %s\n", o.StorageLocation.BucketURL, obj.Key)
}
}
if o.StorageLocation.BucketURL == "" {
o.StorageLocation.BucketURL, err = util.PickValue("Bucket URL:", o.StorageLocation.BucketURL, false, "The go-cloud bucket URL for storage such as 'gs://mybucket/ or s3://bucket2/", o.In, o.Out, o.Err)
if err != nil {
return errors.Wrapf(err, "failed to pick the bucket URL")
}
}
if o.StorageLocation.BucketURL == "" {
if o.BatchMode {
if currentLocation.GitURL == "" {
return util.MissingOption("git-url")
}
o.StorageLocation.GitURL = currentLocation.GitURL
} else {
o.StorageLocation.GitURL, err = util.PickValue("Git repository URL to store content:", currentLocation.GitURL, false, "The Git URL will be used to clone and push the storage to", o.In, o.Out, o.Err)
if err != nil {
return errors.Wrapf(err, "failed to pick the git URL")
}
}
}
}
callback := func(env *v1.Environment) error {
env.Spec.TeamSettings.SetStorageLocation(classifier, o.StorageLocation)
return nil
}
return o.ModifyDevEnvironment(callback)
}
// createBucket creates a bucket if it does not already exist
func (o *EditStorageOptions) createBucket(bucketURL string, bucket *blob.Bucket) error {
u, err := url.Parse(bucketURL)
if err != nil {
return err
}
switch u.Scheme {
case "gs":
return o.createGcsBucket(u, bucket)
default:
return fmt.Errorf("Cannot create a bucket for provider %s", bucketURL)
}
}
func (o *EditStorageOptions) createGcsBucket(u *url.URL, bucket *blob.Bucket) error {
var err error
if o.GKEProjectID == "" {
o.GKEProjectID, err = o.getGoogleProjectId()
if err != nil {
return err
}
}
err = o.CreateOptions.CommonOptions.runCommandVerbose(
"gcloud", "config", "set", "project", o.GKEProjectID)
if err != nil {
return err
}
if o.GKEZone == "" {
defaultZone := ""
if cluster, err := gke.ClusterName(o.Kube()); err == nil && cluster != "" {
if clusterZone, err := gke.ClusterZone(cluster); err == nil {
defaultZone = clusterZone
}
}
o.GKEZone, err = o.getGoogleZoneWithDefault(o.GKEProjectID, defaultZone)
if err != nil {
return err
}
}
bucketName := u.Host
region := gke.GetRegionFromZone(o.GKEZone, )
err = gke.CreateBucket(o.GKEProjectID, bucketName, region)
if err != nil {
return errors.Wrapf(err, "creating bucket %s in project %s and region %s", bucketName, o.GKEProjectID, region)
}
return nil
}