github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/upgrade126.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"github.com/juju/names"
     8  	"gopkg.in/juju/charm.v6-unstable/hooks"
     9  
    10  	"github.com/juju/juju/worker/uniter/operation"
    11  )
    12  
    13  // AddInstalledToUniterState sets the Installed boolean in state to true
    14  // if the charm has been installed. The only occasion where this is not
    15  // true is if we are currently installing.
    16  func AddInstalledToUniterState(tag names.UnitTag, dataDir string) error {
    17  	logger.Tracef("entering upgrade step AddInstalledToUniterState")
    18  	defer logger.Tracef("leaving upgrade step AddInstalledToUniterState")
    19  
    20  	opsFile := getUniterStateFile(dataDir, tag)
    21  	state, err := readUnsafe(opsFile)
    22  	switch err {
    23  	case nil:
    24  		return addInstalled(opsFile, state)
    25  	case operation.ErrNoStateFile:
    26  		logger.Warningf("no uniter state file found for unit %s, skipping uniter upgrade step", tag)
    27  		return nil
    28  	default:
    29  		return err
    30  	}
    31  }
    32  
    33  func addInstalled(opsFile string, state *operation.State) error {
    34  	statefile := operation.NewStateFile(opsFile)
    35  	if state.Kind == operation.Install {
    36  		return nil
    37  	}
    38  	if state.Kind == operation.RunHook && state.Hook.Kind == hooks.Install {
    39  		return nil
    40  	}
    41  	if !state.Installed {
    42  		state.Installed = true
    43  		return statefile.Write(state)
    44  	}
    45  	return nil
    46  }