本文整理汇总了Golang中launchpad/net/juju-core/worker/uniter/hook.Info.Validate方法的典型用法代码示例。如果您正苦于以下问题:Golang Info.Validate方法的具体用法?Golang Info.Validate怎么用?Golang Info.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类launchpad/net/juju-core/worker/uniter/hook.Info
的用法示例。
在下文中一共展示了Info.Validate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: runHook
// runHook executes the supplied hook.Info in an appropriate hook context. If
// the hook itself fails to execute, it returns errHookFailed.
func (u *Uniter) runHook(hi hook.Info) (err error) {
// Prepare context.
if err = hi.Validate(); err != nil {
return err
}
hookName := string(hi.Kind)
relationId := -1
if hi.Kind.IsRelation() {
relationId = hi.RelationId
if hookName, err = u.relationers[relationId].PrepareHook(hi); err != nil {
return err
}
}
hctxId := fmt.Sprintf("%s:%s:%d", u.unit.Name(), hookName, u.rand.Int63())
hctx := &HookContext{
service: u.service,
unit: u.unit,
id: hctxId,
relationId: relationId,
remoteUnitName: hi.RemoteUnit,
relations: map[int]*ContextRelation{},
}
for id, r := range u.relationers {
hctx.relations[id] = r.Context()
}
// Prepare server.
getCmd := func(ctxId, cmdName string) (cmd.Command, error) {
// TODO: switch to long-running server with single context;
// use nonce in place of context id.
if ctxId != hctxId {
return nil, fmt.Errorf("expected context id %q, got %q", hctxId, ctxId)
}
return jujuc.NewCommand(hctx, cmdName)
}
socketPath := filepath.Join(u.baseDir, "agent.socket")
srv, err := jujuc.NewServer(getCmd, socketPath)
if err != nil {
return err
}
go srv.Run()
defer srv.Close()
// Run the hook.
if err := u.writeState(RunHook, Pending, &hi, nil); err != nil {
return err
}
log.Printf("worker/uniter: running %q hook", hookName)
if err := hctx.RunHook(hookName, u.charm.Path(), u.toolsDir, socketPath); err != nil {
log.Printf("worker/uniter: hook failed: %s", err)
return errHookFailed
}
if err := u.writeState(RunHook, Done, &hi, nil); err != nil {
return err
}
log.Printf("worker/uniter: ran %q hook", hookName)
return u.commitHook(hi)
}
示例2: runHook
// runHook executes the supplied hook.Info in an appropriate hook context. If
// the hook itself fails to execute, it returns errHookFailed.
func (u *Uniter) runHook(hi hook.Info) (err error) {
// Prepare context.
if err = hi.Validate(); err != nil {
return err
}
hookName := string(hi.Kind)
relationId := -1
if hi.Kind.IsRelation() {
relationId = hi.RelationId
if hookName, err = u.relationers[relationId].PrepareHook(hi); err != nil {
return err
}
}
hctxId := fmt.Sprintf("%s:%s:%d", u.unit.Name(), hookName, u.rand.Int63())
// We want to make sure we don't block forever when locking, but take the
// tomb into account.
checkTomb := func() error {
select {
case <-u.tomb.Dying():
return tomb.ErrDying
default:
// no-op to fall through to return.
}
return nil
}
lockMessage := fmt.Sprintf("%s: running hook %q", u.unit.Name(), hookName)
if err = u.hookLock.LockWithFunc(lockMessage, checkTomb); err != nil {
return err
}
defer u.hookLock.Unlock()
ctxRelations := map[int]*ContextRelation{}
for id, r := range u.relationers {
ctxRelations[id] = r.Context()
}
apiAddrs, err := u.st.APIAddresses()
if err != nil {
return err
}
hctx := NewHookContext(u.unit, hctxId, u.uuid, relationId, hi.RemoteUnit,
ctxRelations, apiAddrs)
// Prepare server.
getCmd := func(ctxId, cmdName string) (cmd.Command, error) {
// TODO: switch to long-running server with single context;
// use nonce in place of context id.
if ctxId != hctxId {
return nil, fmt.Errorf("expected context id %q, got %q", hctxId, ctxId)
}
return jujuc.NewCommand(hctx, cmdName)
}
socketPath := filepath.Join(u.baseDir, "agent.socket")
srv, err := jujuc.NewServer(getCmd, socketPath)
if err != nil {
return err
}
go srv.Run()
defer srv.Close()
// Run the hook.
if err := u.writeState(RunHook, Pending, &hi, nil); err != nil {
return err
}
log.Infof("worker/uniter: running %q hook", hookName)
if err := hctx.RunHook(hookName, u.charm.Path(), u.toolsDir, socketPath); err != nil {
log.Errorf("worker/uniter: hook failed: %s", err)
return errHookFailed
}
if err := u.writeState(RunHook, Done, &hi, nil); err != nil {
return err
}
log.Infof("worker/uniter: ran %q hook", hookName)
return u.commitHook(hi)
}