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