Sha256: 82e2026c641083384bd40fc13f40144c930a151e847d72a7e4e97a8e68d17818

Contents?: true

Size: 1.73 KB

Versions: 11

Compression:

Stored size: 1.73 KB

Contents

require 'docker/cli'

module Dockdev
  class Container

    def initialize(name)
      @cont_name = name
      @cmd_fact = Docker::Cli::CommandFactory.new
    end

    def name
      @cont_name
    end

    def has_container?
      res = @cmd_fact.find_from_all_container(@cont_name, exact_name: true).run
      if res.success?
        not res.is_out_stream_empty?
      else
        raise Error, "Command find from all container failed with error : #{res.err_stream}"
      end
    end

    def running?
      res = @cmd_fact.find_running_container(@cont_name, exact_name: true).run 
      if res.success?
        not res.is_out_stream_empty?
      else
        raise Error, "Command to check is container running failed with error : #{res.err_stream}"
      end
    end

    def attach_and_exec(opts = {})
      optss = {
        tty: true,
        interactive: true
      }
      @cmd_fact.run_command_in_running_container(@cont_name, opts[:command], optss).run 
    end

    def start_with_command(opts = {})

      res = @cmd_fact.start_container(@cont_name).run 
      if res.success? and not res.is_out_stream_empty?
        attach_and_exec(opts)
      else
        raise Error, "Command to start container failed with error : #{res.err_stream}"
      end


    end

    def stop
      res = @cmd_fact.stop_container(@cont_name).run
      if res.success?
        not res.is_out_stream_empty?
      else
        raise Error, "Command stop container failed with error : #{res.err_stream}"
      end
    end

    def destroy
      res = @cmd_fact.delete_container(@cont_name).run
      if res.success?
        not res.is_out_stream_empty?
      else
        raise Error, "Command delete container failed with error : #{res.err_stream}"
      end
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
dockdev-0.4.0 lib/dockdev/container.rb
dockdev-0.3.8 lib/dockdev/container.rb
dockdev-0.3.7 lib/dockdev/container.rb
dockdev-0.3.6 lib/dockdev/container.rb
dockdev-0.3.5 lib/dockdev/container.rb
dockdev-0.3.4 lib/dockdev/container.rb
dockdev-0.3.3 lib/dockdev/container.rb
dockdev-0.3.2 lib/dockdev/container.rb
dockdev-0.3.1 lib/dockdev/container.rb
dockdev-0.3.0 lib/dockdev/container.rb
dockdev-0.2.0 lib/dockdev/container.rb