// AppengineContextProvider provides an injectable and namespaced
// instance of appengine.Context
func AppengineContextProvider(c martini.Context, req *http.Request) {
	gae := appengine.NewContext(req)
	namespace := appengine.ModuleName(gae)
	context, err := appengine.Namespace(gae, namespace)
	if err != nil {
		panic(err)
	}
	c.Map(context)
}
Beispiel #2
0
func handlerStop(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	key := datastore.NewKey(c, "cell", appengine.ModuleName(c), 0, nil)
	_, err := datastore.Put(c, key, &Status{Value: 0})
	if err != nil {
		c.Warningf("Error saving:%v", err.Error())
	}
	c.Infof("---Stopping")
}
Beispiel #3
0
func ResolveGaeContext() gin.HandlerFunc {
	return func(c *gin.Context) {
		gaeRootCtx := appengine.NewContext(c.Request)
		c.Set(gaeRootCtxKey, gaeRootCtx)

		namespace := ""

		if productionDomain := os.Getenv("PRODUCTION_DOMAIN"); productionDomain != "" {
			if strings.HasPrefix(productionDomain, ".") == false {
				productionDomain = fmt.Sprintf(".%s", productionDomain)
			}

			lastIndex := strings.LastIndex(c.Request.Host, productionDomain)

			if lastIndex > -1 {
				namespace = strings.Replace(c.Request.Host, productionDomain, "", lastIndex)
			}
		} else if devNamespace := os.Getenv("DEV_NAMESPACE"); devNamespace != "" {
			namespace = devNamespace
		}

		// Still no namespace. Maybe the request is to the appspot domain
		if namespace == "" {
			requestHost := convertDots(c.Request.Host)
			requestHost = strings.Replace(requestHost, "master.", "", 1)
			hostName, _ := appengine.ModuleHostname(gaeRootCtx, appengine.ModuleName(gaeRootCtx), "master", "")
			hostName = convertDots(hostName)
			hostName = strings.Replace(hostName, "master.", "", 1)
			hostName = fmt.Sprintf(".%s", hostName)

			lastIndex := strings.LastIndex(requestHost, hostName)

			if lastIndex > -1 {
				namespace = strings.Replace(requestHost, hostName, "", lastIndex)
			}
		}

		// Still no namespace? Last resort is a custom header
		if namespace == "" {
			namespace = c.Request.Header.Get(NamespaceHeader)
		}

		gaeRootCtx.Debugf("Using namespace: \"%s\"", namespace)
		nameSpacedGaeCtx, err := appengine.Namespace(gaeRootCtx, namespace)
		if err != nil {
			GetGaeRootContext(c).Errorf("Error creating namespace: %v", err)
			c.AbortWithError(500, err)
			return
		}

		c.Set(gaeCtxKey, nameSpacedGaeCtx)
		c.Set(namespaceKey, namespace)
	}
}
Beispiel #4
0
func handler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	name := appengine.ModuleName(c)

	fmt.Fprintf(w, "Hello from cell %s", appengine.ModuleName(c))
	col := int(name[4] - '0')
	row := int(name[6] - '0')
	if row < 3 {
		cellCallFunc.Call(c, col, row+1)
	}
	key := datastore.NewKey(c, "cell", appengine.ModuleName(c), 0, nil)
	_, err := datastore.Put(c, key, &Status{Value: 2})
	if err != nil {
		c.Warningf("Error saving:%v", err.Error())
	}
	time.Sleep(time.Second)
	_, err = datastore.Put(c, key, &Status{Value: 1})
	if err != nil {
		c.Warningf("Error saving:%v", err.Error())
	}
}
func AppengineContextProvider(c martini.Context, request *http.Request) {
	gae := appengine.NewContext(request)
	namespace := appengine.ModuleName(gae)

	context, err := appengine.Namespace(gae, namespace)

	if err != nil {
		panic(fmt.Sprintf("Could not create GAE context: %v", err))
	}

	c.MapTo(context, (*appengine.Context)(nil))
}
Beispiel #6
0
func handleID(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")

	fmt.Fprintf(w, "appengine.AppID(c) = %q\n", appengine.AppID(c))
	fmt.Fprintf(w, "appengine.VersionID(c) = %q\n", appengine.VersionID(c))

	name := appengine.ModuleName(c)
	hostname, err := appengine.ModuleHostname(c, "", "", "")
	fmt.Fprintf(w, "appengine.ModuleName(c) = %q\n", name)
	fmt.Fprintf(w, `appengine.ModuleHostname(c, "", "", "") = %q (err: %v)`+"\n", hostname, err)

	fmt.Fprintf(w, "----------\n")
	for _, s := range os.Environ() {
		fmt.Fprintln(w, s)
	}
}
func (as AdminService) importScoreBoardV1(c *gin.Context) {
	var importDefinition domain.ScoreBoardV1Import

	c.Bind(&importDefinition)

	if importDefinition.DbDumpUrl == "" {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	createTask := &taskqueue.Task{
		Path:    "/tasks/import/scoreboardv1",
		Payload: []byte(importDefinition.DbDumpUrl),
	}
	hostName, _ := appengine.ModuleHostname(utils.GetGaeRootContext(c), appengine.ModuleName(utils.GetGaeRootContext(c)), "", "")
	createTask.Header = http.Header{}
	createTask.Header.Set("Host", hostName)
	createTask.Header.Set(utils.NamespaceHeader, utils.GetNamespace(c))

	_, err := taskqueue.Add(utils.GetGaeRootContext(c), createTask, "contextqueue")

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("Error calling taskqueue.Add in importScoreBoardV1: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	importDao := dao.CreateImportDao(c)
	importStatus, err := importDao.SetStatus(true, 0, 0, 0, 0, 0, 0)

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("Error calling importDao.setStatus in importScoreBoardV1: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	importStatus.AddLink(relImportStatus, "/api/admin/import/scoreboardv1/status")

	c.JSON(200, importStatus)
}
func ModuleName(ctx netcontext.Context) string { return appengine.ModuleName(fromContext(ctx)) }
Beispiel #9
0
func Get(w http.ResponseWriter, r *http.Request, m map[string]interface{}) *Instance {

	c := appengine.NewContext(r)

	startFunc := time.Now()

	if !ii.LastUpdated.IsZero() {

		age := startFunc.Sub(ii.LastUpdated)

		if age < 200*time.Millisecond {
			c.Infof("instance info update too recently: %v, skipping.\n", age)
			return ii
		}

		if age < 1*time.Hour {
			if len(ii.Hostname) > 2 {
				return ii
			}

		}

		c.Infof("instance info update too old: %v, recomputing.\n", age)
	}

	ii.ModuleName = appengine.ModuleName(c)
	ii.InstanceID = appengine.InstanceID()
	ii.VersionFull = appengine.VersionID(c)

	majorMinor := strings.Split(ii.VersionFull, ".")
	if len(majorMinor) != 2 {
		panic("we need a version string of format X.Y")
	}

	ii.VersionMajor = majorMinor[0]
	ii.VersionMinor = majorMinor[1]

	var err = errors.New("dummy creation error message")

	ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionFull)
	if err != nil {
		// this never works with version full
		// we do not log this - but try version major
		err = nil

		if !util_appengine.IsLocalEnviron() {
			ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionMajor)
			util_err.Err_http(w, r, err, true, "get num instances works only live and without autoscale")
		}

	}

	// in auto scaling, google reports "zero" - which can not be true
	// we assume at least 1
	if ii.NumInstances == 0 {
		ii.NumInstances = 1
	}

	// http://[0-2].1.default.libertarian-islands.appspot.com/instance-info

	ii.Hostname, err = appengine.ModuleHostname(c, ii.ModuleName,
		ii.VersionMajor, "")
	util_err.Err_http(w, r, err, false)

	if !util_appengine.IsLocalEnviron() {
		ii.HostnameInst0, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "0")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 0: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameInst1, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "1")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 1: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameMod02, err = appengine.ModuleHostname(c, "mod02", "", "")
		util_err.Err_http(w, r, err, true)

	}

	ii.LastUpdated = time.Now()

	c.Infof("collectInfo() completed, %v.%v.%v.%v, took %v",
		util.Ellipsoider(ii.InstanceID, 4), ii.VersionMajor, ii.ModuleName,
		ii.Hostname,
		ii.LastUpdated.Sub(startFunc))

	return ii
}