github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/errors"
    10  	"github.com/juju/utils/clock"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/agent"
    14  	"github.com/juju/juju/api/base"
    15  	"github.com/juju/juju/api/storageprovisioner"
    16  	"github.com/juju/juju/cmd/jujud/agent/engine"
    17  	"github.com/juju/juju/storage/provider"
    18  	"github.com/juju/juju/worker"
    19  	"github.com/juju/juju/worker/dependency"
    20  )
    21  
    22  // MachineManifoldConfig defines a storage provisioner's configuration and dependencies.
    23  type MachineManifoldConfig struct {
    24  	AgentName     string
    25  	APICallerName string
    26  	Clock         clock.Clock
    27  }
    28  
    29  func (config MachineManifoldConfig) newWorker(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    30  	if config.Clock == nil {
    31  		return nil, dependency.ErrMissing
    32  	}
    33  
    34  	cfg := a.CurrentConfig()
    35  	api, err := storageprovisioner.NewState(apiCaller, cfg.Tag())
    36  	if err != nil {
    37  		return nil, errors.Trace(err)
    38  	}
    39  
    40  	tag, ok := cfg.Tag().(names.MachineTag)
    41  	if !ok {
    42  		return nil, errors.Errorf("this manifold may only be used inside a machine agent")
    43  	}
    44  
    45  	storageDir := filepath.Join(cfg.DataDir(), "storage")
    46  	w, err := NewStorageProvisioner(Config{
    47  		Scope:       tag,
    48  		StorageDir:  storageDir,
    49  		Volumes:     api,
    50  		Filesystems: api,
    51  		Life:        api,
    52  		Registry:    provider.CommonStorageProviders(),
    53  		Machines:    api,
    54  		Status:      api,
    55  		Clock:       config.Clock,
    56  	})
    57  	if err != nil {
    58  		return nil, errors.Trace(err)
    59  	}
    60  	return w, nil
    61  }
    62  
    63  // MachineManifold returns a dependency.Manifold that runs a storage provisioner.
    64  func MachineManifold(config MachineManifoldConfig) dependency.Manifold {
    65  	typedConfig := engine.AgentAPIManifoldConfig{
    66  		AgentName:     config.AgentName,
    67  		APICallerName: config.APICallerName,
    68  	}
    69  	return engine.AgentAPIManifold(typedConfig, config.newWorker)
    70  }