forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rackspace.go
179 lines (160 loc) · 4.91 KB
/
rackspace.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
package rackspace
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/machine/drivers"
"github.com/docker/machine/drivers/openstack"
)
const (
dockerConfigDir = "/etc/docker"
)
// Driver is a machine driver for Rackspace. It's a specialization of the generic OpenStack one.
type Driver struct {
*openstack.Driver
APIKey string
}
// CreateFlags stores the command-line arguments given to "machine create".
type CreateFlags struct {
Username *string
APIKey *string
Region *string
MachineName *string
EndpointType *string
ImageID *string
FlavorID *string
SSHUser *string
SSHPort *int
CaCertPath string
PrivateKeyPath string
SwarmMaster bool
SwarmHost string
SwarmDiscovery string
}
func init() {
drivers.Register("rackspace", &drivers.RegisteredDriver{
New: NewDriver,
GetCreateFlags: GetCreateFlags,
})
}
// GetCreateFlags registers the "machine create" flags recognized by this driver, including
// their help text and defaults.
func GetCreateFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
EnvVar: "OS_USERNAME",
Name: "rackspace-username",
Usage: "Rackspace account username",
Value: "",
},
cli.StringFlag{
EnvVar: "OS_API_KEY",
Name: "rackspace-api-key",
Usage: "Rackspace API key",
Value: "",
},
cli.StringFlag{
EnvVar: "OS_REGION_NAME",
Name: "rackspace-region",
Usage: "Rackspace region name",
Value: "",
},
cli.StringFlag{
EnvVar: "OS_ENDPOINT_TYPE",
Name: "rackspace-endpoint-type",
Usage: "Rackspace endpoint type (adminURL, internalURL or the default publicURL)",
Value: "publicURL",
},
cli.StringFlag{
Name: "rackspace-image-id",
Usage: "Rackspace image ID. Default: Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)",
Value: "",
},
cli.StringFlag{
Name: "rackspace-flavor-id",
Usage: "Rackspace flavor ID. Default: General Purpose 1GB",
Value: "general1-1",
},
cli.StringFlag{
Name: "rackspace-ssh-user",
Usage: "SSH user for the newly booted machine. Set to root by default",
Value: "root",
},
cli.IntFlag{
Name: "rackspace-ssh-port",
Usage: "SSH port for the newly booted machine. Set to 22 by default",
Value: 22,
},
cli.StringFlag{
Name: "rackspace-docker-install",
Usage: "Set if docker have to be installed on the machine",
Value: "true",
},
}
}
// NewDriver instantiates a Rackspace driver.
func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) {
log.WithFields(log.Fields{
"machineName": machineName,
"storePath": storePath,
"caCert": caCert,
"privateKey": privateKey,
}).Debug("Instantiating Rackspace driver.")
client := &Client{}
inner, err := openstack.NewDerivedDriver(machineName, storePath, client, caCert, privateKey)
if err != nil {
return nil, err
}
driver := &Driver{Driver: inner}
client.driver = driver
return driver, nil
}
// DriverName is the user-visible name of this driver.
func (d *Driver) DriverName() string {
return "rackspace"
}
func (d *Driver) GetDockerConfigDir() string {
return dockerConfigDir
}
func missingEnvOrOption(setting, envVar, opt string) error {
return fmt.Errorf(
"%s must be specified either using the environment variable %s or the CLI option %s",
setting,
envVar,
opt,
)
}
// SetConfigFromFlags assigns and verifies the command-line arguments presented to the driver.
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.Username = flags.String("rackspace-username")
d.APIKey = flags.String("rackspace-api-key")
d.Region = flags.String("rackspace-region")
d.EndpointType = flags.String("rackspace-endpoint-type")
d.ImageId = flags.String("rackspace-image-id")
d.FlavorId = flags.String("rackspace-flavor-id")
d.SSHUser = flags.String("rackspace-ssh-user")
d.SSHPort = flags.Int("rackspace-ssh-port")
d.EnableDockerInstall = flags.String("rackspace-docker-install") == "true"
d.SwarmMaster = flags.Bool("swarm-master")
d.SwarmHost = flags.String("swarm-host")
d.SwarmDiscovery = flags.String("swarm-discovery")
if d.Region == "" {
return missingEnvOrOption("Region", "OS_REGION_NAME", "--rackspace-region")
}
if d.Username == "" {
return missingEnvOrOption("Username", "OS_USERNAME", "--rackspace-username")
}
if d.APIKey == "" {
return missingEnvOrOption("API key", "OS_API_KEY", "--rackspace-api-key")
}
if d.ImageId == "" {
// Default to the Ubuntu 14.04 image.
// This is done here, rather than in the option registration, to keep the default value
// from making "machine create --help" ugly.
d.ImageId = "598a4282-f14b-4e50-af4c-b3e52749d9f9"
}
if d.EndpointType != "publicURL" && d.EndpointType != "adminURL" && d.EndpointType != "internalURL" {
return fmt.Errorf(`Invalid endpoint type "%s". Endpoint type must be publicURL, adminURL or internalURL.`, d.EndpointType)
}
return nil
}