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

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasmodelupgrader
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/loggo"
     9  	"gopkg.in/juju/names.v2"
    10  	"gopkg.in/juju/worker.v1"
    11  
    12  	"github.com/juju/juju/core/status"
    13  	jujuworker "github.com/juju/juju/worker"
    14  	"github.com/juju/juju/worker/gate"
    15  )
    16  
    17  var logger = loggo.GetLogger("juju.worker.modelupgrader")
    18  
    19  // Facade exposes capabilities required by the worker.
    20  type Facade interface {
    21  	SetModelStatus(names.ModelTag, status.Status, string, map[string]interface{}) error
    22  }
    23  
    24  // Config holds the configuration and dependencies for a worker.
    25  type Config struct {
    26  	// Facade holds the API facade used by this worker for getting,
    27  	// setting and watching the model's environ version.
    28  	Facade Facade
    29  
    30  	// GateUnlocker holds a gate.Unlocker that the worker must call
    31  	// after the model has been successfully upgraded.
    32  	GateUnlocker gate.Unlocker
    33  
    34  	// ModelTag holds the tag of the model to which this worker is
    35  	// scoped.
    36  	ModelTag names.ModelTag
    37  }
    38  
    39  // Validate returns an error if the config cannot be expected
    40  // to drive a functional worker.
    41  func (config Config) Validate() error {
    42  	if config.Facade == nil {
    43  		return errors.NotValidf("nil Facade")
    44  	}
    45  	if config.GateUnlocker == nil {
    46  		return errors.NotValidf("nil GateUnlocker")
    47  	}
    48  	if config.ModelTag == (names.ModelTag{}) {
    49  		return errors.NotValidf("empty ModelTag")
    50  	}
    51  	return nil
    52  }
    53  
    54  // NewWorker returns a worker that unlocks the model upgrade gate.
    55  func NewWorker(config Config) (worker.Worker, error) {
    56  	if err := config.Validate(); err != nil {
    57  		return nil, errors.Trace(err)
    58  	}
    59  	// There are no upgrade steps for a CAAS model.
    60  	// We just set the status to available and unlock the gate.
    61  	return jujuworker.NewSimpleWorker(func(<-chan struct{}) error {
    62  		setStatus := func(s status.Status, info string) error {
    63  			return config.Facade.SetModelStatus(config.ModelTag, s, info, nil)
    64  		}
    65  		if err := setStatus(status.Available, ""); err != nil {
    66  			return errors.Trace(err)
    67  		}
    68  		config.GateUnlocker.Unlock()
    69  		return nil
    70  	}), nil
    71  }