Project

General

Profile

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

foreman_docker / app / controllers / containers_controller.rb @ ada932ab

1
# rubocop:disable Metrics/ClassLength
2
class ContainersController < ::ApplicationController
3
  before_filter :find_container, :only => [:show, :auto_complete_image, :auto_complete_image_tags,
4
                                           :search_image, :commit]
5
  before_filter :find_registry, :only => [:auto_complete_image, :auto_complete_image_tags,
6
                                          :search_image]
7

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

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

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

    
36
  def show
37
  end
38

    
39
  def auto_complete_image
40
    exist = if @registry.nil?
41
              @container.compute_resource.exist?(params[:search])
42
            else
43
              registry_auto_complete_image(params[:search])
44
            end
45
    render :text => exist.to_s
46
  end
47

    
48
  def registry_auto_complete_image(term)
49
    result = ::Service::RegistryApi.new(:url => @registry.url).search(term)
50
    registry_name = term.split('/').size > 1 ? term :
51
        'library/' + term
52
    result['results'].any? { |r| r['name'] == registry_name }
53
  end
54

    
55
  def auto_complete_image_tags
56
    # This is the format jQuery UI autocomplete expects
57
    tags = if @registry.nil?
58
             @container.compute_resource.tags(params[:search])
59
           else
60
             registry_auto_complete_image_tags(params[:search])
61
           end
62
    respond_to do |format|
63
      format.js do
64
        tags.map! { |tag| { :label => CGI.escapeHTML(tag), :value => CGI.escapeHTML(tag) } }
65
        render :json => tags
66
      end
67
    end
68
  end
69

    
70
  def registry_auto_complete_image_tags(term)
71
    ::Service::RegistryApi.new(:url => @registry.url).list_repository_tags(term).keys
72
  end
73

    
74
  def commit
75
    Docker::Container.get(@container.uuid).commit(:author  => params[:commit][:author],
76
                                                  :repo    => params[:commit][:repo],
77
                                                  :tag     => params[:commit][:tag],
78
                                                  :comment => params[:commit][:comment])
79

    
80
    process_success :success_redirect => :back,
81
                    :success_msg      => _("%{container} commit was successful") %
82
                                         { :container => @container }
83
  rescue => e
84
    process_error :redirect => :back, :error_msg => _("Failed to commit %{container}: %{e}") %
85
                                                    { :container => @container, :e => e }
86
  end
87

    
88
  def search_image
89
    images = if @registry.nil?
90
               @container.compute_resource.search(params[:search])
91
             else
92
               r = ::Service::RegistryApi.new(:url => @registry.url).search(params[:search])
93
               r['results']
94
             end
95
    respond_to do |format|
96
      format.js { render :partial => 'image_search_results', :locals => { :images => images } }
97
    end
98
  end
99

    
100
  private
101

    
102
  def action_permission
103
    case params[:action]
104
    when 'auto_complete_image', 'auto_complete_image_tags', 'search_image'
105
      :view
106
    when 'commit'
107
      :commit
108
    else
109
      super
110
    end
111
  end
112

    
113
  def resource_deletion
114
    # Unmanaged container - only present in Compute Resource
115
    if params[:compute_resource_id].present?
116
      @deleted_identifier  = params[:id]
117
      destroy_compute_resource_vm(params[:compute_resource_id], params[:id])
118
    else # Managed container
119
      find_resource
120
      @deleted_identifier = @container.name
121

    
122
      destroy_compute_resource_vm(@container.compute_resource, @container.uuid) &&
123
      @container.destroy
124
    end
125
  end
126

    
127
  def destroy_compute_resource_vm(resource_id, uuid)
128
    @container_resource = ComputeResource.authorized(:destroy_compute_resources_vms)
129
                                         .find(resource_id)
130
    @container_resource.destroy_vm(uuid)
131
  rescue => error
132
    logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
133
    false
134
  end
135

    
136
  def allowed_resources
137
    ComputeResource.authorized(:view_compute_resources)
138
  end
139

    
140
  # To be replaced by find_resource after 1.6 support is deprecated
141
  def find_container
142
    if params[:id].blank?
143
      not_found
144
      return
145
    end
146
    @container = Container.authorized("#{action_permission}_#{controller_name}".to_sym)
147
                          .find(params[:id])
148
  end
149

    
150
  def find_registry
151
    return if params[:registry_id].empty?
152
    @registry = DockerRegistry.authorized("#{action_permission}_#{controller_name}".to_sym)
153
    .find(params[:registry_id])
154
  end
155
end