Project

General

Profile

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

foreman-docker / app / models / docker_registry.rb @ 04b6e05e

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
  def api
46
    @api ||= Service::RegistryApi.new(url: url, user: username,
47
                                      password: password, verify_ssl: verify_ssl)
48
  end
49

    
50
  private
51

    
52
  def attempt_login
53
    api.ok?
54
  rescue Excon::Error::Certificate
55
    message = 'Unable to verify certificate.\
56
              (Disable "Verify ssl" if you still want to use this Docker Registry)'
57
    errors.add(:base, _(message))
58
  rescue => error
59
    errors.add(:base, _('Unable to log in to this Docker Registry - %s') % error)
60
  end
61
end