Project

General

Profile

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

foreman-docker / app / models / docker_registry.rb @ cc6d0d73

1
class DockerRegistry < ActiveRecord::Base
2
  include Authorizable
3
  include Taxonomix
4
  include Encryptable
5

    
6
  default_scope do
7
    with_taxonomy_scope do
8
      order('docker_registries.name')
9
    end
10
  end
11

    
12
  has_many :containers, :foreign_key => "registry_id", :dependent => :destroy
13
  encrypts :password
14

    
15
  validates_lengths_from_database
16
  validates :name, :presence => true, :uniqueness => true
17
  validates :url,  :presence => true, :uniqueness => true,
18
    :url_schema => ['http', 'https']
19
  validate :attempt_login
20

    
21
  scoped_search :on => :name, :complete_value => true
22
  scoped_search :on => :url
23

    
24
  def used_location_ids
25
    Location.joins(:taxable_taxonomies).where(
26
      'taxable_taxonomies.taxable_type' => 'DockerRegistry',
27
      'taxable_taxonomies.taxable_id' => id).pluck("#{Taxonomy.table_name}.id")
28
  end
29

    
30
  def used_organization_ids
31
    Organization.joins(:taxable_taxonomies).where(
32
      'taxable_taxonomies.taxable_type' => 'DockerRegistry',
33
      'taxable_taxonomies.taxable_id' => id).pluck("#{Taxonomy.table_name}.id")
34
  end
35

    
36
  def prefixed_url(image_name)
37
    uri = URI(url)
38
    "#{uri.hostname}:#{uri.port}/#{image_name}"
39
  end
40

    
41
  def self.humanize_class_name(_name = nil)
42
    _("Docker/Registry")
43
  end
44

    
45
  private
46

    
47
  def attempt_login
48
    login_endpoint = RestClient::Resource.new(url + '/v1/users',
49
                                              :user => username,
50
                                              :password => password)
51
    login_endpoint.get == "\"OK\""
52
  rescue => e
53
    errors.add(:base, _('Unable to log in to this Docker Registry - %s') % e)
54
  end
55
end