예제 #1
0
func setupWebsocketProxy() {
	config.Parse()
	config.Config.NumStats = 1
	config.Config.CAdvisorUrl = "http://localhost:8080"
	config.Config.ParsedPublicKey = wsp_utils.ParseTestPublicKey()
	privateKey = wsp_utils.ParseTestPrivateKey()

	conf := test_utils.GetTestConfig(":1111")
	p := &proxy.ProxyStarter{
		BackendPaths:  []string{"/v1/connectbackend"},
		FrontendPaths: []string{"/v1/{logs:logs}/", "/v1/{stats:stats}", "/v1/{stats:stats}/{statsid}", "/v1/exec/"},
		StatsPaths: []string{"/v1/{hoststats:hoststats(\\/project)?(\\/)?}",
			"/v1/{containerstats:containerstats(\\/service)?(\\/)?}",
			"/v1/{containerstats:containerstats}/{containerid}"},
		Config: conf,
	}

	log.Infof("Starting websocket proxy. Listening on [%s], Proxying to cattle API at [%s].",
		conf.ListenAddr, conf.CattleAddr)

	go p.StartProxy()
	time.Sleep(time.Second)
	signedToken := wsp_utils.CreateBackendToken("1", privateKey)

	handlers := make(map[string]backend.Handler)
	handlers["/v1/stats/"] = &StatsHandler{}
	handlers["/v1/hoststats/"] = &HostStatsHandler{}
	handlers["/v1/containerstats/"] = &ContainerStatsHandler{}
	go backend.ConnectToProxy("ws://localhost:1111/v1/connectbackend?token="+signedToken, handlers)
	time.Sleep(300 * time.Millisecond)
}
예제 #2
0
func TestMain(m *testing.M) {
	c := getTestConfig()
	privateKey = test_utils.ParseTestPrivateKey()

	ps := &ProxyStarter{
		BackendPaths:       []string{"/v1/connectbackend"},
		FrontendPaths:      []string{"/v1/echo", "/v1/oneanddone", "/v1/repeat", "/v1/sendafterclose"},
		CattleWSProxyPaths: []string{"/v1/subscribe"},
		CattleProxyPaths:   []string{"/{cattle-proxy:.*}"},
		Config:             c,
	}
	go ps.StartProxy()

	handlers := make(map[string]backend.Handler)
	handlers["/v1/echo"] = &echoHandler{}
	handlers["/v1/oneanddone"] = &oneAndDoneHandler{}
	handlers["/v1/repeat"] = &repeatingHandler{}
	handlers["/v1/sendafterclose"] = &sendAfterCloseHandler{}
	signedToken := test_utils.CreateBackendToken("1", privateKey)
	url := "ws://localhost:1111/v1/connectbackend?token=" + signedToken
	go backend.ConnectToProxy(url, handlers)

	router := mux.NewRouter()
	router.HandleFunc("/v1/subscribe", getWsHandler())
	router.HandleFunc("/{foo:.*}", func(rw http.ResponseWriter, req *http.Request) {
		rw.Write([]byte("SUCCESS"))
	})
	go http.ListenAndServe("127.0.0.1:3333", router)

	time.Sleep(50 * time.Millisecond) // Give front and back a chance to initialize

	os.Exit(m.Run())
}
예제 #3
0
func (s *ProxyTestSuite) setupWebsocketProxy() {
	// TODO Deduplicate. This method and the two below are close copies of the ones in logs_test.go.
	config.Parse()
	config.Config.HostUuid = "1"
	config.Config.ParsedPublicKey = wsp_utils.ParseTestPublicKey()
	s.privateKey = wsp_utils.ParseTestPrivateKey()

	conf := test_utils.GetTestConfig(":4444")
	p := &proxy.ProxyStarter{
		BackendPaths:  []string{"/v1/connectbackend"},
		FrontendPaths: []string{"/v1/{dockersocket:dockersocket}/"},
		Config:        conf,
	}

	log.Infof("Starting websocket proxy. Listening on [%s].", conf.ListenAddr)

	go p.StartProxy()
	time.Sleep(time.Second)
	signedToken := wsp_utils.CreateBackendToken("1", s.privateKey)

	handlers := make(map[string]backend.Handler)
	handlers["/v1/dockersocket/"] = &Handler{}
	go backend.ConnectToProxy("ws://localhost:4444/v1/connectbackend?token="+signedToken, handlers)
	s.pullImage("ibuildthecloud/helloworld", "latest")
}
예제 #4
0
func (s *LogsTestSuite) setupWebsocketProxy() {
	config.Parse()
	config.Config.HostUuid = "1"
	config.Config.ParsedPublicKey = wsp_utils.ParseTestPublicKey()
	privateKey = wsp_utils.ParseTestPrivateKey()

	conf := test_utils.GetTestConfig(":3333")
	p := &proxy.ProxyStarter{
		BackendPaths:  []string{"/v1/connectbackend"},
		FrontendPaths: []string{"/v1/{logs:logs}/"},
		Config:        conf,
	}

	log.Infof("Starting websocket proxy. Listening on [%s], Proxying to cattle API at [%s].",
		conf.ListenAddr, conf.CattleAddr)

	go p.StartProxy()
	time.Sleep(time.Second)
	signedToken := wsp_utils.CreateBackendToken("1", privateKey)

	handlers := make(map[string]backend.Handler)
	handlers["/v1/logs/"] = &LogsHandler{}
	go backend.ConnectToProxy("ws://localhost:3333/v1/connectbackend?token="+signedToken, handlers)
	s.pullImage("hello-world", "latest")
}
예제 #5
0
파일: main.go 프로젝트: yasker/host-api
func main() {
	err := config.Parse()
	if err != nil {
		logrus.Fatal(err)
	}

	flag.Parse()
	defer glog.Flush()

	if config.Config.PidFile != "" {
		logrus.Infof("Writing pid %d to %s", os.Getpid(), config.Config.PidFile)
		if err := ioutil.WriteFile(config.Config.PidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
			logrus.Fatalf("Failed to write pid file %s: %v", config.Config.PidFile, err)
		}
	}

	if config.Config.LogFile != "" {
		if output, err := os.OpenFile(config.Config.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {
			logrus.Fatalf("Failed to log to file %s: %v", config.Config.LogFile, err)
		} else {
			logrus.SetOutput(output)
		}
	}

	if config.Config.HaProxyMonitor {
		err := healthcheck.Poll()
		if err != nil {
			logrus.Fatal(err)
		}
		os.Exit(0)
	}

	processor := events.NewDockerEventsProcessor(config.Config.EventsPoolSize)
	err = processor.Process()
	if err != nil {
		logrus.Fatal(err)
	}

	rancherClient, err := util.GetRancherClient()
	if err != nil {
		logrus.Fatal(err)
	}

	tokenRequest := &rclient.HostApiProxyToken{
		ReportedUuid: config.Config.HostUuid,
	}
	tokenResponse, err := getConnectionToken(0, tokenRequest, rancherClient)
	if err != nil {
		logrus.Fatal(err)
	} else if tokenResponse == nil {
		// nil error and blank token means the proxy is turned off. Just block forever so main function doesn't exit
		var block chan bool
		<-block
	}

	handlers := make(map[string]backend.Handler)
	handlers["/v1/logs/"] = &logs.LogsHandler{}
	handlers["/v1/stats/"] = &stats.StatsHandler{}
	handlers["/v1/hoststats/"] = &stats.HostStatsHandler{}
	handlers["/v1/containerstats/"] = &stats.ContainerStatsHandler{}
	handlers["/v1/exec/"] = &exec.ExecHandler{}
	handlers["/v1/dockersocket/"] = &dockersocketproxy.Handler{}
	backend.ConnectToProxy(tokenResponse.Url+"?token="+tokenResponse.Token, handlers)
}