github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/storageprovisioner/manifold_machine.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storageprovisioner
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	"github.com/juju/clock"
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/names.v2"
    12  	"gopkg.in/juju/worker.v1"
    13  	"gopkg.in/juju/worker.v1/dependency"
    14  
    15  	"github.com/juju/juju/agent"
    16  	"github.com/juju/juju/api/base"
    17  	"github.com/juju/juju/api/storageprovisioner"
    18  	"github.com/juju/juju/cmd/jujud/agent/engine"
    19  	"github.com/juju/juju/storage/provider"
    20  	"github.com/juju/juju/worker/common"
    21  )
    22  
    23  // MachineManifoldConfig defines a storage provisioner's configuration and dependencies.
    24  type MachineManifoldConfig struct {
    25  	AgentName                    string
    26  	APICallerName                string
    27  	Clock                        clock.Clock
    28  	NewCredentialValidatorFacade func(base.APICaller) (common.CredentialAPI, error)
    29  }
    30  
    31  func (config MachineManifoldConfig) newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    32  	if config.Clock == nil {
    33  		return nil, dependency.ErrMissing
    34  	}
    35  
    36  	cfg := a.CurrentConfig()
    37  	api, err := storageprovisioner.NewState(apiCaller)
    38  	if err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  
    42  	tag, ok := cfg.Tag().(names.MachineTag)
    43  	if !ok {
    44  		return nil, errors.Errorf("this manifold may only be used inside a machine agent")
    45  	}
    46  
    47  	credentialAPI, err := config.NewCredentialValidatorFacade(apiCaller)
    48  	if err != nil {
    49  		return nil, errors.Trace(err)
    50  	}
    51  
    52  	storageDir := filepath.Join(cfg.DataDir(), "storage")
    53  	w, err := NewStorageProvisioner(Config{
    54  		Scope:            tag,
    55  		StorageDir:       storageDir,
    56  		Volumes:          api,
    57  		Filesystems:      api,
    58  		Life:             api,
    59  		Registry:         provider.CommonStorageProviders(),
    60  		Machines:         api,
    61  		Status:           api,
    62  		Clock:            config.Clock,
    63  		CloudCallContext: common.NewCloudCallContext(credentialAPI, nil),
    64  	})
    65  	if err != nil {
    66  		return nil, errors.Trace(err)
    67  	}
    68  	return w, nil
    69  }
    70  
    71  // MachineManifold returns a dependency.Manifold that runs a storage provisioner.
    72  func MachineManifold(config MachineManifoldConfig) dependency.Manifold {
    73  	typedConfig := engine.AgentAPIManifoldConfig{
    74  		AgentName:     config.AgentName,
    75  		APICallerName: config.APICallerName,
    76  	}
    77  	return engine.AgentAPIManifold(typedConfig, config.newWorker)
    78  }