Ejemplo n.º 1
0
func leadershipSettingsAccessorFactory(
	st *state.State,
	resources *common.Resources,
	auth common.Authorizer,
) *leadershipapiserver.LeadershipSettingsAccessor {
	registerWatcher := func(serviceId string) (string, error) {
		settingsWatcher := st.WatchLeadershipSettings(serviceId)
		if _, ok := <-settingsWatcher.Changes(); ok {
			return resources.Register(settingsWatcher), nil
		}

		return "", watcher.EnsureErr(settingsWatcher)
	}
	// TODO(katco-): <2015-01-21 Wed>
	// Due to time constraints, we're translating between
	// map[string]interface{} and map[string]string. At some point we
	// should support a native read of this format straight from
	// state.
	getSettings := func(serviceId string) (map[string]string, error) {
		settings, err := st.ReadLeadershipSettings(serviceId)
		if err != nil {
			return nil, err
		}
		// Perform the conversion
		rawMap := settings.Map()
		leadershipSettings := make(map[string]string)
		for k, v := range rawMap {
			leadershipSettings[k] = v.(string)
		}
		return leadershipSettings, nil
	}
	writeSettings := func(serviceId string, settings map[string]string) error {
		currentSettings, err := st.ReadLeadershipSettings(serviceId)
		if err != nil {
			return err
		}
		rawSettings := make(map[string]interface{})
		for k, v := range settings {
			rawSettings[k] = v
		}
		currentSettings.Update(rawSettings)
		_, err = currentSettings.Write()
		return errors.Annotate(err, "could not write changes")
	}
	ldrMgr := leadership.NewLeadershipManager(lease.Manager())
	return leadershipapiserver.NewLeadershipSettingsAccessor(
		auth,
		registerWatcher,
		getSettings,
		writeSettings,
		ldrMgr.Leader,
	)
}
Ejemplo n.º 2
0
func (ctx *context) apiLogin(c *gc.C) {
	password, err := utils.RandomPassword()
	c.Assert(err, jc.ErrorIsNil)
	err = ctx.unit.SetPassword(password)
	c.Assert(err, jc.ErrorIsNil)
	st := ctx.s.OpenAPIAs(c, ctx.unit.Tag(), password)
	c.Assert(st, gc.NotNil)
	c.Logf("API: login as %q successful", ctx.unit.Tag())
	ctx.api, err = st.Uniter()
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(ctx.api, gc.NotNil)
	ctx.leader = leadership.NewLeadershipManager(lease.Manager())
}
Ejemplo n.º 3
0
// isLeader will return true if the unitTag passed corresponds to the leader.
// TODO(perrito666): this must go in 1.25, it is not a fault proof approach
// it cannot guarantee that the unit will be leader during the duration of
// wathever operation we are doing nor has a sane approach to obtaining a
// lease manager.
func isLeader(st state.EntityFinder, unitTag string) (bool, error) {

	unit, err := getUnit(st, unitTag)
	if err != nil {
		return false, errors.Annotatef(err, "cannot obtain unit %q to check leadership", unitTag)
	}
	service, err := unit.Service()
	if err != nil {
		return false, err
	}

	leaseManager := lease.Manager()
	leadershipManager := leadership.NewLeadershipManager(leaseManager)
	return leadershipManager.Leader(service.Name(), unit.Name())
}
Ejemplo n.º 4
0
func (s *serviceSuite) claimLeadership(c *gc.C, unit *state.Unit, service *state.Service) {
	leadership := leadership.NewLeadershipManager(lease.Manager())
	err := leadership.ClaimLeadership(service.Name(), unit.Name(), time.Minute)
	c.Assert(err, jc.ErrorIsNil)
}
Ejemplo n.º 5
0
	// MinLeaseRequest is the shortest duration for which we will accept
	// a leadership claim.
	MinLeaseRequest = 5 * time.Second

	// MaxLeaseRequest is the longest duration for which we will accept
	// a leadership claim.
	MaxLeaseRequest = 5 * time.Minute
)

var (
	logger = loggo.GetLogger("juju.apiserver.leadership")
	// Begin injection-chain so we can instantiate leadership
	// services. Exposed as variables so we can change the
	// implementation for testing purposes.
	leaseMgr  = lease.Manager()
	leaderMgr = leadership.NewLeadershipManager(leaseMgr)
)

func init() {

	common.RegisterStandardFacade(
		FacadeName,
		1,
		NewLeadershipServiceFn(leaderMgr),
	)
}

// NewLeadershipServiceFn returns a function which can construct a
// LeadershipService when passed a state, resources, and authorizer.
// This function signature conforms to Juju's required API server
// signature.