2020-11-17 12:48:26 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-02 10:19:27 -04:00
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
2020-11-17 12:48:26 -05:00
|
|
|
"fmt"
|
2022-08-02 10:19:27 -04:00
|
|
|
"io"
|
2020-11-17 12:48:26 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-08-02 10:19:27 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-11-17 12:48:26 -05:00
|
|
|
"time"
|
2021-12-30 03:24:44 -05:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-11-17 12:48:26 -05:00
|
|
|
)
|
|
|
|
|
2022-07-21 11:17:53 -04:00
|
|
|
func parseDate(layout, datetime string) time.Time {
|
2020-11-17 12:48:26 -05:00
|
|
|
t, err := time.Parse(layout, datetime)
|
|
|
|
if err != nil {
|
2021-12-30 03:24:44 -05:00
|
|
|
log.Errorln(err)
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
2020-11-27 02:23:59 -05:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2022-07-21 11:17:53 -04:00
|
|
|
func parseDateToString(layout, datetime, format string) string {
|
2020-11-27 02:23:59 -05:00
|
|
|
return parseDate(layout, datetime).Format(format)
|
|
|
|
}
|
|
|
|
|
2022-07-21 11:17:53 -04:00
|
|
|
func parseDateToUnix(layout, datetime string) int64 {
|
2020-11-27 02:23:59 -05:00
|
|
|
return parseDate(layout, datetime).Unix()
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func runBash(script string) string {
|
2021-12-30 03:24:44 -05:00
|
|
|
log.Debugln(script)
|
2020-11-17 12:48:26 -05:00
|
|
|
cmd := exec.Command("bash", "-c", script)
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2022-07-21 11:17:53 -04:00
|
|
|
return fmt.Sprint(err) + " : " + string(stdout)
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
return string(stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fExist(path string) bool {
|
|
|
|
var _, err = os.Stat(path)
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
} else if err != nil {
|
2022-07-21 11:17:53 -04:00
|
|
|
log.Fatalf("fExist: %s", err)
|
2020-11-17 12:48:26 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func fRead(path string) string {
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2022-07-21 11:17:53 -04:00
|
|
|
log.Warning(err)
|
|
|
|
return ""
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(content)
|
|
|
|
}
|
|
|
|
|
2022-08-02 10:19:27 -04:00
|
|
|
func fCreate(path string) error {
|
2020-11-17 12:48:26 -05:00
|
|
|
var _, err = os.Stat(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
var file, err = os.Create(path)
|
|
|
|
if err != nil {
|
2021-12-30 03:24:44 -05:00
|
|
|
log.Errorln(err)
|
2022-08-02 10:19:27 -04:00
|
|
|
return err
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
}
|
2022-08-02 10:19:27 -04:00
|
|
|
return nil
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
|
2022-08-02 10:19:27 -04:00
|
|
|
func fWrite(path, content string) error {
|
2020-11-17 12:48:26 -05:00
|
|
|
err := ioutil.WriteFile(path, []byte(content), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-08-02 10:19:27 -04:00
|
|
|
return nil
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
|
2022-08-02 10:19:27 -04:00
|
|
|
func fDelete(path string) error {
|
2020-11-17 12:48:26 -05:00
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-08-02 10:19:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fCopy(src, dst string) error {
|
|
|
|
sfi, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !sfi.Mode().IsRegular() {
|
|
|
|
// cannot copy non-regular files (e.g., directories, symlinks, devices, etc.)
|
|
|
|
return fmt.Errorf("fCopy: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
|
|
|
|
}
|
|
|
|
dfi, err := os.Stat(dst)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !(dfi.Mode().IsRegular()) {
|
|
|
|
return fmt.Errorf("fCopy: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
|
|
|
|
}
|
|
|
|
if os.SameFile(sfi, dfi) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = os.Link(src, dst); err == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
cerr := out.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if _, err = io.Copy(out, in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = out.Sync()
|
|
|
|
return err
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func fDownload(path, url string, basicAuth bool) error {
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if basicAuth {
|
|
|
|
req.SetBasicAuth(*masterBasicAuthUser, *masterBasicAuthPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2022-07-21 11:17:53 -04:00
|
|
|
log.Warnf("WARNING: Download file operation for url %s finished with status code %d\n", url, resp.StatusCode)
|
2020-11-17 12:48:26 -05:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fCreate(path)
|
|
|
|
fWrite(path, string(body))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-02 10:19:27 -04:00
|
|
|
|
|
|
|
func createArchiveFromDir(dir, path string) error {
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error writing archive %s: %s", path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
gw := gzip.NewWriter(out)
|
|
|
|
defer gw.Close()
|
|
|
|
tw := tar.NewWriter(gw)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
// Iterate over files and add them to the tar archive
|
|
|
|
for _, filePath := range files {
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Error writing archive %s: %s", path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get FileInfo about our file providing file size, mode, etc.
|
|
|
|
info, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a tar Header from the FileInfo data
|
|
|
|
header, err := tar.FileInfoHeader(info, info.Name())
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Name = strings.Replace(filePath, dir+"/", "", 1)
|
|
|
|
|
|
|
|
// Write file header to the tar archive
|
|
|
|
err = tw.WriteHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy file content to tar archive
|
|
|
|
_, err = io.Copy(tw, file)
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractFromArchive(archive, path string) error {
|
|
|
|
// Open the file which will be written into the archive
|
|
|
|
file, err := os.Open(archive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Write file header to the tar archive
|
|
|
|
uncompressedStream, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("extractFromArchive(): NewReader failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
tarReader := tar.NewReader(uncompressedStream)
|
|
|
|
|
|
|
|
for true {
|
|
|
|
header, err := tarReader.Next()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("extractFromArchive: Next() failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch header.Typeflag {
|
|
|
|
case tar.TypeDir:
|
|
|
|
if err := os.Mkdir(path+"/"+header.Name, 0755); err != nil {
|
|
|
|
log.Fatalf("extractFromArchive: Mkdir() failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
case tar.TypeReg:
|
|
|
|
outFile, err := os.Create(path + "/" + header.Name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("extractFromArchive: Create() failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(outFile, tarReader); err != nil {
|
|
|
|
log.Fatalf("extractFromArchive: Copy() failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
outFile.Close()
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Fatalf(
|
|
|
|
"extractFromArchive: uknown type: %s in %s", header.Typeflag, header.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|