Beispiel #1
0
func minifyApp(app provision.App) (miniApp, error) {
	units, err := app.Units()
	if err != nil {
		return miniApp{}, err
	}
	return miniApp{
		Name:  app.GetName(),
		Units: units,
		CName: app.GetCname(),
		Ip:    app.GetIp(),
		Lock:  app.GetLock(),
	}, nil
}
Beispiel #2
0
func (p *FakeProvisioner) Restart(app provision.App, process string, w io.Writer) error {
	if err := p.getError("Restart"); err != nil {
		return err
	}
	p.mut.Lock()
	defer p.mut.Unlock()
	pApp, ok := p.apps[app.GetName()]
	if !ok {
		return errNotProvisioned
	}
	pApp.restarts[process]++
	p.apps[app.GetName()] = pApp
	if w != nil {
		fmt.Fprintf(w, "restarting app")
	}
	r := routertest.FakeRouter
	err := r.AddBackend(app.GetName())
	if err != nil && err != router.ErrBackendExists {
		return err
	}
	var newAddr string
	if newAddr, err = r.Addr(app.GetName()); err == nil && newAddr != app.GetIp() {
		var conn *db.Storage
		conn, err = db.Conn()
		if err != nil {
			return err
		}
		defer conn.Close()
		err = conn.Apps().Update(bson.M{"name": app.GetName()}, bson.M{"$set": bson.M{"ip": newAddr}})
		if err != nil {
			return err
		}
	}
	for _, cname := range app.GetCname() {
		err = r.SetCName(cname, app.GetName())
		if err != nil && err != router.ErrCNameExists {
			return err
		}
	}
	oldRoutes, err := r.Routes(app.GetName())
	if err != nil {
		return err
	}
	expectedMap := make(map[string]*url.URL)
	units := p.apps[app.GetName()].units
	if err != nil {
		return err
	}
	for _, unit := range units {
		expectedMap[unit.Address.String()] = unit.Address
	}
	var toRemove []*url.URL
	for _, url := range oldRoutes {
		if _, isPresent := expectedMap[url.String()]; isPresent {
			delete(expectedMap, url.String())
		} else {
			toRemove = append(toRemove, url)
		}
	}
	for _, toAddUrl := range expectedMap {
		err := r.AddRoute(app.GetName(), toAddUrl)
		if err != nil {
			return err
		}
	}
	for _, toRemoveUrl := range toRemove {
		err := r.RemoveRoute(app.GetName(), toRemoveUrl)
		if err != nil {
			return err
		}
	}
	return nil
}