Class: Dockerun::Command::RemoveContainer

Inherits:
Object
  • Object
show all
Includes:
Dockerun::CliHelper::CliPrompt, DockerCommandFactoryHelper, TR::CondUtils, TTY::Option
Defined in:
lib/dockerun/command/remove_container.rb

Instance Method Summary collapse

Methods included from Dockerun::CliHelper::CliPrompt

#cli

Methods included from DockerCommandFactoryHelper

#dcFact

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dockerun/command/remove_container.rb', line 25

def run
  if params[:help]
    print help
    exit(0)

  else

    config = ::Dockerun::Config.from_storage

    imageName = nil
    if is_empty?(config.image_names)
      raise Error, "No image found"
    elsif config.image_names.length == 1
      imageName = config.image_names.first
    else
      imageName = cli.select("Please select one of the image below to start : ") do |m|
        config.image_names.each do |n|
          m.choice n,n
        end
      end
    end # find imageName

    if is_empty?(config.container_names(imageName))
      STDOUT.puts "There is no container registered under image '#{imageName}'"
    else

      loop do

        sel = cli.select("Please select the container that would like to be removed : ") do |m|
          config.container_names(imageName).each do |n|
            m.choice n,n
          end

          m.choice "Quit", :quit
        end

        case sel
        when :quit
          break
        else
          dcFact.stop_container(sel).run
          res = dcFact.delete_container(sel).run
          if res.failed? 
            if res.err_stream =~ /No such container/
              remove = cli.yes?("It seems the container already deleted. Do you want to remove it from the list?")
              if remove
                config.remove_container(imageName, sel)
                config.to_storage
                break if is_empty?(config.container_names(imageName))
              end
            else
              STDERR.puts "Failed to delete container '#{sel}'. Error was : #{res.err_stream}"
            end
          else

            config.remove_container(imageName, sel)
            config.to_storage

            break if is_empty?(config.container_names(imageName))
          end

        end

      end
    end
    


  end
  
end