Project

General

Profile

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

foreman-docker / app / controllers / containers_controller.rb @ 7ff105d1

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

    
4
  before_filter :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 container_deletion
24
      process_success(:success_redirect => containers_path,
25
                      :success_msg => (_("Container %s is being deleted.") %
26
                                            @deleted_identifier))
27
    else
28
      process_error(:redirect => containers_path)
29
    end
30
  rescue ActiveRecord::RecordNotFound
31
    not_found
32
  end
33

    
34
  def show
35
  end
36

    
37
  def commit
38
    ForemanDocker::Docker.get_container(@container).commit(:author  => params[:commit][:author],
39
                                                           :repo => params[:commit][:repo],
40
                                                           :tag => params[:commit][:tag],
41
                                                           :comment => params[:commit][:comment])
42

    
43
    process_success :success_redirect => :back,
44
                    :success_msg      => _("%{container} commit was successful") %
45
                      { :container => @container }
46
  rescue => e
47
    process_error :redirect => :back, :error_msg => _("Failed to commit %{container}: %{e}") %
48
      { :container => @container, :e => e }
49
  end
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

    
73
  private
74

    
75
  def action_permission
76
    case params[:action]
77
    when 'auto_complete_repository_name', 'auto_complete_tag', 'search_repository'
78
      :view
79
    when 'commit'
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
91
    else
92
      super
93
    end
94
  end
95

    
96
  def container_deletion
97
    # Unmanaged container - only present in Compute Resource
98
    if params[:compute_resource_id].present?
99
      @deleted_identifier = params[:id]
100
      destroy_compute_resource_vm(params[:compute_resource_id], params[:id])
101
    else # Managed container
102
      find_container
103
      @deleted_identifier = @container.name
104

    
105
      destroy_compute_resource_vm(@container.compute_resource, @container.uuid) &&
106
        @container.destroy
107
    end
108
  end
109

    
110
  def destroy_compute_resource_vm(resource_id, uuid)
111
    @container_resource = ComputeResource.authorized(:destroy_compute_resources_vms)
112
                          .find(resource_id)
113
    @container_resource.destroy_vm(uuid)
114
  rescue => error
115
    logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
116
    false
117
  end
118
end