[Ovirt-devel] [PATCH server] Revised attempt at refactoring the vm_controller using the service layer.

Ian Main imain at redhat.com
Tue Apr 21 20:14:06 UTC 2009


So with this service layer.. with it being included and setting local properties of the object, will that be thread safe?  Or will we have to do locking around each call?

I'm not sure yet if the agent will be threaded.. but it certainly could have a thread per method invocation..

    Ian


> +module VmService
> +
> +  include ApplicationService
> +
> +  def svc_show(vm_id)
> +    # from before_filter
> +    @vm = Vm.find(vm_id)
> +    @current_pool_id=@vm.vm_resource_pool.id
> +    authorized!(Privilege::VIEW, at vm.vm_resource_pool)
> +  end
> +
> +  def svc_create(vm_hash, start_now)
> +    # from before_filter
> +    vm_hash[:state] = Vm::STATE_PENDING
> +    @vm = Vm.new(params[:vm])
> +    @current_pool_id=@vm.vm_resource_pool.id
> +    authorized!(Privilege::MODIFY, at vm.vm_resource_pool)
> +
> +    alert = "VM was successfully created."
> +    Vm.transaction do
> +      @vm.save!
> +      vm_provision
> +      @task = VmTask.new({ :user        => @user,
> +                           :task_target => @vm,
> +                           :action      => VmTask::ACTION_CREATE_VM,
> +                           :state       => Task::STATE_QUEUED})
> +      @task.save!
> +      if start_now
> +        if @vm.get_action_list.include?(VmTask::ACTION_START_VM)
> +          @task = VmTask.new({ :user        => @user,
> +                               :task_target => @vm,
> +                               :action      => VmTask::ACTION_START_VM,
> +                               :state       => Task::STATE_QUEUED})
> +          @task.save!
> +          alert += " VM Start action queued."
> +        else
> +          alert += " Resources are not available to start VM now."
> +        end
> +      end
> +    end
> +    return alert
> +  end
> +
> +  def svc_update(vm_id, vm_hash, start_now, restart_now)
> +    # from before_filter
> +    @vm = Vm.find(vm_id)
> +    @current_pool_id=@vm.vm_resource_pool.id
> +    authorized!(Privilege::MODIFY, @vm.vm_resource_pool)
> +
> +    #needs restart if certain fields are changed
> +    # (since those will only take effect the next startup)
> +    needs_restart = false
> +    unless @vm.get_pending_state == Vm::STATE_STOPPED
> +      Vm::NEEDS_RESTART_FIELDS.each do |field|
> +        unless @vm[field].to_s == vm_hash[field]
> +          needs_restart = true
> +          break
> +        end
> +      end
> +      current_storage_ids = @vm.storage_volume_ids.sort
> +      new_storage_ids = vm_hash[:storage_volume_ids]
> +      new_storage_ids = [] unless new_storage_ids
> +      new_storage_ids = new_storage_ids.sort.collect {|x| x.to_i }
> +      needs_restart = true unless current_storage_ids == new_storage_ids
> +    end
> +    vm_hash[:needs_restart] = 1 if needs_restart
> +
> +
> +    alert = "VM was successfully updated."
> +    Vm.transaction do
> +      @vm.update_attributes!(vm_hash)
> +      vm_provision
> +      if start_now
> +        if @vm.get_action_list.include?(VmTask::ACTION_START_VM)
> +          @task = VmTask.new({ :user        => @user,
> +                               :task_target => @vm,
> +                               :action      => VmTask::ACTION_START_VM,
> +                               :state       => Task::STATE_QUEUED})
> +          @task.save!
> +          alert += " VM Start action queued."
> +        else
> +          alert += " Resources are not available to start VM now."
> +        end
> +      elsif restart_now
> +        if @vm.get_action_list.include?(VmTask::ACTION_SHUTDOWN_VM)
> +          @task = VmTask.new({ :user        => @user,
> +                               :task_target => @vm,
> +                               :action      => VmTask::ACTION_SHUTDOWN_VM,
> +                               :state       => Task::STATE_QUEUED})
> +          @task.save!
> +          @task = VmTask.new({ :user    => @user,
> +                               :task_target => @vm,
> +                               :action  => VmTask::ACTION_START_VM,
> +                               :state   => Task::STATE_QUEUED})
> +          @task.save!
> +          alert += " VM Restart action queued."
> +        else
> +          alert += " Restart action was not available."
> +        end
> +      end
> +    end
> +    return alert
> +  end
> +
> +  def svc_destroy(vm_id)
> +    # from before_filter
> +    @vm = Vm.find(vm_id)
> +    @current_pool_id=@vm.vm_resource_pool.id
> +    authorized!(Privilege::MODIFY, @vm.vm_resource_pool)
> +
> +    unless @vm.is_destroyable?
> +      raise ActionError.new("Virtual Machine must be stopped to delete it")
> +    end
> +    destroy_cobbler_system(@vm)
> +    @vm.destroy
> +    return "Virtual Machine wa ssuccessfully deleted."
> +  end
> +
> +  def svc_vm_action(vm_id, vm_action, action_args)
> +    @vm = Vm.find(vm_id)
> +    @current_pool_id=@vm.vm_resource_pool.id
> +    authorized!(Privilege::MODIFY, @vm.vm_resource_pool)
> +    unless @vm.queue_action(@user, vm_action, action_args)
> +      raise ActionError.new("#{vm_action} is an invalid action.")
> +    end
> +    return "#{vm_action} was successfully queued."
> +  end
> +
> +  def svc_cancel_queued_tasks(vm_id)
> +    @vm = Vm.find(vm_id)
> +    @current_pool_id=@vm.vm_resource_pool.id
> +    authorized!(Privilege::MODIFY, @vm.vm_resource_pool)
> +
> +    Task.transaction do
> +      @vm.tasks.queued.each { |task| task.cancel}
> +    end
> +    return "Queued tasks were successfully canceled."
> +  end
> +
> +  def vm_provision
> +    if @vm.uses_cobbler?
> +      # spaces are invalid in the cobbler name
> +      name = @vm.uuid
> +      system = Cobbler::System.find_one(name)
> +      unless system
> +        system = Cobbler::System.new("name" => name,
> +                                     @vm.cobbler_type => @vm.cobbler_name)
> +        system.interfaces = [Cobbler::NetworkInterface.new(
> +                                    {'mac_address' => @vm.vnic_mac_addr})]
> +        system.save
> +      end
> +    end
> +  end
> +
> +  def destroy_cobbler_system(vm)
> +    # Destroy the Cobbler system first if it's defined
> +    if vm.uses_cobbler?
> +      system = Cobbler::System.find_one(vm.cobbler_system_name)
> +      system.remove if system
> +    end
> +  end
> +
> +end
> diff --git a/src/app/views/vm/_form.rhtml b/src/app/views/vm/_form.rhtml
> index 58a3d61..610f2bc 100644
> --- a/src/app/views/vm/_form.rhtml
> +++ b/src/app/views/vm/_form.rhtml
> @@ -3,8 +3,7 @@
>  <%  start_resources = @vm.vm_resource_pool.available_resources_for_vm(@vm) %>
>  
>  <!--[form:vm]-->
> -<%= hidden_field 'vm', 'vm_resource_pool_id' unless @vm_resource_pool_name %>
> -<%= hidden_field_tag 'vm_resource_pool_name', @vm_resource_pool_name if @vm_resource_pool_name %>
> +<%= hidden_field 'vm', 'vm_resource_pool_id' %>
>  <%= hidden_field_tag 'hardware_pool_id', @hardware_pool.id if @hardware_pool %>
>  
>      <%= text_field_with_label "Name:", "vm", "description", {:style=>"width:250px;"}  %>
> -- 
> 1.6.0.6
> 
> _______________________________________________
> Ovirt-devel mailing list
> Ovirt-devel at redhat.com
> https://www.redhat.com/mailman/listinfo/ovirt-devel




More information about the ovirt-devel mailing list