Skip to content

Instantly share code, notes, and snippets.

@craigfurman
Created June 12, 2017 15:38
Show Gist options
  • Save craigfurman/0eaf34c220d4343d67bc3d235b91ddd1 to your computer and use it in GitHub Desktop.
Save craigfurman/0eaf34c220d4343d67bc3d235b91ddd1 to your computer and use it in GitHub Desktop.
Process CF firehose while pushing app
package main
import (
"crypto/tls"
"fmt"
"os"
"os/exec"
"github.com/cloudfoundry/noaa/consumer"
"github.com/cloudfoundry/sonde-go/events"
)
func main() {
cfSystemDomain := envMustHave("CF_SYSTEM_DOMAIN")
cfUser := envMustHave("CF_USER")
cfPassword := envMustHave("CF_PASSWORD")
cfOrg := envMustHave("CF_ORG")
cfSpace := envMustHave("CF_SPACE")
appPath := envMustHave("APP_PATH")
fmt.Println("logging in to Cloud Foundry")
loginCmd := exec.Command("cf", "login", "--skip-ssl-validation",
"-a", fmt.Sprintf("https://api.%s", cfSystemDomain), "-u", cfUser, "-p",
cfPassword, "-o", cfOrg, "-s", cfSpace)
loginOutput, err := loginCmd.CombinedOutput()
if err != nil {
fmt.Println("login failed!")
fmt.Println(string(loginOutput))
os.Exit(1)
}
authToken, err := exec.Command("cf", "oauth-token").CombinedOutput()
if err != nil {
fmt.Println("get auth token failed!")
fmt.Println(string(authToken))
os.Exit(1)
}
firehose := consumer.New(fmt.Sprintf("wss://doppler.%s:443", cfSystemDomain), &tls.Config{InsecureSkipVerify: true}, nil)
defer firehose.Close()
messages, errs := firehose.Firehose("benchy-bench", string(authToken))
go processMessages(messages, errs)
fmt.Printf("pushing app %s\n", appPath)
appName := "benchy-bench"
cmd := exec.Command("cf", "push", "-p", appPath, appName)
pushOutput, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("push failed!")
fmt.Println(string(pushOutput))
os.Exit(1)
}
}
func processMessages(messages <-chan *events.Envelope, errs <-chan error) {
for {
select {
case msg := <-messages:
go processMessage(msg)
case err := <-errs:
fmt.Println(err)
os.Exit(1)
}
}
}
func processMessage(msg *events.Envelope) {
// TODO!
}
func envMustHave(key string) string {
value := os.Getenv(key)
if value == "" {
fmt.Printf("must set %s\n", key)
os.Exit(1)
}
return value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment