Beispiel #1
0
func (this *Job) printResourcesAndActors() {
	lines := make([]string, 0)
	header := "Topic|Cluster|Ctime|Mtime"
	lines = append(lines, header)
	// jobs
	jobQueues := this.zkzone.ChildrenWithData(zk.PubsubJobQueues)
	sortedName := make([]string, 0, len(jobQueues))
	for name := range jobQueues {
		sortedName = append(sortedName, name)
	}
	sort.Strings(sortedName)

	manager.Default = dummy.New("")
	for _, topic := range sortedName {
		zdata := jobQueues[topic]
		if appid := manager.Default.TopicAppid(topic); appid == "" {
			lines = append(lines, fmt.Sprintf("?%s|%s|%s|%s", topic,
				string(zdata.Data()), zdata.Ctime(), zdata.Mtime()))
		} else {
			lines = append(lines, fmt.Sprintf("%s|%s|%s|%s", topic,
				string(zdata.Data()), zdata.Ctime(), zdata.Mtime()))
		}

	}
	if len(lines) > 1 {
		this.Ui.Info("Jobs")
		this.Ui.Output(columnize.SimpleFormat(lines))
	}

	// webhooks
	lines = lines[:0]
	header = "Topic|Endpoints|Ctime|Mtime"
	lines = append(lines, header)
	webhoooks := this.zkzone.ChildrenWithData(zk.PubsubWebhooks)
	sortedName = sortedName[:0]
	for name := range webhoooks {
		sortedName = append(sortedName, name)
	}
	sort.Strings(sortedName)

	for _, topic := range sortedName {
		zdata := webhoooks[topic]
		var hook zk.WebhookMeta
		hook.From(zdata.Data())
		if appid := manager.Default.TopicAppid(topic); appid == "" {
			lines = append(lines, fmt.Sprintf("?%s|%+v|%s|%s", topic, hook.Endpoints,
				zdata.Ctime(), zdata.Mtime()))
		} else {
			lines = append(lines, fmt.Sprintf("%s|%+v|%s|%s", topic, hook.Endpoints,
				zdata.Ctime(), zdata.Mtime()))
		}
	}
	if len(lines) > 1 {
		this.Ui.Info("Webhooks")
		this.Ui.Output(columnize.SimpleFormat(lines))
	}

	// actors
	lines = lines[:0]
	header = "Actor|Ctime|Mtime"
	lines = append(lines, header)
	actors := this.zkzone.ChildrenWithData(zk.PubsubActors)
	sortedName = sortedName[:0]
	for name := range actors {
		sortedName = append(sortedName, name)
	}
	sort.Strings(sortedName)

	for _, name := range sortedName {
		zdata := actors[name]
		lines = append(lines, fmt.Sprintf("%s|%s|%s", name,
			zdata.Ctime(), zdata.Mtime()))
	}
	if len(lines) > 1 {
		this.Ui.Info("Actors")
		this.Ui.Output(columnize.SimpleFormat(lines))
	}

	// job <-> actor
	lines = lines[:0]
	header = "Topic|Actor|Ctime|Mtime"
	lines = append(lines, header)
	jobQueueOwners := this.zkzone.ChildrenWithData(zk.PubsubJobQueueOwners)
	sortedName = sortedName[:0]
	for name := range jobQueueOwners {
		sortedName = append(sortedName, name)
	}
	sort.Strings(sortedName)

	for _, jobQueue := range sortedName {
		zdata := jobQueueOwners[jobQueue]
		lines = append(lines, fmt.Sprintf("%s|%s|%s|%s", jobQueue, string(zdata.Data()),
			zdata.Ctime(), zdata.Mtime()))
	}
	if len(lines) > 1 {
		this.Ui.Info("Job<->Actor")
		this.Ui.Output(columnize.SimpleFormat(lines))
	}

	// webhook <-> actor
	lines = lines[:0]
	header = "Topic|Actor|Ctime|Mtime"
	lines = append(lines, header)
	webhookOwners := this.zkzone.ChildrenWithData(zk.PubsubWebhookOwners)
	sortedName = sortedName[:0]
	for name := range webhookOwners {
		sortedName = append(sortedName, name)
	}
	sort.Strings(sortedName)

	for _, topic := range sortedName {
		zdata := webhookOwners[topic]
		lines = append(lines, fmt.Sprintf("%s|%s|%s|%s", topic, string(zdata.Data()),
			zdata.Ctime(), zdata.Mtime()))
	}
	if len(lines) > 1 {
		this.Ui.Info("Webhook<->Actor")
		this.Ui.Output(columnize.SimpleFormat(lines))
	}
}
Beispiel #2
0
// @rest PUT /v1/webhook/:appid/:topic/:ver?group=xx
func (this *manServer) createWebhookHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	topic := params.ByName(UrlParamTopic)
	if !manager.Default.ValidateTopicName(topic) {
		log.Warn("illegal topic: %s", topic)

		writeBadRequest(w, "illegal topic")
		return
	}

	query := r.URL.Query()
	group := query.Get("group")
	realIp := getHttpRemoteIp(r)
	hisAppid := params.ByName(UrlParamAppid)
	myAppid := r.Header.Get(HttpHeaderAppid)
	ver := params.ByName(UrlParamVersion)

	if err := manager.Default.AuthSub(myAppid, r.Header.Get(HttpHeaderSubkey),
		hisAppid, topic, group); err != nil {
		log.Error("+webhook[%s/%s] -(%s): {%s.%s.%s UA:%s} %v",
			myAppid, group, realIp, hisAppid, topic, ver, r.Header.Get("User-Agent"), err)

		writeAuthFailure(w, err)
		return
	}

	cluster, found := manager.Default.LookupCluster(hisAppid)
	if !found {
		log.Error("+webhook[%s/%s] -(%s): {%s.%s.%s UA:%s} undefined cluster",
			myAppid, group, realIp, hisAppid, topic, ver, r.Header.Get("User-Agent"))

		writeBadRequest(w, "invalid appid")
		return
	}

	log.Info("+webhook[%s/%s] %s(%s): {%s.%s.%s UA:%s}",
		myAppid, group, r.RemoteAddr, realIp, hisAppid, topic, ver, r.Header.Get("User-Agent"))

	rawTopic := manager.Default.KafkaTopic(hisAppid, topic, ver)
	var hook zk.WebhookMeta
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&hook); err != nil {
		log.Error("+webhook[%s/%s] %s(%s): {%s.%s.%s UA:%s} %v",
			myAppid, group, r.RemoteAddr, realIp, hisAppid, topic, ver, r.Header.Get("User-Agent"), err)

		writeBadRequest(w, err.Error())
		return
	}
	r.Body.Close()

	// validate the url
	for _, ep := range hook.Endpoints {
		_, err := url.ParseRequestURI(ep)
		if err != nil {
			log.Error("+webhook[%s/%s] %s(%s): {%s.%s.%s UA:%s} %+v %v",
				myAppid, group, r.RemoteAddr, realIp, hisAppid, topic, ver, r.Header.Get("User-Agent"), hook.Endpoints, err)

			writeBadRequest(w, err.Error())
			return
		}
	}

	hook.Cluster = cluster // cluster is decided by server
	if err := this.gw.zkzone.CreateOrUpdateWebhook(rawTopic, hook); err != nil {
		log.Error("+webhook[%s/%s] %s(%s): {%s.%s.%s UA:%s} %v",
			myAppid, group, r.RemoteAddr, realIp, hisAppid, topic, ver, r.Header.Get("User-Agent"), err)

		writeServerError(w, err.Error())
		return
	}

	w.Write(ResponseOk)
}