forked from openhpi-cloud/docker-plugin-cinder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
155 lines (128 loc) · 5.26 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
_log "log"
"os"
"github.com/coreos/go-systemd/activation"
log "github.com/sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
)
type tConfig struct {
Debug bool
Quiet bool
IdentityEndpoint string `json:"endpoint,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
DomainID string `json:"domainID,omitempty"`
DomainName string `json:"domainName,omitempty"`
TenantID string `json:"tenantId,omitempty"`
TenantName string `json:"tenantName,omitempty"`
ApplicationCredentialID string `json:"applicationCredentialId,omitempty"`
ApplicationCredentialName string `json:"applicationCredentialName,omitempty"`
ApplicationCredentialSecret string `json:"applicationCredentialSecret,omitempty"`
Region string `json:"region,omitempty"`
MachineID string `json:"machineID,omitempty"`
MountDir string `json:"mountDir,omitempty"`
Filesystem string `json:"filesystem,omitempty"`
DefaultSize string `json:"defaultSize,omitempty"`
DefaultType string `json:"defaultType,omitempty"`
VolumeSubDir string `json:"volumeSubDir,omitempty"`
EncryptionKey string `json:"encryptionKey,omitempty"`
TimeoutVolumeState int `json:"timeoutVolumeState,omitempty"`
TimeoutDeviceWait int `json:"timeoutDeviceWait,omitempty"`
DelayVolumeState int `json:"delayVolumeState,omitempty"`
DelayDeviceWait int `json:"delayDeviceWait,omitempty"`
}
func init() {
_log.SetOutput(ioutil.Discard)
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
}
func main() {
var config tConfig
var configFile string
flag.BoolVar(&config.Debug, "debug", false, "Enable debug logging")
flag.BoolVar(&config.Quiet, "quiet", false, "Only report errors")
flag.StringVar(&configFile, "config", "cinder.json", "Config file")
flag.StringVar(&config.MountDir, "mountDir", "/var/lib/cinder/mount", "Cinder mount directory")
flag.StringVar(&config.MachineID, "machineID", "", "force machine ID")
flag.StringVar(&config.Filesystem, "filesystem", "ext4", "New volumes filesystem (ext4)")
flag.StringVar(&config.DefaultSize, "defaultSize", "10", "New volumes default size (10)")
flag.StringVar(&config.DefaultType, "defaultType", "classic", "New volumes default type (classic)")
flag.StringVar(&config.VolumeSubDir, "volumeSubDir", "data", "Volumes subdirectory (data)")
flag.StringVar(&config.EncryptionKey, "encryptionKey", "", "LUKS encryption key path")
flag.IntVar(&config.TimeoutVolumeState, "timeoutVolumeState", 5, "Timeout for waitOnVolumeState (s)")
flag.IntVar(&config.TimeoutDeviceWait, "timeoutDeviceWait", 5, "Timeout when waiting for device attachment (s)")
flag.IntVar(&config.DelayVolumeState, "delayVolumeState", 1, "Delay after waitOnVolumeState (s)")
flag.IntVar(&config.DelayDeviceWait, "delayDeviceWait", 1, "Delay after device attachment (s)")
flag.Parse()
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
log.SetOutput(os.Stdout)
content, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal(err.Error())
}
err = json.Unmarshal(content, &config)
if err != nil {
log.Fatal(err.Error())
}
if len(config.MountDir) == 0 {
log.Fatal("No mountDir configured. Abort.")
}
if config.Quiet {
log.SetLevel(log.ErrorLevel)
}
if config.Debug {
log.SetLevel(log.DebugLevel)
}
log.Debug("Debug logging enabled")
if len(config.IdentityEndpoint) == 0 {
log.Fatal("Identity endpoint missing")
}
opts := gophercloud.AuthOptions{
IdentityEndpoint: config.IdentityEndpoint,
Username: config.Username,
Password: config.Password,
DomainID: config.DomainID,
DomainName: config.DomainName,
TenantID: config.TenantID,
TenantName: config.TenantName,
ApplicationCredentialID: config.ApplicationCredentialID,
ApplicationCredentialName: config.ApplicationCredentialName,
ApplicationCredentialSecret: config.ApplicationCredentialSecret,
AllowReauth: true,
}
logger := log.WithField("endpoint", opts.IdentityEndpoint)
logger.Info("Connecting...")
provider, err := openstack.AuthenticatedClient(opts)
if err != nil {
logger.WithError(err).Fatal(err.Error())
}
endpointOpts := gophercloud.EndpointOpts{
Region: config.Region,
}
plugin, err := newPlugin(provider, endpointOpts, &config)
if err != nil {
logger.WithError(err).Fatal(err.Error())
}
handler := volume.NewHandler(plugin)
logger.Info("Connected.")
listeners, err := activation.Listeners()
if err != nil {
logger.WithError(err).Error(err.Error())
}
if len(listeners) > 0 {
logger.Debugf("Started with socket activation")
err = handler.Serve(listeners[0])
} else {
err = handler.ServeUnix("cinder", 0)
}
if err != nil {
logger.WithError(err).Fatal(err.Error())
}
}