当前位置: 首页>>代码示例>>Golang>>正文


Golang trivial.ErrorContextf函数代码示例

本文整理汇总了Golang中launchpad/net/juju-core/trivial.ErrorContextf函数的典型用法代码示例。如果您正苦于以下问题:Golang ErrorContextf函数的具体用法?Golang ErrorContextf怎么用?Golang ErrorContextf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ErrorContextf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Destroy

// Destroy ensures that the relation will be removed at some point; if no units
// are currently in scope, it will be removed immediately.
func (r *Relation) Destroy() (err error) {
	defer trivial.ErrorContextf(&err, "cannot destroy relation %q", r)
	defer func() {
		if err == nil {
			// This is a white lie; the document might actually be removed.
			r.doc.Life = Dying
		}
	}()
	rel := &Relation{r.st, r.doc}
	// In this context, aborted transactions indicate that the number of units
	// in scope have changed between 0 and not-0. The chances of 5 successive
	// attempts each hitting this change -- which is itself an unlikely one --
	// are considered to be extremely small.
	for attempt := 0; attempt < 5; attempt++ {
		ops, _, err := rel.destroyOps("")
		if err == errAlreadyDying {
			return nil
		} else if err != nil {
			return err
		}
		if err := rel.st.runner.Run(ops, "", nil); err != txn.ErrAborted {
			return err
		}
		if err := rel.Refresh(); IsNotFound(err) {
			return nil
		} else if err != nil {
			return err
		}
	}
	return ErrExcessiveContention
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:33,代码来源:relation.go

示例2: RemoveUnit

// RemoveUnit removes the given unit from s.
func (s *Service) RemoveUnit(u *Unit) (err error) {
	defer trivial.ErrorContextf(&err, "cannot remove unit %q", u)
	if u.doc.Life != Dead {
		return errors.New("unit is not dead")
	}
	if u.doc.Service != s.doc.Name {
		return fmt.Errorf("unit is not assigned to service %q", s)
	}
	svc := &Service{s.st, s.doc}
	unit := &Unit{u.st, u.doc}
	for i := 0; i < 5; i++ {
		ops := svc.removeUnitOps(unit)
		if err := svc.st.runner.Run(ops, "", nil); err != txn.ErrAborted {
			return err
		}
		if err := unit.Refresh(); IsNotFound(err) {
			return nil
		} else if err != nil {
			return err
		}
		if err := svc.Refresh(); IsNotFound(err) {
			return nil
		} else if err != nil {
			return err
		}
	}
	return ErrExcessiveContention
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:29,代码来源:service.go

示例3: AddUnitSubordinateTo

// AddUnitSubordinateTo adds a new subordinate unit to the service, subordinate
// to principal. It does not verify relation state sanity or pre-existence of
// other subordinates of the same service; is deprecated; and only continues
// to exist for the convenience of certain tests, which are themselves due for
// overhaul.
func (s *Service) AddUnitSubordinateTo(principal *Unit) (unit *Unit, err error) {
	log.Printf("state: Service.AddUnitSubordinateTo is DEPRECATED; subordinate units should be created only as a side-effect of a principal entering relation scope")
	defer trivial.ErrorContextf(&err, "cannot add unit to service %q as a subordinate of %q", s, principal)
	ch, _, err := s.Charm()
	if err != nil {
		return nil, err
	}
	if !ch.Meta().Subordinate {
		return nil, fmt.Errorf("service is not a subordinate")
	}
	if !principal.IsPrincipal() {
		return nil, fmt.Errorf("unit is not a principal")
	}
	name, ops, err := s.addUnitOps(principal.doc.Name, false)
	if err != nil {
		return nil, err
	}
	if err = s.st.runner.Run(ops, "", nil); err == nil {
		return s.Unit(name)
	} else if err != txn.ErrAborted {
		return nil, err
	}
	if alive, err := isAlive(s.st.services, s.doc.Name); err != nil {
		return nil, err
	} else if !alive {
		return nil, fmt.Errorf("service is not alive")
	}
	if alive, err := isAlive(s.st.units, principal.doc.Name); err != nil {
		return nil, err
	} else if !alive {
		return nil, fmt.Errorf("principal unit is not alive")
	}
	return nil, fmt.Errorf("inconsistent state")
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:39,代码来源:export_test.go

示例4: validate

// validate returns an error if the state violates expectations.
func (st State) validate() (err error) {
	defer trivial.ErrorContextf(&err, "invalid uniter state")
	hasHook := st.Hook != nil
	hasCharm := st.CharmURL != nil
	switch st.Op {
	case Install:
		if hasHook {
			return fmt.Errorf("unexpected hook info")
		}
		fallthrough
	case Upgrade:
		if !hasCharm {
			return fmt.Errorf("missing charm URL")
		}
	case Continue, RunHook:
		if !hasHook {
			return fmt.Errorf("missing hook info")
		} else if hasCharm {
			return fmt.Errorf("unexpected charm URL")
		}
	default:
		return fmt.Errorf("unknown operation %q", st.Op)
	}
	switch st.OpStep {
	case Queued, Pending, Done:
	default:
		return fmt.Errorf("unknown operation step %q", st.OpStep)
	}
	if hasHook {
		return st.Hook.Validate()
	}
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:34,代码来源:state.go

示例5: AssignUnit

// AssignUnit places the unit on a machine. Depending on the policy, and the
// state of the environment, this may lead to new instances being launched
// within the environment.
func (st *State) AssignUnit(u *Unit, policy AssignmentPolicy) (err error) {
	if !u.IsPrincipal() {
		return fmt.Errorf("subordinate unit %q cannot be assigned directly to a machine", u)
	}
	defer trivial.ErrorContextf(&err, "cannot assign unit %q to machine", u)
	var m *Machine
	switch policy {
	case AssignLocal:
		m, err = st.Machine("0")
		if err != nil {
			return err
		}
		return u.AssignToMachine(m)
	case AssignUnused:
		if _, err = u.AssignToUnusedMachine(); err != noUnusedMachines {
			return err
		}
		for {
			// TODO(rog) take out a lease on the new machine
			// so that we don't have a race here.
			m, err := st.AddMachine(JobHostUnits)
			if err != nil {
				return err
			}
			err = u.assignToMachine(m, true)
			if err == inUseErr {
				// Someone else has grabbed the machine we've
				// just allocated, so try again.
				continue
			}
			return err
		}
	}
	panic(fmt.Errorf("unknown unit assignment policy: %q", policy))
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:38,代码来源:state.go

示例6: RemoveMachine

// RemoveMachine removes the machine with the the given id.
func (st *State) RemoveMachine(id string) (err error) {
	defer trivial.ErrorContextf(&err, "cannot remove machine %s", id)
	m, err := st.Machine(id)
	if err != nil {
		return err
	}
	if m.doc.Life != Dead {
		return fmt.Errorf("machine is not dead")
	}
	sel := D{
		{"_id", id},
		{"life", Dead},
	}
	ops := []txn.Op{{
		C:      st.machines.Name,
		Id:     id,
		Assert: sel,
		Remove: true,
	}}
	if err := st.runner.Run(ops, "", nil); err != nil {
		// If aborted, the machine is either dead or recreated.
		return onAbort(err, nil)
	}
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:26,代码来源:state.go

示例7: Write

// Write atomically writes to disk the relation state change in hi.
// It must be called after the respective hook was executed successfully.
// Write doesn't validate hi but guarantees that successive writes of
// the same hi are idempotent.
func (d *StateDir) Write(hi hook.Info) (err error) {
	defer trivial.ErrorContextf(&err, "failed to write %q hook info for %q on state directory", hi.Kind, hi.RemoteUnit)
	if hi.Kind == hook.RelationBroken {
		return d.Remove()
	}
	name := strings.Replace(hi.RemoteUnit, "/", "-", 1)
	path := filepath.Join(d.path, name)
	if hi.Kind == hook.RelationDeparted {
		if err = os.Remove(path); err != nil && !os.IsNotExist(err) {
			return err
		}
		// If atomic delete succeeded, update own state.
		delete(d.state.Members, hi.RemoteUnit)
		return nil
	}
	di := diskInfo{&hi.ChangeVersion, hi.Kind == hook.RelationJoined}
	if err := trivial.WriteYaml(path, &di); err != nil {
		return err
	}
	// If write was successful, update own state.
	d.state.Members[hi.RemoteUnit] = hi.ChangeVersion
	if hi.Kind == hook.RelationJoined {
		d.state.ChangedPending = hi.RemoteUnit
	} else {
		d.state.ChangedPending = ""
	}
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:32,代码来源:relation.go

示例8: DestroyUnits

// DestroyUnits removes the specified units from the state.
func (conn *Conn) DestroyUnits(names ...string) (err error) {
	defer trivial.ErrorContextf(&err, "cannot destroy units")
	var units []*state.Unit
	for _, name := range names {
		unit, err := conn.State.Unit(name)
		switch {
		case state.IsNotFound(err):
			return fmt.Errorf("unit %q is not alive", name)
		case err != nil:
			return err
		case unit.Life() != state.Alive:
			return fmt.Errorf("unit %q is not alive", name)
		case unit.IsPrincipal():
			units = append(units, unit)
		default:
			return fmt.Errorf("unit %q is a subordinate", name)
		}
	}
	for _, unit := range units {
		if err := unit.EnsureDying(); err != nil {
			return err
		}
	}
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:26,代码来源:conn.go

示例9: SetResolved

// SetResolved marks the unit as having had any previous state transition
// problems resolved, and informs the unit that it may attempt to
// reestablish normal workflow. The resolved mode parameter informs
// whether to attempt to reexecute previous failed hooks or to continue
// as if they had succeeded before.
func (u *Unit) SetResolved(mode ResolvedMode) (err error) {
	defer trivial.ErrorContextf(&err, "cannot set resolved mode for unit %q", u)
	switch mode {
	case ResolvedNone, ResolvedRetryHooks, ResolvedNoHooks:
	default:
		return fmt.Errorf("invalid error resolution mode: %q", mode)
	}
	assert := append(notDeadDoc, D{{"resolved", ResolvedNone}}...)
	ops := []txn.Op{{
		C:      u.st.units.Name,
		Id:     u.doc.Name,
		Assert: assert,
		Update: D{{"$set", D{{"resolved", mode}}}},
	}}
	if err := u.st.runner.Run(ops, "", nil); err != nil {
		if err == txn.ErrAborted {
			// Find which assertion failed so we can give a
			// more specific error.
			u1, err := u.st.Unit(u.Name())
			if err != nil {
				return err
			}
			if u1.Life() != Alive {
				return errNotAlive
			}
			return fmt.Errorf("already resolved")
		}
		return err
	}
	u.doc.Resolved = mode
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:37,代码来源:unit.go

示例10: Validate

// Validate returns an error if the supplied hook.Info does not represent
// a valid change to the relation state. Hooks must always be validated
// against the current state before they are run, to ensure that the system
// meets its guarantees about hook execution order.
func (s *State) Validate(hi hook.Info) (err error) {
	defer trivial.ErrorContextf(&err, "inappropriate %q for %q", hi.Kind, hi.RemoteUnit)
	if hi.RelationId != s.RelationId {
		return fmt.Errorf("expected relation %d, got relation %d", s.RelationId, hi.RelationId)
	}
	if s.Members == nil {
		return fmt.Errorf(`relation is broken and cannot be changed further`)
	}
	unit, kind := hi.RemoteUnit, hi.Kind
	if kind == hook.RelationBroken {
		if len(s.Members) == 0 {
			return nil
		}
		return fmt.Errorf(`cannot run "relation-broken" while units still present`)
	}
	if s.ChangedPending != "" {
		if unit != s.ChangedPending || kind != hook.RelationChanged {
			return fmt.Errorf(`expected "relation-changed" for %q`, s.ChangedPending)
		}
	} else if _, joined := s.Members[unit]; joined && kind == hook.RelationJoined {
		return fmt.Errorf("unit already joined")
	} else if !joined && kind != hook.RelationJoined {
		return fmt.Errorf("unit has not joined")
	}
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:30,代码来源:relation.go

示例11: init

func (u *Uniter) init(name string) (err error) {
	defer trivial.ErrorContextf(&err, "failed to initialize uniter for unit %q", name)
	u.unit, err = u.st.Unit(name)
	if err != nil {
		return err
	}
	ename := u.unit.EntityName()
	u.toolsDir = environs.AgentToolsDir(u.dataDir, ename)
	if err := EnsureJujucSymlinks(u.toolsDir); err != nil {
		return err
	}
	u.baseDir = filepath.Join(u.dataDir, "agents", ename)
	u.relationsDir = filepath.Join(u.baseDir, "state", "relations")
	if err := os.MkdirAll(u.relationsDir, 0755); err != nil {
		return err
	}
	u.service, err = u.st.Service(u.unit.ServiceName())
	if err != nil {
		return err
	}
	u.relationers = map[int]*Relationer{}
	u.relationHooks = make(chan hook.Info)
	u.charm = charm.NewGitDir(filepath.Join(u.baseDir, "charm"))
	u.bundles = charm.NewBundlesDir(filepath.Join(u.baseDir, "state", "bundles"))
	u.deployer = charm.NewDeployer(filepath.Join(u.baseDir, "state", "deployer"))
	u.sf = NewStateFile(filepath.Join(u.baseDir, "state", "uniter"))
	u.rand = rand.New(rand.NewSource(time.Now().Unix()))
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:29,代码来源:uniter.go

示例12: Destroy

// Destroy ensures that the service and all its relations will be removed at
// some point; if the service has no units, and no relation involving the
// service has any units in scope, they are all removed immediately.
func (s *Service) Destroy() (err error) {
	defer trivial.ErrorContextf(&err, "cannot destroy service %q", s)
	defer func() {
		if err != nil {
			// This is a white lie; the document might actually be removed.
			s.doc.Life = Dying
		}
	}()
	svc := &Service{s.st, s.doc}
	for i := 0; i < 5; i++ {
		ops, err := svc.destroyOps()
		switch {
		case err == errRefresh:
		case err == errAlreadyDying:
			return nil
		case err != nil:
			return err
		default:
			if err := svc.st.runner.Run(ops, "", nil); err != txn.ErrAborted {
				return err
			}
		}
		if err := svc.Refresh(); IsNotFound(err) {
			return nil
		} else if err != nil {
			return err
		}
	}
	return ErrExcessiveContention
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:33,代码来源:service.go

示例13: ReadAllStateDirs

// ReadAllStateDirs loads and returns every StateDir persisted directly inside
// the supplied dirPath. If dirPath does not exist, no error is returned.
func ReadAllStateDirs(dirPath string) (dirs map[int]*StateDir, err error) {
	defer trivial.ErrorContextf(&err, "cannot load relations state from %q", dirPath)
	if _, err := os.Stat(dirPath); os.IsNotExist(err) {
		return nil, nil
	} else if err != nil {
		return nil, err
	}
	fis, err := ioutil.ReadDir(dirPath)
	if err != nil {
		return nil, err
	}
	dirs = map[int]*StateDir{}
	for _, fi := range fis {
		// Entries with integer names must be directories containing StateDir
		// data; all other names will be ignored.
		relationId, err := strconv.Atoi(fi.Name())
		if err != nil {
			// This doesn't look like a relation.
			continue
		}
		dir, err := ReadStateDir(dirPath, relationId)
		if err != nil {
			return nil, err
		}
		dirs[relationId] = dir
	}
	return dirs, nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:30,代码来源:relation.go

示例14: verifyConfig

func verifyConfig(cfg *MachineConfig) (err error) {
	defer trivial.ErrorContextf(&err, "invalid machine configuration")
	if !state.IsMachineId(cfg.MachineId) {
		return fmt.Errorf("invalid machine id")
	}
	if cfg.ProviderType == "" {
		return fmt.Errorf("missing provider type")
	}
	if cfg.DataDir == "" {
		return fmt.Errorf("missing var directory")
	}
	if cfg.Tools == nil {
		return fmt.Errorf("missing tools")
	}
	if cfg.Tools.URL == "" {
		return fmt.Errorf("missing tools URL")
	}
	if cfg.StateInfo == nil {
		return fmt.Errorf("missing state info")
	}
	if len(cfg.StateInfo.CACert) == 0 {
		return fmt.Errorf("missing CA certificate")
	}
	if cfg.StateServer {
		if cfg.InstanceIdAccessor == "" {
			return fmt.Errorf("missing instance id accessor")
		}
		if cfg.Config == nil {
			return fmt.Errorf("missing environment configuration")
		}
		if cfg.StateInfo.EntityName != "" {
			return fmt.Errorf("entity name must be blank when starting a state server")
		}
		if len(cfg.StateServerCert) == 0 {
			return fmt.Errorf("missing state server certificate")
		}
		if len(cfg.StateServerKey) == 0 {
			return fmt.Errorf("missing state server private key")
		}
	} else {
		if len(cfg.StateInfo.Addrs) == 0 {
			return fmt.Errorf("missing state hosts")
		}
		if cfg.StateInfo.EntityName != state.MachineEntityName(cfg.MachineId) {
			return fmt.Errorf("entity name must match started machine")
		}
	}
	for _, r := range cfg.StateInfo.Password {
		if r == '\'' || r == '\\' || r < 32 {
			return fmt.Errorf("password has disallowed characters")
		}
	}
	return nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:54,代码来源:cloudinit.go

示例15: Relations

// Relations returns a Relation for every relation the service is in.
func (s *Service) Relations() (relations []*Relation, err error) {
	defer trivial.ErrorContextf(&err, "can't get relations for service %q", s)
	docs := []relationDoc{}
	err = s.st.relations.Find(D{{"endpoints.servicename", s.doc.Name}}).All(&docs)
	if err != nil {
		return nil, err
	}
	for _, v := range docs {
		relations = append(relations, newRelation(s.st, &v))
	}
	return relations, nil
}
开发者ID:prabhakhar,项目名称:juju-core,代码行数:13,代码来源:service.go


注:本文中的launchpad/net/juju-core/trivial.ErrorContextf函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。