blob: 9619efbbf63bd48fce6909c9222b204c35274ac2 [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"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00008 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00009 "strings"
Matus Fabian5409d332024-05-28 13:39:13 +020010 "time"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000011)
12
Maros Ondrejickae7625d02023-02-28 16:55:01 +010013const networkTopologyDir string = "topo-network/"
14const containerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000015
16type Stanza struct {
17 content string
18 pad int
19}
20
21type ActionResult struct {
22 Err error
23 Desc string
24 ErrOutput string
25 StdOutput string
26}
27
28type JsonResult struct {
29 Code int
30 Desc string
31 ErrOutput string
32 StdOutput string
33}
34
Adrian Villin4677d922024-06-14 09:32:39 +020035func AssertFileSize(f1, f2 string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000036 fi1, err := os.Stat(f1)
37 if err != nil {
38 return err
39 }
40
41 fi2, err1 := os.Stat(f2)
42 if err1 != nil {
43 return err1
44 }
45
46 if fi1.Size() != fi2.Size() {
47 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
48 }
49 return nil
50}
51
Adrian Villin4677d922024-06-14 09:32:39 +020052func (c *Stanza) NewStanza(name string) *Stanza {
53 c.Append("\n" + name + " {")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000054 c.pad += 2
55 return c
56}
57
Adrian Villin4677d922024-06-14 09:32:39 +020058func (c *Stanza) Append(name string) *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000059 c.content += strings.Repeat(" ", c.pad)
60 c.content += name + "\n"
61 return c
62}
63
Adrian Villin4677d922024-06-14 09:32:39 +020064func (c *Stanza) Close() *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000065 c.content += "}\n"
66 c.pad -= 2
67 return c
68}
69
Adrian Villin4677d922024-06-14 09:32:39 +020070func (s *Stanza) ToString() string {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000071 return s.content
72}
73
Adrian Villin4677d922024-06-14 09:32:39 +020074func (s *Stanza) SaveToFile(fileName string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000075 fo, err := os.Create(fileName)
76 if err != nil {
77 return err
78 }
79 defer fo.Close()
80
81 _, err = io.Copy(fo, strings.NewReader(s.content))
82 return err
83}
Matus Fabian5409d332024-05-28 13:39:13 +020084
Adrian Villin4677d922024-06-14 09:32:39 +020085// NewHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds.
86func NewHttpClient() *http.Client {
Matus Fabian5409d332024-05-28 13:39:13 +020087 transport := http.DefaultTransport
88 transport.(*http.Transport).Proxy = nil
89 transport.(*http.Transport).DisableKeepAlives = true
90 client := &http.Client{
91 Transport: transport,
92 Timeout: time.Second * 30,
93 CheckRedirect: func(req *http.Request, via []*http.Request) error {
94 return http.ErrUseLastResponse
95 }}
96 return client
97}
Matus Fabian82ad9662024-06-04 19:00:00 +020098
Adrian Villin4677d922024-06-14 09:32:39 +020099func TcpSendReceive(address, data string) (string, error) {
Matus Fabian82ad9662024-06-04 19:00:00 +0200100 conn, err := net.DialTimeout("tcp", address, time.Second*30)
101 if err != nil {
102 return "", err
103 }
104 defer conn.Close()
105 err = conn.SetDeadline(time.Now().Add(time.Second * 30))
106 if err != nil {
107 return "", err
108 }
109 _, err = conn.Write([]byte(data))
110 if err != nil {
111 return "", err
112 }
113 reply := make([]byte, 1024)
114 _, err = conn.Read(reply)
115 if err != nil {
116 return "", err
117 }
118 return string(reply), nil
119}