示例#1
0
func main() {
	config := config.New()
	apiConfig, err := config.Registry.Config()
	if err != nil {
		log.Fatal(err.Error())
	}

	kv, err := consul.NewKV(apiConfig)
	if err != nil {
		log.Fatal(err.Error())
	}

	consul := consul.NewConsul(kv, config.Registry.Prefix)

	// set up initial sync
	remote, err := config.Marathon.NewMarathon()
	if err != nil {
		log.Fatal(err.Error())
	}
	sync := marathon.NewMarathonSync(remote, consul)
	go sync.Sync()

	// set up routes
	http.HandleFunc("/health", HealthHandler)
	forwarderHandler := &ForwardHandler{consul}
	http.HandleFunc("/events", forwarderHandler.Handle)

	log.WithField("port", config.Web.Listen).Info("listening")
	log.Fatal(http.ListenAndServe(config.Web.Listen, nil))
}
示例#2
0
func TestForwardHandlerHandleTerminationEvent(t *testing.T) {
	t.Parallel()

	// create a handler
	kv := mocks.NewKVer()
	consul := consul.NewConsul(kv, "")
	handler := ForwardHandler{consul}

	err := consul.UpdateApp(testApp)
	assert.Nil(t, err)

	body, err := json.Marshal(events.AppTerminatedEvent{
		Type:  "app_terminated_event",
		AppID: testApp.ID,
	})
	assert.Nil(t, err)

	// test!
	recorder := httptest.NewRecorder()
	handler.HandleTerminationEvent(recorder, body)

	assert.Equal(t, 200, recorder.Code)
	assert.Equal(t, "OK\n", recorder.Body.String())

	result, _, err := kv.Get(testApp.Key())
	assert.Nil(t, err)
	assert.Nil(t, result)
}
示例#3
0
func TestForwardHandlerHandleTerminationEvent(t *testing.T) {
	t.Parallel()

	// create a handler
	kv := mocks.NewKVer()
	consul := consul.NewConsul(kv, "")
	handler := ForwardHandler{consul}

	err := consul.UpdateApp(testApp)
	assert.Nil(t, err)

	body, err := json.Marshal(events.AppTerminatedEvent{
		Type:  "app_terminated_event",
		AppID: testApp.ID,
	})
	assert.Nil(t, err)

	// test!
	err = handler.HandleTerminationEvent(body)
	assert.Nil(t, err)

	result, _, err := kv.Get(testApp.Key())
	assert.Nil(t, err)
	assert.Nil(t, result)
}
示例#4
0
func TestForwardHandlerHandleStatusEvent(t *testing.T) {
	t.Parallel()

	// create a handler
	kv := mocks.NewKVer()
	consul := consul.NewConsul(kv, "")
	handler := ForwardHandler{consul}

	// deletes
	for _, status := range []string{"TASK_FINISHED", "TASK_FAILED", "TASK_KILLED", "TASK_LOST"} {
		tempBody := tempTaskBody(status)

		err := consul.UpdateTask(testTask)
		assert.Nil(t, err)

		// test
		recorder := httptest.NewRecorder()
		handler.HandleStatusEvent(recorder, tempBody)
		assert.Equal(t, 200, recorder.Code)
		assert.Equal(t, "OK\n", recorder.Body.String())

		// assert
		result, _, err := kv.Get(testTask.Key())
		assert.Nil(t, err)
		assert.Nil(t, result)
	}

	// puts
	for _, status := range []string{"TASK_STAGING", "TASK_STARTING", "TASK_RUNNING"} {
		tempBody := tempTaskBody(status)
		tempTask, _ := tasks.ParseTask(tempBody)

		// test
		recorder := httptest.NewRecorder()
		handler.HandleStatusEvent(recorder, tempBody)
		assert.Equal(t, 200, recorder.Code)
		assert.Equal(t, "OK\n", recorder.Body.String())

		// assert
		result, _, err := kv.Get(tempTask.Key())
		assert.Nil(t, err)
		assert.Equal(t, result, tempTask.KV())

		// cleanup
		_, err = kv.Delete(testTask.Key())
		assert.Nil(t, err)
	}

	// bad status
	tempBody := tempTaskBody("TASK_BATMAN")
	recorder := httptest.NewRecorder()
	handler.HandleStatusEvent(recorder, tempBody)
	assert.Equal(t, 500, recorder.Code)
	assert.Equal(t, "unknown task status\n", recorder.Body.String())
}
示例#5
0
func TestForwardHandlerHandleAppEvent(t *testing.T) {
	t.Parallel()

	// create a handler
	kv := mocks.NewKVer()
	consul := consul.NewConsul(kv, "")
	handler := ForwardHandler{consul}

	body, err := json.Marshal(events.APIPostEvent{"api_post_event", testApp})
	assert.Nil(t, err)

	// test!
	err = handler.HandleAppEvent(body)
	assert.Nil(t, err)

	result, _, err := kv.Get(testApp.Key())
	assert.Nil(t, err)
	assert.Equal(t, result, testAppKV)
}
示例#6
0
func TestForwardHandlerHandleAppEvent(t *testing.T) {
	t.Parallel()

	// create a handler
	kv := mocks.NewKVer()
	consul := consul.NewConsul(kv, "")
	handler := ForwardHandler{consul}

	body, err := json.Marshal(events.APIPostEvent{"api_post_event", testApp})
	assert.Nil(t, err)

	// test!
	recorder := httptest.NewRecorder()
	handler.HandleAppEvent(recorder, body)

	assert.Equal(t, 200, recorder.Code)
	assert.Equal(t, "OK\n", recorder.Body.String())

	result, _, err := kv.Get(testApp.Key())
	assert.Nil(t, err)
	assert.Equal(t, result, testAppKV)
}
示例#7
0
func main() {
	config := config.New()
	apiConfig, err := config.Registry.Config()
	if err != nil {
		log.Fatal(err.Error())
	}

	kv, err := consul.NewKV(apiConfig)
	if err != nil {
		log.Fatal(err.Error())
	}

	consul := consul.NewConsul(kv, config.Registry.Prefix)

	// set up initial sync
	remote, err := config.Marathon.NewMarathon()
	if err != nil {
		log.Fatal(err.Error())
	}
	sync := marathon.NewMarathonSync(remote, consul)
	go sync.Sync()

	fh := &ForwardHandler{consul}

	v, err := remote.Version()
	if err != nil {
		log.WithError(err).Warn("version parsing failed, assuming >= 0.9.0")
		v, _ = version.NewVersion("0.9.0")
	}
	minVersion, _ := version.NewConstraint(">= 0.9.0")

	if minVersion.Check(v) {
		log.WithField("version", v).Info("detected Marathon events endpoint")
		SubscribeToEventStream(config, remote, fh)
	} else {
		log.WithField("version", v).Info("detected old Marathon version -- make sure to set up an eventSubscription for this process")
		ServeWebhookReceiver(config, fh)
	}
}