github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/toolsversionchecker/manifold.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package toolsversionchecker
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/names/v5"
    11  	"github.com/juju/worker/v3"
    12  	"github.com/juju/worker/v3/dependency"
    13  
    14  	"github.com/juju/juju/agent"
    15  	apiagent "github.com/juju/juju/api/agent/agent"
    16  	"github.com/juju/juju/api/base"
    17  	"github.com/juju/juju/api/controller/agenttools"
    18  	"github.com/juju/juju/cmd/jujud/agent/engine"
    19  )
    20  
    21  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    22  type ManifoldConfig engine.AgentAPIManifoldConfig
    23  
    24  // Manifold returns a dependency manifold that runs a toolsversionchecker worker,
    25  // using the api connection resource named in the supplied config.
    26  func Manifold(config ManifoldConfig) dependency.Manifold {
    27  	typedConfig := engine.AgentAPIManifoldConfig(config)
    28  	return engine.AgentAPIManifold(typedConfig, newWorker)
    29  }
    30  
    31  func newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    32  	tag := a.CurrentConfig().Tag()
    33  	if tag.Kind() != names.MachineTagKind {
    34  		return nil, errors.New("this manifold may only be used inside a machine agent")
    35  	}
    36  
    37  	isController, err := apiagent.IsController(apiCaller, tag)
    38  	if err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  	if !isController {
    42  		return nil, dependency.ErrMissing
    43  	}
    44  
    45  	// 4 times a day seems a decent enough amount of checks.
    46  	checkerParams := VersionCheckerParams{
    47  		CheckInterval: time.Hour * 6,
    48  	}
    49  	return New(agenttools.NewFacade(apiCaller), &checkerParams), nil
    50  }