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