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


Golang Info.Validate方法代码示例

本文整理汇总了Golang中github.com/juju/juju/worker/uniter/hook.Info.Validate方法的典型用法代码示例。如果您正苦于以下问题:Golang Info.Validate方法的具体用法?Golang Info.Validate怎么用?Golang Info.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/juju/juju/worker/uniter/hook.Info的用法示例。


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

示例1: NewRunHook

// NewRunHook is part of the Factory interface.
func (f *factory) NewRunHook(hookInfo hook.Info) (Operation, error) {
	if err := hookInfo.Validate(); err != nil {
		return nil, err
	}
	return &runHook{
		info:          hookInfo,
		callbacks:     f.config.Callbacks,
		runnerFactory: f.config.RunnerFactory,
	}, nil
}
开发者ID:ktsakalozos,项目名称:juju,代码行数:11,代码来源:factory.go

示例2: NewHookRunner

// NewHookRunner exists to satisfy the Factory interface.
func (f *factory) NewHookRunner(hookInfo hook.Info) (Runner, error) {
	if err := hookInfo.Validate(); err != nil {
		return nil, errors.Trace(err)
	}

	ctx, err := f.contextFactory.HookContext(hookInfo)
	if err != nil {
		return nil, errors.Trace(err)
	}
	runner := NewRunner(ctx, f.paths)
	return runner, nil
}
开发者ID:mhilton,项目名称:juju,代码行数:13,代码来源:factory.go

示例3: 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)
	actionParams := map[string]interface{}(nil)

	relationId := -1
	if hi.Kind.IsRelation() {
		relationId = hi.RelationId
		if hookName, err = u.relationers[relationId].PrepareHook(hi); err != nil {
			return err
		}
	} else if hi.Kind == hooks.ActionRequested {
		action, err := u.st.Action(names.NewActionTag(hi.ActionId))
		if err != nil {
			return err
		}
		actionParams = action.Params()
		hookName = action.Name()
	}
	hctxId := fmt.Sprintf("%s:%s:%d", u.unit.Name(), hookName, u.rand.Int63())

	lockMessage := fmt.Sprintf("%s: running hook %q", u.unit.Name(), hookName)
	if err = u.acquireHookLock(lockMessage); err != nil {
		return err
	}
	defer u.hookLock.Unlock()

	hctx, err := u.getHookContext(hctxId, relationId, hi.RemoteUnit, actionParams)
	if err != nil {
		return err
	}
	srv, socketPath, err := u.startJujucServer(hctx)
	if err != nil {
		return err
	}
	defer srv.Close()

	// Run the hook.
	if err := u.writeState(RunHook, Pending, &hi, nil); err != nil {
		return err
	}
	logger.Infof("running %q hook", hookName)

	ranHook := true
	// The reason for the switch at this point is that once inside RunHook,
	// we don't know whether we're running an Action or a regular Hook.
	// RunAction simply calls the exact same method as RunHook, but with
	// the location as "actions" instead of "hooks".
	if hi.Kind == hooks.ActionRequested {
		err = hctx.RunAction(hookName, u.charmPath, u.toolsDir, socketPath)
	} else {
		err = hctx.RunHook(hookName, u.charmPath, u.toolsDir, socketPath)
	}

	if IsMissingHookError(err) {
		ranHook = false
	} else if err != nil {
		logger.Errorf("hook failed: %s", err)
		u.notifyHookFailed(hookName, hctx)
		return errHookFailed
	}
	if err := u.writeState(RunHook, Done, &hi, nil); err != nil {
		return err
	}
	if ranHook {
		logger.Infof("ran %q hook", hookName)
		u.notifyHookCompleted(hookName, hctx)
	} else {
		logger.Infof("skipped %q hook (missing)", hookName)
	}
	return u.commitHook(hi)
}
开发者ID:klyachin,项目名称:juju,代码行数:78,代码来源:uniter.go

示例4: NewHookRunner

