Project

General

Profile

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

foreman-docker / app / controllers / containers_controller.rb @ ab73d5cf

1
class ContainersController < ::ApplicationController
2
  before_filter :find_container, :only => [:show,
3
                                           :auto_complete_image,
4
                                           :auto_complete_image_tags,
5
                                           :commit]
6

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

    
18
  def new
19
    @container = Container.create
20
    redirect_to container_step_path(:container_id => @container.id, :id => :preliminary)
21
  end
22

    
23
  def destroy
24
    if resource_deletion
25
      process_success(:success_redirect => containers_path,
26
                      :success_msg      => (_("Container %s is being deleted.") %
27
                                            @deleted_identifier))
28
    else
29
      process_error(:redirect => containers_path)
30
    end
31
  rescue ActiveRecord::RecordNotFound
32
    not_found
33
  end
34

    
35
  def show
36
  end
37

    
38
  def auto_complete_image
39
    if @container.compute_resource.exist?(params[:search])
40
      render :text => 'true'
41
    else
42
      render :text => 'false'
43
    end
44
  end
45

    
46
  def auto_complete_image_tags
47
    images = @container.compute_resource.all_images(params[:search]).map do |image|
48
      image.info['RepoTags'].map do |image_tag|
49
        _, tag = image_tag.split(':')
50
        { :label => CGI.escapeHTML(tag), :value => CGI.escapeHTML(tag) }
51
      end
52
    end
53
    render :json => images.flatten
54
  end
55

    
56
  def commit
57
    Docker::Container.get(@container.uuid).commit(:author  => params[:commit][:author],
58
                                                  :repo    => params[:commit][:repo],
59
                                                  :tag     => params[:commit][:tag],
60
                                                  :comment => params[:commit][:comment])
61

    
62
    process_success :success_redirect => :back,
63
                    :success_msg      => _("%{container} commit was successful") %
64
                                         { :container => @container }
65
  rescue => e
66
    process_error :redirect => :back, :error_msg => _("Failed to commit %{container}: %{e}") %
67
                                                    { :container => @container, :e => e }
68
  end
69

    
70
  private
71

    
72
  def action_permission
73
    case params[:action]
74
    when 'auto_complete_image', 'auto_complete_image_tags'
75
      :view
76
    when 'commit'
77
      :commit
78
    else
79
      super
80
    end
81
  end
82

    
83
  def resource_deletion
84
    # Unmanaged container - only present in Compute Resource
85
    if params[:compute_resource_id].present?
86
      @deleted_identifier  = params[:id]
87
      destroy_compute_resource_vm(params[:compute_resource_id], params[:id])
88
    else # Managed container
89
      find_resource
90
      @deleted_identifier = @container.name
91

    
92
      destroy_compute_resource_vm(@container.compute_resource, @container.uuid) &&
93
      @container.destroy
94
    end
95
  end
96

    
97
  def destroy_compute_resource_vm(resource_id, uuid)
98
    @container_resource = ComputeResource.authorized(:destroy_compute_resources_vms)
99
                                         .find(resource_id)
100
    @container_resource.destroy_vm(uuid)
101
  rescue => error
102
    logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
103
    false
104
  end
105

    
106
  def allowed_resources
107
    ComputeResource.authorized(:view_compute_resources)
108
  end
109

    
110
  # To be replaced by find_resource after 1.6 support is deprecated
111
  def find_container
112
    if params[:id].blank?
113
      not_found
114
      return
115
    end
116
    @container = Container.authorized("#{action_permission}_#{controller_name}".to_sym)
117
                          .find(params[:id])
118
  end
119
end