Project

General

Profile

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

foreman_docker / app / controllers / containers_controller.rb @ ad3852f2

1
# rubocop:disable Metrics/ClassLength
2
class ContainersController < ::ApplicationController
3
  before_filter :find_container, :only => [:show, :auto_complete_repository_name,
4
                                           :auto_complete_tag,
5
                                           :search_repository, :commit]
6
  before_filter :find_registry, :only => [:auto_complete_repository_name,
7
                                          :auto_complete_tag,
8
                                          :search_repository]
9

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

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

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

    
38
  def show
39
  end
40

    
41
  def auto_complete_repository_name
42
    exist = if @registry.nil?
43
              @container.compute_resource.exist?(params[:search])
44
            else
45
              registry_auto_complete_repository(params[:search])
46
            end
47
    render :text => exist.to_s
48
  end
49

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

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

    
72
  def registry_auto_complete_tags(term)
73
    ::Service::RegistryApi.new(:url => @registry.url).list_repository_tags(term).keys
74
  end
75

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

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

    
90
  def search_repository
91
    repositories = if @registry.nil?
92
                     @container.compute_resource.search(params[:search])
93
                   else
94
                     r = ::Service::RegistryApi.new(:url => @registry.url)
95
                       .search(params[:search])
96
                     r['results']
97
                   end
98
    respond_to do |format|
99
      format.js do
100
        render :partial => 'repository_search_results', :locals => { :repositories => repositories }
101
      end
102
    end
103
  end
104

    
105
  private
106

    
107
  def action_permission
108
    case params[:action]
109
    when 'auto_complete_repository_name', 'auto_complete_tag', 'search_repository'
110
      :view
111
    when 'commit'
112
      :commit
113
    else
114
      super
115
    end
116
  end
117

    
118
  def resource_deletion
119
    # Unmanaged container - only present in Compute Resource
120
    if params[:compute_resource_id].present?
121
      @deleted_identifier  = params[:id]
122
      destroy_compute_resource_vm(params[:compute_resource_id], params[:id])
123
    else # Managed container
124
      find_resource
125
      @deleted_identifier = @container.name
126

    
127
      destroy_compute_resource_vm(@container.compute_resource, @container.uuid) &&
128
      @container.destroy
129
    end
130
  end
131

    
132
  def destroy_compute_resource_vm(resource_id, uuid)
133
    @container_resource = ComputeResource.authorized(:destroy_compute_resources_vms)
134
                                         .find(resource_id)
135
    @container_resource.destroy_vm(uuid)
136
  rescue => error
137
    logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
138
    false
139
  end
140

    
141
  def allowed_resources
142
    ForemanDocker::Docker.authorized(:view_compute_resources)
143
  end
144

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

    
155
  def find_registry
156
    return if params[:registry_id].empty?
157
    @registry = DockerRegistry.authorized("#{action_permission}_#{controller_name}".to_sym)
158
    .find(params[:registry_id])
159
  end
160
end