service/service_instance.go
func (si *ServiceInstance) BindUnit(app bind.App, unit bind.Unit) error {
	s, err := Get(si.ServiceName)
	if err != nil {
		return err
	}
	endpoint, err := s.getClient("production")
	if err != nil {
		return err
	}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	updateOp := bson.M{
		"$addToSet": bson.M{
			"bound_units": bson.D([]bson.DocElem{
				{Name: "appname", Value: app.GetName()},
				{Name: "id", Value: unit.GetID()},
				{Name: "ip", Value: unit.GetIp()},
			}),
		},
	}
	err = conn.ServiceInstances().Update(bson.M{"name": si.Name, "service_name": si.ServiceName, "bound_units.id": bson.M{"$ne": unit.GetID()}}, updateOp)
	conn.Close()
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil
		}
		return err
	}
	err = endpoint.BindUnit(si, app, unit)
	if err != nil {
		updateOp = bson.M{
			"$pull": bson.M{
				"bound_units": bson.D([]bson.DocElem{
					{Name: "appname", Value: app.GetName()},
					{Name: "id", Value: unit.GetID()},
					{Name: "ip", Value: unit.GetIp()},
				}),
			},
		}
		rollbackErr := si.updateData(updateOp)
		if rollbackErr != nil {
			log.Errorf("[bind unit] could not remove stil unbound unit from db after failure: %s", rollbackErr)
		}
		return err
	}
	return nil
}
func (si *ServiceInstance) BindUnit(app bind.App, unit bind.Unit) error {
	s, err := Get(si.ServiceName)
	if err != nil {
		return err
	}
	endpoint, err := s.getClient("production")
	if err != nil {
		return err
	}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	updateOp := bson.M{
		"$addToSet": bson.M{
			"bound_units": bson.D([]bson.DocElem{
				{Name: "appname", Value: app.GetName()},
				{Name: "id", Value: unit.GetID()},
				{Name: "ip", Value: unit.GetIp()},
			}),
		},
	}
	err = conn.ServiceInstances().Update(bson.M{"name": si.Name, "service_name": si.ServiceName, "bound_units.id": bson.M{"$ne": unit.GetID()}}, updateOp)
	conn.Close()
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil
		}
		return err
	}
	err = endpoint.BindUnit(si, app, unit)
	if err != nil {
		updateOp = bson.M{
			"$pull": bson.M{
				"bound_units": bson.D([]bson.DocElem{
					{Name: "appname", Value: app.GetName()},
					{Name: "id", Value: unit.GetID()},
					{Name: "ip", Value: unit.GetIp()},
				}),
			},
		}
		rollbackErr := si.updateData(updateOp)
		if rollbackErr != nil {
			log.Errorf("[bind unit] could not remove stil unbound unit from db after failure: %s", rollbackErr)
		}
		return err
	}
	return nil
}
// BindUnit makes the bind between the binder and an unit.