forked from rancher/rancher-compose
/
volume.go
178 lines (152 loc) · 3.94 KB
/
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
package rancher
import (
"fmt"
"golang.org/x/net/context"
"github.com/Sirupsen/logrus"
"github.com/docker/libcompose/config"
"github.com/docker/libcompose/project"
"github.com/rancher/go-rancher/v2"
)
type RancherVolumesFactory struct {
Context *Context
}
func (f *RancherVolumesFactory) Create(projectName string, volumeConfigs map[string]*config.VolumeConfig, serviceConfigs *config.ServiceConfigs, volumeEnabled bool) (project.Volumes, error) {
volumes := make([]*Volume, 0, len(volumeConfigs))
for name, config := range volumeConfigs {
volume := NewVolume(projectName, name, config, f.Context)
volumes = append(volumes, volume)
}
return &Volumes{
volumes: volumes,
volumeEnabled: volumeEnabled,
Context: f.Context,
}, nil
}
type Volumes struct {
volumes []*Volume
volumeEnabled bool
Context *Context
}
func (v *Volumes) Initialize(ctx context.Context) error {
if !v.volumeEnabled {
return nil
}
for _, volume := range v.volumes {
if err := volume.EnsureItExists(ctx); err != nil {
return err
}
}
return nil
}
func (v *Volumes) Remove(ctx context.Context) error {
if !v.volumeEnabled {
return nil
}
for _, volume := range v.volumes {
if err := volume.Remove(ctx); err != nil {
return err
}
}
return nil
}
type Volume struct {
context *Context
name string
projectName string
driver string
driverOptions map[string]string
external bool
perContainer bool
}
// InspectTemplate looks up a volume template
func (v *Volume) InspectTemplate(ctx context.Context) (*client.VolumeTemplate, error) {
volumes, err := v.context.Client.VolumeTemplate.List(&client.ListOpts{
Filters: map[string]interface{}{
"name": v.name,
"stackId": v.context.Stack.Id,
},
})
if err != nil {
return nil, err
}
if len(volumes.Data) > 0 {
return &volumes.Data[0], nil
}
return nil, nil
}
// InspectExternal looks up a volume
func (v *Volume) InspectExternal(ctx context.Context) (*client.Volume, error) {
volumes, err := v.context.Client.Volume.List(&client.ListOpts{
Filters: map[string]interface{}{
"name": v.name,
},
})
if err != nil {
return nil, err
}
if len(volumes.Data) > 0 {
return &volumes.Data[0], nil
}
return nil, nil
}
func (v *Volume) Remove(ctx context.Context) error {
if v.external {
return nil
}
volumeResource, err := v.InspectTemplate(ctx)
if err != nil {
return err
}
return v.context.Client.VolumeTemplate.Delete(volumeResource)
}
func (v *Volume) EnsureItExists(ctx context.Context) error {
if v.external {
volumeResource, err := v.InspectExternal(ctx)
if err != nil {
return err
}
if volumeResource == nil {
return fmt.Errorf("Volume %s declared as external, but could not be found. Please create the volume manually and try again.", v.name)
}
return nil
}
volumeResource, err := v.InspectTemplate(ctx)
if err != nil {
return err
}
if volumeResource == nil {
logrus.Infof("Creating volume template %s", v.name)
return v.create(ctx)
} else {
logrus.Infof("Existing volume template found for %s", v.name)
}
if v.driver != "" && volumeResource.Driver != v.driver {
return fmt.Errorf("Volume %q needs to be recreated - driver has changed", v.name)
}
return nil
}
func (v *Volume) create(ctx context.Context) error {
driverOptions := map[string]interface{}{}
for k, v := range v.driverOptions {
driverOptions[k] = v
}
_, err := v.context.Client.VolumeTemplate.Create(&client.VolumeTemplate{
Name: v.name,
Driver: v.driver,
DriverOpts: driverOptions,
StackId: v.context.Stack.Id,
PerContainer: v.perContainer,
})
return err
}
func NewVolume(projectName, name string, config *config.VolumeConfig, context *Context) *Volume {
return &Volume{
context: context,
name: name,
projectName: projectName,
driver: config.Driver,
driverOptions: config.DriverOpts,
external: config.External.External,
perContainer: config.PerContainer,
}
}