forked from openshift/origin
/
newapp.go
397 lines (341 loc) · 14.3 KB
/
newapp.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package cmd
import (
"fmt"
"io"
"os"
"sort"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
ctl "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
kutil "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
"github.com/golang/glog"
"github.com/spf13/cobra"
buildapi "github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
dockerutil "github.com/openshift/origin/pkg/cmd/util/docker"
configcmd "github.com/openshift/origin/pkg/config/cmd"
newapp "github.com/openshift/origin/pkg/generate/app"
newcmd "github.com/openshift/origin/pkg/generate/app/cmd"
imageapi "github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/util"
)
type usage interface {
UsageError(commandName string) string
}
var errExit = fmt.Errorf("exit directly")
const (
newAppLong = `
Create a new application by specifying source code, templates, and/or images
This command will try to build up the components of an application using images, templates,
or code that has a public repository. It will lookup the images on the local Docker installation
(if available), a Docker registry, or an integrated image stream.
If you specify a source code URL, it will set up a build that takes your source code and converts
it into an image that can run inside of a pod. Local source must be in a git repository that has a
remote repository that the server can see. The images will be deployed via a deployment
configuration, and a service will be connected to the first public port of the app. You may either specify
components using the various existing flags or let new-app autodetect what kind of components
you have provided.
If you provide source code, you may need to run a build with 'start-build' after the
application is created.`
newAppExample = `
// Create an application based on the source code in the current git repository (with a public remote)
// and a Docker image
$ %[1]s new-app . --docker-image=repo/langimage
// Create a Ruby application based on the provided [image]~[source code] combination
$ %[1]s new-app openshift/ruby-20-centos7~https://github.com/openshift/ruby-hello-world.git
// Use the public Docker Hub MySQL image to create an app. Generated artifacts will be labeled with db=mysql
$ %[1]s new-app mysql -l db=mysql
// Use a MySQL image in a private registry to create an app and override application artifacts' names
$ %[1]s new-app --docker-image=myregistry.com/mycompany/mysql --name=private
// Create an application from a remote repository using its beta4 branch
$ %[1]s new-app https://github.com/openshift/ruby-hello-world#beta4
// Create an application based on a stored template, explicitly setting a parameter value
$ %[1]s new-app --template=ruby-helloworld-sample --param=MYSQL_USER=admin
// Create an application from a remote repository and specify a context directory
$ %[1]s new-app https://github.com/youruser/yourgitrepo --context-dir=src/build
// Create an application based on a template file, explicitly setting a parameter value
$ %[1]s new-app --file=./example/myapp/template.json --param=MYSQL_USER=admin
// Search for "mysql" in all image repositories and stored templates
$ %[1]s new-app --search mysql
// Search for "ruby", but only in stored templates (--template, --image and --docker-image
// can be used to filter search results)
$ %[1]s new-app --search --template=ruby
// Search for "ruby" in stored templates and print the output as an YAML
$ %[1]s new-app --search --template=ruby --output=yaml`
)
// NewCmdNewApplication implements the OpenShift cli new-app command
func NewCmdNewApplication(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
mapper, typer := f.Object()
clientMapper := f.ClientMapperForCommand()
config := newcmd.NewAppConfig(typer, mapper, clientMapper)
cmd := &cobra.Command{
Use: "new-app (IMAGE | IMAGESTREAM | TEMPLATE | PATH | URL ...)",
Short: "Create a new application",
Long: newAppLong,
Example: fmt.Sprintf(newAppExample, fullName),
Run: func(c *cobra.Command, args []string) {
err := RunNewApplication(fullName, f, out, c, args, config)
if err == errExit {
os.Exit(1)
}
cmdutil.CheckErr(err)
},
}
cmd.Flags().Var(&config.SourceRepositories, "code", "Source code to use to build this application.")
cmd.Flags().StringVar(&config.ContextDir, "context-dir", "", "Context directory to be used for the build.")
cmd.Flags().VarP(&config.ImageStreams, "image", "", "Name of an image stream to use in the app. (deprecated)")
cmd.Flags().VarP(&config.ImageStreams, "image-stream", "i", "Name of an image stream to use in the app.")
cmd.Flags().Var(&config.DockerImages, "docker-image", "Name of a Docker image to include in the app.")
cmd.Flags().Var(&config.Templates, "template", "Name of a stored template to use in the app.")
cmd.Flags().VarP(&config.TemplateFiles, "file", "f", "Path to a template file to use for the app.")
cmd.Flags().VarP(&config.TemplateParameters, "param", "p", "Specify a list of key value pairs (eg. -p FOO=BAR,BAR=FOO) to set/override parameter values in the template.")
cmd.Flags().Var(&config.Groups, "group", "Indicate components that should be grouped together as <comp1>+<comp2>.")
cmd.Flags().VarP(&config.Environment, "env", "e", "Specify key value pairs of environment variables to set into each container.")
cmd.Flags().StringVar(&config.Name, "name", "", "Set name to use for generated application artifacts")
cmd.Flags().StringVar(&config.Strategy, "strategy", "", "Specify the build strategy to use if you don't want to detect (docker|source).")
cmd.Flags().StringP("labels", "l", "", "Label to set in all resources for this application.")
cmd.Flags().BoolVar(&config.InsecureRegistry, "insecure-registry", false, "If true, indicates that the referenced Docker images are on insecure registries and should bypass certificate checking")
cmd.Flags().BoolVarP(&config.AsSearch, "search", "S", false, "Search for components that match the arguments provided and print the results.")
// TODO AddPrinterFlags disabled so that it doesn't conflict with our own "template" flag.
// Need a better solution.
// cmdutil.AddPrinterFlags(cmd)
cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml|template|templatefile.")
cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers.")
cmd.Flags().String("output-template", "", "Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]")
return cmd
}
// RunNewApplication contains all the necessary functionality for the OpenShift cli new-app command
func RunNewApplication(fullName string, f *clientcmd.Factory, out io.Writer, c *cobra.Command, args []string, config *newcmd.AppConfig) error {
if err := setupAppConfig(f, c, args, config); err != nil {
return err
}
if config.AsSearch {
result, err := config.RunSearch(out, c.Out())
if err != nil {
return handleRunError(c, err)
}
if len(cmdutil.GetFlagString(c, "output")) != 0 {
return f.Factory.PrintObject(c, result.List, out)
}
return printHumanReadableSearchResult(result, out, fullName)
}
if err := setAppConfigLabels(c, config); err != nil {
return err
}
result, err := config.RunAll(out, c.Out())
if err != nil {
return handleRunError(c, err)
}
if err := setLabels(config.Labels, result); err != nil {
return err
}
if len(cmdutil.GetFlagString(c, "output")) != 0 {
return f.Factory.PrintObject(c, result.List, out)
}
if err := createObjects(f, out, result); err != nil {
return err
}
hasMissingRepo := false
for _, item := range result.List.Items {
switch t := item.(type) {
case *kapi.Service:
portMappings := "."
if len(t.Spec.Ports) > 0 {
portMappings = fmt.Sprintf(" with port mappings %s.", describeServicePorts(t.Spec))
}
fmt.Fprintf(c.Out(), "Service %q created at %s%s\n", t.Name, t.Spec.ClusterIP, portMappings)
case *buildapi.BuildConfig:
fmt.Fprintf(c.Out(), "A build was created - you can run `%s start-build %s` to start it.\n", fullName, t.Name)
case *imageapi.ImageStream:
if len(t.Status.DockerImageRepository) == 0 {
if hasMissingRepo {
continue
}
hasMissingRepo = true
fmt.Fprintf(c.Out(), "WARNING: We created an image stream %q, but it does not look like a Docker registry has been integrated with the server. Automatic builds and deployments depend on that integration to detect new images and will not function properly.\n", t.Name)
}
}
}
fmt.Fprintf(c.Out(), "Run '%s status' to view your app.\n", fullName)
return nil
}
func setAppConfigLabels(c *cobra.Command, config *newcmd.AppConfig) error {
labelStr := cmdutil.GetFlagString(c, "labels")
if len(labelStr) != 0 {
var err error
config.Labels, err = ctl.ParseLabels(labelStr)
if err != nil {
return err
}
}
return nil
}
func setupAppConfig(f *clientcmd.Factory, c *cobra.Command, args []string, config *newcmd.AppConfig) error {
namespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
dockerClient, _, err := dockerutil.NewHelper().GetClient()
if err == nil {
if err = dockerClient.Ping(); err == nil {
config.SetDockerClient(dockerClient)
}
}
if err != nil {
glog.V(2).Infof("No local Docker daemon detected: %v", err)
}
osclient, _, err := f.Clients()
if err != nil {
return err
}
config.SetOpenShiftClient(osclient, namespace)
unknown := config.AddArguments(args)
if len(unknown) != 0 {
return cmdutil.UsageError(c, "Did not recognize the following arguments: %v", unknown)
}
return nil
}
func setLabels(labels map[string]string, result *newcmd.AppResult) error {
if len(labels) == 0 {
if len(result.Name) > 0 {
labels = map[string]string{"app": result.Name}
}
}
for _, object := range result.List.Items {
err := util.AddObjectLabels(object, labels)
if err != nil {
return err
}
}
return nil
}
func createObjects(f *clientcmd.Factory, out io.Writer, result *newcmd.AppResult) error {
// TODO: Validate everything before building
mapper, typer := f.Factory.Object()
bulk := configcmd.Bulk{
Mapper: mapper,
Typer: typer,
RESTClientFactory: f.Factory.RESTClient,
After: configcmd.NewPrintNameOrErrorAfter(out, os.Stderr),
}
if errs := bulk.Create(result.List, result.Namespace); len(errs) != 0 {
return errExit
}
return nil
}
func describeServicePorts(spec kapi.ServiceSpec) string {
switch len(spec.Ports) {
case 1:
if spec.Ports[0].TargetPort.String() == "0" || spec.ClusterIP == kapi.ClusterIPNone || spec.Ports[0].Port == spec.Ports[0].TargetPort.IntVal {
return fmt.Sprintf("%d", spec.Ports[0].Port)
}
return fmt.Sprintf("%d->%s", spec.Ports[0].Port, spec.Ports[0].TargetPort.String())
default:
pairs := []string{}
for _, port := range spec.Ports {
if port.TargetPort.String() == "0" || spec.ClusterIP == kapi.ClusterIPNone {
pairs = append(pairs, fmt.Sprintf("%d", port.Port))
continue
}
pairs = append(pairs, fmt.Sprintf("%d->%s", port.Port, port.TargetPort.String()))
}
return strings.Join(pairs, ", ")
}
}
func handleRunError(c *cobra.Command, err error) error {
if err == nil {
return nil
}
if errs, ok := err.(errors.Aggregate); ok {
if len(errs.Errors()) == 1 {
err = errs.Errors()[0]
}
}
if err == newcmd.ErrNoInputs {
// TODO: suggest things to the user
return cmdutil.UsageError(c, "You must specify one or more images, image streams, templates or source code locations to create an application.")
}
return err
}
func printHumanReadableSearchResult(r *newcmd.SearchResult, out io.Writer, fullName string) error {
if len(r.Matches) == 0 {
return fmt.Errorf("no matches found")
}
templates := newapp.ComponentMatches{}
imageStreams := newapp.ComponentMatches{}
dockerImages := newapp.ComponentMatches{}
for _, match := range r.Matches {
switch {
case match.IsTemplate():
templates = append(templates, match)
case match.IsImage() && match.ImageStream != nil:
imageStreams = append(imageStreams, match)
case match.IsImage() && match.Image != nil:
dockerImages = append(dockerImages, match)
}
}
sort.Sort(newapp.ScoredComponentMatches(templates))
sort.Sort(newapp.ScoredComponentMatches(imageStreams))
sort.Sort(newapp.ScoredComponentMatches(dockerImages))
if len(templates) > 0 {
fmt.Fprintln(out, "Templates (oc new-app --template=<template>)")
fmt.Fprintln(out, "-----")
for _, match := range templates {
template := match.Template
description := template.ObjectMeta.Annotations["description"]
fmt.Fprintln(out, template.Name)
fmt.Fprintf(out, " Project: %v\n", template.Namespace)
if len(description) > 0 {
fmt.Fprintf(out, " %v\n", description)
}
}
fmt.Fprintln(out)
}
if len(imageStreams) > 0 {
fmt.Fprintln(out, "Image streams (oc new-app --image-stream=<image-stream> [--code=<source>])")
fmt.Fprintln(out, "-----")
for _, match := range imageStreams {
imageStream := match.ImageStream
description := imageStream.ObjectMeta.Annotations["description"]
tags := "<none>"
if len(imageStream.Status.Tags) > 0 {
set := kutil.NewStringSet()
for tag := range imageStream.Status.Tags {
set.Insert(tag)
}
tags = strings.Join(set.List(), ",")
}
fmt.Fprintln(out, imageStream.Name)
fmt.Fprintf(out, " Project: %v\n", imageStream.Namespace)
fmt.Fprintf(out, " Tracks: %v\n", imageStream.Status.DockerImageRepository)
fmt.Fprintf(out, " Tags: %v\n", tags)
if len(description) > 0 {
fmt.Fprintf(out, " %v\n", description)
}
}
fmt.Fprintln(out)
}
if len(dockerImages) > 0 {
fmt.Fprintln(out, "Docker images (oc new-app --docker-image=<docker-image> [--code=<source>])")
fmt.Fprintln(out, "-----")
for _, match := range dockerImages {
image := match.Image
name, tag, ok := imageapi.SplitImageStreamTag(match.Name)
if !ok {
name = match.Name
tag = match.ImageTag
}
fmt.Fprintln(out, name)
fmt.Fprintf(out, " Registry: %v\n", match.Meta["registry"])
fmt.Fprintf(out, " Tags: %v\n", tag)
if len(image.Comment) > 0 {
fmt.Fprintf(out, " %v\n", image.Comment)
}
}
fmt.Fprintln(out)
}
return nil
}