-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_builder.rb
235 lines (203 loc) · 7.2 KB
/
vm_builder.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
require 'rubygems'
require 'socket'
require 'timeout'
require 'rest_client'
require 'base64'
require 'json'
require 'net/ssh'
require 'uri'
class VmBuilder
attr_accessor :fullname
def initialize args
raise TypeError unless args.is_a? Hash
# set some defaults
@url = args.delete(:url) || "http://127.0.0.1:3000"
@user = args.delete(:user) || 'admin'
@pass = args.delete(:pass) || 'changeme'
# Make a class var out of all the remaining args
args.each { |k,v| instance_variable_set "@#{k}", v }
end
def headers(user=@user,pass=@pass)
{ "Content_Type" => 'application/json', "Accept" => 'application/json', "Authorization" => "Basic #{Base64.encode64("#{@user}:#{@pass}")}" }
end
def check_connection
begin
response = RestClient.get "#{@url}/status", headers
if response.code == 200
puts "Connection to #{@url} ok: Foreman version #{JSON.parse(response.body)['version']}"
else
puts "Bad response from #{@url}: #{response.code} : #{response.body}"
exit 1
end
response = RestClient.get "#{@url}/hosts", headers
if response.code == 200
puts "Auth Connection to #{@url} ok: #{JSON.parse(response.body).size} hosts found"
end
rescue Errno::ECONNREFUSED => e
puts "Connection refused from: #{@url}"
exit 2
rescue RestClient::Request::Unauthorized => e
puts "Got 401 Unauthorized: check your credentials"
exit 3
rescue => e
puts "Problem testing connection to #{@url}: #{e.message}\n#{e.class}"
puts e.backtrace
exit 4
end
end
def check_and_create_host
begin
print "Creating host '#{@hostname}'"
response = RestClient.get "#{@url}/api/hosts", headers.merge({:params => {:search => "name ~ #{@hostname}"}})
if JSON.parse(response.body).empty?
raise RestClient::ResourceNotFound
else
@fullname = JSON.parse(response.body).first['host']['name']
response = RestClient.get "#{@url}/api/hosts/#{@fullname}", headers
if response.code == 200
if @delete == true then
print " [exists, deleting]"
del_res = RestClient.delete "#{@url}/api/hosts/#{@fullname}", headers
raise unless del_res.code == 200
raise RestClient::ResourceNotFound # jump to creation
else
puts " [exists, skipped]"
end
end
end
rescue RestClient::ResourceNotFound
@fullname = create_host['host']['name']
puts " [done]"
rescue => e
puts e.message
end
end
def wait_for_connection
print "Waiting for host to finish install "
while get_host_status == 'Pending Installation'
print '.'
sleep 60
end
puts " [done]"
print "Waiting for port 22 to open "
while is_port_open?(ip,22) == false
sleep 1
print '.'
end
puts " [done]"
end
def ip
return @ip unless @ip.nil?
res = RestClient.get("#{@url}/api/hosts/#{@fullname}", headers)
@ip = JSON.parse(res.body)['host']['ip']
return @ip
end
def ssh_setup(commands)
puts "SSHing to target"
puts "----------------"
# TODO: Could move all this to a custom finish-script for the packaging hostgroup
begin
Net::SSH.start(ip, 'root', :password => 'test', :paranoid=>false) do |ssh|
# open a new channel and configure a minimal set of callbacks, then run
# the event loop until the channel finishes (closes)
channel = ssh.open_channel do |ch|
ch.exec commands.join(" && ") do |ch, success|
raise "could not execute command" unless success
# "on_data" is called when the process writes something to stdout
ch.on_data do |c, data|
$stdout.print data
end
# "on_extended_data" is called when the process writes something to stderr
ch.on_extended_data do |c, type, data|
$stderr.print data
end
ch.on_close { puts "SSH Complete" }
end
end
channel.wait
end
rescue => e
puts "Error with SSH: #{e.message}\n#{e.backtrace}"
end
end
private
def get_host_status
response = RestClient.get "#{@url}/api/hosts/#{@fullname}/status", headers
return JSON.parse(response.body)['status']
end
def hostgroup_info
return @hostgroup_info unless @hostgroup_info.nil?
response = RestClient.get "#{@url}/api/hostgroups/#{@hostgroup}", headers
@hostgroup_info = JSON.parse(response.body)['hostgroup']
end
def compute_resource_id
return @compute_resource_id unless @compute_resource_id.nil?
response = RestClient.get "#{@url}/api/compute_resources/#{@compute_resource}", headers
@compute_resource_id = JSON.parse(response.body)['compute_resource']['id']
end
def architecture_id
return @architecture_id unless @architecture_id.nil?
response = RestClient.get "#{@url}/api/architectures/#{@architecture}", headers
@architecture_id = JSON.parse(response.body)['architecture']['id']
end
def operatingsystem_id
return @operatingsystem_id unless @operatingsystem_id.nil?
response = RestClient.get "#{@url}/api/operatingsystems", headers
@operatingsystem_id = JSON.parse(response.body).select { |k| k['operatingsystem']['name'] == @os_name and k['operatingsystem']['major'] == @os_version }.first['operatingsystem']['id']
end
def environment_id
return @environment_id unless @environment_id.nil?
response = RestClient.get "#{@url}/api/environments/#{@environment}", headers
@environment_id = JSON.parse(response.body)['environment']['id']
end
def create_host
# Create it
host_hash = {
"host" => {
"name" => @hostname,
"hostgroup_id" => hostgroup_info['id'],
"compute_resource_id" => compute_resource_id,
"location_id" => @location,
"organization_id" => @organisation,
"architecture_id" => architecture_id,
"operatingsystem_id" => operatingsystem_id,
"environment_id" => environment_id,
"build" => 1,
"provision_method" => "image",
"compute_attributes" => {
"flavor_ref" => "2",
"image_ref" => "252861eb-f1a6-4243-9152-9ae58434341b",
"tenant_id" => "b7b85528b7c448d9bdec77148c2e8a97",
"security_groups" =>"default",
"network" =>"public",
}
},
"capabilities"=>"image",
}
response = RestClient::Request.execute(:method => :post, :url => "#{@url}/api/hosts", :payload => host_hash, :headers => headers, :timeout => 600)
return JSON.parse(response.body)
end
def is_port_open?(ip, port)
begin
Timeout::timeout(1) do
begin
s = TCPSocket.new(ip, port)
s.close
return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
return false
end
end
rescue Timeout::Error
end
return false
end
end
# ---- reference
#@arch=nil
#response = RestClient.get "#{url}/api/architectures", headers
#puts response.body
#puts "---"
#JSON.parse(response.body).each do |arch|
# @arch=arch['architecture'] if arch['architecture']['name'] == 'i386'
#end