Project

General

Profile

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

foreman-docker / app / models / docker_registry.rb @ 26c5ba32

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
  attr_accessible :name, :url, :username, :password, :locations, :organizations,
16
    :description
17

    
18
  validates_lengths_from_database
19
  validates :name, :presence => true, :uniqueness => true
20
  validates :url,  :presence => true, :uniqueness => true,
21
    :url_schema => ['http', 'https']
22
  validate :attempt_login
23

    
24
  scoped_search :on => :name, :complete_value => true
25
  scoped_search :on => :url
26

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

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

    
39
  def prefixed_url(image_name)
40
    uri = URI(url)
41
    "#{uri.hostname}:#{uri.port}/#{image_name}"
42
  end
43

    
44
  def self.humanize_class_name(_name = nil)
45
    _("Docker/Registry")
46
  end
47

    
48
  private
49

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