Project

General

Profile

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

foreman-docker / app / controllers / image_search_controller.rb @ c80f3b10

1
class ImageSearchController < ::ApplicationController
2
  before_filter :find_resource
3

    
4
  # this is incredibly odd. for some reason, rails sees the
5
  # requests ImageSearchControllerTest makes as requests from another host
6
  # thus, violating CSRF.
7
  protect_from_forgery :only => :nothing if Rails.env.test?
8

    
9
  def auto_complete_repository_name
10
    catch_network_errors do
11
      text = if use_hub?
12
               hub_image_exists?(params[:search])
13
             else
14
               registry_image_exists?(params[:search])
15
             end
16
      render :text => text.to_s
17
    end
18
  end
19

    
20
  def auto_complete_image_tag
21
    catch_network_errors do
22
      # This is the format jQuery UI autocomplete expects
23
      tags = if use_hub?
24
               hub_auto_complete_image_tags(params[:search])
25
             else
26
               registry_auto_complete_image_tags(params[:search])
27
             end
28
      respond_to do |format|
29
        format.js do
30
          tags.map! { |tag| { :label => CGI.escapeHTML(tag), :value => CGI.escapeHTML(tag) } }
31
          render :json => tags
32
        end
33
      end
34
    end
35
  end
36

    
37
  def search_repository
38
    catch_network_errors do
39
      repositories = if use_hub?
40
                       hub_search_image(params[:search])
41
                     else
42
                       registry_search_image(params[:search])
43
                     end
44

    
45
      respond_to do |format|
46
        format.js do
47
          render :partial => 'repository_search_results',
48
                 :locals  => { :repositories => repositories,
49
                               :use_hub => use_hub? }
50
        end
51
      end
52
    end
53
  end
54

    
55
  def catch_network_errors
56
    yield
57
  rescue Docker::Error::NotFoundError => e
58
    # not an error
59
    logger.debug "image not found: #{e.backtrace}"
60
    render :js, :nothing => true
61
  rescue Docker::Error::DockerError, Excon::Errors::Error, SystemCallError => e
62
    render :js => _("An error occured during repository search: '%s'") % e.message,
63
           :status => 500
64
  end
65

    
66
  def use_hub?
67
    @registry.nil?
68
  end
69

    
70
  def hub_image_exists?(terms)
71
    @compute_resource.exist?(terms)
72
  end
73

    
74
  def hub_auto_complete_image_tags(terms)
75
    @compute_resource.tags(terms)
76
  end
77

    
78
  def hub_search_image(terms)
79
    @compute_resource.search(terms).map do |item|
80
      # el7 returns -> "name" => "docker.io: docker.io/centos",
81
      # while f20 returns -> "name" => "centos"
82
      # we need repo name to be => "docker.io/centos" for el7 and "centos" for fedora
83
      # to ensure proper search with respect to the tags, image creation etc.
84
      new_item = item.clone
85
      new_item["name"] = item["name"].split.last
86
      new_item
87
    end
88
  end
89

    
90
  def registry_image_exists?(term)
91
    result = ::Service::RegistryApi.new(:url => @registry.url,
92
                                        :user => @registry.username,
93
                                        :password => @registry.password).search(term)
94
    registry_name = if term.split('/').size > 1
95
                      term
96
                    else
97
                      "library/#{term}"
98
                    end
99

    
100
    result['results'].any? { |r| r['name'] == registry_name }
101
  end
102

    
103
  def registry_auto_complete_image_tags(terms)
104
    ::Service::RegistryApi.new(:url => @registry.url,
105
                               :user => @registry.username,
106
                               :password => @registry.password).list_repository_tags(terms).keys
107
  end
108

    
109
  def registry_search_image(terms)
110
    r = ::Service::RegistryApi.new(:url => @registry.url,
111
                                   :user => @registry.username,
112
                                   :password => @registry.password).search(terms)
113
    r['results']
114
  end
115

    
116
  def action_permission
117
    case params[:action]
118
    when 'auto_complete_repository_name', 'auto_complete_image_tag', 'search_repository'
119
      :search_repository
120
    else
121
      super
122
    end
123
  end
124

    
125
  def find_resource
126
    if params[:registry_id].present?
127
      @registry = DockerRegistry.authorized(:view_registries).find(params[:registry_id])
128
    else
129
      @compute_resource = ComputeResource.authorized(:view_compute_resources).find(params[:id])
130
    end
131
  rescue ActiveRecord::RecordNotFound
132
    not_found
133
  end
134
end