File: interface_tool.go

package info (click to toggle)
golang-github-bougou-go-ipmi 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,900 kB
  • sloc: makefile: 38
file content (108 lines) | stat: -rw-r--r-- 2,625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ipmi

import (
	"bytes"
	"context"
	"encoding/hex"
	"fmt"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
)

var (
	toolError = regexp.MustCompile(`^Unable to send RAW command \(channel=0x(?P<channel>[0-9a-fA-F]+) netfn=0x(?P<netfn>[0-9a-fA-F]+) lun=0x(?P<lun>[0-9a-fA-F]+) cmd=0x(?P<cmd>[0-9a-fA-F]+) rsp=0x(?P<rsp>[0-9a-fA-F]+)\): (?P<message>.*)`)
)

// ConnectTool try to initialize the client.
func (c *Client) ConnectTool(ctx context.Context, devnum int32) error {
	return nil
}

// closeTool closes the ipmi dev file.
func (c *Client) closeTool(ctx context.Context) error {
	return nil
}

func (c *Client) exchangeTool(ctx context.Context, request Request, response Response) error {
	data := request.Pack()
	msg := make([]byte, 2+len(data))
	msg[0] = uint8(request.Command().NetFn)
	msg[1] = uint8(request.Command().ID)
	copy(msg[2:], data)

	args := append([]string{"raw"}, rawEncode(msg)...)

	path := c.Host
	if path == "" {
		path = "ipmitool"
	}

	cmd := exec.Command(path, args...)
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	c.Debugf(">>> Run cmd: \n>>> %s\n", cmd.String())
	err := cmd.Run()
	if err != nil {
		if bytes.HasPrefix(stderr.Bytes(), []byte("Unable to send RAW command")) {
			submatches := toolError.FindSubmatch(stderr.Bytes())
			if len(submatches) == 7 && len(submatches[5]) == 2 {
				code, err := strconv.ParseUint(string(submatches[5]), 16, 0)
				if err != nil {
					return fmt.Errorf("CompletionCode parse failed, err: %w", err)
				}
				return &ResponseError{
					completionCode: CompletionCode(uint8(code)),
					description:    fmt.Sprintf("Raw command failed, err: %s", string(submatches[6])),
				}
			}
		}
		return fmt.Errorf("ipmitool run failed, err: %w", err)
	}

	output := stdout.String()
	resp, err := rawDecode(strings.TrimSpace(output))
	if err != nil {
		return fmt.Errorf("decode response failed, err: %w", err)
	}
	if err := response.Unpack(resp); err != nil {
		return fmt.Errorf("unpack response failed, err: %w", err)
	}

	return nil
}

func rawDecode(data string) ([]byte, error) {
	var buf bytes.Buffer

	data = strings.ReplaceAll(data, "\n", "")
	for _, s := range strings.Split(data, " ") {
		b, err := hex.DecodeString(s)
		if err != nil {
			return nil, err
		}

		_, err = buf.Write(b)
		if err != nil {
			return nil, err
		}
	}

	return buf.Bytes(), nil
}

func rawEncode(data []byte) []string {
	n := len(data)
	buf := make([]string, 0, n)

	// ipmitool needs every byte to be a separate argument
	for i := 0; i < n; i++ {
		buf = append(buf, "0x"+hex.EncodeToString(data[i:i+1]))
	}

	return buf
}