-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.rb
282 lines (211 loc) · 9.75 KB
/
config.rb
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require "ostruct"
module VagrantPlugins
module SoftLayer
class Config < Vagrant.plugin("2", :config)
# The API key to access SoftLayer.
attr_accessor :api_key
# Th SoftLayer API request timeout value in seconds
attr_accessor :api_timeout
# The endpoint SoftLayer API url.
attr_accessor :endpoint_url
# The username to access SoftLayer.
attr_accessor :username
# The datacenter shortname.
attr_accessor :datacenter
# Whether to allocate a dedicated instance.
attr_accessor :dedicated
# The disk image capacity
attr_accessor :disk_capacity
# The domain of the instance.
attr_accessor :domain
# Force the use of the private IP for all communication even if a public IP is available
attr_accessor :force_private_ip
# The hostname of the instance.
attr_accessor :hostname
# The billing type of the instance (true for hourly, false for monthly).
attr_accessor :hourly_billing
# The global identifier of the compute or flex image to use.
attr_accessor :image_guid
# The disk type of the instance (true for local, false for SAN).
attr_accessor :local_disk
# The amount of RAM of the instance.
attr_accessor :max_memory
# Network port speed in Mbps.
attr_accessor :network_speed
# The instance operating system identifier.
attr_accessor :operating_system
# URI of post-install script to download.
attr_accessor :post_install
# Whether or not the instance only has access to the private network.
attr_accessor :private_only
# The amount of time in seconds to wait for provision to complete.
attr_accessor :provision_timeout
# The amount of time in seconds to wait for rebuild to complete.
attr_accessor :rebuild_timeout
# The id or name of the ssh key to be provisioned.
attr_accessor :ssh_key
# The number of processors of the instance.
attr_accessor :start_cpus
# Whether to wait for transaction completion on actions that place orders
attr_accessor :transaction_wait
# User defined metadata string.
attr_accessor :user_data
# The ID, name or qualified name of the private VLAN.
attr_accessor :vlan_private
# The ID, name or qualified name of the public VLAN.
attr_accessor :vlan_public
# The load balancers service groups to join.
attr_reader :load_balancers
# Automatically update DNS on create and destroy.
attr_accessor :manage_dns
def initialize
@api_key = UNSET_VALUE
@api_timeout = UNSET_VALUE
@endpoint_url = UNSET_VALUE
@username = UNSET_VALUE
@datacenter = UNSET_VALUE
@dedicated = UNSET_VALUE
@disk_capacity = UNSET_VALUE
@domain = UNSET_VALUE
@force_private_ip = UNSET_VALUE
@hostname = UNSET_VALUE
@image_guid = UNSET_VALUE
@hourly_billing = UNSET_VALUE
@local_disk = UNSET_VALUE
@max_memory = UNSET_VALUE
@network_speed = UNSET_VALUE
@operating_system = UNSET_VALUE
@post_install = UNSET_VALUE
@private_only = UNSET_VALUE
@provision_timeout = UNSET_VALUE
@rebuild_timeout = UNSET_VALUE
@ssh_key = UNSET_VALUE
@start_cpus = UNSET_VALUE
@transaction_wait = UNSET_VALUE
@user_data = UNSET_VALUE
@vlan_private = UNSET_VALUE
@vlan_public = UNSET_VALUE
@load_balancers = []
@manage_dns = UNSET_VALUE
end
# Set the load balancer service group to join.
#
# Available options:
#
# :method => Routing method. Default to round robin.
# :port => Load balancer virtual port.
# :type => Routing type. Default to TCP.
# :vip => Load balancer virtual IP address.
#
# An optional block will accept parameters for the
# balanced service. Available parameters:
#
# :destination_port => TCP port on the node.
# :health_check => Service health check. Default to ping.
# :weight => Service weight. Default to 1.
#
def join_load_balancer(opts = {}, &block)
# Defaults
opts[:method] ||= "ROUND ROBIN"
opts[:type] ||= "TCP"
opts[:service] = OpenStruct.new(:destination_port => nil, :health_check => "PING", :weight => 1)
yield opts[:service] if block_given?
# Convert all options that belongs to
# an enumeration in uppercase.
opts[:method].upcase!
opts[:type].upcase!
opts[:service].health_check.upcase!
@load_balancers << opts
end
def finalize!
# Try to get username and api key from environment variables.
# They will default to nil if the environment variables are not present.
@api_key = ENV["SL_API_KEY"] if @api_key == UNSET_VALUE
@username = ENV["SL_USERNAME"] if @username == UNSET_VALUE
# Th SoftLayer API request timeout value in seconds
@api_timeout = 60 if @api_timeout == UNSET_VALUE
# Endpoint url defaults to public SoftLayer API url.
@endpoint_url = API_PUBLIC_ENDPOINT if @endpoint_url == UNSET_VALUE
# No default datacenter.
@datacenter = nil if @datacenter == UNSET_VALUE
# Shared instance by default.
@dedicated = false if @dedicated == UNSET_VALUE
# 25GB disk capacity image by default.
@disk_capacity = nil if @disk_capacity == UNSET_VALUE
# Domain should be specified in Vagrantfile, so we set default to nil.
@domain = nil if @domain == UNSET_VALUE
# Disable the use of force private IP so the default selection can take effect
@force_private_ip = false if @force_private_ip == UNSET_VALUE
# Hostname should be specified in Vagrantfile, either using `config.vm.hostname`
# or the provider specific configuration entry.
@hostname = nil if @hostname == UNSET_VALUE
# Bill hourly by default.
@hourly_billing = true if @hourly_billing == UNSET_VALUE
# Disable the use of a specific block device image so we can leave the OS template as default
@image_guid = nil if @image_guid == UNSET_VALUE
# Use local disk by default.
@local_disk = true if @local_disk == UNSET_VALUE
# 1Gb of RAM by default.
@max_memory = 1024 if @max_memory == UNSET_VALUE
# 10Mbps by default.
@network_speed = 10 if @network_speed == UNSET_VALUE
# Provision with the latest Ubuntu by default.
@operating_system = "UBUNTU_LATEST" if @operating_system == UNSET_VALUE
# No post install script by default.
@post_install = nil if @post_install == UNSET_VALUE
# Private-network only is false by default.
@private_only = false if @private_only == UNSET_VALUE
# The amount of time in seconds to wait for provision to complete.
@provision_timeout = 1200 if @provision_timeout == UNSET_VALUE
# The amount of time in seconds to wait for rebuild to complete.
@rebuild_timeout = 1200 if @rebuild_timeout == UNSET_VALUE
# SSH key should be specified in Vagrantfile, so we set default to nil.
@ssh_key = nil if @ssh_key == UNSET_VALUE
# One processor by default.
@start_cpus = 1 if @start_cpus == UNSET_VALUE
# Whether to wait for transaction completion on actions that place orders
@transaction_wait = true if @start_cpus == UNSET_VALUE
# No user metadata by default.
@user_data = nil if @user_data == UNSET_VALUE
# No specific private VLAN by default.
@vlan_private = nil if @vlan_private == UNSET_VALUE
# No specific public VLAN by default.
@vlan_public = nil if @vlan_public == UNSET_VALUE
# DNS management off by default
@manage_dns = false if @manage_dns == UNSET_VALUE
end
# Aliases for ssh_key for beautiful semantic.
def ssh_keys=(value)
@ssh_key = value
end
alias_method :ssh_key_id=, :ssh_keys=
alias_method :ssh_key_ids=, :ssh_keys=
alias_method :ssh_key_name=, :ssh_keys=
alias_method :ssh_key_names=, :ssh_keys=
def validate(machine)
errors = []
errors << I18n.t("vagrant_softlayer.config.api_key_required") if !@api_key
errors << I18n.t("vagrant_softlayer.config.username_required") if !@username
errors << I18n.t("vagrant_softlayer.config.domain_required") if !@domain
errors << I18n.t("vagrant_softlayer.config.ssh_key_required") if !@ssh_key
errors << I18n.t("vagrant_softlayer.config.img_guid_os_code_mutually_exclusive") if @image_guid && @operating_system
errors << I18n.t("vagrant_softlayer.config.img_guid_capacity_mutually_exclusive") if @image_guid && @disk_capacity
# Fail if both `vm.hostname` and `provider.hostname` are nil.
if !@hostname && !machine.config.vm.hostname
errors << I18n.t("vagrant_softlayer.config.hostname_required")
end
# Fail if a load balancer has been specified without vip, port or destination port.
unless @load_balancers.empty?
@load_balancers.each do |lb|
errors << I18n.t("vagrant_softlayer.config.lb_port_vip_required") unless (lb[:vip] && lb[:port] && lb[:service].destination_port)
end
end
# Fail if two or more load balancers has been specified with same vip and port.
if @load_balancers.map { |lb| { :port => lb[:port], :vip => lb[:vip] } }.uniq!
errors << I18n.t("vagrant_softlayer.config.lb_duplicate")
end
{ "SoftLayer" => errors }
end
end
end
end