-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcloud_foundry.go
143 lines (117 loc) · 3.13 KB
/
cloud_foundry.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
package out
import (
"fmt"
"github.com/concourse/cf-resource/out/zdt"
"os"
"os/exec"
)
//go:generate counterfeiter . PAAS
type PAAS interface {
Login(api string, username string, password string, clientID string, clientSecret string, insecure bool) error
Target(organization string, space string) error
PushApp(manifest string, path string, currentAppName string, vars map[string]interface{}, varsFiles []string, dockerUser string, showLogs bool, noStart bool) error
}
type CloudFoundry struct {
verbose bool
}
func NewCloudFoundry(verbose bool) *CloudFoundry {
return &CloudFoundry{verbose}
}
func (cf *CloudFoundry) Login(api string, username string, password string, clientID string, clientSecret string, insecure bool) error {
args := []string{"api", api}
if insecure {
args = append(args, "--skip-ssl-validation")
}
err := cf.cf(args...).Run()
if err != nil {
return err
}
if clientID != "" && clientSecret != "" {
return cf.cf("auth", "--client-credentials", clientID, clientSecret).Run()
}
return cf.cf("auth", username, password).Run()
}
func (cf *CloudFoundry) Target(organization string, space string) error {
return cf.cf("target", "-o", organization, "-s", space).Run()
}
func (cf *CloudFoundry) PushApp(
manifest string,
path string,
currentAppName string,
vars map[string]interface{},
varsFiles []string,
dockerUser string,
showLogs bool,
noStart bool,
) error {
if zdt.CanPush(cf.cf, currentAppName) {
pushFunction := func() error {
return cf.simplePush(manifest, path, currentAppName, vars, varsFiles, dockerUser, noStart)
}
return zdt.Push(cf.cf, currentAppName, pushFunction, showLogs)
} else {
return cf.simplePush(manifest, path, currentAppName, vars, varsFiles, dockerUser, noStart)
}
}
func (cf *CloudFoundry) simplePush(
manifest string,
path string,
currentAppName string,
vars map[string]interface{},
varsFiles []string,
dockerUser string,
noStart bool,
) error {
args := []string{"push"}
if currentAppName != "" {
args = append(args, currentAppName)
}
args = append(args, "-f", manifest)
if noStart {
args = append(args, "--no-start")
}
for name, value := range vars {
args = append(args, "--var", fmt.Sprintf("%s=%s", name, value))
}
for _, varsFile := range varsFiles {
args = append(args, "--vars-file", varsFile)
}
if dockerUser != "" {
args = append(args, "--docker-username", dockerUser)
}
if path != "" {
stat, err := os.Stat(path)
if err != nil {
return err
}
if stat.IsDir() {
args = append(args, "-p", ".")
return chdir(path, cf.cf(args...).Run)
}
// path is a zip file, add it to the args
args = append(args, "-p", path)
}
return cf.cf(args...).Run()
}
func chdir(path string, f func() error) error {
oldpath, err := os.Getwd()
if err != nil {
return err
}
err = os.Chdir(path)
if err != nil {
return err
}
defer os.Chdir(oldpath)
return f()
}
func (cf *CloudFoundry) cf(args ...string) *exec.Cmd {
cmd := exec.Command("cf", args...)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), "CF_COLOR=true", "CF_DIAL_TIMEOUT=30")
if cf.verbose {
cmd.Env = append(cmd.Env, "CF_TRACE=true")
}
return cmd
}