2022-10-17 07:24:07 +00:00
|
|
|
package hubtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-07 11:21:04 +00:00
|
|
|
"net"
|
2022-10-17 07:24:07 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-12-07 11:21:04 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-10-17 07:24:07 +00:00
|
|
|
)
|
|
|
|
|
2024-01-15 08:16:03 +00:00
|
|
|
func IsAlive(target string) (bool, error) {
|
|
|
|
start := time.Now()
|
|
|
|
for {
|
|
|
|
conn, err := net.Dial("tcp", target)
|
|
|
|
if err == nil {
|
|
|
|
log.Debugf("'%s' is up after %s", target, time.Since(start))
|
|
|
|
conn.Close()
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
if time.Since(start) > 10*time.Second {
|
|
|
|
return false, fmt.Errorf("took more than 10s for %s to be available", target)
|
|
|
|
}
|
2023-10-16 07:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 09:17:02 +00:00
|
|
|
func Copy(src string, dst string) error {
|
|
|
|
content, err := os.ReadFile(src)
|
2022-10-17 07:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
err = os.WriteFile(dst, content, 0o644)
|
2022-10-17 07:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-03 09:17:02 +00:00
|
|
|
|
2022-10-17 07:24:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPathNotContained returns an error if 'subpath' is inside 'path'
|
|
|
|
func checkPathNotContained(path string, subpath string) error {
|
|
|
|
absPath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
absSubPath, err := filepath.Abs(subpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
current := absSubPath
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2022-10-17 07:24:07 +00:00
|
|
|
for {
|
|
|
|
if current == absPath {
|
|
|
|
return fmt.Errorf("cannot copy a folder onto itself")
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2022-10-17 07:24:07 +00:00
|
|
|
up := filepath.Dir(current)
|
|
|
|
if current == up {
|
|
|
|
break
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2022-10-17 07:24:07 +00:00
|
|
|
current = up
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2022-10-17 07:24:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CopyDir(src string, dest string) error {
|
|
|
|
err := checkPathNotContained(src, dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-03 09:17:02 +00:00
|
|
|
|
2022-10-17 07:24:07 +00:00
|
|
|
if !file.IsDir() {
|
|
|
|
return fmt.Errorf("Source " + file.Name() + " is not a directory!")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(dest, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := os.ReadDir(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDir() {
|
2023-10-03 09:17:02 +00:00
|
|
|
if err = CopyDir(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())); err != nil {
|
2022-10-17 07:24:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-03 09:17:02 +00:00
|
|
|
} else {
|
|
|
|
if err = Copy(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())); err != nil {
|
2022-10-17 07:24:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|