diff options
Diffstat (limited to 'cloudmasterd/lib/cloudmasterd.rb')
-rw-r--r-- | cloudmasterd/lib/cloudmasterd.rb | 138 |
1 files changed, 84 insertions, 54 deletions
diff --git a/cloudmasterd/lib/cloudmasterd.rb b/cloudmasterd/lib/cloudmasterd.rb index 6cd9696..2bffbbc 100644 --- a/cloudmasterd/lib/cloudmasterd.rb +++ b/cloudmasterd/lib/cloudmasterd.rb @@ -18,6 +18,54 @@ Camping.goes :Cloudmasterd Cloudmasterd.picnic! module Cloudmasterd::Helpers + class Syncer + def self.run + # Figure out the current amount of memory + freemem = 0 + `func '*' call virt freemem`.split("\n").each{|mem| freemem = freemem + mem.to_i} + + memory = Cloudmasterd::Models::Memory.find(:all) + if memory.empty? then + Cloudmasterd::Models::Memory.create :free => freemem + else + memory[0].update_attribute(:free, freemem) + end + + current_state = {} + Open3.popen3("func '*' call virt state") do |stdin, stdout, stderr| + # Parse out the hosts based on the func output (stripping off https:// and the port) + hosts = stderr.read.split("\n").collect{|host| host.split(" ")[1].split(":")[1][2..-1]} + + # 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 + end + class Cobblerd def initialize(repo) @repo = repo @@ -81,11 +129,15 @@ module Cloudmasterd::Helpers end module Cloudmasterd::Models + class Memory < Base; end class Machine < Base; end class CreateTheBasics < V 1.0 def self.up - create_table :cloudmasterd_machines do |t| + create_table :cloudmasterd_memory do |t| + t.column :free, :string, :null => false, :limit => 255 + 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 @@ -93,7 +145,8 @@ module Cloudmasterd::Models end end def self.down - drop_table :cloudmasterd_machines + drop_table :cloudmasterd_memory + drop_table :cloudmasterd_machine end end end @@ -174,6 +227,8 @@ module Cloudmasterd::Controllers 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 = Memory.find(:all)[0][:free] render :cloud end end @@ -201,24 +256,30 @@ module Cloudmasterd::Views end def cloud - @machines.keys.each do |email| - div do - h2 email - table do - tr do - th 'Name' - th 'Cloud Instance' - th 'State' - th '' - end - @machines[email].each do |machine| + div.memory! do + h2 "Available cloud memory: #{@memory}" + end + + div.machines! do + @machines.keys.each do |email| + div do + h2 email + table do tr do - td machine.name - td machine.cloud - td machine.state - td do - form(:method => :delete, :action => R(Cloudmasterd::Controllers::Koan, "#{machine.name}")) do - input :type => :submit, :value => 'Destroy' + 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 => R(Cloudmasterd::Controllers::Koan, "#{machine.name}")) do + input :type => :submit, :value => 'Destroy' + end end end end @@ -256,52 +317,21 @@ 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) -end - -def sync_state - current_state = {} - Open3.popen3("func '*' call virt state") do |stdin, stdout, stderr| - # Parse out the hosts based on the func output (stripping off https:// and the port) - hosts = stderr.read.split("\n").collect{|host| host.split(" ")[1].split(":")[1][2..-1]} - - # 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 => machines[:state] - end + # Sync up the state initially + Cloudmasterd::Helpers::Syncer.run end Thread.new do while true do - sync_state + Cloudmasterd::Helpers::Syncer.run sleep 5 end end |