blob: 05b7b36548789e06a285d58d6d6ed5c2f2652024 [file] [log] [blame]
Adrian Villin4677d922024-06-14 09:32:39 +02001package hst
Filip Tehlar229f5fc2022-08-09 14:44:47 +00002
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "fmt"
5 "io"
Matus Fabian82ad9662024-06-04 19:00:00 +02006 "net"
Matus Fabian5409d332024-05-28 13:39:13 +02007 "net/http"
Matus Fabian8ca6ce62024-06-20 17:08:26 +02008 "net/http/httputil"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00009 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000010 "strings"
Matus Fabian5409d332024-05-28 13:39:13 +020011 "time"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000012)
13
Maros Ondrejickae7625d02023-02-28 16:55:01 +010014const networkTopologyDir string = "topo-network/"
15const containerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000016
17type Stanza struct {
18 content string
19 pad int
20}
21
22type ActionResult struct {
23 Err error
24 Desc string
25 ErrOutput string
26 StdOutput string
27}
28
29type JsonResult struct {
30 Code int
31 Desc string
32 ErrOutput string
33 StdOutput string
34}
35
Adrian Villin4677d922024-06-14 09:32:39 +020036func AssertFileSize(f1, f2 string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000037 fi1, err := os.Stat(f1)
38 if err != nil {
39 return err
40 }
41
42 fi2, err1 := os.Stat(f2)
43 if err1 != nil {
44 return err1
45 }
46
47 if fi1.Size() != fi2.Size() {
48 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
49 }
50 return nil
51}
52
Adrian Villin4677d922024-06-14 09:32:39 +020053func (c *Stanza) NewStanza(name string) *Stanza {
54 c.Append("\n" + name + " {")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000055 c.pad += 2
56 return c
57}
58
Adrian Villin4677d922024-06-14 09:32:39 +020059func (c *Stanza) Append(name string) *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000060 c.content += strings.Repeat(" ", c.pad)
61 c.content += name + "\n"
62 return c
63}
64
Adrian Villin4677d922024-06-14 09:32:39 +020065func (c *Stanza) Close() *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000066 c.content += "}\n"
67 c.pad -= 2
68 return c
69}
70
Adrian Villin4677d922024-06-14 09:32:39 +020071func (s *Stanza) ToString() string {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000072 return s.content
73}
74
Adrian Villin4677d922024-06-14 09:32:39 +020075func (s *Stanza) SaveToFile(fileName string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000076 fo, err := os.Create(fileName)
77 if err != nil {
78 return err
79 }
80 defer fo.Close()
81
82 _, err = io.Copy(fo, strings.NewReader(s.content))
83 return err
84}
Matus Fabian5409d332024-05-28 13:39:13 +020085
Adrian Villin4677d922024-06-14 09:32:39 +020086// NewHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds.
87func NewHttpClient() *http.Client {
Matus Fabian5409d332024-05-28 13:39:13 +020088 transport := http.DefaultTransport
89 transport.(*http.Transport).Proxy = nil
90 transport.(*http.Transport).DisableKeepAlives = true
91 client := &http.Client{
92 Transport: transport,
93 Timeout: time.Second * 30,
94 CheckRedirect: func(req *http.Request, via []*http.Request) error {
95 return http.ErrUseLastResponse
96 }}
97 return client
98}
Matus Fabian82ad9662024-06-04 19:00:00 +020099
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200100func DumpHttpResp(resp *http.Response, body bool) string {
101 dump, err := httputil.DumpResponse(resp, body)
102 if err != nil {
103 return ""
104 }
105 return string(dump)
106}
107
Adrian Villin4677d922024-06-14 09:32:39 +0200108func TcpSendReceive(address, data string) (string, error) {
Matus Fabian82ad9662024-06-04 19:00:00 +0200109 conn, err := net.DialTimeout("tcp", address, time.Second*30)
110 if err != nil {
111 return "", err
112 }
113 defer conn.Close()
114 err = conn.SetDeadline(time.Now().Add(time.Second * 30))
115 if err != nil {
116 return "", err
117 }
118 _, err = conn.Write([]byte(data))
119 if err != nil {
120 return "", err
121 }
122 reply := make([]byte, 1024)
123 _, err = conn.Read(reply)
124 if err != nil {
125 return "", err
126 }
127 return string(reply), nil
128}