Project

General

Profile

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

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

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

    
4
  def auto_complete_repository_name
5
    catch_network_errors do
6
      text = if use_hub?
7
               hub_image_exists?(params[:search])
8
             else
9
               registry_image_exists?(params[:search])
10
             end
11
      render :text => text.to_s
12
    end
13
  end
14

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

    
32
  def search_repository
33
    catch_network_errors do
34
      repositories = if use_hub?
35
                       hub_search_image(params[:search])
36
                     else
37
                       registry_search_image(params[:search])
38
                     end
39

    
40
      respond_to do |format|
41
        format.js do
42
          render :partial => 'repository_search_results',
43
                 :locals  => { :repositories => repositories,
44
                               :use_hub => use_hub? }
45
        end
46
      end
47
    end
48
  end
49

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

    
61
  def use_hub?
62
    @registry.nil?
63
  end
64

    
65
  def hub_image_exists?(terms)
66
    @compute_resource.exist?(terms)
67
  end
68

    
69
  def hub_auto_complete_image_tags(terms)
70
    @compute_resource.tags(terms)
71
  end
72

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

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

    
95
    result['results'].any? { |r| r['name'] == registry_name }
96
  end
97

    
98
  def registry_auto_complete_image_tags(terms)
99
    ::Service::RegistryApi.new(:url => @registry.url,
100
                               :user => @registry.username,
101
                               :password => @registry.password).list_repository_tags(terms).keys
102
  end
103

    
104
  def registry_search_image(terms)
105
    r = ::Service::RegistryApi.new(:url => @registry.url,
106
                                   :user => @registry.username,
107
                                   :password => @registry.password).search(terms)
108
    r['results']
109
  end
110

    
111
  def action_permission
112
    case params[:action]
113
    when 'auto_complete_repository_name', 'auto_complete_image_tag', 'search_repository'
114
      :search_repository
115
    else
116
      super
117
    end
118
  end
119

    
120
  def find_resource
121
    if params[:registry_id].present?
122
      @registry = DockerRegistry.authorized(:view_registries).find(params[:registry_id])
123
    else
124
      @compute_resource = ComputeResource.authorized(:view_compute_resources).find(params[:id])
125
    end
126
  rescue ActiveRecord::RecordNotFound
127
    not_found
128
  end
129
end