Class: Percheron::Unit

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, ConfigDelegator
Includes:
ImageHelper
Defined in:
lib/percheron/unit.rb,
lib/percheron/unit/image_helper.rb

Defined Under Namespace

Modules: ImageHelper

Instance Method Summary collapse

Methods included from ConfigDelegator

def_config_item_with_default

Methods included from ImageHelper

#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({}) # FIXME
  self
end

Instance Method Details

#buildable?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/percheron/unit.rb', line 136

def buildable?
  !dockerfile.nil? && unit_config.docker_image.nil?
end

#built_versionObject



83
84
85
# File 'lib/percheron/unit.rb', line 83

def built_version
  @built_version ||= Semantic::Version.new(built_image_version)
end

#containerObject



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_nameObject



65
66
67
# File 'lib/percheron/unit.rb', line 65

def display_name
  '%s:%s' % [ stack.name, name ]
end

#dockerfileObject



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

Returns:

  • (Boolean)


120
121
122
# File 'lib/percheron/unit.rb', line 120

def dockerfile_md5s_match?
  dockerfile_md5 == current_dockerfile_md5
end

#exists?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/percheron/unit.rb', line 132

def exists?
  !info.empty?
end

#exposed_portsObject



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_nameObject



61
62
63
# File 'lib/percheron/unit.rb', line 61

def full_name
  '%s_%s' % [ stack.name, name ]
end

#hostnameObject



45
46
47
# File 'lib/percheron/unit.rb', line 45

def hostname
  unit_config.fetch('hostname', full_name)
end

#idObject



41
42
43
# File 'lib/percheron/unit.rb', line 41

def id
  exists? ? info.id[0...12] : nil
end

#imageObject



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

#ipObject



105
106
107
# File 'lib/percheron/unit.rb', line 105

def ip
  exists? ? info.NetworkSettings.IPAddress : 'n/a'
end

#labelsObject



101
102
103
# File 'lib/percheron/unit.rb', line 101

def labels
  { version: version.to_s, created_by: "Percheron #{Percheron::VERSION}" }
end


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

#metastore_keyObject



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_namesObject



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_unitsObject



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

#privilegedObject



57
58
59
# File 'lib/percheron/unit.rb', line 57

def privileged
  unit_config.fetch('privileged', false)
end

#pseudo?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/percheron/unit.rb', line 144

def pseudo?
  !pseudo_name.nil?
end

#pseudo_full_nameObject



69
70
71
# File 'lib/percheron/unit.rb', line 69

def pseudo_full_name
  '%s_%s' % [ stack.name, pseudo_name ]
end

#restart_policyObject



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

Returns:

  • (Boolean)


128
129
130
# File 'lib/percheron/unit.rb', line 128

def running?
  exists? && info.State.Running
end

#startable_needed_unitsObject



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

#valid?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/percheron/unit.rb', line 140

def valid?
  Validators::Unit.new(self).valid?
end

#versionObject



79
80
81
# File 'lib/percheron/unit.rb', line 79

def version
  @version ||= Semantic::Version.new(unit_config.version)
end

#versions_match?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/percheron/unit.rb', line 124

def versions_match?
  version == built_version
end