Exemplo n.º 1
0
func (c *Client) Use(k *config.Konfig) error {
	c.init()

	if err := k.Valid(); err != nil {
		return err
	}

	return c.commit(makeUseFunc(k))
}
Exemplo n.º 2
0
func makeUseFunc(konfig *config.Konfig) func(cache *config.Cache) error {
	return func(cache *config.Cache) error {
		konfigs := make(config.Konfigs)

		if err := cache.GetValue("konfigs", &konfigs); isFatal(err) {
			return err
		}

		id := konfig.ID()

		konfigs[id] = konfig

		return nonil(
			cache.SetValue("konfigs", konfigs),
			cache.SetValue("konfigs.used", &usedKonfig{ID: id}),
		)
	}
}
Exemplo n.º 3
0
func migrateKiteKey(konfig *config.Konfig) error {
	// KiteKey already exists in the DB - we don't care
	// whether it's our one or user overriden it explictely
	// as long as it's there.
	if konfig.KiteKey != "" {
		return nil
	}

	defaultKitekey := config.NewKonfig(&config.Environments{Env: konfig.Environment}).KiteKeyFile
	if defaultKitekey == "" {
		defaultKitekey = filepath.FromSlash("/etc/kite/kite.key")
	}

	kitekey := konfig.KiteKeyFile

	if kitekey == "" {
		kitekey = defaultKitekey
	}

	if _, err := os.Stat(kitekey); err != nil {
		// Either no access to the file or it does not exist,
		// in either case nothing to do here.
		return nil
	}

	p, err := ioutil.ReadFile(kitekey)
	if err != nil {
		return err
	}

	if konfig.KiteKeyFile == defaultKitekey {
		konfig.KiteKeyFile = ""
	}

	konfig.KiteKey = string(p)

	return nil
}
Exemplo n.º 4
0
func migrateKonfigBolt(cache *config.Cache) error {
	var used usedKonfig
	var oldKonfig config.Konfig
	var konfigs = make(config.Konfigs)

	if err := cache.GetValue("konfig", &oldKonfig); isFatal(err) {
		return err
	}

	if err := cache.GetValue("konfigs", &konfigs); isFatal(err) {
		return err
	}

	if err := cache.GetValue("konfigs.used", &used); isFatal(err) {
		return err
	}

	// If old konfig exists, try to migrate it over to konfigs.
	if oldKonfig.Valid() == nil {
		id := oldKonfig.ID()

		if _, ok := konfigs[id]; !ok {
			if oldKonfig.Endpoints == nil {
				oldKonfig.Endpoints = &config.Endpoints{}
			}

			if u, err := url.Parse(oldKonfig.KontrolURL); err == nil && oldKonfig.KontrolURL != "" {
				u.Path = ""
				oldKonfig.Endpoints.Koding = config.NewEndpointURL(u)
			}

			if oldKonfig.TunnelURL != "" {
				oldKonfig.Endpoints.Tunnel = config.NewEndpoint(oldKonfig.TunnelURL)
			}

			// Best-effort attemp to ensure /etc/kite/kite.key is stored
			// in ~/.config/koding/konfig.bolt, so it is possible to
			// use kd / konfig with koding deployments that sign with
			// different kontrol keys, e.g. production <-> sandbox or
			// production <-> self-hosted opensource version.
			_ = migrateKiteKey(&oldKonfig)

			konfigs[id] = &oldKonfig

			_ = cache.SetValue("konfigs", konfigs)
		}
	}

	// If no konfig is in use (e.g. we just migrated one),
	// try to set to the default one.
	if used.ID == "" && len(konfigs) == 1 {
		for id, konfig := range konfigs {
			if konfig.Valid() == nil {
				_ = cache.SetValue("konfigs.used", &usedKonfig{ID: id})
			}
			break
		}
	}

	return nil
}