github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/reboot/manifold.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package reboot 5 6 import ( 7 "github.com/juju/clock" 8 "github.com/juju/errors" 9 "github.com/juju/worker/v3" 10 "github.com/juju/worker/v3/dependency" 11 12 "github.com/juju/juju/agent" 13 "github.com/juju/juju/api" 14 "github.com/juju/juju/api/agent/reboot" 15 "github.com/juju/juju/api/base" 16 "github.com/juju/juju/core/machinelock" 17 ) 18 19 // ManifoldConfig defines the names of the manifolds on which a Manifold will depend. 20 type ManifoldConfig struct { 21 AgentName string 22 APICallerName string 23 MachineLock machinelock.Lock 24 Clock clock.Clock 25 } 26 27 // Manifold returns a dependency manifold that runs a reboot worker, 28 // using the resource names defined in the supplied config. 29 func Manifold(config ManifoldConfig) dependency.Manifold { 30 return dependency.Manifold{ 31 Inputs: []string{ 32 config.AgentName, 33 config.APICallerName, 34 }, 35 Start: func(context dependency.Context) (worker.Worker, error) { 36 var agent agent.Agent 37 if err := context.Get(config.AgentName, &agent); err != nil { 38 return nil, err 39 } 40 var apiCaller base.APICaller 41 if err := context.Get(config.APICallerName, &apiCaller); err != nil { 42 return nil, err 43 } 44 if config.Clock == nil { 45 return nil, errors.NotValidf("missing Clock") 46 } 47 if config.MachineLock == nil { 48 return nil, errors.NotValidf("missing MachineLock") 49 } 50 return newWorker(agent, apiCaller, config.MachineLock, config.Clock) 51 }, 52 } 53 } 54 55 // newWorker starts a new reboot worker. 56 // 57 // TODO(mjs) - It's not tested at the moment, because the scaffolding 58 // necessary is too unwieldy/distracting to introduce at this point. 59 func newWorker(a agent.Agent, apiCaller base.APICaller, machineLock machinelock.Lock, clock clock.Clock) (worker.Worker, error) { 60 apiConn, ok := apiCaller.(api.Connection) 61 if !ok { 62 return nil, errors.New("unable to obtain api.Connection") 63 } 64 rebootState, err := reboot.NewFromConnection(apiConn) 65 if err != nil { 66 return nil, errors.Trace(err) 67 } 68 w, err := NewReboot(rebootState, a.CurrentConfig(), machineLock, clock) 69 if err != nil { 70 return nil, errors.Annotate(err, "cannot start reboot worker") 71 } 72 return w, nil 73 }