github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/proxyupdater/manifold.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package proxyupdater
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/utils/proxy"
     9  
    10  	"github.com/juju/juju/agent"
    11  	"github.com/juju/juju/api/base"
    12  	"github.com/juju/juju/api/proxyupdater"
    13  	"github.com/juju/juju/worker"
    14  	"github.com/juju/juju/worker/dependency"
    15  )
    16  
    17  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    18  type ManifoldConfig struct {
    19  	AgentName      string
    20  	APICallerName  string
    21  	WorkerFunc     func(Config) (worker.Worker, error)
    22  	ExternalUpdate func(proxy.Settings) error
    23  }
    24  
    25  // Manifold returns a dependency manifold that runs a proxy updater worker,
    26  // using the api connection resource named in the supplied config.
    27  func Manifold(config ManifoldConfig) dependency.Manifold {
    28  	return dependency.Manifold{
    29  		Inputs: []string{
    30  			config.AgentName,
    31  			config.APICallerName,
    32  		},
    33  		Start: func(context dependency.Context) (worker.Worker, error) {
    34  			if config.WorkerFunc == nil {
    35  				return nil, errors.NotValidf("missing WorkerFunc")
    36  			}
    37  			var agent agent.Agent
    38  			if err := context.Get(config.AgentName, &agent); err != nil {
    39  				return nil, err
    40  			}
    41  			var apiCaller base.APICaller
    42  			if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    43  				return nil, err
    44  			}
    45  
    46  			agentConfig := agent.CurrentConfig()
    47  			proxyAPI, err := proxyupdater.NewAPI(apiCaller, agentConfig.Tag())
    48  			if err != nil {
    49  				return nil, err
    50  			}
    51  			w, err := config.WorkerFunc(Config{
    52  				Directory:      "/home/ubuntu",
    53  				RegistryPath:   `HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings`,
    54  				Filename:       ".juju-proxy",
    55  				API:            proxyAPI,
    56  				ExternalUpdate: config.ExternalUpdate,
    57  			})
    58  			if err != nil {
    59  				return nil, errors.Trace(err)
    60  			}
    61  			return w, nil
    62  		},
    63  	}
    64  }