github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/meterstatus/context.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package meterstatus 5 6 import ( 7 "fmt" 8 "math/rand" 9 "time" 10 11 "github.com/juju/errors" 12 13 "github.com/juju/juju/worker/uniter/runner/context" 14 "github.com/juju/juju/worker/uniter/runner/jujuc" 15 ) 16 17 type limitedContext struct { 18 jujuc.RestrictedContext 19 20 env map[string]string 21 22 unitName string 23 id string 24 } 25 26 // NewLimitedContext creates a new context that implements just the bare minimum 27 // of the jujuc.Context interface. 28 func NewLimitedContext(unitName string) *limitedContext { 29 // TODO(fwereade): 2016-03-17 lp:1558657 30 id := fmt.Sprintf("%s-%s-%d", unitName, "meter-status", rand.New(rand.NewSource(time.Now().Unix())).Int63()) 31 return &limitedContext{unitName: unitName, id: id} 32 } 33 34 // HookVars implements runner.Context. 35 func (ctx *limitedContext) HookVars(paths context.Paths) ([]string, error) { 36 vars := []string{ 37 "JUJU_CHARM_DIR=" + paths.GetCharmDir(), 38 "JUJU_CONTEXT_ID=" + ctx.id, 39 "JUJU_AGENT_SOCKET=" + paths.GetJujucSocket(), 40 "JUJU_UNIT_NAME=" + ctx.unitName, 41 } 42 for key, val := range ctx.env { 43 vars = append(vars, fmt.Sprintf("%s=%s", key, val)) 44 } 45 return append(vars, context.OSDependentEnvVars(paths)...), nil 46 } 47 48 // SetEnvVars sets additional environment variables to be exported by the context. 49 func (ctx *limitedContext) SetEnvVars(vars map[string]string) { 50 if ctx.env == nil { 51 ctx.env = vars 52 return 53 } 54 for key, val := range vars { 55 ctx.env[key] = val 56 } 57 } 58 59 // UnitName implements runner.Context. 60 func (ctx *limitedContext) UnitName() string { 61 return ctx.unitName 62 } 63 64 // SetProcess implements runner.Context. 65 func (ctx *limitedContext) SetProcess(process context.HookProcess) {} 66 67 // ActionData implements runner.Context. 68 func (ctx *limitedContext) ActionData() (*context.ActionData, error) { 69 return nil, jujuc.ErrRestrictedContext 70 } 71 72 // Flush implementes runner.Context. 73 func (ctx *limitedContext) Flush(_ string, err error) error { 74 return err 75 } 76 77 // HasExecutionSetUnitStatus implements runner.Context. 78 func (ctx *limitedContext) HasExecutionSetUnitStatus() bool { return false } 79 80 // ResetExecutionSetUnitStatus implements runner.Context. 81 func (ctx *limitedContext) ResetExecutionSetUnitStatus() {} 82 83 // Id implements runner.Context. 84 func (ctx *limitedContext) Id() string { return ctx.id } 85 86 // Prepare implements runner.Context. 87 func (ctx *limitedContext) Prepare() error { 88 return jujuc.ErrRestrictedContext 89 } 90 91 // Component implements runner.Context. 92 func (ctx *limitedContext) Component(name string) (jujuc.ContextComponent, error) { 93 return nil, errors.NotFoundf("context component %q", name) 94 }