github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/proxy"
     9  	"gopkg.in/juju/worker.v1"
    10  	"gopkg.in/juju/worker.v1/dependency"
    11  
    12  	"github.com/juju/juju/agent"
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/api/proxyupdater"
    15  )
    16  
    17  // Logger represents the methods used for logging messages.
    18  type Logger interface {
    19  	Errorf(string, ...interface{})
    20  	Warningf(string, ...interface{})
    21  	Infof(string, ...interface{})
    22  	Debugf(string, ...interface{})
    23  	Tracef(string, ...interface{})
    24  }
    25  
    26  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    27  type ManifoldConfig struct {
    28  	AgentName       string
    29  	APICallerName   string
    30  	Logger          Logger
    31  	WorkerFunc      func(Config) (worker.Worker, error)
    32  	ExternalUpdate  func(proxy.Settings) error
    33  	InProcessUpdate func(proxy.Settings) error
    34  	RunFunc         func(string, string, ...string) (string, error)
    35  }
    36  
    37  // Manifold returns a dependency manifold that runs a proxy updater worker,
    38  // using the api connection resource named in the supplied config.
    39  func Manifold(config ManifoldConfig) dependency.Manifold {
    40  	return dependency.Manifold{
    41  		Inputs: []string{
    42  			config.AgentName,
    43  			config.APICallerName,
    44  		},
    45  		Start: func(context dependency.Context) (worker.Worker, error) {
    46  			if config.WorkerFunc == nil {
    47  				return nil, errors.NotValidf("missing WorkerFunc")
    48  			}
    49  			if config.InProcessUpdate == nil {
    50  				return nil, errors.NotValidf("missing InProcessUpdate")
    51  			}
    52  			var agent agent.Agent
    53  			if err := context.Get(config.AgentName, &agent); err != nil {
    54  				return nil, err
    55  			}
    56  			var apiCaller base.APICaller
    57  			if err := context.Get(config.APICallerName, &apiCaller); err != nil {
    58  				return nil, err
    59  			}
    60  
    61  			agentConfig := agent.CurrentConfig()
    62  			proxyAPI, err := proxyupdater.NewAPI(apiCaller, agentConfig.Tag())
    63  			if err != nil {
    64  				return nil, err
    65  			}
    66  			w, err := config.WorkerFunc(Config{
    67  				SystemdFiles:    []string{"/etc/juju-proxy-systemd.conf"},
    68  				EnvFiles:        []string{"/etc/juju-proxy.conf"},
    69  				RegistryPath:    `HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings`,
    70  				API:             proxyAPI,
    71  				ExternalUpdate:  config.ExternalUpdate,
    72  				InProcessUpdate: config.InProcessUpdate,
    73  				Logger:          config.Logger,
    74  				RunFunc:         config.RunFunc,
    75  			})
    76  			if err != nil {
    77  				return nil, errors.Trace(err)
    78  			}
    79  			return w, nil
    80  		},
    81  	}
    82  }