Project

General

Profile

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

foreman-docker / app / models / docker_registry.rb @ 9ea40591

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

    
6
  has_many :containers, :foreign_key => "registry_id", :dependent => :destroy
7
  encrypts :password
8

    
9
  attr_accessible :name, :url, :username, :password, :locations, :organizations,
10
    :description
11

    
12
  validates_lengths_from_database
13
  validates :name, :presence => true, :uniqueness => true
14
  validates :url,  :presence => true, :uniqueness => true,
15
    :url_schema => ['http', 'https']
16
  validate :attempt_login
17

    
18
  scoped_search :on => :name, :complete_value => true
19
  scoped_search :on => :url
20

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

    
27
  def used_organization_ids
28
    Organization.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 prefixed_url(image_name)
34
    uri = URI(url)
35
    "#{uri.hostname}:#{uri.port}/#{image_name}"
36
  end
37

    
38
  def self.humanize_class_name(_name = nil)
39
    _("Docker/Registry")
40
  end
41

    
42
  private
43

    
44
  def attempt_login
45
    credentials = { 'username' => username, 'password' => password }
46
    connection = ::Docker::Connection.new(
47
      url,
48
      credentials)
49
    ::Docker.authenticate!(credentials, connection)
50
  rescue => e
51
    errors.add(:base, _('Unable to log in to this Docker Registry - %s') % e)
52
  end
53
end