github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/diag.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package libvirttools 18 19 import ( 20 "fmt" 21 22 libvirtxml "github.com/libvirt/libvirt-go-xml" 23 24 "github.com/Mirantis/virtlet/pkg/diag" 25 "github.com/Mirantis/virtlet/pkg/virt" 26 ) 27 28 // LibvirtDiagSource dumps libvirt domains, storage pools and storage 29 // volumes as a set of XML files. 30 type LibvirtDiagSource struct { 31 domainConn virt.DomainConnection 32 storageConn virt.StorageConnection 33 } 34 35 var _ diag.Source = &LibvirtDiagSource{} 36 37 // NewLibvirtDiagSource creates a new LibvirtDiagSource. 38 func NewLibvirtDiagSource(domainConn virt.DomainConnection, storageConn virt.StorageConnection) *LibvirtDiagSource { 39 return &LibvirtDiagSource{ 40 domainConn: domainConn, 41 storageConn: storageConn, 42 } 43 } 44 45 func (s *LibvirtDiagSource) listDomains(dr *diag.Result) error { 46 domains, err := s.domainConn.ListDomains() 47 if err != nil { 48 return fmt.Errorf("error listing domains: %v", err) 49 } 50 for _, d := range domains { 51 xml, err := d.XML() 52 if err != nil { 53 return fmt.Errorf("error getting domain xml: %v", err) 54 } 55 if err := addLibvirtXMLToDiagResult("domain", xml.Name, xml, dr); err != nil { 56 return err 57 } 58 } 59 return nil 60 } 61 62 func (s *LibvirtDiagSource) listPoolsAndVolumes(dr *diag.Result) error { 63 pools, err := s.storageConn.ListPools() 64 if err != nil { 65 return fmt.Errorf("error listing pools: %v", err) 66 } 67 for _, p := range pools { 68 xml, err := p.XML() 69 if err != nil { 70 return fmt.Errorf("error getting domain xml: %v", err) 71 } 72 if err := addLibvirtXMLToDiagResult("pool", xml.Name, xml, dr); err != nil { 73 return err 74 } 75 if err := s.listVolumes(p, dr); err != nil { 76 return err 77 } 78 } 79 return nil 80 } 81 82 func (s *LibvirtDiagSource) listVolumes(p virt.StoragePool, dr *diag.Result) error { 83 vols, err := p.ListVolumes() 84 if err != nil { 85 return fmt.Errorf("error listing volumes: %v", err) 86 } 87 for _, v := range vols { 88 xml, err := v.XML() 89 if err != nil { 90 return fmt.Errorf("error getting domain xml: %v", err) 91 } 92 if err := addLibvirtXMLToDiagResult("volume", xml.Name, xml, dr); err != nil { 93 return err 94 } 95 } 96 return nil 97 } 98 99 // DiagnosticInfo implements DiagnosticInfo method of the Source 100 // interface. 101 func (s *LibvirtDiagSource) DiagnosticInfo() (diag.Result, error) { 102 dr := diag.Result{ 103 IsDir: true, 104 Children: make(map[string]diag.Result), 105 } 106 err := s.listDomains(&dr) 107 if err == nil { 108 err = s.listPoolsAndVolumes(&dr) 109 } 110 if err != nil { 111 return diag.Result{}, err 112 } 113 return dr, nil 114 } 115 116 func addLibvirtXMLToDiagResult(objType, name string, xml libvirtxml.Document, dr *diag.Result) error { 117 out, err := xml.Marshal() 118 if err != nil { 119 return fmt.Errorf("error marshalling the pool to xml: %v", err) 120 } 121 fileName := fmt.Sprintf("%s-%s", objType, name) 122 dr.Children[fileName] = diag.Result{ 123 Name: fileName, 124 Ext: "xml", 125 Data: out, 126 } 127 return nil 128 }