mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-11 13:40:07 +00:00
CLOUD-535 Rework archive and dumper
This commit is contained in:
@@ -2,57 +2,32 @@ package archive
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Create(src string) error {
|
func TarWrite(path string, data map[string][]byte) error {
|
||||||
var buf bytes.Buffer
|
tarFile, err := os.Create(path + ".tar.gz")
|
||||||
|
if err != nil {
|
||||||
zr := gzip.NewWriter(&buf)
|
return err
|
||||||
|
}
|
||||||
|
defer tarFile.Close()
|
||||||
|
zr := gzip.NewWriter(tarFile)
|
||||||
tw := tar.NewWriter(zr)
|
tw := tar.NewWriter(zr)
|
||||||
|
defer zr.Close()
|
||||||
filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
|
defer tw.Close()
|
||||||
header, err := tar.FileInfoHeader(fi, file)
|
for name, content := range data {
|
||||||
if err != nil {
|
hdr := &tar.Header{
|
||||||
|
Name: name,
|
||||||
|
Mode: 0600,
|
||||||
|
Size: int64(len(content)),
|
||||||
|
}
|
||||||
|
if err := tw.WriteHeader(hdr); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
header.Name = filepath.ToSlash(file)
|
if _, err := tw.Write(content); err != nil {
|
||||||
|
|
||||||
err = tw.WriteHeader(header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !fi.IsDir() {
|
|
||||||
data, err := os.Open(file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := io.Copy(tw, data); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := tw.Close(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := zr.Close(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.OpenFile("./cluster-dump.tar.gzip", os.O_CREATE|os.O_RDWR, os.FileMode(777))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := io.Copy(file, &buf); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -3,9 +3,7 @@ package dumper
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
@@ -17,6 +15,7 @@ type Dumper struct {
|
|||||||
resources []string
|
resources []string
|
||||||
location string
|
location string
|
||||||
Errors map[string]string
|
Errors map[string]string
|
||||||
|
Files map[string][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// New return new Dumper object
|
// New return new Dumper object
|
||||||
@@ -37,6 +36,7 @@ func New(location string) Dumper {
|
|||||||
},
|
},
|
||||||
location: directory,
|
location: directory,
|
||||||
Errors: make(map[string]string),
|
Errors: make(map[string]string),
|
||||||
|
Files: make(map[string][]byte),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,11 +61,6 @@ func (d *Dumper) DumpCluster() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, ns := range nss.Items {
|
for _, ns := range nss.Items {
|
||||||
err = os.MkdirAll(d.location+"/"+ns.Name, 0755)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
output, err = d.runCmd("get", "pods", "-o", "json", "--namespace", ns.Name)
|
output, err = d.runCmd("get", "pods", "-o", "json", "--namespace", ns.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue // runCmd already stored this error in Dumper.Errors
|
continue // runCmd already stored this error in Dumper.Errors
|
||||||
@@ -81,20 +76,7 @@ func (d *Dumper) DumpCluster() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue // runCmd already stored this error in Dumper.Errors
|
continue // runCmd already stored this error in Dumper.Errors
|
||||||
}
|
}
|
||||||
err = os.MkdirAll(d.location+"/"+ns.Name+"/"+pod.Name, 0755)
|
d.Files[d.location+"/"+ns.Name+"/"+pod.Name+"/logs.txt"] = output
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(d.location + "/" + ns.Name + "/" + pod.Name + "/logs.txt")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = f.Write(output)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, resource := range d.resources {
|
for _, resource := range d.resources {
|
||||||
@@ -149,14 +131,7 @@ func (d *Dumper) getAndWriteToFile(name, namespace string) error {
|
|||||||
return nil // runCmd already stored this error in Dumper.Errors
|
return nil // runCmd already stored this error in Dumper.Errors
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.Create(location + "/" + name + ".yaml")
|
d.Files[location+"/"+name+".yaml"] = output
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = f.Write(output)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -175,27 +150,11 @@ func (d *Dumper) writeErrorsToFile() error {
|
|||||||
for cmd, errS := range d.Errors {
|
for cmd, errS := range d.Errors {
|
||||||
errStr += cmd + ": " + errS + "\n"
|
errStr += cmd + ": " + errS + "\n"
|
||||||
}
|
}
|
||||||
f, err := os.Create(d.location + "/errors.txt")
|
d.Files[d.location+"/errors.txt"] = []byte(errStr)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = f.WriteString(errStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDumpDir delete Dumper.location
|
|
||||||
func (d *Dumper) DeleteDumpDir() error {
|
|
||||||
if d.location == "/" {
|
|
||||||
return errors.New("don't do this, please") // just for being sure
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.RemoveAll(d.location)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetLocation return Dumper.location
|
// GetLocation return Dumper.location
|
||||||
func (d *Dumper) GetLocation() string {
|
func (d *Dumper) GetLocation() string {
|
||||||
return d.location
|
return d.location
|
||||||
|
@@ -21,9 +21,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
archive.Create(d.GetLocation())
|
err = archive.TarWrite(d.GetLocation(), d.Files)
|
||||||
|
|
||||||
err = d.DeleteDumpDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
Reference in New Issue
Block a user