/
create-volume.go
138 lines (115 loc) · 4.61 KB
/
create-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
package cmd
import (
"io/ioutil"
"os"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/emc-advanced-dev/pkg/errors"
"github.com/solo-io/unik/pkg/client"
unikos "github.com/solo-io/unik/pkg/os"
)
var data string
var size int
var volumeType string
var rawVolume bool
const (
VolTypeExt2 = "ext2"
)
var cvCmd = &cobra.Command{
Use: "create-volume",
Short: "Create a unik-managed data volume",
Long: `Create a data volume which can be attached to and detached from
unik-managed instances.
Volumes can be created from a directory, which will copy the contents
of the directory onto the voume. Empty volume can also be created.
Volumes will persist after instances are deleted, allowing application data
to be persisted beyond the lifecycle of individual instances.
If specifying a data folder (with --data), specifying a size for the volume is
not necessary. UniK will automatically size the volume to fit the data provided.
A larger volume can be requested with the --size flag.
If no data directory is provided, --size is a required parameter to specify the
desired size for the empty volume to be createad.
Volumes are created for a specific provider, specified with the --provider flag.
Volumes can only be attached to instances of the same provider type.
To see a list of available providers, run 'unik providers'
Volume names must be unique. If a volume exists with the same name, you will be
required to remove the volume with 'unik delete-volume' before the new volume
can be created.
--size parameter uses MB
Example usage:
unik create-volume --name myVolume --data ./myApp/data --provider aws
# will create an EBS-backed AWS volume named myVolume using the data found in ./myApp/src,
# the size will be either 1GB (the default minimum size on AWS) or greater, if the size of the
volume is greater
Another example (empty volume):
unik create-volume -name anotherVolume --size 500 -provider vsphere
# will create a 500mb sparse vmdk file and upload it to the vsphere datastore,
where it can be attached to a vsphere instance
`,
Run: func(cmd *cobra.Command, args []string) {
if err := func() error {
if name == "" {
return errors.New("--name must be set", nil)
}
if data == "" && size == 0 {
return errors.New("either --data or --size must be set", nil)
}
if provider == "" {
return errors.New("--provider must be set", nil)
}
if volumeType == "" {
volumeType = VolTypeExt2
} else {
volumeType = strings.ToLower(volumeType)
}
if err := readClientConfig(); err != nil {
return err
}
if host == "" {
host = clientConfig.Host
}
logrus.WithFields(logrus.Fields{
"name": name,
"data": data,
"size": size,
"provider": provider,
"host": host,
"volumeType": volumeType,
}).Infof("creating volume")
if data != "" {
dataTar, err := ioutil.TempFile("", "data.tar.gz.")
if err != nil {
logrus.WithError(err).Error("failed to create tmp tar file")
}
if false {
defer os.Remove(dataTar.Name())
}
if err := unikos.Compress(data, dataTar.Name()); err != nil {
return errors.New("failed to tar data", err)
}
data = dataTar.Name()
logrus.Infof("Data packaged as tarball: %s\n", dataTar.Name())
}
volume, err := client.UnikClient(host).Volumes().Create(name, data, provider, rawVolume, size, volumeType, noCleanup)
if err != nil {
return errors.New("creatinv volume image failed", err)
}
printVolumes(volume)
return nil
}(); err != nil {
logrus.Errorf("create-volume failed: %v", err)
os.Exit(-1)
}
},
}
func init() {
RootCmd.AddCommand(cvCmd)
cvCmd.Flags().StringVar(&name, "name", "", "<string,required> name to give the volume. must be unique")
cvCmd.Flags().StringVar(&data, "data", "", "<string,special> path to data folder (or file if --raw is provided). optional if --size is provided")
cvCmd.Flags().BoolVar(&rawVolume, "raw", false, "<bool,optional> if true then then data is expected to be a file that will be used as is. if false (default) data should point to a folder which will be turned into a volume.")
cvCmd.Flags().IntVar(&size, "size", 0, "<int,special> size to create volume in MB. optional if --data is provided")
cvCmd.Flags().StringVar(&provider, "provider", "", "<string,required> name of the target infrastructure to compile for")
cvCmd.Flags().StringVar(&volumeType, "type", "", "<string,optional> FS type of the volume. ext2 or FAT are supported. defaults to ext2")
cvCmd.Flags().BoolVar(&noCleanup, "no-cleanup", false, "<bool, optional> for debugging; do not clean up artifacts for volumes that fail to build")
}