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