forked from cloudfoundry-community/cf-plugin-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.go
303 lines (264 loc) · 6.27 KB
/
seed.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/cloudfoundry/cli/plugin"
"github.com/codegangsta/cli"
"gopkg.in/yaml.v2"
)
//VERSION of seeder
const VERSION = "0.0.2"
func fatalIf(err error) {
if err != nil {
fmt.Fprintln(os.Stdout, "error:", err)
os.Exit(1)
}
}
func main() {
plugin.Start(&SeedPlugin{})
}
//SeedPlugin empty struct for plugin
type SeedPlugin struct{}
//Run of seeder plugin
func (plugin SeedPlugin) Run(cliConnection plugin.CliConnection, args []string) {
app := cli.NewApp()
app.Name = "seed"
app.Version = VERSION
app.Author = "Long Nguyen"
app.Email = "[email protected]"
app.Usage = "Seeds Cloud Foundry and setups apps/orgs/services on a given Cloud Foundry setup"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "f",
Value: "",
Usage: "seed manifest for seeding Cloud Foundry",
},
cli.BoolFlag{
Name: "c",
Usage: "cleanup all things created by the manifest",
},
}
app.Action = func(c *cli.Context) {
if !c.IsSet("f") {
cli.ShowAppHelp(c)
os.Exit(1)
}
fileName := c.String("f")
seedRepo := NewSeedRepo(cliConnection, fileName)
err := seedRepo.ReadManifest()
fatalIf(err)
if c.Bool("c") {
err = seedRepo.DeleteApps()
fatalIf(err)
err = seedRepo.DeleteServices()
fatalIf(err)
err = seedRepo.DeleteSpaces()
fatalIf(err)
err = seedRepo.DeleteOrganizations()
fatalIf(err)
} else {
err = seedRepo.CreateOrganizations()
fatalIf(err)
err = seedRepo.CreateSpaces()
fatalIf(err)
err = seedRepo.CreateServices()
fatalIf(err)
err = seedRepo.CreateApps()
fatalIf(err)
}
}
app.Run(args)
}
//GetMetadata of plugin
func (SeedPlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "cf-plugin-seed",
Commands: []plugin.Command{
{
Name: "seed",
HelpText: "Seeds Cloud Foundry and setups apps/orgs/services on new Cloud Foundry setup",
},
},
}
}
//SeedRepo of cli
type SeedRepo struct {
conn plugin.CliConnection
fileName string
Manifest SeederManifest
}
func NewSeedRepo(conn plugin.CliConnection, fileName string) *SeedRepo {
return &SeedRepo{
conn: conn,
fileName: fileName,
}
}
func (repo *SeedRepo) ReadManifest() error {
file, err := ioutil.ReadFile(repo.fileName)
if err != nil {
return err
}
repo.Manifest = SeederManifest{}
err = yaml.Unmarshal(file, &repo.Manifest)
if err != nil {
return err
}
return nil
}
func (repo *SeedRepo) CreateOrganizations() error {
for _, org := range repo.Manifest.Organizations {
_, err := repo.conn.CliCommand("create-org", org.Name)
if err != nil {
return err
}
}
return nil
}
func (repo *SeedRepo) DeleteOrganizations() error {
for _, org := range repo.Manifest.Organizations {
_, err := repo.conn.CliCommand("delete-org", org.Name, "-f")
if err != nil {
return err
}
}
return nil
}
func (repo *SeedRepo) CreateSpaces() error {
for _, org := range repo.Manifest.Organizations {
repo.conn.CliCommand("target", "-o", org.Name)
for _, space := range org.Spaces {
_, err := repo.conn.CliCommand("create-space", space.Name)
if err != nil {
return err
}
}
}
return nil
}
func (repo *SeedRepo) DeleteSpaces() error {
for _, org := range repo.Manifest.Organizations {
repo.conn.CliCommand("target", "-o", org.Name)
for _, space := range org.Spaces {
_, err := repo.conn.CliCommand("delete-space", space.Name, "-f")
if err != nil {
return err
}
}
}
return nil
}
func (repo *SeedRepo) CreateServices() error {
for _, org := range repo.Manifest.Organizations {
for _, space := range org.Spaces {
repo.conn.CliCommand("target", "-o", org.Name, "-s", space.Name)
for _, service := range space.Services {
_, err := repo.conn.CliCommand("create-service", service.Service, service.Plan, service.Name)
if err != nil {
return err
}
}
}
}
return nil
}
func (repo *SeedRepo) DeleteServices() error {
for _, org := range repo.Manifest.Organizations {
for _, space := range org.Spaces {
repo.conn.CliCommand("target", "-o", org.Name, "-s", space.Name)
for _, service := range space.Services {
_, err := repo.conn.CliCommand("delete-service", service.Name, "-f")
if err != nil {
return err
}
}
}
}
return nil
}
func (repo *SeedRepo) CreateApps() error {
for _, org := range repo.Manifest.Organizations {
for _, space := range org.Spaces {
repo.conn.CliCommand("target", "-o", org.Name, "-s", space.Name)
for _, app := range space.Apps {
err := repo.DeployApp(app)
if err != nil {
return err
}
}
}
}
return nil
}
func (repo *SeedRepo) DeleteApps() error {
for _, org := range repo.Manifest.Organizations {
for _, space := range org.Spaces {
repo.conn.CliCommand("target", "-o", org.Name, "-s", space.Name)
for _, app := range space.Apps {
err := repo.DeleteApp(app)
if err != nil {
return err
}
}
}
}
return nil
}
//DeleteApp deletes a single app
func (repo *SeedRepo) DeleteApp(app App) error {
_, err := repo.conn.CliCommand("delete", app.Name, "-f", "-r")
if err != nil {
return err
}
return nil
}
//DeployApp deploys a single app
func (repo *SeedRepo) DeployApp(app App) error {
args := []string{"push", app.Name}
if app.Repo != "" {
wd, _ := os.Getwd()
appPath := wd + "/apps/" + app.Name
os.MkdirAll(appPath, 0777)
files, _ := ioutil.ReadDir(appPath)
if len(files) == 0 {
gitPath, err := exec.LookPath("git")
if err != nil {
return err
}
err = exec.Command(gitPath, "clone", app.Repo, appPath).Run()
if err != nil {
return nil
}
}
args = append(args, "-p", appPath)
} else if app.Path != "" {
args = append(args, "-p", app.Path)
} else if app.Manifest != "" {
args = append(args, "-f", app.Manifest)
} else {
errMsg := fmt.Sprintf("App need repo or path %s", app.Name)
return errors.New(errMsg)
}
if app.Disk != "" {
args = append(args, "-k", app.Disk)
}
if app.Memory != "" {
args = append(args, "-m", app.Memory)
}
if app.Instances != "" {
args = append(args, "-i", app.Instances)
}
if app.Hostname != "" {
args = append(args, "-n", app.Hostname)
}
if app.Domain != "" {
args = append(args, "-d", app.Domain)
}
if app.Buildpack != "" {
args = append(args, "-b", app.Buildpack)
}
repo.conn.CliCommand(args...)
return nil
}