i split patch and reformat it<br>first part is  for set actual data to db about vm<br>second add wui button for enable/disable restart vm if crash host and code for do it (without fencing mechanism)<br><br>if source corrupt by mail agent i attach both files<br>
<br><b>PATCH.1</b><br><br>diff --git a/src/db-omatic/db_omatic.rb b/src/db-omatic/db_omatic.rb<br>index 4afffb1..c499610 100755<br>--- a/src/db-omatic/db_omatic.rb<br>+++ b/src/db-omatic/db_omatic.rb<br>@@ -74,6 +74,64 @@ class DbOmatic < Qpid::Qmf::Console<br>
         domain[:synced] = true<br>     end<br> <br>+    #find hostname from values['node'] where values['class_type'] == 'domain'<br>+    def get_host_id(abank,bbank)<br>+     begin<br>+       @cached_objects.keys.each do |objkey|<br>
+         if @cached_objects[objkey][:agent_bank].to_s == abank and @cached_objects[objkey][:broker_bank].to_s == bbank and @cached_objects[objkey][:class_type].to_s == 'node'<br>+               return Host.find(:first, :conditions => ['hostname = ?',@cached_objects[objkey]["hostname"].to_s]).id<br>
+                   break<br>+         end<br>+       end<br>+      rescue => ex<br>+       log("error in get_host_id")<br>+       log(ex)<br>+     end<br>+    end<br>+<br>+    def set_host(values,digit)<br>+      begin<br>
+       vm = Vm.find(:first, :conditions => ['description = ?',values["name"].to_s])<br>+       if vm and digit<br>+       vm.host_id = digit<br>+       vm.save!<br>+       else<br>+               log("this vm not exist #{values["name"]}")<br>
+       end<br>+      rescue => ex<br>+       puts "error when set_host for #{values["name"]}"<br>+       puts ex<br>+      end<br>+    end<br>+<br>+    def start_crashed_vm(vm)<br>+       task = VmTask.new( :user => 'db-omatic', :task_target => vm, :action => 'start_vm', :state => 'queued')<br>
+       task.save!<br>+       log("set task for start crashed vm #{<a href="http://vm.id">vm.id</a>}")<br>+    end<br>+<br>+    def set_domain_stopped(domain)<br>+      begin<br>+        vm = Vm.find(:first, :conditions => ['uuid = ?', domain['uuid']])<br>
+        if vm != nil<br>+         curstate = vm.state<br>+          vm.state = Vm::STATE_STOPPED<br>+          vm.host_id = nil<br>+          vm.save<br>+         domain['state'] = 'crashed'  # and now i will use ipmi for reboot anavailable host - simple fencing<br>
+         if curstate == Vm::STATE_RUNNING and vm.ha # vm.ha true or false<br>+            start_crashed_vm(vm)<br>+         end<br>+        else<br>+          log('vm == nil ')<br>+        end<br>+        log("domain  #{domain['id']} already stopped")<br>
+      rescue => ex<br>+        log("can\'t set domain #{domain['id']} stopped")<br>+       log(ex)<br>+      end<br>+    end<br>+<br>     def update_host_state(host_info, state)<br>         db_host = Host.find(:first, :conditions => [ "hostname = ?", host_info['hostname'] ])<br>
         if db_host<br>@@ -131,6 +189,7 @@ class DbOmatic < Qpid::Qmf::Console<br> <br>             domain_state_change = false<br> <br>+        change_node = false<br>             obj.properties.each do |key, newval|<br>
                 if values[key.to_s] != newval<br>                     values[key.to_s] = newval<br>@@ -138,12 +197,30 @@ class DbOmatic < Qpid::Qmf::Console<br>                     if type == "domain" and key.to_s == "state"<br>
                         domain_state_change = true<br>                     end<br>+                   if type == "domain" and key.to_s == "node"<br>+                       change_node = true<br>+                   end<br>
+<br>+<br>                 end<br>             end<br> <br>             if domain_state_change<br>                 update_domain_state(values)<br>             end<br>+       if change_node          <br>+       values.each do |key,val|<br>
+         if key == 'state' and val == 'running'<br>+               abank = values['node'].to_s.split('-')[3]<br>+               bbank = values['node'].to_s.split('-')[4]<br>
+               @@host_id = get_host_id(abank,bbank)<br>+               set_host(values,@@host_id)<br>+               log("update node data for #{values['name']}")<br>+               break<br>+         end<br>
+       end<br>+       end<br>+<br> <br>             if new_object<br>                 if type == "node"<br>@@ -187,11 +264,6 @@ class DbOmatic < Qpid::Qmf::Console<br>         end<br>     end<br> <br>-<br>-    def del_agent(agent)<br>
-        agent_disconnected(agent)<br>-    end<br>-<br>     # This method marks objects associated with the given agent as timed out/invalid.  Called either<br>     # when the agent heartbeats out, or we get a del_agent callback.<br>
     def agent_disconnected(agent)<br>@@ -205,8 +277,10 @@ class DbOmatic < Qpid::Qmf::Console<br>                     if values[:class_type] == 'node'<br>                         update_host_state(values, Host::STATE_UNAVAILABLE)<br>
                     elsif values[:class_type] == 'domain'<br>-                        update_domain_state(values, Vm::STATE_UNREACHABLE)<br>-                    end<br>+                       set_domain_stopped(values)<br>
+                       values[:timed_out] = true<br>+                   @cached_objects.delete(objkey)<br>+                   end<br>                 end<br>             values[:timed_out] = true<br>             end<br>@@ -248,6 +322,7 @@ class DbOmatic < Qpid::Qmf::Console<br>
         db_vm = Vm.find(:all)<br>         db_vm.each do |vm|<br>             log "Marking vm #{vm.description} as stopped."<br>+        vm.host_id = nil<br>             vm.state = Vm::STATE_STOPPED<br>             vm.save<br>
         end<br><br><b>PATCH.2</b><br><br>diff --git a/src/app/controllers/vm_controller.rb b/src/app/controllers/vm_controller.rb<br>index 56501fd..0f43680 100644<br>--- a/src/app/controllers/vm_controller.rb<br>+++ b/src/app/controllers/vm_controller.rb<br>
@@ -20,11 +20,22 @@ require 'socket'<br> <br> class VmController < ApplicationController<br>   # GETs should be safe (see <a href="http://www.w3.org/2001/tag/doc/whenToUseGet.html">http://www.w3.org/2001/tag/doc/whenToUseGet.html</a>)<br>
-  verify :method => :post, :only => [ :destroy, :create, :update ],<br>+  verify :method => :post, :only => [ :destroy, :create, :update , :change_ha_vm],<br>          :redirect_to => { :controller => 'dashboard' }<br>
 <br>   before_filter :pre_vm_action, :only => [:vm_action, :cancel_queued_tasks, :console]<br> <br>+   def change_ha_vm<br>+     vm = Vm.find_by_id(params[:id])<br>+     if vm.ha <br>+       then vm.ha = false<br>+       else vm.ha = true<br>
+     end<br>+     vm.save!   <br>+     alert = "vm was change ha successfully "<br>+     render :json => { :object => "vm", :success => true, :alert => alert  }<br>+   end<br>+<br>   def index<br>
       roles = "('" +<br>           Permission::roles_for_privilege(Permission::PRIV_VIEW).join("', '") +<br>diff --git a/src/app/views/vm/show.rhtml b/src/app/views/vm/show.rhtml<br>index f361131..4809bbe 100644<br>
--- a/src/app/views/vm/show.rhtml<br>+++ b/src/app/views/vm/show.rhtml<br>@@ -29,6 +29,9 @@<br>         </a><br>       <% end -%><br>     <% end %><br>+    <a href="#" onClick="change_ha_vm()" rel="facebox[.bolder]"><br>
+      <%= image_tag "icon_x.png" %> Change HA<br>+    </a><br>     <a href="#confirm_cancel"  rel="facebox[.bolder]"><br>       <%= image_tag "icon_x.png" %> Cancel queued tasks<br>
     </a> <br>@@ -40,6 +43,19 @@<br> <%= confirmation_dialog("confirm_cancel", "Are you sure?", "cancel_queued_tasks()") %><br> <%= confirmation_dialog("confirm_delete", "Are you sure?", "delete_vm()") %><br>
 <script type="text/javascript"><br>+   function change_ha_vm()<br>+   {<br>+     $(document).trigger('close.facebox');<br>+     $.post('<%= url_for :controller => "vm", :action => "change_ha_vm", :id => @vm %>',<br>
+            {x: 1},<br>+             function(data,status){<br>+               $("#vms_grid").flexReload();<br>+               if (data.alert) {<br>+                 $.jGrowl(data.alert);<br>+               }<br>
+               empty_summary('vms_selection', 'Virtual Machine');<br>+             }, 'json');<br>+   }<br>   function cancel_queued_tasks()<br>   {<br>     $(document).trigger('close.facebox');<br>
<br>diff --git a/src/db/migrate/006_create_vms.rb b/src/db/migrate/006_create_vms.rb<br>index 74794b6..a5460f0 100644<br>--- a/src/db/migrate/006_create_vms.rb<br>+++ b/src/db/migrate/006_create_vms.rb<br>@@ -34,6 +34,7 @@ class CreateVms < ActiveRecord::Migration<br>
       t.string  :boot_device,    :null => false<br>       t.integer :vnc_port<br>       t.integer :lock_version,   :default => 0<br>+      t.boolean :ha , :default => true <br>     end<br>     execute "alter table vms add constraint fk_vms_hosts<br>
              foreign key (host_id) references hosts(id)"<br>-- <br>С уважением, Дмитрий.<br>