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

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperator
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/utils"
    11  	"gopkg.in/juju/charm.v6"
    12  )
    13  
    14  // LocalState is a cache of the state of the operator
    15  // It is generally compared to the remote state of the
    16  // the application as stored in the controller.
    17  type LocalState struct {
    18  	// CharmModifiedVersion increases any time the charm,
    19  	// or any part of it, is changed in some way.
    20  	CharmModifiedVersion int
    21  
    22  	// CharmURL reports the currently installed charm URL. This is set
    23  	// by the committing of deploy (install/upgrade) ops.
    24  	CharmURL *charm.URL
    25  }
    26  
    27  // ErrNoStateFile is used to indicate an operator state file does not exist.
    28  var ErrNoStateFile = errors.New("operator state file does not exist")
    29  
    30  // StateFile holds the disk state for an operator.
    31  type StateFile struct {
    32  	path string
    33  }
    34  
    35  // NewStateFile returns a new StateFile using path.
    36  func NewStateFile(path string) *StateFile {
    37  	return &StateFile{path}
    38  }
    39  
    40  // Read reads a State from the file. If the file does not exist it returns
    41  // ErrNoStateFile.
    42  func (f *StateFile) Read() (*LocalState, error) {
    43  	var st LocalState
    44  	if err := utils.ReadYaml(f.path, &st); err != nil {
    45  		if os.IsNotExist(err) {
    46  			return nil, ErrNoStateFile
    47  		}
    48  	}
    49  	return &st, nil
    50  }
    51  
    52  // Write stores the supplied state to the file.
    53  func (f *StateFile) Write(st *LocalState) error {
    54  	return utils.WriteYaml(f.path, st)
    55  }