module Jarl class Application attr_reader :group, :name, :image, :volumes, :ports, :environment, :entrypoint, :command # Create Application object from application definition: # # app_name: # image: blabla # volumes: # - /host/path:/container/path # ports: # - 1234:5678 # command: blabla # def initialize(group, name, params) @group = group @name = name @image = params['image'] || fail("Application '#{self}' has no 'image' defined") unless @image.is_a?(String) fail "Application '#{self}' has invalid 'image' definition, String is expected" end @volumes = params['volumes'] || [] unless @volumes.is_a?(Array) fail "Application '#{self}' has invalid 'volumes' definition, Array is expected" end @ports = params['ports'] || [] unless @ports.is_a?(Array) fail "Application '#{self}' has invalid 'ports' definition, Array is expected" end @environment = params['environment'] || {} unless @environment.is_a?(Hash) fail "Application '#{self}' has invalid 'environment' definition, Hash is expected" end @entrypoint = params['entrypoint'] if @entrypoint && !@entrypoint.is_a?(String) fail "Application '#{self}' has invalid 'entrypoint' definition, String is expected" end @command = params['command'] if @command && !@command.is_a?(String) fail "Application '#{self}' has invalid 'command' definition, String is expected" end end def full_name "#{group}.#{name}" end def image_is_a_path? image =~ /^[~\.\/]/ end def running? running_container end def running_container Docker.containers_running.find { |c| c.name == full_name } end def start running_container.stop! if running? Docker.start( name: full_name, image: image_name, volumes: volumes, ports: ports, environment: environment, command: command ) end def build fail 'Not a file based image' unless image_is_a_path? Docker::Image.new(image_name, image_path).build! end def image_name image_is_a_path? ? name : image end def image_path image_is_a_path? ? image : nil end def show_log(*_args) return unless running? begin IO.popen "docker logs -f #{running_container.id}", 'r' do |p| str = '<<< STARTED >>>' puts "#{esc_yellow full_name}: #{str}" while str = p.gets end rescue Interrupt # end end def to_s full_name end end # class Application end # module Jarl