Project

General

Profile

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

foreman-docker / app / models / container.rb @ d2e54db4

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
  def repository_pull_url
33
    repo = tag.blank? ? repository_name : "#{repository_name}:#{tag}"
34
    repo = registry.prefixed_url(repo) if registry
35
    repo
36
  end
37

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

    
53
  def in_fog
54
    @fog_container ||= compute_resource.vms.get(uuid)
55
  end
56

    
57
  def self.humanize_class_name(_name = nil)
58
    _("Docker/Container")
59
  end
60
end