Ejemplo n.º 1
0
// Get information about a service
func getService(rw http.ResponseWriter, req *http.Request) {
	// /services/{svcId}
	svcId := req.URL.Query().Get(":svcId")

	service, err := common.GetService(svcId)
	if err != nil {
		writeError(rw, req, err, http.StatusNotFound)
		return
	}
	writeBody(rw, req, service, http.StatusOK)
}
Ejemplo n.º 2
0
// Get information about a backend server
func getServers(rw http.ResponseWriter, req *http.Request) {
	// /services/{svcId}/servers
	svcId := req.URL.Query().Get(":svcId")
	service, err := common.GetService(svcId)
	if err != nil {
		writeError(rw, req, err, http.StatusNotFound)
		return
	}
	if service.Servers == nil {
		service.Servers = make([]core.Server, 0, 0)
	}
	// writeBody(rw, req, service, http.StatusOK)
	writeBody(rw, req, service.Servers, http.StatusOK)
}
Ejemplo n.º 3
0
// DeleteService tells all members to remove the service from their database.
// rolls back on failure
func (r *Redis) DeleteService(id string) error {
	conn := pool.Get()
	defer conn.Close()

	oldService, err := common.GetService(id)
	// this should not return nil to ensure the service is gone from entire cluster
	if err != nil && !strings.Contains(err.Error(), "No Service Found") {
		return err
	}

	// publishString to others
	err = r.publishString(conn, "delete-service", id)
	if err != nil {
		// if i failed to publishString, request should fail
		return err
	}

	actionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("delete-service %s", id))))

	// ensure all members applied action
	err = r.waitForMembers(conn, actionHash)
	if err != nil {
		uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-service %s", oldService))))
		// cleanup rollback cruft. clear actionHash ensures no mistakes on re-submit
		defer conn.Do("DEL", uActionHash, actionHash)
		// attempt rollback - no need to waitForMembers here
		uerr := r.publishJson(conn, "set-service", oldService)
		if uerr != nil {
			err = fmt.Errorf("%v - %v", err, uerr)
		}
		return err
	}

	if database.CentralStore {
		return database.DeleteService(id)
	}

	return nil
}
Ejemplo n.º 4
0
// SetServers tells all members to replace a service's servers with a new set.
// rolls back on failure
func (r *Redis) SetServers(svcId string, servers []core.Server) error {
	conn := pool.Get()
	defer conn.Close()

	service, err := common.GetService(svcId)
	if err != nil {
		return NoServiceError
	}
	oldServers := service.Servers

	// publishStringJson to others
	err = r.publishStringJson(conn, "set-servers", svcId, servers)
	if err != nil {
		return err
	}

	actionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-servers %s %s", servers, svcId))))

	// ensure all members applied action
	err = r.waitForMembers(conn, actionHash)
	if err != nil {
		uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-servers %s %s", oldServers, svcId))))
		// cleanup rollback cruft. clear actionHash ensures no mistakes on re-submit
		defer conn.Do("DEL", uActionHash, actionHash)
		// attempt rollback - no need to waitForMembers here
		uerr := r.publishStringJson(conn, "set-servers", svcId, oldServers)
		if uerr != nil {
			err = fmt.Errorf("%v - %v", err, uerr)
		}
		return err
	}

	if database.CentralStore {
		return database.SetServers(svcId, servers)
	}

	return nil
}
Ejemplo n.º 5
0
// Create a service
func postService(rw http.ResponseWriter, req *http.Request) {
	// /services
	service, err := parseReqService(req)
	if err != nil {
		writeError(rw, req, err, http.StatusBadRequest)
		return
	}

	// idempotent additions (don't update service on post)
	if svc, _ := common.GetService(service.Id); svc != nil {
		writeBody(rw, req, service, http.StatusOK)
		return
	}

	// save to cluster
	err = cluster.SetService(service)
	if err != nil {
		writeError(rw, req, err, http.StatusInternalServerError)
		return
	}

	writeBody(rw, req, service, http.StatusOK)
}
Ejemplo n.º 6
0
func (n None) GetService(id string) (*core.Service, error) {
	return common.GetService(id)
}
Ejemplo n.º 7
0
// GetService - likely will never be called
func (r *Redis) GetService(id string) (*core.Service, error) {
	return common.GetService(id)
}