/
aws_volume.go
266 lines (225 loc) · 5.96 KB
/
aws_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
//go:build aws || !onlyprovider
package aws
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/nanovms/ops/lepton"
)
// CreateVolume creates a snapshot and use it to create a volume
func (a *AWS) CreateVolume(ctx *lepton.Context, name, data, provider string) (lepton.NanosVolume, error) {
config := ctx.Config()
var sizeInGb int64
var vol lepton.NanosVolume
if config.BaseVolumeSz != "" {
size, err := lepton.GetSizeInGb(config.BaseVolumeSz)
if err != nil {
return vol, fmt.Errorf("cannot get volume size: %v", err)
}
config.BaseVolumeSz = "" // create minimum-sized local volume
sizeInGb = int64(size)
}
// Create volume
vol, err := lepton.CreateLocalVolume(config, name, data, provider)
if err != nil {
return vol, fmt.Errorf("create local volume: %v", err)
}
defer os.Remove(vol.Path)
config.CloudConfig.ImageName = vol.Name
err = a.Storage.CopyToBucket(config, vol.Path)
if err != nil {
return vol, fmt.Errorf("copy volume archive to aws bucket: %v", err)
}
bucket := config.CloudConfig.BucketName
key := vol.Name
input := &ec2.ImportSnapshotInput{
Description: aws.String("name"),
DiskContainer: &ec2.SnapshotDiskContainer{
Description: aws.String("snapshot imported"),
Format: aws.String("raw"),
UserBucket: &ec2.UserBucket{
S3Bucket: aws.String(bucket),
S3Key: aws.String(key),
},
},
}
res, err := a.ec2.ImportSnapshot(input)
if err != nil {
return vol, fmt.Errorf("import snapshot: %v", err)
}
snapshotID, err := a.waitSnapshotToBeReady(config, res.ImportTaskId)
if err != nil {
return vol, err
}
// delete the tmp s3 volume
err = a.Storage.DeleteFromBucket(config, key)
if err != nil {
return vol, err
}
// Create tags to assign to the volume
tags, _ := buildAwsTags(config.CloudConfig.Tags, name)
// Create volume from snapshot
createVolumeInput := &ec2.CreateVolumeInput{
AvailabilityZone: aws.String(config.CloudConfig.Zone),
SnapshotId: snapshotID,
TagSpecifications: []*ec2.TagSpecification{
{
ResourceType: aws.String("volume"),
Tags: tags,
},
},
}
if sizeInGb != 0 {
createVolumeInput.Size = &sizeInGb
}
_, err = a.ec2.CreateVolume(createVolumeInput)
if err != nil {
return vol, fmt.Errorf("create aws volume: %v", err)
}
return vol, nil
}
// GetAllVolumes finds and returns all volumes
func (a *AWS) GetAllVolumes(ctx *lepton.Context) (*[]lepton.NanosVolume, error) {
vols := &[]lepton.NanosVolume{}
input := &ec2.DescribeVolumesInput{
Filters: []*ec2.Filter{
{Name: aws.String("tag:CreatedBy"), Values: []*string{aws.String("ops")}},
},
}
output, err := a.ec2.DescribeVolumes(input)
if err != nil {
return nil, err
}
for _, volume := range output.Volumes {
var name string
var attachments []string
for _, tag := range volume.Tags {
if *tag.Key == "Name" {
name = *tag.Value
}
}
for _, att := range volume.Attachments {
attachments = append(attachments, *att.InstanceId)
}
vol := lepton.NanosVolume{
ID: *volume.VolumeId,
Name: name,
Status: *volume.State,
Size: strconv.Itoa(int(*volume.Size)),
Path: "",
CreatedAt: volume.CreateTime.String(),
AttachedTo: strings.Join(attachments, ";"),
}
*vols = append(*vols, vol)
}
return vols, nil
}
// DeleteVolume deletes a volume
func (a *AWS) DeleteVolume(ctx *lepton.Context, name string) error {
vol, err := a.findVolumeByName(name)
if err != nil {
return err
}
input := &ec2.DeleteVolumeInput{
VolumeId: aws.String(*vol.VolumeId),
}
_, err = a.ec2.DeleteVolume(input)
if err != nil {
return err
}
return nil
}
// AttachVolume attaches a volume to an instance
func (a *AWS) AttachVolume(ctx *lepton.Context, instanceName, name string, attachID int) error {
vol, err := a.findVolumeByName(name)
if err != nil {
return err
}
instance, err := a.findInstanceByName(instanceName)
if err != nil {
return err
}
device := ""
if attachID >= 0 {
if attachID >= 1 && attachID <= 25 {
device = "/dev/sd" + string(rune('a'+attachID))
} else {
return fmt.Errorf("invalid attachment point %d; allowed values: 1-25", attachID)
}
} else {
// Look for an unused device name to be assigned to the volume, starting from "/dev/sdb"
for deviceLetter := 'b'; deviceLetter <= 'z'; deviceLetter++ {
name := "/dev/sd" + string(deviceLetter)
nameUsed := false
for _, mapping := range instance.BlockDeviceMappings {
if *mapping.DeviceName == name {
nameUsed = true
break
}
}
if !nameUsed {
device = name
break
}
}
if device == "" {
return errors.New("no available device names")
}
}
input := &ec2.AttachVolumeInput{
Device: aws.String(device),
InstanceId: aws.String(*instance.InstanceId),
VolumeId: aws.String(*vol.VolumeId),
}
_, err = a.ec2.AttachVolume(input)
if err != nil {
return err
}
return nil
}
// DetachVolume detachs a volume from an instance
func (a *AWS) DetachVolume(ctx *lepton.Context, instanceName, name string) error {
vol, err := a.findVolumeByName(name)
if err != nil {
return err
}
instance, err := a.findInstanceByName(instanceName)
if err != nil {
return err
}
input := &ec2.DetachVolumeInput{
InstanceId: aws.String(*instance.InstanceId),
VolumeId: aws.String(*vol.VolumeId),
}
_, err = a.ec2.DetachVolume(input)
if err != nil {
return err
}
return nil
}
func (a *AWS) findVolumeByName(name string) (*ec2.Volume, error) {
input := &ec2.DescribeVolumesInput{
Filters: []*ec2.Filter{
{Name: aws.String("tag:CreatedBy"), Values: []*string{aws.String("ops")}},
},
}
output, err := a.ec2.DescribeVolumes(input)
if err != nil {
return nil, err
}
for _, volume := range output.Volumes {
if *volume.VolumeId == name {
return volume, nil
}
for _, tag := range volume.Tags {
if (*tag.Key == "Name") && (*tag.Value == name) {
return volume, nil
}
}
}
return nil, fmt.Errorf("volume '%s' not found", name)
}