Example #1
0
func (c *Client) CacheOptions(app string) *config.CacheOptions {
	c.init()

	oldFile := filepath.Join(c.home(), app+".bolt")
	file := c.boltFile(app)

	if _, err := os.Stat(file); oldFile != file && os.IsNotExist(err) {
		if _, err := os.Stat(oldFile); err == nil {
			// Bolt file exists in old location but not in the new one,
			// most likely we just migrated from old config version.
			if err := os.Rename(oldFile, file); err != nil {
				// If it's not possible to move - symlink.
				if e := os.Symlink(oldFile, file); e != nil {
					log.Printf("unable to move old bolt file to new location %q: %s, %s", file, err, e)
				}
			}
		}
	}

	dir := filepath.Dir(file)

	// Best-effort attempts, ignore errors.
	_ = os.MkdirAll(dir, 0755)
	_ = util.Chown(dir, c.owner().User)

	return &config.CacheOptions{
		File: file,
		BoltDB: &bolt.Options{
			Timeout: 5 * time.Second,
		},
		Bucket: []byte(app),
	}
}
Example #2
0
func newBoltDB(o *CacheOptions) (*bolt.DB, error) {
	dir := filepath.Dir(o.File)

	// Best-effort attempts, ignore errors.
	_ = os.MkdirAll(dir, 0755)
	_ = util.Chown(dir, o.owner().User)

	return bolt.Open(o.File, 0644, o.BoltDB)
}
Example #3
0
func (c *Client) commitMetadata(m Metadata, fn metadataCommitFunc) error {
	var de WriteMetadataError

	// Try to read ID for Koding Base URL from the metadata, if not available
	// read currently used one from konfig.bolt. If both fails, do not
	// use ID at all.
	id := m.ID()
	if id == "" {
		// Best-effort attempt to rea
		if k, err := c.Used(); err == nil {
			id = k.ID()
		}
	}

	for key, value := range m {
		var file, bucket, keyValue string

		switch s := strings.SplitN(key, ".", 3); len(s) {
		case 1:
			file = s[0]
			bucket = s[0]
			keyValue = s[0]
		case 2:
			file = s[0]
			bucket = s[1]
			keyValue = s[1]
		case 3:
			file = s[0]
			bucket = s[1]
			keyValue = s[2]
		}

		// The konfig.bolt metadata is global for all koding
		// deployments.
		if file != "konfig" && id != "" {
			file = file + "." + id + ".bolt"
		} else {
			file = file + ".bolt"
		}

		opts := &config.CacheOptions{
			File: filepath.Join(c.home(), file),
			BoltDB: &bolt.Options{
				Timeout: 5 * time.Second,
			},
			Bucket: []byte(bucket),
			Owner:  c.owner(),
		}

		db, err := config.NewBoltCache(opts)

		if err != nil {
			de.Errs = append(de.Errs, &MetadataError{
				Key: key,
				Err: err,
			})
			continue
		}

		if value == nil {
			v := make(map[string]interface{})
			value = &v
			m[key] = v
		}

		err = nonil(fn(db, keyValue, value), db.Close(), util.Chown(opts.File, c.owner().User))

		if err != nil {
			de.Errs = append(de.Errs, &MetadataError{
				Key: key,
				Err: err,
			})
		}
	}

	if len(de.Errs) == 0 {
		return nil
	}

	return &de
}