|
16 | 16 |
|
17 | 17 | package resource
|
18 | 18 |
|
19 |
| -func (api EBSDiscoveryClient) ConfirmEBSVolumeIsAttached(deviceName, volumeID string) error { |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os/exec" |
| 23 | + "strings" |
| 24 | + |
| 25 | + log "github.com/cihub/seelog" |
| 26 | + "github.com/pkg/errors" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + diskNumberOffset = 0 |
| 31 | + volumeIdOffset = 1 |
| 32 | + deviceNameOffset = 2 |
| 33 | + volumeInfoLength = 3 |
| 34 | +) |
| 35 | + |
| 36 | +func (api *EBSDiscoveryClient) ConfirmEBSVolumeIsAttached(deviceName, volumeID string) error { |
| 37 | + ctxWithTimeout, cancel := context.WithTimeout(api.ctx, ebsVolumeDiscoveryTimeout) |
| 38 | + defer cancel() |
| 39 | + output, err := exec.CommandContext(ctxWithTimeout, |
| 40 | + "C:\\PROGRAMDATA\\Amazon\\Tools\\ebsnvme-id.exe").CombinedOutput() |
| 41 | + if err != nil { |
| 42 | + return errors.Wrapf(err, "failed to run ebsnvme-id.exe: %s", string(output)) |
| 43 | + } |
| 44 | + |
| 45 | + _, err = parseExecutableOutput(output, volumeID, deviceName) |
| 46 | + if err != nil { |
| 47 | + return errors.Wrapf(err, "failed to parse ebsnvme-id.exe output for volumeID: %s and deviceName: %s", |
| 48 | + volumeID, deviceName) |
| 49 | + } |
| 50 | + |
| 51 | + log.Info(fmt.Sprintf("found volume with volumeID: %s and deviceName: %s", volumeID, deviceName)) |
| 52 | + |
20 | 53 | return nil
|
21 | 54 | }
|
| 55 | + |
| 56 | +// parseExecutableOutput parses the output of `ebsnvme-id.exe` and returns the volumeId. |
| 57 | +func parseExecutableOutput(output []byte, candidateVolumeId string, candidateDeviceName string) (string, error) { |
| 58 | + /* The output of the ebsnvme-id.exe is emitted like the following: |
| 59 | + Disk Number: 0 |
| 60 | + Volume ID: vol-0a1234f340444abcd |
| 61 | + Device Name: sda1 |
| 62 | +
|
| 63 | + Disk Number: 1 |
| 64 | + Volume ID: vol-abcdef1234567890a |
| 65 | + Device Name: /dev/sdf */ |
| 66 | + |
| 67 | + out := string(output) |
| 68 | + // Replace double line with a single line and split based on single line |
| 69 | + volumeInfo := strings.Split(strings.Replace(string(out), "\r\n\r\n", "\r\n", -1), "\r\n") |
| 70 | + |
| 71 | + if len(volumeInfo) < volumeInfoLength { |
| 72 | + return "", errors.New("cannot find the volume ID. Encountered error message: " + out) |
| 73 | + } |
| 74 | + |
| 75 | + //Read every 3 lines of disk information |
| 76 | + for volumeIndex := 0; volumeIndex <= len(volumeInfo)-volumeInfoLength; volumeIndex = volumeIndex + volumeInfoLength { |
| 77 | + _, volumeId, deviceName, err := parseSet(volumeInfo[volumeIndex : volumeIndex+volumeInfoLength]) |
| 78 | + if err != nil { |
| 79 | + return "", errors.Wrapf(err, "failed to parse the output for volumeID: %s and deviceName: %s. "+ |
| 80 | + "Output:%s", candidateVolumeId, candidateDeviceName, out) |
| 81 | + } |
| 82 | + |
| 83 | + if volumeId == candidateVolumeId && deviceName == candidateDeviceName { |
| 84 | + return volumeId, nil |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + return "", errors.New("cannot find the volume ID:" + candidateVolumeId) |
| 90 | +} |
| 91 | + |
| 92 | +// parseSet parses the single volume information that is 3 lines long |
| 93 | +func parseSet(lines []string) (string, string, string, error) { |
| 94 | + if len(lines) != 3 { |
| 95 | + return "", "", "", errors.New("the number of entries in the volume information is insufficient to parse. Expected 3 lines") |
| 96 | + } |
| 97 | + |
| 98 | + diskNumber, err := parseValue(lines[diskNumberOffset], "Disk Number:") |
| 99 | + if err != nil { |
| 100 | + return "", "", "", err |
| 101 | + } |
| 102 | + volumeId, err := parseValue(lines[volumeIdOffset], "Volume ID:") |
| 103 | + if err != nil { |
| 104 | + return "", "", "", err |
| 105 | + } |
| 106 | + deviceName, err := parseValue(lines[deviceNameOffset], "Device Name:") |
| 107 | + if err != nil { |
| 108 | + return "", "", "", err |
| 109 | + } |
| 110 | + return diskNumber, volumeId, deviceName, nil |
| 111 | +} |
| 112 | + |
| 113 | +// parseValue looks for the volume information identifier and replaces it to return just the value |
| 114 | +func parseValue(inputBuffer string, stringToTrim string) (string, error) { |
| 115 | + // if the input buffer doesn't have the identifier for the information, return an error |
| 116 | + if !strings.Contains(inputBuffer, stringToTrim) { |
| 117 | + return "", errors.New("output buffer was missing the string:" + stringToTrim) |
| 118 | + } |
| 119 | + |
| 120 | + return strings.TrimSpace(strings.Replace(inputBuffer, stringToTrim, "", -1)), nil |
| 121 | +} |
0 commit comments