Пример #1
0
// NodeAddresses is an implementation of Instances.NodeAddresses.
func (gce *GCECloud) NodeAddresses(_ string) ([]api.NodeAddress, error) {
	internalIP, err := metadata.Get("instance/network-interfaces/0/ip")
	if err != nil {
		return nil, fmt.Errorf("couldn't get internal IP: %v", err)
	}
	externalIP, err := metadata.Get("instance/network-interfaces/0/access-configs/0/external-ip")
	if err != nil {
		return nil, fmt.Errorf("couldn't get external IP: %v", err)
	}
	return []api.NodeAddress{
		{Type: api.NodeInternalIP, Address: internalIP},
		{Type: api.NodeExternalIP, Address: externalIP},
	}, nil
}
Пример #2
0
func getCurrentExternalID() (string, error) {
	externalID, err := metadata.Get("instance/id")
	if err != nil {
		return "", fmt.Errorf("couldn't get external ID: %v", err)
	}
	return externalID, nil
}
Пример #3
0
func getGceInstanceID() info.InstanceID {
	instanceID, err := metadata.Get("instance/id")
	if err != nil {
		return info.UnknownInstance
	}
	return info.InstanceID(info.InstanceType(instanceID))
}
Пример #4
0
func (cs computeSource) Token() (*oauth2.Token, error) {
	if !metadata.OnGCE() {
		return nil, errors.New("oauth2/google: can't get a token from the metadata service; not running on GCE")
	}
	acct := cs.account
	if acct == "" {
		acct = "default"
	}
	tokenJSON, err := metadata.Get("instance/service-accounts/" + acct + "/token")
	if err != nil {
		return nil, err
	}
	var res struct {
		AccessToken  string `json:"access_token"`
		ExpiresInSec int    `json:"expires_in"`
		TokenType    string `json:"token_type"`
	}
	err = json.NewDecoder(strings.NewReader(tokenJSON)).Decode(&res)
	if err != nil {
		return nil, fmt.Errorf("oauth2/google: invalid token JSON from metadata: %v", err)
	}
	if res.ExpiresInSec == 0 || res.AccessToken == "" {
		return nil, fmt.Errorf("oauth2/google: incomplete token received from metadata")
	}
	return &oauth2.Token{
		AccessToken: res.AccessToken,
		TokenType:   res.TokenType,
		Expiry:      time.Now().Add(time.Duration(res.ExpiresInSec) * time.Second),
	}, nil
}
Пример #5
0
func getGceInstanceType() info.InstanceType {
	machineType, err := metadata.Get("instance/machine-type")
	if err != nil {
		return info.UnknownInstance
	}

	responseParts := strings.Split(machineType, "/") // Extract the instance name from the machine type.
	return info.InstanceType(responseParts[len(responseParts)-1])
}
Пример #6
0
func getInstanceID() (string, error) {
	result, err := metadata.Get("instance/hostname")
	if err != nil {
		return "", err
	}
	parts := strings.Split(result, ".")
	if len(parts) == 0 {
		return "", fmt.Errorf("unexpected response: %s", result)
	}
	return parts[0], nil
}
Пример #7
0
func getNetworkName() (string, error) {
	result, err := metadata.Get("instance/network-interfaces/0/network")
	if err != nil {
		return "", err
	}
	parts := strings.Split(result, "/")
	if len(parts) != 4 {
		return "", fmt.Errorf("unexpected response: %s", result)
	}
	return parts[3], nil
}
Пример #8
0
func getProjectAndZone() (string, string, error) {
	result, err := metadata.Get("instance/zone")
	if err != nil {
		return "", "", err
	}
	parts := strings.Split(result, "/")
	if len(parts) != 4 {
		return "", "", fmt.Errorf("unexpected response: %s", result)
	}
	return parts[1], parts[3], nil
}
Пример #9
0
func initGCE() error {
	initGCECalled = true
	// Use the staging project if not on GCE. This assumes the DefaultTokenSource
	// credential used below has access to that project.
	if !metadata.OnGCE() {
		projectID = stagingProjectID
	}
	var err error
	projectID, err = metadata.ProjectID()
	if err != nil {
		return fmt.Errorf("failed to get current GCE ProjectID: %v", err)
	}

	inStaging = projectID == stagingProjectID
	if inStaging {
		log.Printf("Running in staging cluster (%q)", projectID)
	}

	tokenSource, _ = google.DefaultTokenSource(oauth2.NoContext)
	httpClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	serviceCtx = cloud.NewContext(projectID, httpClient)

	projectZone, err = metadata.Get("instance/zone")
	if err != nil || projectZone == "" {
		return fmt.Errorf("failed to get current GCE zone: %v", err)
	}

	// Convert the zone from "projects/1234/zones/us-central1-a" to "us-central1-a".
	projectZone = path.Base(projectZone)
	if !hasComputeScope() {
		return errors.New("The coordinator is not running with access to read and write Compute resources. VM support disabled.")

	}
	projectRegion = projectZone[:strings.LastIndex(projectZone, "-")] // "us-central1"

	externalIP, err = metadata.ExternalIP()
	if err != nil {
		return fmt.Errorf("ExternalIP: %v", err)
	}
	computeService, _ = compute.New(httpClient)
	errTryDeps = checkTryBuildDeps()
	if errTryDeps != nil {
		log.Printf("TryBot builders disabled due to error: %v", errTryDeps)
	} else {
		log.Printf("TryBot builders enabled.")
	}

	go gcePool.pollQuotaLoop()
	return nil
}
Пример #10
0
// Checks that the required auth scope is present.
func verifyAuthScope(expectedScope string) error {
	scopes, err := metadata.Get(metadataAuthScopes)
	if err != nil {
		return err
	}

	for _, scope := range strings.Fields(scopes) {
		if scope == expectedScope {
			return nil
		}
	}

	return fmt.Errorf("Current instance does not have the expected scope (%q). Actual scopes: %v", expectedScope, scopes)
}
Пример #11
0
// Get a token for performing GCE requests.
func getAuthToken() (authToken, error) {
	rawToken, err := metadata.Get(metadataAuthToken)
	if err != nil {
		return authToken{}, err
	}

	var token authToken
	err = json.Unmarshal([]byte(rawToken), &token)
	if err != nil {
		return authToken{}, fmt.Errorf("failed to unmarshal service account token with output %q: %v", rawToken, err)
	}

	return token, err
}
func (cmo *CloudMonitoringOutput) Init(config interface{}) (err error) {
	cmo.conf = config.(*CloudMonitoringConfig)

	if metadata.OnGCE() {
		if cmo.conf.ProjectId == "" {
			if cmo.conf.ProjectId, err = metadata.ProjectID(); err != nil {
				return
			}
		}
		if cmo.conf.ResourceId == "" {
			if cmo.conf.ResourceId, err = metadata.InstanceID(); err != nil {
				return
			}
		}
		if cmo.conf.Zone == "" {
			if cmo.conf.Zone, err = metadata.Get("instance/zone"); err != nil {
				return
			}
		}
	}
	if cmo.conf.ProjectId == "" {
		return errors.New("ProjectId cannot be blank")
	}

	cmo.batchChan = make(chan MonitoringBatch)
	cmo.backChan = make(chan []*cloudmonitoring.TimeseriesPoint, 2)
	cmo.outputExit = make(chan error)
	if cmo.client, err = google.DefaultClient(oauth2.NoContext,
		cloudmonitoring.MonitoringScope); err != nil {
		return
	}
	if cmo.service, err = cloudmonitoring.New(cmo.client); err != nil {
		return
	}
	r := &cloudmonitoring.ListMetricDescriptorsRequest{Kind: "cloudmonitoring#listMetricDescriptorsRequest"}
	_, err = cmo.service.MetricDescriptors.List(cmo.conf.ProjectId, r).Do()
	if err != nil {
		log.Print("Init CloudMonitoringOutput Error: %v", err)
	}
	return
}
func (clo *CloudLoggingOutput) Init(config interface{}) (err error) {
	clo.conf = config.(*CloudLoggingConfig)

	if metadata.OnGCE() {
		if clo.conf.ProjectId == "" {
			if clo.conf.ProjectId, err = metadata.ProjectID(); err != nil {
				return
			}
		}
		if clo.conf.ResourceId == "" {
			if clo.conf.ResourceId, err = metadata.InstanceID(); err != nil {
				return
			}
		}
		if clo.conf.Zone == "" {
			if clo.conf.Zone, err = metadata.Get("instance/zone"); err != nil {
				return
			}
		}
	}
	if clo.conf.ProjectId == "" {
		return errors.New("ProjectId cannot be blank")
	}

	clo.batchChan = make(chan LogBatch)
	clo.backChan = make(chan []*logging.LogEntry, 2)
	clo.outputExit = make(chan error)
	if clo.client, err = google.DefaultClient(oauth2.NoContext,
		logging.CloudPlatformScope); err != nil {
		return
	}
	if clo.service, err = logging.New(clo.client); err != nil {
		return
	}
	_, err = clo.service.Projects.LogServices.List(clo.conf.ProjectId).Do()
	if err != nil {
		log.Print("Init CloudLoggingOutput Error: %v", err)
	}
	return
}