forked from openshift/origin
/
newapp.go
148 lines (123 loc) · 5.26 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
package cmd
import (
"fmt"
"io"
"os"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/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"
newcmd "github.com/openshift/origin/pkg/generate/app/cmd"
imageapi "github.com/openshift/origin/pkg/image/api"
)
type usage interface {
UsageError(commandName string) string
}
const newAppLongDesc = `
Create a new application in OpenShift by specifying source code, templates, and/or images.
This command will try to build up the components of an application using images or code
located on your system. It will lookup the images on the local Docker installation (if
available), a Docker registry, or an OpenShift image repository. 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. The images will be deployed via a deployment
configuration, and a service will be hookup up to the first public port of the app.
Examples:
# Try to create an application based on the source code in the current directory
$ %[1]s new-app .
$ Use the public Docker Hub MySQL image to create an app
$ %[1]s new-app mysql
# Use a MySQL image in a private registry to create an app
$ %[1]s new-app myregistry.com/mycompany/mysql
If you specify source code, you may need to run a build with 'start-build' after the
application is created.
ALPHA: This command is under active development - feedback is appreciated.
`
func NewCmdNewApplication(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
config := newcmd.NewAppConfig()
helper := dockerutil.NewHelper()
cmd := &cobra.Command{
Use: "new-app <components> [--code=<path|url>]",
Short: "Create a new application",
Long: fmt.Sprintf(newAppLongDesc, fullName),
Run: func(c *cobra.Command, args []string) {
namespace, err := f.DefaultNamespace(c)
checkErr(err)
if dockerClient, _, err := helper.GetClient(); err == nil {
if err := dockerClient.Ping(); err == nil {
config.SetDockerClient(dockerClient)
} else {
glog.V(2).Infof("No local Docker daemon detected: %v", err)
}
}
osclient, _, err := f.Clients(c)
if err != nil {
glog.Fatalf("Error getting client: %v", err)
}
config.SetOpenShiftClient(osclient, namespace)
unknown := config.AddArguments(args)
if len(unknown) != 0 {
glog.Fatalf("Did not recognize the following arguments: %v", unknown)
}
result, err := config.Run(out)
if err != 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
glog.Fatal("You must specify one or more images, image repositories, or source code locations to create an application.")
}
if u, ok := err.(usage); ok {
glog.Fatal(u.UsageError(c.CommandPath()))
}
glog.Fatalf("Error: %v", err)
}
if len(cmdutil.GetFlagString(c, "output")) != 0 {
if err := f.Factory.PrintObject(c, result.List, out); err != nil {
glog.Fatalf("Error: %v", err)
}
return
}
bulk := configcmd.Bulk{
Factory: f.Factory,
Command: c,
After: configcmd.NewPrintNameOrErrorAfter(out, os.Stderr),
}
if errs := bulk.Create(result.List, namespace); len(errs) != 0 {
os.Exit(1)
}
hasMissingRepo := false
for _, item := range result.List.Items {
switch t := item.(type) {
case *kapi.Service:
fmt.Fprintf(os.Stderr, "Service %q created at %s:%d to talk to pods over port %d.\n", t.Name, t.Spec.PortalIP, t.Spec.Port, t.Spec.ContainerPort.IntVal)
case *buildapi.BuildConfig:
fmt.Fprintf(os.Stderr, "A build was created - you can run `osc start-build %s` to start it.\n", t.Name)
case *imageapi.ImageRepository:
if len(t.Status.DockerImageRepository) == 0 {
if hasMissingRepo {
continue
}
hasMissingRepo = true
fmt.Fprintf(os.Stderr, "WARNING: We created an image repository %q, but it does not look like a Docker registry has been integrated with the OpenShift server. Automatic builds and deployments depend on that integration to detect new images and will not function properly.\n", t.Name)
}
}
}
},
}
cmd.Flags().Var(&config.SourceRepositories, "code", "Source code to use to build this application.")
cmd.Flags().VarP(&config.ImageStreams, "image", "i", "Name of an OpenShift image repository 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.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.TypeOfBuild, "build", "", "Specify the type of build to use if you don't want to detect (docker|source)")
cmdutil.AddPrinterFlags(cmd)
return cmd
}