Class: Percheron::Unit
Defined Under Namespace
Modules: ImageHelper
Instance Method Summary
collapse
def_config_item_with_default
#image_exists?, #image_name, #image_repo, #image_size, #image_version
Constructor Details
#initialize(config, stack, unit_name) ⇒ Unit
Returns a new instance of Unit.
15
16
17
18
19
20
21
|
# File 'lib/percheron/unit.rb', line 15
def initialize(config, stack, unit_name)
@config = config
@stack = stack
@unit_name = unit_name
@unit_config = stack.unit_configs[unit_name] || Hashie::Mash.new({}) self
end
|
Instance Method Details
#buildable? ⇒ Boolean
136
137
138
|
# File 'lib/percheron/unit.rb', line 136
def buildable?
!dockerfile.nil? && unit_config.docker_image.nil?
end
|
#built_version ⇒ Object
83
84
85
|
# File 'lib/percheron/unit.rb', line 83
def built_version
@built_version ||= Semantic::Version.new(built_image_version)
end
|
#container ⇒ Object
95
96
97
98
99
|
# File 'lib/percheron/unit.rb', line 95
def container
Connection.perform(Docker::Container, :get, full_name)
rescue Docker::Error::NotFoundError, Excon::Errors::SocketError
NullUnit.new
end
|
#display_name ⇒ Object
65
66
67
|
# File 'lib/percheron/unit.rb', line 65
def display_name
'%s:%s' % [ stack.name, name ]
end
|
#dockerfile ⇒ Object
109
110
111
112
|
# File 'lib/percheron/unit.rb', line 109
def dockerfile
return nil unless unit_config.dockerfile
Pathname.new(File.expand_path(unit_config.dockerfile, config.file_base_path))
end
|
#dockerfile_md5s_match? ⇒ Boolean
120
121
122
|
# File 'lib/percheron/unit.rb', line 120
def dockerfile_md5s_match?
dockerfile_md5 == current_dockerfile_md5
end
|
#exists? ⇒ Boolean
132
133
134
|
# File 'lib/percheron/unit.rb', line 132
def exists?
!info.empty?
end
|
#exposed_ports ⇒ Object
87
88
89
|
# File 'lib/percheron/unit.rb', line 87
def exposed_ports
ports.each_with_object({}) { |p, all| all[p.split(':')[1]] = {} }
end
|
#full_name ⇒ Object
61
62
63
|
# File 'lib/percheron/unit.rb', line 61
def full_name
'%s_%s' % [ stack.name, name ]
end
|
#hostname ⇒ Object
45
46
47
|
# File 'lib/percheron/unit.rb', line 45
def hostname
unit_config.fetch('hostname', full_name)
end
|
#id ⇒ Object
41
42
43
|
# File 'lib/percheron/unit.rb', line 41
def id
exists? ? info.id[0...12] : nil
end
|
#image ⇒ Object
73
74
75
76
77
|
# File 'lib/percheron/unit.rb', line 73
def image
Connection.perform(Docker::Image, :get, image_name)
rescue Docker::Error::NotFoundError
nil
end
|
#ip ⇒ Object
105
106
107
|
# File 'lib/percheron/unit.rb', line 105
def ip
exists? ? info.NetworkSettings.IPAddress : 'n/a'
end
|
#labels ⇒ Object
101
102
103
|
# File 'lib/percheron/unit.rb', line 101
def labels
{ version: version.to_s, created_by: "Percheron #{Percheron::VERSION}" }
end
|
#links ⇒ Object
91
92
93
|
# File 'lib/percheron/unit.rb', line 91
def links
startable_needed_units.map { |_, unit| '%s:%s' % [ unit.full_name, unit.full_name ] }
end
|
37
38
39
|
# File 'lib/percheron/unit.rb', line 37
def metastore_key
@metastore_key ||= '%s.units.%s' % [ stack.metastore_key, name ]
end
|
#needed_unit_names ⇒ Object
23
24
25
|
# File 'lib/percheron/unit.rb', line 23
def needed_unit_names
unit_config.fetch('needed_unit_names', unit_config.fetch('dependant_unit_names', []))
end
|
#needed_units ⇒ Object
27
28
29
30
31
|
# File 'lib/percheron/unit.rb', line 27
def needed_units
needed_unit_names.each_with_object({}) do |unit_name, all|
all[unit_name] = stack.units[unit_name]
end
end
|
#privileged ⇒ Object
57
58
59
|
# File 'lib/percheron/unit.rb', line 57
def privileged
unit_config.fetch('privileged', false)
end
|
#pseudo? ⇒ Boolean
144
145
146
|
# File 'lib/percheron/unit.rb', line 144
def pseudo?
!pseudo_name.nil?
end
|
#pseudo_full_name ⇒ Object
69
70
71
|
# File 'lib/percheron/unit.rb', line 69
def pseudo_full_name
'%s_%s' % [ stack.name, pseudo_name ]
end
|
#restart_policy ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/percheron/unit.rb', line 49
def restart_policy
@restart_policy ||= begin
name = unit_config.fetch('restart_policy', 'always')
max_retry_count = unit_config.fetch('restart_policy_retry_count', 0)
{ 'Name' => name, 'MaximumRetryCount' => max_retry_count }
end
end
|
#running? ⇒ Boolean
128
129
130
|
# File 'lib/percheron/unit.rb', line 128
def running?
exists? && info.State.Running
end
|
#startable_needed_units ⇒ Object
33
34
35
|
# File 'lib/percheron/unit.rb', line 33
def startable_needed_units
needed_units.select { |_, unit| unit.startable? }
end
|
#update_dockerfile_md5! ⇒ Object
114
115
116
117
118
|
# File 'lib/percheron/unit.rb', line 114
def update_dockerfile_md5!
md5 = current_dockerfile_md5
$logger.debug "Setting MD5 for '#{name}' unit to #{md5}"
$metastore.set("#{metastore_key}.dockerfile_md5", md5)
end
|
#version ⇒ Object
79
80
81
|
# File 'lib/percheron/unit.rb', line 79
def version
@version ||= Semantic::Version.new(unit_config.version)
end
|
#versions_match? ⇒ Boolean
124
125
126
|
# File 'lib/percheron/unit.rb', line 124
def versions_match?
version == built_version
end
|