[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Ovirt-devel] [PATCH] added ability to reprovision vm via edit vm form



---
 wui/src/app/controllers/vm_controller.rb |   73 +++++++++++++++++++++++++-----
 wui/src/app/views/vm/_form.rhtml         |    6 ++-
 2 files changed, 65 insertions(+), 14 deletions(-)

diff --git a/wui/src/app/controllers/vm_controller.rb b/wui/src/app/controllers/vm_controller.rb
index e55ec28..82d8952 100644
--- a/wui/src/app/controllers/vm_controller.rb
+++ b/wui/src/app/controllers/vm_controller.rb
@@ -96,6 +96,28 @@ class VmController < ApplicationController
       end
       params[:vm][:needs_restart] = 1 if needs_restart
       @vm.update_attributes!(params[:vm])
+      _setup_vm_provision(params)
+
+      if (params[:start_now] and @vm.get_action_list.include?(VmTask::ACTION_START_VM) )
+        @task = VmTask.new({ :user    => @user,
+                             :vm_id   => @vm.id,
+                             :action  => VmTask::ACTION_START_VM,
+                             :state   => Task::STATE_QUEUED})
+        @task.save!
+      elsif ( params[:restart_now] and @vm.get_action_list.include?(VmTask::ACTION_SHUTDOWN_VM) )
+        @task = VmTask.new({ :user    => @user,
+                             :vm_id   => @vm.id,
+                             :action  => VmTask::ACTION_SHUTDOWN_VM,
+                             :state   => Task::STATE_QUEUED})
+        @task.save!
+        @task = VmTask.new({ :user    => @user,
+                             :vm_id   => @vm.id,
+                             :action  => VmTask::ACTION_START_VM,
+                             :state   => Task::STATE_QUEUED})
+        @task.save!
+      end
+
+
       render :json => { :object => "vm", :success => true, 
                         :alert => 'Vm was successfully updated.'  }
     rescue
@@ -193,6 +215,43 @@ class VmController < ApplicationController
   end
 
   protected
+  def _setup_provisioning_options
+    @provisioning_options = [[Vm::PXE_OPTION_LABEL, Vm::PXE_OPTION_VALUE],
+                             [Vm::HD_OPTION_LABEL, Vm::HD_OPTION_VALUE]]
+    # FIXME add cobbler images too
+    begin
+      @provisioning_options += Cobbler::Profile.find.collect do |profile|
+        [profile.name + Vm::COBBLER_PROFILE_SUFFIX, profile.name]
+
+    end
+    rescue
+      #if cobbler doesn't respond/is misconfigured/etc just don't add profiles
+    end
+  end
+
+  def _setup_vm_provision(params)
+    # spaces are invalid in the cobbler name
+    name = params[:vm][:description].gsub(" ", "-")
+    provision = params[:vm][:provisioning_and_boot_settings]
+    unless provision == Vm::PXE_OPTION_VALUE or
+           provision == Vm::HD_OPTION_VALUE
+      found = false
+      Cobbler::System.find.each{ |system|
+        if system.name == name
+          system.profile = provision
+          system.save
+          found = true
+        end
+      }
+      unless found
+        system = Cobbler::System.create("name" => name,
+                                        "profile" => provision)
+        # do we need to set any of the other system attributes?
+        system.save
+      end
+    end
+  end
+
   def pre_new
     # if no vm_resource_pool is passed in, find (or auto-create) it based on hardware_pool_id
     unless params[:vm_resource_pool_id]
@@ -229,18 +288,7 @@ class VmController < ApplicationController
     @perm_obj = @vm.vm_resource_pool
     @redir_controller = 'resources'
     @current_pool_id= perm_obj id
-    @provisioning_options = [[Vm::PXE_OPTION_LABEL, Vm::PXE_OPTION_VALUE],
-                             [Vm::HD_OPTION_LABEL, Vm::HD_OPTION_VALUE]]
-    # FIXME add cobbler images too
-    begin
-      @provisioning_options += Cobbler::Profile.find.collect do |profile|
-        [profile.name + Vm::COBBLER_PROFILE_SUFFIX,
-         Vm::COBBLER_PREFIX + Vm::PROVISIONING_DELIMITER +
-         Vm::PROFILE_PREFIX + Vm::PROVISIONING_DELIMITER + profile.name]
-      end
-    rescue
-      #if cobbler doesn't respond/is misconfigured/etc just don't add profiles
-    end
+    _setup_provisioning_options
   end
   def pre_create
     params[:vm][:state] = Vm::STATE_PENDING
@@ -267,6 +315,7 @@ class VmController < ApplicationController
     @perm_obj = @vm.vm_resource_pool
     @redir_obj = @vm
     @current_pool_id= perm_obj id
+    _setup_provisioning_options
   end
   def pre_vm_action
     pre_edit
diff --git a/wui/src/app/views/vm/_form.rhtml b/wui/src/app/views/vm/_form.rhtml
index 60ed003..3d4cd79 100644
--- a/wui/src/app/views/vm/_form.rhtml
+++ b/wui/src/app/views/vm/_form.rhtml
@@ -8,7 +8,8 @@
 <%= hidden_field_tag 'hardware_pool_id', @hardware_pool.id if @hardware_pool %>
 
     <%= text_field_with_label "Name:", "vm", "description", {:style=>"width:250px;"}  %>
-    <%= select_with_label "Operating System:", 'vm', 'provisioning_and_boot_settings', @provisioning_options, :style=>"width:250px;"  if create %>
+    <%= select_with_label "Operating System:", 'vm', 'provisioning_and_boot_settings', @provisioning_options, :style=>"width:250px;" %>
+    <% if controller.action_name == "edit" %><b style="color: #FF0000">*Warning* Editing provision could overwrite vm</b><% end %>
     <div class="clear_row" style="height:15px;"></div>
 
     <div class="form_heading">Resources</div>
@@ -50,7 +51,8 @@
     <div class="clear_row"></div>
     <div class="clear_row"></div>
 
-   <%= check_box_tag_with_label "Start VM Now? (pending current resource availability)", "start_now", nil if create%>
+   <%= check_box_tag_with_label "Start VM Now? (pending current resource availability)", "start_now", nil if create or @vm.state == Vm::STATE_STOPPED %>
+   <%= check_box_tag_with_label "Restart VM Now? (pending current resource availability)", "restart_now", nil if @vm.state == Vm::STATE_RUNNING %>
 
 <!--[eoform:vm]-->
 <script type="text/javascript">
-- 
1.5.4.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]