github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/agent/uninstall.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/names"
    12  )
    13  
    14  const (
    15  	// UninstallFile is the name of the file inside the data dir that,
    16  	// if it exists, will cause the machine agent to uninstall itself
    17  	// when it receives the termination signal or ErrTerminateAgent.
    18  	UninstallFile = "uninstall-agent"
    19  )
    20  
    21  // WARNING: this is linked with the use of UninstallFile in the
    22  // provider/manual package. Don't change it without extreme care,
    23  // and handling for mismatches with already-deployed agents.
    24  func uninstallFile(a Agent) string {
    25  	return filepath.Join(a.CurrentConfig().DataDir(), UninstallFile)
    26  }
    27  
    28  // SetCanUninstall creates the uninstall file in the data dir. It does
    29  // nothing if the supplied agent doesn't have a machine tag.
    30  func SetCanUninstall(a Agent) error {
    31  	tag := a.CurrentConfig().Tag()
    32  	if _, ok := tag.(names.MachineTag); !ok {
    33  		logger.Debugf("cannot uninstall non-machine agent %q", tag)
    34  		return nil
    35  	}
    36  	logger.Infof("marking agent ready for uninstall")
    37  	return ioutil.WriteFile(uninstallFile(a), nil, 0644)
    38  }
    39  
    40  // CanUninstall returns true if the uninstall file exists in the agent's
    41  // data dir. If it encounters an error, it fails safe and returns false.
    42  func CanUninstall(a Agent) bool {
    43  	if _, err := os.Stat(uninstallFile(a)); err != nil {
    44  		logger.Debugf("agent not marked ready for uninstall")
    45  		return false
    46  	}
    47  	logger.Infof("agent already marked ready for uninstall")
    48  	return true
    49  }