github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/agent/identity.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package agent 5 6 import ( 7 "os" 8 9 "github.com/juju/errors" 10 "github.com/juju/utils" 11 ) 12 13 var ErrNoStateServingInfo = errors.New("StateServingInfo missing") 14 15 func WriteSystemIdentityFile(c Config) error { 16 info, ok := c.StateServingInfo() 17 if !ok { 18 return errors.Trace(ErrNoStateServingInfo) 19 } 20 // Write non-empty contents to the file, otherwise delete it 21 if info.SystemIdentity != "" { 22 logger.Infof("writing system identity file") 23 err := utils.AtomicWriteFile(c.SystemIdentityPath(), []byte(info.SystemIdentity), 0600) 24 if err != nil { 25 return errors.Annotate(err, "cannot write system identity") 26 } 27 } else { 28 logger.Infof("removing system identity file") 29 os.Remove(c.SystemIdentityPath()) 30 } 31 return nil 32 }