Project

General

Profile

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

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

1
class Container < ActiveRecord::Base
2
  include Authorizable
3
  include Taxonomix
4

    
5
  belongs_to :compute_resource
6
  belongs_to :registry, :class_name => "DockerRegistry", :foreign_key => :registry_id
7
  has_many :environment_variables, :dependent  => :destroy, :foreign_key => :reference_id,
8
                                   :inverse_of => :container,
9
                                   :class_name => 'EnvironmentVariable',
10
                                   :validate => false
11
  accepts_nested_attributes_for :environment_variables, :allow_destroy => true
12
  include ForemanDocker::ParameterValidators
13

    
14
  has_many :exposed_ports,  :dependent  => :destroy, :foreign_key => :reference_id,
15
                            :inverse_of => :container,
16
                            :class_name => 'ExposedPort',
17
                            :validate => true
18

    
19
  has_many :dns,  :dependent  => :destroy, :foreign_key => :reference_id,
20
                  :inverse_of => :container,
21
                  :class_name => 'ForemanDocker::Dns',
22
                  :validate => true
23

    
24
  accepts_nested_attributes_for :exposed_ports, :allow_destroy => true
25
  scoped_search :on => :name
26

    
27
  attr_accessible :command, :repository_name, :name, :compute_resource_id, :entrypoint,
28
                  :cpu_set, :cpu_shares, :memory, :tty, :attach_stdin, :registry_id,
29
                  :attach_stdout, :attach_stderr, :tag, :uuid, :environment_variables_attributes,
30
                  :katello, :exposed_ports_attributes, :dns
31

    
32
  validates :name, :uniqueness => { :scope => :compute_resource_id }
33

    
34
  def repository_pull_url
35
    repo = tag.blank? ? repository_name : "#{repository_name}:#{tag}"
36
    repo = registry.prefixed_url(repo) if registry
37
    repo
38
  end
39

    
40
  def parametrize
41
    { 'name'  => name, # key has to be lower case to be picked up by the Docker API
42
      'Image' => repository_pull_url,
43
      'Tty'          => tty,
44
      'Memory'       => ::ForemanDocker::Utility.parse_memory(memory),
45
      'Entrypoint'   => entrypoint.try(:split), 'Cmd'          => command.try(:split),
46
      'AttachStdout' => attach_stdout,          'AttachStdin'  => attach_stdin,
47
      'AttachStderr' => attach_stderr,          'CpuShares'    => cpu_shares,
48
      'Cpuset'       => cpu_set,
49
      'Env' => environment_variables.map { |env| "#{env.name}=#{env.value}" },
50
      'ExposedPorts' => Hash[*exposed_ports.map { |v| [v.name + "/" + v.value, {}] }.flatten],
51
      'HostConfig' => {
52
        'Dns' => dns.map { |env| "#{env.name}" }
53
      } }
54
  end
55

    
56
  def in_fog
57
    @fog_container ||= compute_resource.vms.get(uuid)
58
  end
59

    
60
  def self.humanize_class_name(_name = nil)
61
    _("Docker/Container")
62
  end
63
end