Project

General

Profile

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

foreman-docker / app / models / container.rb @ 2b80fdd9

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

    
11
  accepts_nested_attributes_for :environment_variables, :allow_destroy => true
12

    
13
  has_many :exposed_ports,  :dependent  => :destroy, :foreign_key => :reference_id,
14
                            :inverse_of => :container,
15
                            :class_name => 'ExposedPort'
16

    
17
  has_many :dns,  :dependent  => :destroy, :foreign_key => :reference_id,
18
                  :inverse_of => :container,
19
                  :class_name => 'ForemanDocker::Dns'
20

    
21
  include ForemanDocker::ParameterValidators
22

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

    
26
  validates :name, :uniqueness => { :scope => :compute_resource_id }
27

    
28
  def repository_pull_url
29
    repo = tag.blank? ? repository_name : "#{repository_name}:#{tag}"
30
    repo = registry.prefixed_url(repo) if registry
31
    repo
32
  end
33

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

    
50
  def in_fog
51
    @fog_container ||= compute_resource.vms.get(uuid)
52
  end
53

    
54
  def self.humanize_class_name(_name = nil)
55
    _("Docker/Container")
56
  end
57
end