示例#1
0
func memcacheAddr(c appengine.Context) (string, error) {
	// Use the load-balanced hostname for the "memcache" module.
	hostname, err := appengine.ModuleHostname(c, "memcache", "", "")
	if err != nil {
		return "", err
	}
	return "http://" + hostname, nil
}
示例#2
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)
	}
}
示例#3
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)
	}
}
示例#4
0
func CallModule(module string, c appengine.Context) (string, error) {
	hostname, err := appengine.ModuleHostname(c, module, "", "")
	if err != nil {
		return "", err
	}
	url := "http://" + hostname + "/call"
	client := urlfetch.Client(c)
	resp, err := client.Get(url)
	if err != nil {
		return "", err
	}
	//fmt.Fprintf(w, "HTTP GET returned status %v", resp.Status)
	defer resp.Body.Close()
	contents, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	return string(contents), nil

}
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)
}
示例#6
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
}