Class: ImageSearchController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ImageSearchController
- Defined in:
- app/controllers/image_search_controller.rb
Instance Method Summary collapse
- #action_permission ⇒ Object
- #auto_complete_image_tag ⇒ Object
- #auto_complete_repository_name ⇒ Object
- #catch_network_errors ⇒ Object
- #find_resource ⇒ Object
- #hub_auto_complete_image_tags(terms) ⇒ Object
- #hub_image_exists?(terms) ⇒ Boolean
- #hub_search_image(terms) ⇒ Object
- #registry_auto_complete_image_tags(terms) ⇒ Object
- #registry_image_exists?(term) ⇒ Boolean
- #registry_search_image(terms) ⇒ Object
- #search_repository ⇒ Object
- #use_hub? ⇒ Boolean
Instance Method Details
#action_permission ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'app/controllers/image_search_controller.rb', line 116 def case params[:action] when 'auto_complete_repository_name', 'auto_complete_image_tag', 'search_repository' :search_repository else super end end |
#auto_complete_image_tag ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/controllers/image_search_controller.rb', line 20 def auto_complete_image_tag catch_network_errors do # This is the format jQuery UI autocomplete expects = if use_hub? (params[:search]) else (params[:search]) end respond_to do |format| format.js do .map! { |tag| { :label => CGI.escapeHTML(tag), :value => CGI.escapeHTML(tag) } } render :json => end end end end |
#auto_complete_repository_name ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'app/controllers/image_search_controller.rb', line 9 def auto_complete_repository_name catch_network_errors do text = if use_hub? hub_image_exists?(params[:search]) else registry_image_exists?(params[:search]) end render :text => text.to_s end end |
#catch_network_errors ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'app/controllers/image_search_controller.rb', line 55 def catch_network_errors yield rescue Docker::Error::NotFoundError => e # not an error logger.debug "image not found: #{e.backtrace}" render :js, :nothing => true rescue Docker::Error::DockerError, Excon::Errors::Error, SystemCallError => e render :js => _("An error occured during repository search: '%s'") % e., :status => 500 end |
#find_resource ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'app/controllers/image_search_controller.rb', line 125 def find_resource if params[:registry_id].present? @registry = DockerRegistry.(:view_registries).find(params[:registry_id]) else @compute_resource = ComputeResource.(:view_compute_resources).find(params[:id]) end rescue ActiveRecord::RecordNotFound not_found end |
#hub_auto_complete_image_tags(terms) ⇒ Object
74 75 76 |
# File 'app/controllers/image_search_controller.rb', line 74 def (terms) @compute_resource.(terms) end |
#hub_image_exists?(terms) ⇒ Boolean
70 71 72 |
# File 'app/controllers/image_search_controller.rb', line 70 def hub_image_exists?(terms) @compute_resource.exist?(terms) end |
#hub_search_image(terms) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/image_search_controller.rb', line 78 def hub_search_image(terms) @compute_resource.search(terms).map do |item| # el7 returns -> "name" => "docker.io: docker.io/centos", # while f20 returns -> "name" => "centos" # we need repo name to be => "docker.io/centos" for el7 and "centos" for fedora # to ensure proper search with respect to the tags, image creation etc. new_item = item.clone new_item["name"] = item["name"].split.last new_item end end |
#registry_auto_complete_image_tags(terms) ⇒ Object
103 104 105 106 107 |
# File 'app/controllers/image_search_controller.rb', line 103 def (terms) ::Service::RegistryApi.new(:url => @registry.url, :user => @registry.username, :password => @registry.password).(terms).keys end |
#registry_image_exists?(term) ⇒ Boolean
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/controllers/image_search_controller.rb', line 90 def registry_image_exists?(term) result = ::Service::RegistryApi.new(:url => @registry.url, :user => @registry.username, :password => @registry.password).search(term) registry_name = if term.split('/').size > 1 term else "library/#{term}" end result['results'].any? { |r| r['name'] == registry_name } end |
#registry_search_image(terms) ⇒ Object
109 110 111 112 113 114 |
# File 'app/controllers/image_search_controller.rb', line 109 def registry_search_image(terms) r = ::Service::RegistryApi.new(:url => @registry.url, :user => @registry.username, :password => @registry.password).search(terms) r['results'] end |
#search_repository ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/controllers/image_search_controller.rb', line 37 def search_repository catch_network_errors do repositories = if use_hub? hub_search_image(params[:search]) else registry_search_image(params[:search]) end respond_to do |format| format.js do render :partial => 'repository_search_results', :locals => { :repositories => repositories, :use_hub => use_hub? } end end end end |
#use_hub? ⇒ Boolean
66 67 68 |
# File 'app/controllers/image_search_controller.rb', line 66 def use_hub? @registry.nil? end |