lib/percheron/container/main.rb in percheron-0.2.3 vs lib/percheron/container/main.rb in percheron-0.2.4

- old
+ new

@@ -3,39 +3,39 @@ class Main extend Forwardable extend ConfigDelegator - def_delegators :container_config, :name, :version + def_delegators :container_config, :name, :dockerfile_md5 + def_config_item_with_default :container_config, false, :auto_recreate def_config_item_with_default :container_config, [], :env, :ports, :volumes, :dependant_container_names + alias_method :auto_recreate?, :auto_recreate + def initialize(config, stack, container_name) @config = config @stack = stack @container_name = container_name valid? self end - def stop! - Container::Actions::Stop.new(self).execute! - rescue Errors::ContainerNotRunning - $logger.debug "Container '#{name}' is not running" + def id + exists? ? info.id[0...12] : 'N/A' end - def start! - Container::Actions::Create.new(self).execute! unless exists? - Container::Actions::Start.new(self).execute! + def image + '%s:%s' % [ name, version.to_s ] end - def id - exists? ? info.id[0...12] : 'N/A' + def version + Semantic::Version.new(container_config.version) end - def image - '%s:%s' % [ name, version ] + def built_version + exists? ? Semantic::Version.new(built_image_version) : nil end def dockerfile container_config.dockerfile ? Pathname.new(File.expand_path(container_config.dockerfile, config.file_base_path)): nil end @@ -57,10 +57,29 @@ Docker::Container.get(name) rescue Docker::Error::NotFoundError, Excon::Errors::SocketError Container::Null.new end + def stop! + Container::Actions::Stop.new(self).execute! + rescue Errors::ContainerNotRunning + $logger.debug "Container '#{name}' is not running" + end + + def start! + Container::Actions::Create.new(self).execute! unless exists? + Container::Actions::Start.new(self).execute! + end + + def recreatable? + exists? && !md5s_match? + end + + def recreate? + recreatable? && versions_mismatch? && auto_recreate? + end + def running? exists? && info.State.Running end def exists? @@ -72,9 +91,29 @@ end protected attr_reader :config, :stack, :container_name + + def md5s_match? + stored_dockerfile_md5 == current_dockerfile_md5 + end + + def versions_mismatch? + version > built_version + end + + def built_image_version + info.Config.Image.split(':')[1] + end + + def stored_dockerfile_md5 + dockerfile_md5 || current_dockerfile_md5 + end + + def current_dockerfile_md5 + Digest::MD5.file(dockerfile).hexdigest + end def info Hashie::Mash.new(docker_container.info) end