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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
$: << File.dirname(File.expand_path(__FILE__))
gem 'reststop', '~> 0.2'
require 'yaml'
require 'fileutils'
require 'rubygems'
require 'picnic'
require 'reststop'
require 'everest'
require 'restr'
require 'xmlrpc/client'
require 'sqlite3'
require 'fastthread'
require 'open3'
require 'pp'
Camping.goes :Cloudmasterd
Cloudmasterd.picnic!
module Cloudmasterd::Helpers
class Syncer
def self.run
begin
sync_memory
sync_state
sync_owners
rescue Exception => e
puts "ERROR: An error occured during syncing - #{e.to_s}"
end
end
def self.parse_hosts(func_stderr)
hosts = []
func_stderr.split("\n").each do |host_line|
# Handle a remote exception coming from a disconnected host
if host_line.index("remote exception") then
hosts.pop()
else
hosts << host_line.split(":")[1][2..-1]
end
end
return hosts
end
def self.sync_memory
current_state = {}
Open3.popen3("func '*' call virt freemem") do |stdin, stdout, stderr|
hosts = parse_hosts(stderr.read)
# Parse out the machines that belong to each host and zip them together in a hash
stdout.read.split("\n").each_with_index do |memory, index|
current_state[hosts[index]] = memory
end
end
Cloudmasterd::Models::Cloud.transaction do
# Sync up the DB state - delete everything first
Cloudmasterd::Models::Cloud.delete_all
# Create the current entries
current_state.each do |host, memory|
Cloudmasterd::Models::Cloud.create :name => host, :memory => memory
end
end
end
def self.sync_state
current_state = {}
Open3.popen3("func '*' call virt state") do |stdin, stdout, stderr|
hosts = parse_hosts(stderr.read)
# Parse out the machines that belong to each host and zip them together in a hash
stdout.read.split("\n").each_with_index do |host, index|
host[1..-2].split(", ").each do |machine|
name, state = machine[1..-2].split(" ")
current_state[name] = {:state => state, :cloud => hosts[index]} unless name == "Domain-0"
end
end
end
# Sync up the state for all the machines
Cloudmasterd::Models::Machine.find(:all).each do |machine|
if current_state.has_key?(machine.name) then
# We have a current state match for this machine
machine.update_attributes(:cloud => current_state[machine.name][:cloud], :state => current_state[machine.name][:state])
# Delete the key so we can determine unaccounted for machines
current_state.delete(machine.name)
else
# Not good - the machine in the DB isn't locateable
machine.update_attribute(:state, "missing")
end
end
# We need to create an 'unknown' instance for all the remaining keys
current_state.each do |name, machine|
Cloudmasterd::Models::Machine.create :name => name, :email => "unknown", :cloud => machine[:cloud], :state => machine[:state]
end
end
def self.sync_owners
# Sync up any unowned machines that now have an owner (handles race condition with syncing states)
unowned_machines = {}
Cloudmasterd::Models::Machine.find(:all, :conditions => "email = 'unknown'").each do |machine|
unowned_machines[machine.name] = machine
end
owned_machines = {}
Cloudmasterd::Models::Machine.find(:all, :conditions => "email <> 'unknown'").each do |machine|
owned_machines[machine.name] = machine
end
owned_machines.each do |name, machine|
if unowned_machines.has_key?(name) then
# Delete the unowned machine and update the owned one
u_machine = unowned_machines[name]
machine.update_attributes(:cloud => u_machine.cloud, :state => u_machine.state)
u_machine.destroy()
end
end
end
end
class Cobblerd
def initialize(repo)
@repo = repo
@xmlrpc = XMLRPC::Client.new2("http://#{@repo}:25152")
@token = @xmlrpc.call2("login", "cobbler", "password")[1]
end
def _call(method, *args)
return @xmlrpc.call2(method, *args)[1]
end
def distros
_call("get_distros", @token)
end
def profiles
_call("get_profiles", @token)
end
def systems
_call("get_systems", @token)
end
def repos
_call("get_repos", @token)
end
def distro(name)
_call("get_distro", name, @token)
end
def distro_for_koan(name)
_call("get_distro_for_koan", name, @token)
end
def profile(name)
_call("get_profile", name, @token)
end
def profile_for_koan(name)
_call("get_profile_for_koan", name, @token)
end
def system(name)
_call("get_system", name, @token)
end
def system_for_koan(name)
_call("get_system_for_koan", name, @token)
end
def repo(name)
_call("get_repo", name, @token)
end
def repo_for_koan(name)
_call("get_repo_for_koan", name, @token)
end
end
end
module Cloudmasterd::Models
class Cloud < Base; end
class Machine < Base; end
class CreateTheBasics < V 1.0
def self.up
create_table :cloudmasterd_cloud do |t|
t.column :name, :string, :null => false, :limit => 255
t.column :memory, :integer, :null => false
end
create_table :cloudmasterd_machine do |t|
t.column :name, :string, :null => false, :limit => 255
t.column :email, :string, :null => false, :limit => 255
t.column :cloud, :string, :null => false, :limit => 255
t.column :state, :string, :null => false, :limit => 255
end
end
def self.down
drop_table :cloudmasterd_memory
drop_table :cloudmasterd_machine
end
end
end
module Cloudmasterd::Controllers
class Koan < REST 'koan'
# Retrieve the cobbler profile for the given profile name
def _get_cobbler_profile(system_name, repo)
cobblerd = Cloudmasterd::Helpers::Cobblerd.new(repo)
cobbler_system = cobblerd.system(system_name)
return cobblerd.profile(cobbler_system["profile"])
end
# Find the best host on which to create a VM
def _get_best_host(disk_size, ram)
return `func-find-resources -m #{ram}`.chomp()
end
# run the appropriate koan command to create the given "fqdn"
# on the given "host" and "vol_group"
def _koan(host, fqdn, repo)
# Run the koan process
output = `func "#{host}" call virt install #{repo} #{fqdn} True #{fqdn} /images`
# Throw an exception if the process failed
raise output unless $?.success?
# Make the new host autostart
`func "#{host}" call command run "ln -s /etc/xen/#{fqdn} /etc/xen/auto/"`
end
# POST /koan
def create
machine_fqdn = input.machine_fqdn
repo = input.repo
email = input.email
cobbler_profile = _get_cobbler_profile(machine_fqdn, repo)
@host = _get_best_host(cobbler_profile["virt_file_size"], cobbler_profile["virt_ram"])
begin
_koan(@host, machine_fqdn, repo)
Machine.create :name => machine_fqdn, :email => email, :cloud => @host, :state => "Installing"
render :_koan
rescue Exception => e
@exception = e
render :_error
end
end
def destroy(name)
puts "Destroying image #{name}"
Machine.find(:all, :conditions => "name = '#{name}'").each do |machine|
# If the machine is missing, don't bother remotely trying to cleanup
unless machine.state == "missing" then
# Destroy the virtual machine
`func "#{machine.cloud}" call command run "virsh destroy #{machine.name}"`
`func "#{machine.cloud}" call command run "virsh undefine #{machine.name}"`
# Remove the auto start file if it exists
`func "#{machine.cloud}" call command run "rm -rf /etc/xen/auto/#{machine.name}"`
# Remove the image file
`func "#{machine.cloud}" call command run "rm -rf /images/#{machine.name}-disk0"`
end
# Delete the DB record
machine.destroy
end
redirect R(Status)
end
end
class Status < REST 'status'
def list
# TODO: Figure out how to move this to a background thread
Cloudmasterd::Helpers::Syncer.run
@machines = {}
Machine.find(:all, :select => "distinct email").map{|x| x.email}.each do |email|
@machines[email] = Machine.find(:all, :conditions => "email = '#{email}'", :order => 'cloud')
end
@memory = Cloud.sum('memory')
@cloud_machines = Cloud.find(:all)
render :status
end
end
end
module Cloudmasterd::Views
def initialize(*args)
super(*args)
@x = Builder::XmlMarkup.new(:indent => 1)
end
module HTML
def layout
html do
head do
title 'Everest Cloud Master Controller'
link :rel => 'stylesheet', :type => 'text/css', :href => "/styles.css", :media => 'screen'
end
body do
div.content! do
self << yield
end
end
end
end
def status
div.cloud! do
h2 "Available cloud memory: #{@memory}"
table do
tr do
th 'Cloud Instance'
th 'Memory'
end
@cloud_machines.each do |machine|
tr do
td machine.name
td machine.memory
end
end
end
end
div.machines! do
@machines.keys.each do |email|
div.user do
h2 email
table do
tr do
th 'Name'
th 'Cloud Instance'
th 'State'
th ''
end
@machines[email].each do |machine|
tr do
td machine.name
td machine.cloud
td machine.state
td do
form(:method => :delete, :action => "/cloud" + R(Cloudmasterd::Controllers::Koan, "#{machine.name}").to_s) do
input :type => :submit, :value => 'Destroy'
end
end
end
end
end
end
end
end
end
def _koan
@host
end
def _error
"ERROR: #{@exception.to_s}"
end
end
default_format :HTML
module YAML
CONTENT_TYPE = 'text/plain'
def layout
yield
end
end
module XML
def layout
yield
end
end
end
def Cloudmasterd.create
#ActiveRecord::Base.logger = Logger.new(STDOUT)
#ActiveRecord::Base.colorize_logging = true
ActiveRecord::Base.pluralize_table_names = false
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "/var/lib/cloudmaster.db"
)
Cloudmasterd::Models.create_schema :assume => (Cloudmasterd::Models::Machine.table_exists? ? 1.0 : 0.0)
# Sync up the state initially
Cloudmasterd::Helpers::Syncer.run
end
#Thread.new do
# while true do
# Cloudmasterd::Helpers::Syncer.run
# sleep 5
# end
#end
Cloudmasterd.start_picnic
|