This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 435
/
block_volume.go
356 lines (302 loc) · 10.2 KB
/
block_volume.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// Copyright (c) 2017 The heketi Authors
//
// This file is licensed to you under your choice of the GNU Lesser
// General Public License, version 3 or any later version (LGPLv3 or
// later), or the GNU General Public License, version 2 (GPLv2), in all
// cases as published by the Free Software Foundation.
//
package cmdexec
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"unicode"
"github.com/lpabon/godbc"
"github.com/heketi/heketi/executors"
rex "github.com/heketi/heketi/pkg/remoteexec"
)
func (s *CmdExecutor) BlockVolumeCreate(host string,
volume *executors.BlockVolumeRequest) (*executors.BlockVolumeInfo, error) {
godbc.Require(volume != nil)
godbc.Require(host != "")
godbc.Require(volume.Name != "")
type CliOutput struct {
Iqn string `json:"IQN"`
Username string `json:"USERNAME"`
Password string `json:"PASSWORD"`
Portal []string `json:"PORTAL(S)"`
Result string `json:"RESULT"`
ErrCode int `json:"errCode"`
ErrMsg string `json:"errMsg"`
}
var auth_set string
if volume.Auth {
auth_set = "enable"
} else {
auth_set = "disable"
}
cmd := fmt.Sprintf(
"gluster-block create %v/%v ha %v auth %v prealloc %v %v %vGiB --json",
volume.GlusterVolumeName,
volume.Name,
volume.Hacount,
auth_set,
s.BlockVolumeDefaultPrealloc(),
strings.Join(volume.BlockHosts, ","),
volume.Size)
// Initialize the commands with the create command
commands := []string{cmd}
// Execute command
results, err := s.RemoteExecutor.ExecCommands(host, rex.ToCmds(commands), 10)
if err != nil {
return nil, err
}
output := results[0].Output
if output == "" {
output = results[0].ErrOutput
}
var blockVolumeCreate CliOutput
err = json.Unmarshal([]byte(output), &blockVolumeCreate)
if err != nil {
logger.Warning("Unable to parse gluster-block output [%v]: %v",
output, err)
err = fmt.Errorf(
"Unparsable error during block volume create: %v",
output)
} else if blockVolumeCreate.Result == "FAIL" {
// the fail flag was set in the output json
err = fmt.Errorf("Failed to create block volume: %v",
blockVolumeCreate.ErrMsg)
} else if !results.Ok() {
// the fail flag is not set but the command still
// exited non-zero for some reason
err = fmt.Errorf("Failed to create block volume: %v",
results[0].Error())
}
// if any of the cases above set err, log it and return
if err != nil {
logger.LogError("%v", err)
return nil, err
}
var blockVolumeInfo executors.BlockVolumeInfo
blockVolumeInfo.BlockHosts = volume.BlockHosts // TODO: split blockVolumeCreate.Portal into here instead of using request data
blockVolumeInfo.GlusterNode = volume.GlusterNode
blockVolumeInfo.GlusterVolumeName = volume.GlusterVolumeName
blockVolumeInfo.Hacount = volume.Hacount
blockVolumeInfo.Iqn = blockVolumeCreate.Iqn
blockVolumeInfo.Name = volume.Name
blockVolumeInfo.Size = volume.Size
blockVolumeInfo.Username = blockVolumeCreate.Username
blockVolumeInfo.Password = blockVolumeCreate.Password
return &blockVolumeInfo, nil
}
func (s *CmdExecutor) BlockVolumeDestroy(host string, blockHostingVolumeName string, blockVolumeName string) error {
godbc.Require(host != "")
godbc.Require(blockHostingVolumeName != "")
godbc.Require(blockVolumeName != "")
commands := []string{
fmt.Sprintf("gluster-block delete %v/%v --json",
blockHostingVolumeName, blockVolumeName),
}
res, err := s.RemoteExecutor.ExecCommands(host, rex.ToCmds(commands), 10)
if err != nil {
// non-command error conditions
return err
}
r := res[0]
errOutput := r.ErrOutput
if errOutput == "" {
errOutput = r.Output
}
if errOutput == "" {
// we ought to have some output but we don't
return r.Err
}
type CliOutput struct {
Result string `json:"RESULT"`
ResultOnHost string `json:"Result"`
ErrCode int `json:"errCode"`
ErrMsg string `json:"errMsg"`
}
var blockVolumeDelete CliOutput
if e := json.Unmarshal([]byte(errOutput), &blockVolumeDelete); e != nil {
logger.LogError("Failed to unmarshal response from block "+
"volume delete for volume %v", blockVolumeName)
if r.Err != nil {
return logger.Err(r.Err)
}
return logger.LogError("Unable to parse output from block "+
"volume delete: %v", e)
}
if blockVolumeDelete.Result == "FAIL" {
errHas := func(s string) bool {
return strings.Contains(blockVolumeDelete.ErrMsg, s)
}
if (errHas("doesn't exist") && errHas(blockVolumeName)) ||
(errHas("does not exist") && errHas(blockHostingVolumeName)) {
return &executors.VolumeDoesNotExistErr{Name: blockVolumeName}
}
return logger.LogError("%v", blockVolumeDelete.ErrMsg)
}
return r.Err
}
func (c *CmdExecutor) ListBlockVolumes(host string, blockhostingvolume string) ([]string, error) {
godbc.Require(host != "")
godbc.Require(blockhostingvolume != "")
commands := []string{fmt.Sprintf("gluster-block list %v --json", blockhostingvolume)}
results, err := c.RemoteExecutor.ExecCommands(host, rex.ToCmds(commands), 10)
if err := rex.AnyError(results, err); err != nil {
logger.Err(err)
return nil, fmt.Errorf("unable to list blockvolumes on block hosting volume %v : %v", blockhostingvolume, err)
}
type BlockVolumeListOutput struct {
Blocks []string `json:"blocks"`
RESULT string `json:"RESULT"`
}
var blockVolumeList BlockVolumeListOutput
err = json.Unmarshal([]byte(results[0].Output), &blockVolumeList)
if err != nil {
logger.Err(err)
return nil, fmt.Errorf("Unable to get the block volume list for block hosting volume %v : %v", blockhostingvolume, err)
}
return blockVolumeList.Blocks, nil
}
const (
GIGABYTE = 1 << (10 * iota)
TERABYTE
PETABYTE
EXABYTE
)
func ToGigaBytes(s string) (int, error) {
s = strings.Replace(s, " ", "", -1)
s = strings.ToUpper(s)
i := strings.IndexFunc(s, unicode.IsLetter)
if i == -1 {
return 0, fmt.Errorf("Not a valid Size")
}
bytesString, multiple := s[:i], s[i:]
bytes, err := strconv.ParseFloat(bytesString, 64)
if err != nil || bytes < 0 {
return 0, fmt.Errorf("Not a valid Size")
}
switch multiple {
case "E", "EB", "EIB":
return int(bytes * EXABYTE), nil
case "P", "PB", "PIB":
return int(bytes * PETABYTE), nil
case "T", "TB", "TIB":
return int(bytes * TERABYTE), nil
case "G", "GB", "GIB":
return int(bytes), nil
default:
return 0, fmt.Errorf("Not a valid Size")
}
}
func (c *CmdExecutor) InfoBlockVolume(host string, blockhostingvolume string,
blockVolumeName string) (*executors.BlockVolumeInfo, error) {
godbc.Require(host != "")
godbc.Require(blockhostingvolume != "")
godbc.Require(blockVolumeName != "")
type CliOutput struct {
BlockName string `json:"NAME"`
BlockHostingVolumeName string `json:"VOLUME"`
Gbid string `json:"GBID"`
Size string `json:"SIZE"`
Ha int `json:"HA"`
Password string `json:"PASSWORD"`
Portal []string `json:"EXPORTED ON"`
}
commands := []string{fmt.Sprintf("gluster-block info %v/%v --json", blockhostingvolume, blockVolumeName)}
results, err := c.RemoteExecutor.ExecCommands(host, rex.ToCmds(commands), 10)
if err := rex.AnyError(results, err); err != nil {
logger.Err(err)
return nil, fmt.Errorf("unable to get info of blockvolume %v on block hosting volume %v : %v",
blockVolumeName, blockhostingvolume, err)
}
output := results[0].Output
if output == "" {
output = results[0].ErrOutput
}
var blockVolumeInfoExec CliOutput
err = json.Unmarshal([]byte(output), &blockVolumeInfoExec)
if err != nil {
logger.Warning("Unable to parse gluster-block info output [%v]: %v",
output, err)
err = fmt.Errorf("Unparsable error during block volume info: %v",
output)
}
// if any of the cases above set err, log it and return
if err != nil {
logger.LogError("%v", err)
return nil, err
}
var blockVolumeInfo executors.BlockVolumeInfo
blockVolumeInfo.BlockHosts = blockVolumeInfoExec.Portal
blockVolumeInfo.GlusterNode = host
blockVolumeInfo.GlusterVolumeName = blockVolumeInfoExec.BlockHostingVolumeName
blockVolumeInfo.Hacount = blockVolumeInfoExec.Ha
blockVolumeInfo.Iqn = "iqn.2016-12.org.gluster-block:" + blockVolumeInfoExec.Gbid
blockVolumeInfo.Name = blockVolumeInfoExec.BlockName
blockVolumeInfo.Size, err = ToGigaBytes(blockVolumeInfoExec.Size)
if err != nil {
logger.LogError("Error: %v : %v", err, blockVolumeInfoExec.Size)
return nil, err
}
blockVolumeInfo.Username = blockVolumeInfoExec.Gbid
blockVolumeInfo.Password = blockVolumeInfoExec.Password
return &blockVolumeInfo, nil
}
func (s *CmdExecutor) BlockVolumeExpand(host string, blockHostingVolumeName string, blockVolumeName string, newSize int) error {
godbc.Require(host != "")
godbc.Require(blockHostingVolumeName != "")
godbc.Require(blockVolumeName != "")
godbc.Require(newSize > 0)
commands := []string{
fmt.Sprintf("gluster-block modify %v/%v size %vGiB --json",
blockHostingVolumeName, blockVolumeName, newSize),
}
results, err := s.RemoteExecutor.ExecCommands(host, rex.ToCmds(commands), 10)
if err != nil {
// non-command error conditions
return err
}
output := results[0].Output
if output == "" {
output = results[0].ErrOutput
}
type CliOutput struct {
Iqn string `json:"IQN"`
Size string `json:"SIZE"`
SuccessOn []string `json:"SUCCESSFUL ON"`
FailedOn []string `json:"FAILED ON"`
SkippedOn []string `json:"SKIPPED ON"`
Result string `json:"RESULT"`
ErrCod int `json:"errCode"`
ErrMsg string `json:"errMsg"`
}
var blockVolumeExpand CliOutput
err = json.Unmarshal([]byte(output), &blockVolumeExpand)
if err != nil {
logger.Warning("Unable to parse gluster-block output [%v]: %v",
output, err)
err = fmt.Errorf("Unparsable error during block volume expand: %v",
output)
} else if blockVolumeExpand.Result == "FAIL" {
// the fail flag was set in the output json
err = fmt.Errorf("Failed to expand block volume: %v",
blockVolumeExpand.ErrMsg)
} else if !results.Ok() {
// the fail flag is not set but the command still
// exited non-zero for some reason
err = fmt.Errorf("Failed to expand block volume: %v",
results[0].Error())
}
// if any of the cases above set err, log it and return
if err != nil {
logger.LogError("%v", err)
return err
}
return nil
}