forked from cloudfoundry/cli
/
app_deploy.go
107 lines (90 loc) · 2.26 KB
/
app_deploy.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
package servicebrokerstub
import (
"fmt"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/onsi/ginkgo"
"code.cloudfoundry.org/cli/integration/helpers"
. "github.com/onsi/gomega"
)
const (
appNamePrefix = "hydrabroker"
appOrg = "fakeservicebroker"
appSpace = "integration"
defaultMemoryLimit = "32M"
pathToApp = "../../assets/hydrabroker"
)
func ensureAppIsDeployed() {
if !appResponds() {
ensureAppIsPushed()
Eventually(appResponds).Should(BeTrue())
}
}
func appResponds() bool {
resp, err := http.Head(appURL())
Expect(err).ToNot(HaveOccurred())
defer resp.Body.Close()
return resp.StatusCode == http.StatusNoContent
}
func ensureAppIsPushed() {
appExists := func() bool {
session := helpers.CF("app", "--guid", appName())
session.Wait()
return session.ExitCode() == 0
}
pushApp := func() bool {
session := helpers.CF(
"push", appName(),
"-p", pathToApp,
"-m", defaultMemoryLimit,
"-b", "https://github.com/cloudfoundry/go-buildpack.git", // Some legacy envs have buildpack that's too old
)
session.Wait()
return session.ExitCode() == 0
}
cleanupAppsFromPreviousRuns := func() {
session := helpers.CF("apps")
session.Wait()
if session.ExitCode() == 0 {
matchingApps := regexp.MustCompile(fmt.Sprintf(`%s-\d+`, appNamePrefix)).
FindAllString(string(session.Out.Contents()), -1)
for _, app := range matchingApps {
if app != appName() {
session := helpers.CF("delete", app, "-f")
session.Wait()
}
}
}
}
helpers.CreateOrgAndSpaceUnlessExists(appOrg, appSpace)
helpers.WithRandomHomeDir(func() {
helpers.SetAPI()
helpers.LoginCF()
helpers.TargetOrgAndSpace(appOrg, appSpace)
cleanupAppsFromPreviousRuns()
ok := false
for attempts := 0; attempts < 5 && !ok; attempts++ {
ok = appExists()
if !ok {
ok = pushApp()
}
if !ok {
time.Sleep(time.Second)
}
}
Expect(ok).To(BeTrue(), "Failed to push app")
})
}
func appURL(paths ...string) string {
return fmt.Sprintf("http://%s.%s%s", appName(), helpers.DefaultSharedDomain(), strings.Join(paths, ""))
}
func appName() string {
id := ginkgo.GinkgoRandomSeed()
if len(os.Getenv("REUSE_SERVICE_BROKER_APP")) > 0 {
id = 0
}
return fmt.Sprintf("%s-%010d", appNamePrefix, id)
}