Project

General

Profile

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

foreman_docker / app / controllers / containers_controller.rb @ 367f39d2

1 fa9ff801 Daniel Lobato
class ContainersController < ::ApplicationController
2 e4e8739b Daniel Lobato
  include ForemanDocker::FindContainer
3
4 e6eb1c48 Dmitri Dolguikh
  before_filter :find_container, :only => [:show, :commit, :power]
5 f880f56e Daniel Lobato
6 fa9ff801 Daniel Lobato
  def index
7 ad3852f2 Ohad Levy
    @container_resources = allowed_resources
8 466cd5ba Daniel Lobato
    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 9c1fa5fb Daniel Lobato
  # This should only rescue Fog::Errors, but Fog returns all kinds of errors...
13
  rescue
14 fa9ff801 Daniel Lobato
    process_error
15
  end
16
17
  def new
18 e4e8739b Daniel Lobato
    redirect_to wizard_state_step_path(:wizard_state_id => DockerContainerWizardState.create.id,
19 5397ee73 David Davis
                                       :id => :preliminary)
20 fa9ff801 Daniel Lobato
  end
21
22 ebaa383d Daniel Lobato
  def destroy
23 bbb77219 Daniel Lobato
    if (deleted_identifier = container_deletion)
24 38378df7 Daniel Lobato
      process_success(:success_redirect => containers_path,
25 5397ee73 David Davis
                      :success_msg => (_("Container %s is being deleted.") %
26 bbb77219 Daniel Lobato
                                       deleted_identifier))
27 38378df7 Daniel Lobato
    else
28 bbb77219 Daniel Lobato
      error(_('Your container could not be deleted in Docker'))
29
      if @container.present?
30
        process_error(:redirect => containers_path)
31
      else
32
        redirect_to :back
33
      end
34 38378df7 Daniel Lobato
    end
35
  rescue ActiveRecord::RecordNotFound
36
    not_found
37 ebaa383d Daniel Lobato
  end
38
39 f880f56e Daniel Lobato
  def show
40
  end
41
42 387babdd Daniel Lobato
  def commit
43 5397ee73 David Davis
    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 387babdd Daniel Lobato
48
    process_success :success_redirect => :back,
49
                    :success_msg      => _("%{container} commit was successful") %
50 ac230210 David Davis
                      { :container => @container }
51 387babdd Daniel Lobato
  rescue => e
52
    process_error :redirect => :back, :error_msg => _("Failed to commit %{container}: %{e}") %
53 ac230210 David Davis
      { :container => @container, :e => e }
54 387babdd Daniel Lobato
  end
55
56 e6eb1c48 Dmitri Dolguikh
  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_to :back
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_to :back
76
  end
77
78 fa9ff801 Daniel Lobato
  private
79
80 fe8bb25c Dmitri Dolguikh
  def action_permission
81
    case params[:action]
82 c57dae56 David Davis
    when 'auto_complete_repository_name', 'auto_complete_tag', 'search_repository'
83 387babdd Daniel Lobato
      :view
84
    when 'commit'
85
      :commit
86 e6eb1c48 Dmitri Dolguikh
    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 fe8bb25c Dmitri Dolguikh
    else
97
      super
98
    end
99
  end
100
101 e4e8739b Daniel Lobato
  def container_deletion
102 367f39d2 Sebastian Gräßl
    # TODO: Refactor to recognize params[:compute_resource_id] as well.
103
    compute_resource_id = params[:compute_resource_id].tap { |id| id.present? ? id : nil }
104
    if compute_resource_id
105
      container_uuid = params[:id]
106 b2f2c848 Shlomi Zadok
      @container ||= Container.authorized("#{action_permission}_#{controller_name}".to_sym).find_by_uuid(container_uuid)
107 bbb77219 Daniel Lobato
    else
108 30891c0a Daniel Lobato
      find_container
109 bbb77219 Daniel Lobato
      compute_resource_id = @container.compute_resource_id
110
      container_uuid      = @container.uuid
111 ebaa383d Daniel Lobato
    end
112
113 bbb77219 Daniel Lobato
    deleted_identifier = ForemanDocker::ContainerRemover.remove_unmanaged(
114
      compute_resource_id, container_uuid)
115
    @container.destroy if @container.present?
116
    deleted_identifier
117 38378df7 Daniel Lobato
  end
118 fa9ff801 Daniel Lobato
end