Class: BoxGrinder::ElasticHostsPlugin
- Inherits:
-
BasePlugin
show all
- Defined in:
- lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb
Instance Attribute Summary
Attributes inherited from BasePlugin
#deliverables, #plugin_info
Instance Method Summary
collapse
Methods inherited from BasePlugin
#current_platform, #deliverables_exists?, #init, #initialize, #is_supported_os?, #is_supported_platform?, #merge_plugin_config, #read_plugin_config, #register_deliverable, #register_supported_os, #register_supported_platform, #run, #set_default_config_value, #subtype, #supported_oses, #validate_plugin_config
Methods included from Plugins
#plugin
Instance Method Details
#api_url(path) ⇒ Object
96
97
98
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 96
def api_url(path)
"#{@plugin_config['ssl'] ? 'https' : 'http'}://#{CGI.escape(@plugin_config['username'])}:#{@plugin_config['password']}@#{@plugin_config['endpoint']}#{path}"
end
|
#compress(data) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 132
def compress(data)
@log.trace "Compressing #{data.size / 1024} kB chunk of data..."
io = StringIO.new
writer = Zlib::GzipWriter.new(io, Zlib::DEFAULT_COMPRESSION, Zlib::FINISH)
writer.write(data)
writer.close
@log.trace "Data compressed to #{io.size / 1024} kB."
io.string
end
|
#create_remote_disk ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 72
def create_remote_disk
size = disk_size
@log.info "Creating new #{size} GB disk..."
body = hash_to_request(
'size' => size * 1024 *1024 * 1024,
'name' => @plugin_config['drive_name']
)
begin
ret = response_to_hash(RestClient.post(api_url('/drives/create'), body))
@log.info "Disk created with UUID: #{ret['drive']}."
rescue => e
@log.error e.info
raise PluginError, "An error occured while creating the drive, #{e.message}. See logs for more info."
end
ret['drive']
end
|
#create_server ⇒ Object
Creates the server for previously uploaded disk
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
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 182
def create_server
@log.info "Creating new server..."
memory = ((is_cloudsigma? and @appliance_config.hardware.memory < 512) ? 512 : @appliance_config.hardware.memory)
body = hash_to_request(
'name' => "#{@appliance_config.name}-#{@appliance_config.version}.#{@appliance_config.release}",
'cpu' => @appliance_config.hardware.cpus * 1000, 'smp' => 'auto',
'mem' => memory,
'persistent' => 'true', 'ide:0:0' => @plugin_config['drive_uuid'],
'boot' => 'ide:0:0',
'nic:0:model' => 'e1000',
'nic:0:dhcp' => 'auto',
'vnc:ip' => 'auto',
'vnc:password' => (0...8).map { (('a'..'z').to_a + ('A'..'Z').to_a)[rand(52)] }.join )
begin
path = is_cloudsigma? ? '/servers/create' : '/servers/create/stopped'
ret = response_to_hash(RestClient.post(api_url(path), body))
@log.info "Server was registered with '#{ret['name']}' name as '#{ret['server']}' UUID. Use web UI or API tools to start your server."
rescue => e
@log.error e.info
raise PluginError, "An error occurred while creating the server, #{e.message}. See logs for more info."
end
end
|
#disk_size ⇒ Object
46
47
48
49
50
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 46
def disk_size
size = 0
@appliance_config.hardware.partitions.each_value { |partition| size += partition['size'] }
size
end
|
#execute ⇒ Object
41
42
43
44
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 41
def execute
upload
create_server
end
|
#hash_to_request(h) ⇒ Object
52
53
54
55
56
57
58
59
60
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 52
def hash_to_request(h)
body = ""
h.sort.each do |k, v|
body << "#{k} #{v.to_s}\n"
end
body
end
|
#is_cloudsigma? ⇒ Boolean
177
178
179
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 177
def is_cloudsigma?
!@plugin_config['endpoint'].match(/cloudsigma\.com$/).nil?
end
|
#response_to_hash(r) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 62
def response_to_hash(r)
h = {}
r.each_line do |l|
h[$1] = $2 if l =~ /(\w+) (.*)/
end
h
end
|
#upload ⇒ Object
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 100
def upload
@log.info "Uploading appliance..."
@plugin_config['drive_uuid'] = create_remote_disk unless @plugin_config['drive_uuid']
upload_chunks
@log.info "Appliance uploaded."
end
|
#upload_chunk(data, part) ⇒ Object
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
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 146
def upload_chunk(data, part)
try = 1
url = api_url("/drives/#{@plugin_config['drive_uuid']}/write/#{@step * part}")
begin
@log.info "Uploading part #{part+1}..."
= {:content_type => "application/octet-stream"}
['Content-Encoding'] = 'gzip' unless is_cloudsigma?
RestClient.post url,
data,
@log.info "Part #{part+1} uploaded."
rescue => e
@log.warn "An error occured while uploading #{part} chunk, #{e.message}"
try += 1
unless try > @plugin_config['retry']
sleep @plugin_config['wait']
retry
else
@log.error e.info
raise PluginError, "Couldn't upload appliance, #{e.message}."
end
end
end
|
#upload_chunks ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 111
def upload_chunks
@step = @plugin_config['chunk'] * 1024 * 1024 part = @plugin_config['start_part']
@log.info "Uploading disk in #{disk_size * 1024 / @plugin_config['chunk']} parts."
File.open(@previous_deliverables.disk, 'rb') do |f|
while !f.eof?
f.seek(part * @step, File::SEEK_SET)
data = f.read(@step)
data = compress(data) unless is_cloudsigma?
upload_chunk(data, part)
part += 1
end
end
@log.info "Appliance #{@appliance_config.name} uploaded to drive with UUID #{@plugin_config['drive_uuid']}."
end
|
#validate ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/boxgrinder-build/plugins/delivery/elastichosts/elastichosts-plugin.rb', line 29
def validate
set_default_config_value('chunk', 64) set_default_config_value('start_part', 0) set_default_config_value('wait', 5) set_default_config_value('retry', 3) set_default_config_value('ssl', false) set_default_config_value('drive_name', @appliance_config.name)
validate_plugin_config(['endpoint', 'username', 'password'], 'http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#ElasticHosts_Delivery_Plugin')
raise PluginValidationError, "You can use ElasticHosts plugin with base appliances (appliances created with operating system plugins) only, see http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#ElasticHosts_Delivery_Plugin." unless @previous_plugin_info[:type] == :os
end
|