Example #1
0
// setCurrentInstanceID changes a pointer to currently installed instance ID. It
// takes a path to a package directory (.cipd/pkgs/<name>) as input.
func (d *deployerImpl) setCurrentInstanceID(packageDir string, instanceID string) error {
	if err := common.ValidateInstanceID(instanceID); err != nil {
		return err
	}
	if runtime.GOOS == "windows" {
		return d.fs.EnsureFile(filepath.Join(packageDir, currentTxt), []byte(instanceID), 0666)
	}
	return d.fs.EnsureSymlink(filepath.Join(packageDir, currentSymlink), instanceID)
}
Example #2
0
func (client *clientImpl) ResolveVersion(packageName, version string) (common.Pin, error) {
	if err := common.ValidatePackageName(packageName); err != nil {
		return common.Pin{}, err
	}
	// Is it instance ID already? Don't bother calling the backend.
	if common.ValidateInstanceID(version) == nil {
		return common.Pin{PackageName: packageName, InstanceID: version}, nil
	}
	if err := common.ValidateInstanceVersion(version); err != nil {
		return common.Pin{}, err
	}
	client.Logger.Debugf("cipd: resolving version %q of %q...", version, packageName)
	return client.remote.resolveVersion(packageName, version)
}
Example #3
0
func (r *remoteImpl) resolveVersion(packageName, version string) (pin common.Pin, err error) {
	if err = common.ValidatePackageName(packageName); err != nil {
		return
	}
	if err = common.ValidateInstanceVersion(version); err != nil {
		return
	}
	var reply struct {
		Status       string `json:"status"`
		ErrorMessage string `json:"error_message"`
		InstanceID   string `json:"instance_id"`
	}
	params := url.Values{}
	params.Add("package_name", packageName)
	params.Add("version", version)
	err = r.makeRequest("repo/v1/instance/resolve?"+params.Encode(), "GET", nil, &reply)
	if err != nil {
		return
	}
	switch reply.Status {
	case "SUCCESS":
		if common.ValidateInstanceID(reply.InstanceID) != nil {
			err = fmt.Errorf("Backend returned invalid instance ID: %s", reply.InstanceID)
		} else {
			pin = common.Pin{PackageName: packageName, InstanceID: reply.InstanceID}
		}
	case "PACKAGE_NOT_FOUND":
		err = fmt.Errorf("Package '%s' is not registered", packageName)
	case "INSTANCE_NOT_FOUND":
		err = fmt.Errorf("Package '%s' doesn't have instance with version '%s'", packageName, version)
	case "AMBIGUOUS_VERSION":
		err = fmt.Errorf("More than one instance of package '%s' match version '%s'", packageName, version)
	case "ERROR":
		err = errors.New(reply.ErrorMessage)
	default:
		err = fmt.Errorf("Unexpected backend response: %s", reply.Status)
	}
	return
}
Example #4
0
// getCurrentInstanceID returns instance ID of currently installed instance
// given a path to a package directory (.cipd/pkgs/<name>). It returns ("", nil)
// if no package is installed there.
func (d *deployerImpl) getCurrentInstanceID(packageDir string) (string, error) {
	var current string
	var err error
	if runtime.GOOS == "windows" {
		var bytes []byte
		bytes, err = ioutil.ReadFile(filepath.Join(packageDir, currentTxt))
		if err == nil {
			current = strings.TrimSpace(string(bytes))
		}
	} else {
		current, err = os.Readlink(filepath.Join(packageDir, currentSymlink))
	}
	if err != nil {
		if os.IsNotExist(err) {
			return "", nil
		}
		return "", err
	}
	if err = common.ValidateInstanceID(current); err != nil {
		return "", fmt.Errorf(
			"pointer to currently installed instance doesn't look like a valid instance id: %s", err)
	}
	return current, nil
}