github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/upgrades/lxd.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build go1.3 5 6 package upgrades 7 8 import ( 9 "io/ioutil" 10 "strings" 11 12 "github.com/juju/errors" 13 "github.com/juju/utils" 14 15 "github.com/juju/juju/provider/lxd" 16 ) 17 18 func updateLXDCloudCredentials(st StateBackend) error { 19 creds, err := lxd.ReadLegacyCloudCredentials(ioutil.ReadFile) 20 if err != nil { 21 if errors.IsNotFound(err) { 22 // Not running a LXD controller. 23 return nil 24 } 25 return errors.Annotate(err, "reading credentials from disk") 26 } 27 gatewayAddress, err := getDefaultGateway() 28 if err != nil { 29 return errors.Annotate(err, "reading gateway address") 30 } 31 return st.UpdateLegacyLXDCloudCredentials(gatewayAddress, creds) 32 } 33 34 func getDefaultGateway() (string, error) { 35 out, err := utils.RunCommand("ip", "route", "list", "match", "0/0") 36 if err != nil { 37 return "", errors.Trace(err) 38 } 39 if !strings.HasPrefix(out, "default via") { 40 return "", errors.Errorf(`unexpected output from "ip route": %s`, out) 41 } 42 fields := strings.Fields(out) 43 return fields[2], nil 44 }