-
Notifications
You must be signed in to change notification settings - Fork 907
/
main_init_dump.go
96 lines (75 loc) · 2.61 KB
/
main_init_dump.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
yaml "gopkg.in/yaml.v2"
"github.com/canonical/lxd/client"
"github.com/canonical/lxd/shared/api"
)
func (c *cmdInit) RunDump(d lxd.InstanceServer) error {
currentServer, _, err := d.GetServer()
if err != nil {
return fmt.Errorf("Failed to retrieve current server configuration: %w", err)
}
var config api.InitLocalPreseed
config.Config = currentServer.Config
// Only retrieve networks in the default project as the preseed format doesn't support creating
// projects at this time.
networks, err := d.UseProject(api.ProjectDefaultName).GetNetworks()
if err != nil {
return fmt.Errorf("Failed to retrieve current server network configuration for project %q: %w", api.ProjectDefaultName, err)
}
for _, network := range networks {
// Only list managed networks.
if !network.Managed {
continue
}
networksPost := api.InitNetworksProjectPost{}
networksPost.Config = network.Config
networksPost.Description = network.Description
networksPost.Name = network.Name
networksPost.Type = network.Type
networksPost.Project = api.ProjectDefaultName
config.Networks = append(config.Networks, networksPost)
}
storagePools, err := d.GetStoragePools()
if err != nil {
return fmt.Errorf("Failed to retrieve current server configuration: %w", err)
}
for _, storagePool := range storagePools {
storagePoolsPost := api.StoragePoolsPost{}
storagePoolsPost.Config = storagePool.Config
storagePoolsPost.Description = storagePool.Description
storagePoolsPost.Name = storagePool.Name
storagePoolsPost.Driver = storagePool.Driver
config.StoragePools = append(config.StoragePools, storagePoolsPost)
}
profiles, err := d.GetProfiles()
if err != nil {
return fmt.Errorf("Failed to retrieve current server configuration: %w", err)
}
for _, profile := range profiles {
profilesPost := api.ProfilesPost{}
profilesPost.Config = profile.Config
profilesPost.Description = profile.Description
profilesPost.Devices = profile.Devices
profilesPost.Name = profile.Name
config.Profiles = append(config.Profiles, profilesPost)
}
projects, err := d.GetProjects()
if err != nil {
return fmt.Errorf("Failed to retrieve current server configuration: %w", err)
}
for _, project := range projects {
projectsPost := api.ProjectsPost{}
projectsPost.Config = project.Config
projectsPost.Description = project.Description
projectsPost.Name = project.Name
config.Projects = append(config.Projects, projectsPost)
}
out, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("Failed to retrieve current server configuration: %w", err)
}
fmt.Printf("%s\n", out)
return nil
}