-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_quickstart.go
318 lines (285 loc) · 9.28 KB
/
create_quickstart.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package cmd
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/quickstarts"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
)
const (
// JenkinsXQuickstartsOrganisation is the default organisation for quickstarts
JenkinsXQuickstartsOrganisation = "jenkins-x-quickstarts"
)
var (
createQuickstartLong = templates.LongDesc(`
Create a new project from a sample/starter (found in https://github.com/jenkins-x-quickstarts)
This will create a new project for you from the selected template.
It will exclude any work-in-progress repos (containing the "WIP-" pattern)
For more documentation see: [https://jenkins-x.io/developing/create-quickstart/](https://jenkins-x.io/developing/create-quickstart/)
`)
createQuickstartExample = templates.Examples(`
Create a new project from a sample/starter (found in https://github.com/jenkins-x-quickstarts)
This will create a new project for you from the selected template.
It will exclude any work-in-progress repos (containing the "WIP-" pattern)
jx create quickstart
jx create quickstart -f http
`)
)
// CreateQuickstartOptions the options for the create spring command
type CreateQuickstartOptions struct {
CreateProjectOptions
GitHubOrganisations []string
Filter quickstarts.QuickstartFilter
GitProvider gits.GitProvider
GitHost string
IgnoreTeam bool
}
// NewCmdCreateQuickstart creates a command object for the "create" command
func NewCmdCreateQuickstart(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &CreateQuickstartOptions{
CreateProjectOptions: CreateProjectOptions{
ImportOptions: ImportOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "quickstart",
Short: "Create a new app from a Quickstart and import the generated code into Git and Jenkins for CI/CD",
Long: createQuickstartLong,
Example: createQuickstartExample,
Aliases: []string{"arch"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCreateAppFlags(cmd)
cmd.Flags().StringArrayVarP(&options.GitHubOrganisations, "organisations", "g", []string{}, "The GitHub organisations to query for quickstarts")
cmd.Flags().StringArrayVarP(&options.Filter.Tags, "tag", "t", []string{}, "The tags on the quickstarts to filter")
cmd.Flags().StringVarP(&options.Filter.Owner, "owner", "", "", "The owner to filter on")
cmd.Flags().StringVarP(&options.Filter.Language, "language", "l", "", "The language to filter on")
cmd.Flags().StringVarP(&options.Filter.Framework, "framework", "", "", "The framework to filter on")
cmd.Flags().StringVarP(&options.GitHost, "git-host", "", "", "The Git server host if not using GitHub when pushing created project")
cmd.Flags().StringVarP(&options.Filter.Text, "filter", "f", "", "The text filter")
cmd.Flags().StringVarP(&options.Filter.ProjectName, "project-name", "p", "", "The project name (for use with -b batch mode)")
return cmd
}
// Run implements the generic Create command
func (o *CreateQuickstartOptions) Run() error {
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
config := authConfigSvc.Config()
var locations []v1.QuickStartLocation
if !o.IgnoreTeam {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
err = o.registerEnvironmentCRD()
if err != nil {
return err
}
locations, err = kube.GetQuickstartLocations(jxClient, ns)
if err != nil {
return err
}
}
// lets add any extra github organisations if they are not already configured
for _, org := range o.GitHubOrganisations {
found := false
for _, loc := range locations {
if loc.GitURL == gits.GitHubURL && loc.Owner == org {
found = true
break
}
}
if !found {
locations = append(locations, v1.QuickStartLocation{
GitURL: gits.GitHubURL,
GitKind: gits.KindGitHub,
Owner: org,
Includes: []string{"*"},
Excludes: []string{"WIP-*"},
})
}
}
gitMap := map[string]map[string]v1.QuickStartLocation{}
for _, loc := range locations {
m := gitMap[loc.GitURL]
if m == nil {
m = map[string]v1.QuickStartLocation{}
gitMap[loc.GitURL] = m
}
m[loc.Owner] = loc
}
model, err := o.LoadQuickstartsFromMap(config, gitMap)
if err != nil {
return fmt.Errorf("failed to load quickstarts: %s", err)
}
q, err := model.CreateSurvey(&o.Filter, o.BatchMode, o.In, o.Out, o.Err)
if err != nil {
return err
}
if q == nil {
return fmt.Errorf("no quickstart chosen")
}
dir := o.OutDir
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return err
}
}
genDir, err := o.createQuickstart(q, dir)
if err != nil {
return err
}
// if there is a charts folder named after the app name, lets rename it to the generated app name
folder := ""
if q.Quickstart != nil {
folder = q.Quickstart.Name
}
idx := strings.LastIndex(folder, "/")
if idx > 0 {
folder = folder[idx+1:]
}
if folder != "" {
chartsDir := filepath.Join(genDir, "charts", folder)
exists, err := util.FileExists(chartsDir)
if err != nil {
return err
}
if exists {
o.PostDraftPackCallback = func() error {
_, appName := filepath.Split(genDir)
appChartDir := filepath.Join(genDir, "charts", appName)
log.Infof("### PostDraftPack callback copying from %s to %s!!!s\n", chartsDir, appChartDir)
err := util.CopyDirOverwrite(chartsDir, appChartDir)
if err != nil {
return err
}
err = os.RemoveAll(chartsDir)
if err != nil {
return err
}
return o.Git().Remove(genDir, filepath.Join("charts", folder))
}
} else {
log.Infof("### NO charts folder %s\n", chartsDir)
}
}
log.Infof("Created project at %s\n\n", util.ColorInfo(genDir))
o.CreateProjectOptions.ImportOptions.GitProvider = o.GitProvider
return o.ImportCreatedProject(genDir)
}
func (o *CreateQuickstartOptions) createQuickstart(f *quickstarts.QuickstartForm, dir string) (string, error) {
q := f.Quickstart
answer := filepath.Join(dir, f.Name)
u := q.DownloadZipURL
if u == "" {
return answer, fmt.Errorf("quickstart %s does not have a download zip URL", q.ID)
}
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, u, strings.NewReader(""))
if err != nil {
return answer, err
}
userAuth := q.GitProvider.UserAuth()
token := userAuth.ApiToken
username := userAuth.Username
if token != "" && username != "" {
o.Debugf("Downloading Quickstart source zip from %s with basic auth for user: %s\n", u, username)
req.SetBasicAuth(username, token)
}
res, err := client.Do(req)
if err != nil {
return answer, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return answer, err
}
zipFile := filepath.Join(dir, "source.zip")
err = ioutil.WriteFile(zipFile, body, util.DefaultWritePermissions)
if err != nil {
return answer, fmt.Errorf("failed to download file %s due to %s", zipFile, err)
}
tmpDir, err := ioutil.TempDir("", "jx-source-")
if err != nil {
return answer, fmt.Errorf("failed to create temporary directory: %s", err)
}
err = util.Unzip(zipFile, tmpDir)
if err != nil {
return answer, fmt.Errorf("failed to unzip new project file %s due to %s", zipFile, err)
}
err = os.Remove(zipFile)
if err != nil {
return answer, err
}
tmpDir, err = findFirstDirectory(tmpDir)
if err != nil {
return answer, fmt.Errorf("failed to find a directory inside the source download: %s", err)
}
err = util.RenameDir(tmpDir, answer, false)
if err != nil {
return answer, fmt.Errorf("failed to rename temp dir %s to %s: %s", tmpDir, answer, err)
}
log.Infof("Generated quickstart at %s\n", answer)
return answer, nil
}
func findFirstDirectory(dir string) (string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return dir, err
}
for _, f := range files {
if f.IsDir() {
return filepath.Join(dir, f.Name()), nil
}
}
return "", fmt.Errorf("no child directory found in %s", dir)
}
// LoadQuickstartsFromMap Load all quickstarts
func (o *CreateQuickstartOptions) LoadQuickstartsFromMap(config *auth.AuthConfig, gitMap map[string]map[string]v1.QuickStartLocation) (*quickstarts.QuickstartModel, error) {
model := quickstarts.NewQuickstartModel()
for gitURL, m := range gitMap {
for _, location := range m {
kind := location.GitKind
if kind == "" {
kind = gits.KindGitHub
}
gitProvider, err := o.gitProviderForGitServerURL(gitURL, kind)
if err != nil {
return model, err
}
o.Debugf("Searching for repositories in Git server %s owner %s includes %s excludes %s as user %s \n", gitProvider.ServerURL(), location.Owner, strings.Join(location.Includes, ", "), strings.Join(location.Excludes, ", "), gitProvider.CurrentUsername())
err = model.LoadGithubQuickstarts(gitProvider, location.Owner, location.Includes, location.Excludes)
if err != nil {
o.Debugf("Quickstart load error: %s\n", err.Error())
}
}
}
return model, nil
}