Project

General

Profile

Revision e6eb1c48

Added by Dmitri Dolguikh over 8 years ago

fixes #8033: container power on/off redirects to container view now

View differences:

app/controllers/containers_controller.rb
1 1
class ContainersController < ::ApplicationController
2 2
  include ForemanDocker::FindContainer
3 3

  
4
  before_filter :find_container, :only => [:show, :commit]
4
  before_filter :find_container, :only => [:show, :commit, :power]
5 5

  
6 6
  def index
7 7
    @container_resources = allowed_resources
......
48 48
      { :container => @container, :e => e }
49 49
  end
50 50

  
51
  def power
52
    compute_resource = @container.compute_resource
53
    @docker_container = compute_resource.find_vm_by_uuid(@container.uuid)
54
    run_container_action(@docker_container.ready? ? :stop : :start)
55
  end
56

  
57
  def run_container_action(action)
58
    if @docker_container.send(action)
59
      @docker_container.reload
60
      notice _("%{vm} is now %{vm_state}") %
61
        { :vm => @docker_container, :vm_state => @docker_container.state.capitalize }
62
      redirect_to containers_path(:id => @container.id)
63
    else
64
      error _("failed to %{action} %{vm}") % { :action => _(action), :vm => @docker_container }
65
      redirect_to :back
66
    end
67
    # This should only rescue Fog::Errors, but Fog returns all kinds of errors...
68
  rescue => e
69
    error _("Error - %{message}") % { :message => _(e.message) }
70
    redirect_to :back
71
  end
72

  
51 73
  private
52 74

  
53 75
  def action_permission
......
56 78
      :view
57 79
    when 'commit'
58 80
      :commit
81
    when 'power'
82
      :power_compute_resources_vms
83
    else
84
      super
85
    end
86
  end
87

  
88
  def current_permission
89
    if params[:action] == 'power'
90
      :power_compute_resources_vms
59 91
    else
60 92
      super
61 93
    end
app/helpers/containers_helper.rb
39 39
        button_group(
40 40
          link_to(_('Commit'), '#commit-modal', :'data-toggle' => 'modal')
41 41
        ),
42
        button_group(vm_power_action(container.in_fog)),
42
        button_group(container_power_action(container.in_fog)),
43 43
        button_group(
44 44
          display_delete_if_authorized(
45 45
            hash_for_container_path(:id => container.id)
......
51 51
    )
52 52
  end
53 53

  
54
  def container_power_action(vm, authorizer = nil)
55
    if managed?(vm, @compute_resource)
56
      id = Container.find_by_uuid(vm.identity).id
57
      opts = hash_for_power_container_path(:id => id)
58
             .merge(:auth_object => @compute_resource,
59
                    :permission => 'power_compute_resources_vms',
60
                    :authorizer => authorizer)
61
    else
62
      opts = hash_for_power_compute_resource_vm_path(:compute_resource_id => @compute_resource,
63
                                                     :id => vm.identity)
64
             .merge(:auth_object => @compute_resource, :permission => 'power_compute_resources_vms',
65
                    :authorizer => authorizer)
66
    end
67
    html = if vm.ready?
68
             { :confirm => power_on_off_message(vm), :class => "btn btn-danger" }
69
           else
70
             { :class => "btn btn-info" }
71
           end
72

  
73
    display_link_if_authorized "Power #{action_string(vm)}", opts, html.merge(:method => :put)
74
  end
75

  
76
  def power_on_off_message(vm)
77
    _("Are you sure you want to power %{act} %{vm}?") % { :act => action_string(vm).downcase.strip,
78
                                                          :vm => vm  }
79
  end
80

  
54 81
  def auto_complete_docker_search(name, val, options = {})
55 82
    addClass options, 'form-control'
56 83
    text_field_tag(name, val, options)
app/views/compute_resources_vms/index/_docker.html.erb
20 20
      <td> <%= vm.attributes['status'] %> </td>
21 21
      <td> <span <%= vm_power_class(vm.ready?) %>> <%= vm_state(vm) %></span> </td>
22 22
      <td>
23
        <%= action_buttons(vm_power_action(vm),
23
        <%= action_buttons(container_power_action(vm),
24 24
                           display_delete_if_authorized(hash_for_compute_resource_vm_path(:compute_resource_id => @compute_resource, :id => vm.id))) %>
25 25
      </td>
26 26

  
app/views/containers/_list.html.erb
26 26
      <td class="hidden-tablet hidden-xs text-center"><%= link_to resource, compute_resource_path(resource) %> </td>
27 27
      <td class="hidden-tablet hidden-xs text-center"><%= managed_icon(container, resource) %></td>
28 28
      <% @compute_resource = resource %>
29
      <td><%= action_buttons(vm_power_action(container),
29
      <td><%= action_buttons(container_power_action(container),
30 30
                             display_delete_if_authorized(hash_for_container_path(:compute_resource_id => resource,
31 31
                                                          :id => container.id).
32 32
                                                          merge(:auth_object => resource, :auth_action => 'destroy',
app/views/containers/show.html.erb
47 47
              <td><%= _('Environment Variables') %></td>
48 48
              <td>
49 49
                <table id="environment_variables" class="table table-bordered" style="table-layout:fixed; word-wrap: break-word">
50
                  <% @container.in_fog.attributes['config_env'].each do |environment_variable| %>
50
                  <% (@container.in_fog.attributes['config_env'] || []).each do |environment_variable| %>
51 51
                  <% pair = environment_variable.split("=") %>
52 52
                    <tr>
53 53
                      <td><b><%= pair.first %></b></td>
config/routes.rb
2 2
  resources :containers, :only => [:index, :new, :show, :destroy] do
3 3
    member do
4 4
      post :commit
5
      put :power
5 6
    end
6 7
  end
7 8

  
lib/foreman_docker/engine.rb
62 62
          permission :create_containers,  :'containers/steps' => [:show, :update],
63 63
                                          :containers         => [:new]
64 64
          permission :destroy_containers, :containers         => [:destroy]
65
          permission :power_compute_resources_vms, :containers => [:power]
65 66
        end
66 67

  
67 68
        security_block :registries do

Also available in: Unified diff