// NewHookRunner exists to satisfy the Factory interface.
func (f *factory) NewHookRunner(hookInfo hook.Info) (Runner, error) {
	if err := hookInfo.Validate(); err != nil {
		return nil, errors.Trace(err)
	}

	ctx, err := f.coreContext()
	if err != nil {
		return nil, errors.Trace(err)
	}

	hookName := string(hookInfo.Kind)
	if hookInfo.Kind.IsRelation() {
		ctx.relationId = hookInfo.RelationId
		ctx.remoteUnitName = hookInfo.RemoteUnit
		relation, found := ctx.relations[hookInfo.RelationId]
		if !found {
			return nil, errors.Errorf("unknown relation id: %v", hookInfo.RelationId)
		}
		if hookInfo.Kind == hooks.RelationDeparted {
			relation.cache.RemoveMember(hookInfo.RemoteUnit)
		} else if hookInfo.RemoteUnit != "" {
			// Clear remote settings cache for changing remote unit.
			relation.cache.InvalidateMember(hookInfo.RemoteUnit)
		}
		hookName = fmt.Sprintf("%s-%s", relation.Name(), hookInfo.Kind)
	}
	if hookInfo.Kind.IsStorage() {
		ctx.storageTag = names.NewStorageTag(hookInfo.StorageId)
		if _, found := ctx.storage.Storage(ctx.storageTag); !found {
			return nil, errors.Errorf("unknown storage id: %v", hookInfo.StorageId)
		}
		storageName, err := names.StorageName(hookInfo.StorageId)
		if err != nil {
			return nil, errors.Trace(err)
		}
		hookName = fmt.Sprintf("%s-%s", storageName, hookName)
	}
	// Metrics are only sent from the collect-metrics hook.
	if hookInfo.Kind == hooks.CollectMetrics {
		ch, err := f.getCharm()
		if err != nil {
			return nil, errors.Trace(err)
		}
		ctx.definedMetrics = ch.Metrics()

		chURL, err := f.unit.CharmURL()
		if err != nil {
			return nil, errors.Trace(err)
		}

		charmMetrics := map[string]charm.Metric{}
		if ch.Metrics() != nil {
			charmMetrics = ch.Metrics().Metrics
		}
		ctx.metricsRecorder, err = metrics.NewJSONMetricRecorder(
			f.paths.GetMetricsSpoolDir(),
			charmMetrics,
			chURL.String())
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	ctx.id = f.newId(hookName)
	runner := NewRunner(ctx, f.paths)
	return runner, nil
}
开发者ID:claudiu-coblis,项目名称:juju,代码行数:67,代码来源:factory.go

示例5: 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)
	actionParams := map[string]interface{}(nil)

	// This value is needed to pass results of Action param validation
	// in case of error or invalidation.  This is probably bad form; it
	// will be corrected in PR refactoring HookContext.
	// TODO(binary132): handle errors before grabbing hook context.
	var actionParamsErr error = nil

	relationId := -1
	if hi.Kind.IsRelation() {
		relationId = hi.RelationId
		if hookName, err = u.relationers[relationId].PrepareHook(hi); err != nil {
			return err
		}
	} else if hi.Kind == hooks.ActionRequested {
		action, err := u.st.Action(names.NewActionTag(hi.ActionId))
		if err != nil {
			return err
		}
		actionParams = action.Params()
		hookName = action.Name()
		_, actionParamsErr = u.validateAction(hookName, actionParams)
	}
	hctxId := fmt.Sprintf("%s:%s:%d", u.unit.Name(), hookName, u.rand.Int63())

	lockMessage := fmt.Sprintf("%s: running hook %q", u.unit.Name(), hookName)
	if err = u.acquireHookLock(lockMessage); err != nil {
		return err
	}
	defer u.hookLock.Unlock()

	hctx, err := u.getHookContext(hctxId, relationId, hi.RemoteUnit, actionParams)
	if err != nil {
		return err
	}

	srv, socketPath, err := u.startJujucServer(hctx)
	if err != nil {
		return err
	}
	defer srv.Close()

	// Run the hook.
	if err := u.writeState(RunHook, Pending, &hi, nil); err != nil {
		return err
	}
	logger.Infof("running %q hook", hookName)

	ranHook := true
	// The reason for the conditional at this point is that once inside
	// RunHook, we don't know whether we're running an Action or a regular
	// Hook.  RunAction simply calls the exact same method as RunHook, but
	// with the location as "actions" instead of "hooks".
	if hi.Kind == hooks.ActionRequested {
		if actionParamsErr != nil {
			logger.Errorf("action %q param validation failed: %s", hookName, actionParamsErr.Error())
			u.notifyHookFailed(hookName, hctx)
			return u.commitHook(hi)
		}
		err = hctx.RunAction(hookName, u.charmPath, u.toolsDir, socketPath)
	} else {
		err = hctx.RunHook(hookName, u.charmPath, u.toolsDir, socketPath)
	}

	// Since the Action validation error was separated, regular error pathways
	// will still occur correctly.
	if IsMissingHookError(err) {
		ranHook = false
	} else if err != nil {
		logger.Errorf("hook failed: %s", err)
		u.notifyHookFailed(hookName, hctx)
		return errHookFailed
	}
	if err := u.writeState(RunHook, Done, &hi, nil); err != nil {
		return err
	}
	if ranHook {
		logger.Infof("ran %q hook", hookName)
		u.notifyHookCompleted(hookName, hctx)
	} else {
		logger.Infof("skipped %q hook (missing)", hookName)
	}
	return u.commitHook(hi)
}
开发者ID:kapilt,项目名称:juju,代码行数:93,代码来源:uniter.go


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