Project

General

Profile

Download (3.93 KB) Statistics
| Branch: | Tag: | Revision:

foreman_docker / app / controllers / containers_controller.rb @ 401c0065

1
class ContainersController < ::ApplicationController
2
  include ForemanDocker::FindContainer
3

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

    
6
  def index
7
    @container_resources = allowed_resources
8
    if @container_resources.empty?
9
      warning('You need a Compute Resource of type Docker to start managing containers')
10
      redirect_to new_compute_resource_path
11
    end
12
  # This should only rescue Fog::Errors, but Fog returns all kinds of errors...
13
  rescue
14
    process_error
15
  end
16

    
17
  def new
18
    redirect_to wizard_state_step_path(:wizard_state_id => DockerContainerWizardState.create.id,
19
                                       :id => :preliminary)
20
  end
21

    
22
  def destroy
23
    if (deleted_identifier = container_deletion)
24
      process_success(:success_redirect => containers_path,
25
                      :success_msg => (_("Container %s is being deleted.") %
26
                                       deleted_identifier))
27
    else
28
      error(_('Your container could not be deleted in Docker'))
29
      if @container.present?
30
        process_error(:redirect => containers_path)
31
      else
32
        redirect_back(:fallback_location => containers_path)
33
      end
34
    end
35
  rescue ActiveRecord::RecordNotFound
36
    not_found
37
  end
38

    
39
  def show
40
  end
41

    
42
  def commit
43
    ForemanDocker::Docker.get_container(@container).commit(:author  => params[:commit][:author],
44
                                                           :repo => params[:commit][:repo],
45
                                                           :tag => params[:commit][:tag],
46
                                                           :comment => params[:commit][:comment])
47

    
48
    process_success :success_redirect => :back,
49
                    :success_msg      => _("%{container} commit was successful") %
50
                      { :container => @container }
51
  rescue => e
52
    process_error :redirect => :back, :error_msg => _("Failed to commit %{container}: %{e}") %
53
      { :container => @container, :e => e }
54
  end
55

    
56
  def power
57
    compute_resource = @container.compute_resource
58
    @docker_container = compute_resource.find_vm_by_uuid(@container.uuid)
59
    run_container_action(@docker_container.ready? ? :stop : :start)
60
  end
61

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

    
78
  private
79

    
80
  def action_permission
81
    case params[:action]
82
    when 'auto_complete_repository_name', 'auto_complete_tag', 'search_repository'
83
      :view
84
    when 'commit'
85
      :commit
86
    when 'power'
87
      :power_compute_resources_vms
88
    else
89
      super
90
    end
91
  end
92

    
93
  def current_permission
94
    if params[:action] == 'power'
95
      :power_compute_resources_vms
96
    else
97
      super
98
    end
99
  end
100

    
101
  def container_deletion
102
    # TODO: Refactor to recognize params[:compute_resource_id] as well.
103
    compute_resource_id = params[:compute_resource_id].present? ? params[:compute_resource_id] : nil
104
    if compute_resource_id
105
      container_uuid = params[:id]
106
      @container ||= Container.authorized("#{action_permission}_#{controller_name}".to_sym).find_by_uuid(container_uuid)
107
    else
108
      find_container
109
      compute_resource_id = @container.compute_resource_id
110
      container_uuid      = @container.uuid
111
    end
112

    
113
    deleted_identifier = ForemanDocker::ContainerRemover.remove_unmanaged(
114
      compute_resource_id, container_uuid)
115
    @container.destroy if @container.present?
116
    deleted_identifier
117
  end
118
end