forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
68 lines (56 loc) · 1.81 KB
/
agent.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
package utils
import (
"os"
"os/exec"
"time"
"bytes"
"regexp"
"fmt"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
func BuildAgent() error {
command := exec.Command("./build_agent.bash")
session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
if err != nil {
return err
}
gomega.Eventually(session, 40*time.Minute).Should(gexec.Exit(0))
return nil
}
func StartVagrant(vmName, provider string, osVersion string) error {
if len(provider) == 0 {
provider = "virtualbox"
}
command := exec.Command("vagrant", "up", vmName, fmt.Sprintf("--provider=%s", provider), "--provision")
command.Env = append(os.Environ(), "WINDOWS_OS_VERSION="+osVersion)
session, err := gexec.Start(command, ginkgo.GinkgoWriter, ginkgo.GinkgoWriter)
if err != nil {
return err
}
gomega.Eventually(session, 40*time.Minute).Should(gexec.Exit(0))
return nil
}
func RetrievePrivateIP(vmName string) (string, error) {
command := exec.Command("vagrant", "ssh", vmName, "-c", `hostname -I`)
stdout := new(bytes.Buffer)
session, err := gexec.Start(command, stdout, ginkgo.GinkgoWriter)
if err != nil {
return "", err
}
gomega.Eventually(session, 20*time.Second).Should(gexec.Exit(0))
privateIPMatcher, err := regexp.Compile(`^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
return privateIPMatcher.FindString(stdout.String()), nil
}
func RetrievePublicIP(vmName string) (string, error) {
command := exec.Command("vagrant", "ssh-config", vmName)
stdout := new(bytes.Buffer)
session, err := gexec.Start(command, stdout, ginkgo.GinkgoWriter)
if err != nil {
return "", err
}
gomega.Eventually(session, 20*time.Second).Should(gexec.Exit(0))
hostnameMatcher, err := regexp.Compile(`HostName\s([a-zA-Z0-9\.-]*)\n`)
return hostnameMatcher.FindStringSubmatch(stdout.String())[1], nil
}