Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1 | package hst |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 2 | |
| 3 | import ( |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 4 | "fmt" |
| 5 | "io" |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 6 | "net" |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 7 | "net/http" |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 8 | "net/http/httputil" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 9 | "os" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 10 | "strings" |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 11 | "time" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 12 | ) |
| 13 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 14 | const networkTopologyDir string = "topo-network/" |
| 15 | const containerTopologyDir string = "topo-containers/" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 16 | |
| 17 | type Stanza struct { |
| 18 | content string |
| 19 | pad int |
| 20 | } |
| 21 | |
| 22 | type ActionResult struct { |
| 23 | Err error |
| 24 | Desc string |
| 25 | ErrOutput string |
| 26 | StdOutput string |
| 27 | } |
| 28 | |
| 29 | type JsonResult struct { |
| 30 | Code int |
| 31 | Desc string |
| 32 | ErrOutput string |
| 33 | StdOutput string |
| 34 | } |
| 35 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 36 | func AssertFileSize(f1, f2 string) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 37 | 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 Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 53 | func (c *Stanza) NewStanza(name string) *Stanza { |
| 54 | c.Append("\n" + name + " {") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 55 | c.pad += 2 |
| 56 | return c |
| 57 | } |
| 58 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 59 | func (c *Stanza) Append(name string) *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 60 | c.content += strings.Repeat(" ", c.pad) |
| 61 | c.content += name + "\n" |
| 62 | return c |
| 63 | } |
| 64 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 65 | func (c *Stanza) Close() *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 66 | c.content += "}\n" |
| 67 | c.pad -= 2 |
| 68 | return c |
| 69 | } |
| 70 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 71 | func (s *Stanza) ToString() string { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 72 | return s.content |
| 73 | } |
| 74 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 75 | func (s *Stanza) SaveToFile(fileName string) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 76 | 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 Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 85 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 86 | // NewHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds. |
| 87 | func NewHttpClient() *http.Client { |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 88 | 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 Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 99 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 100 | func 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 Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 108 | func TcpSendReceive(address, data string) (string, error) { |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 109 | 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 | } |