Class: Docker::Cli::CommandFactory

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/docker/cli/command_factory.rb

Instance Method Summary collapse

Instance Method Details

#attach_container(container, opts = { }) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/docker/cli/command_factory.rb', line 149

def attach_container(container, opts = { })

  opts = {} if opts.nil?
  cmd = []
  cmd << Cli.docker_exe
  cmd << "container"
  cmd << "attach"
  cmd << container

  logger.debug "Attach Container : #{cmd.join(" ")}"
  # this is a bit difficult to juggle 
  # it depending on the previous docker configuration
  # but to be save, just open up a new terminal
  Command.new(cmd, true)
end

#build_image(name = "", opts = { }) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/docker/cli/command_factory.rb', line 9

def build_image(name = "", opts = {  })

  opts = {  } if opts.nil?
  cmd = []
  cmd << Cli.docker_exe
  cmd << "build"
  if not_empty?(name)
    cmd << "-t #{name}"
  end

  if not_empty?(opts[:dockerfile])
    cmd << "-f #{opts[:dockerfile]}"
  end

  root = opts[:context_root]
  root = "." if is_empty?(root)
  cmd << root

  logger.debug "Build Image : #{cmd.join(" ")}"
  Command.new(cmd)

end

#create_container_from_image(image, opts = { }) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/docker/cli/command_factory.rb', line 109

def create_container_from_image(image, opts = { })
  opts = {} if opts.nil?
  cmd = []
  cmd << Cli.docker_exe
  cmd << "run"
  cmd << "-i" if opts[:interactive] == true
  cmd << "-t" if opts[:tty] == true
  if not (opts[:container_name].nil? or opts[:container_name].empty?)
    cmd << "--name #{opts[:container_name]}"
  end

  cmd << process_mount(opts)
  cmd << process_port(opts)

  cmd << image

  if not_empty?(opts[:command])
    cmd << "\"#{opts[:command]}\""
  end

  interactive = false
  interactive = true if opts[:interactive] or opts[:tty]
  
  logger.debug "Run Container from Image : #{cmd.join(" ")}"
  Command.new(cmd, (interactive ? true : false))
end

#delete_container(container, opts = { }) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/docker/cli/command_factory.rb', line 179

def delete_container(container, opts = { })

  cmd = []
  cmd << Cli.docker_exe
  cmd << "container"
  cmd << "rm"
  cmd << container

  logger.debug "Delete Container : #{cmd.join(" ")}"
  Command.new(cmd)
end

#delete_image(name, tag = "", opts = { }) ⇒ Object

find_image



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/docker/cli/command_factory.rb', line 50

def delete_image(name, tag = "", opts = { })

  if not_empty?(name)

    cmd = []
    cmd << Cli.docker_exe
    cmd << "rmi"
    if not_empty?(tag)
      cmd << "#{name}:#{tag}"
    else
      cmd << name
    end

    logger.debug "Delete image: #{cmd.join(" ")}"
    Command.new(cmd)

  else
    raise Error, "Name is required for delete operation"
  end

end

#find_from_all_container(name, opts = { }) ⇒ Object

Find from container even if it is already stopped

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/docker/cli/command_factory.rb', line 91

def find_from_all_container(name, opts = { })
  raise Error, "Name is required" if is_empty?(name)     
  cmd = []
  cmd << Cli.docker_exe
  cmd << "ps"
  # return all info instead of only the container ID
  #cmd << "-a"
  cmd << "-aq"
  cmd << "-f"
  # From little testing seems the command by default already support regex formatting
  # So can use the regex marker to get exact match
  # e.g. if want exact match, pass in ^#{name}\z
  cmd << "name=\"#{name}\""

  logger.debug "Find from all container: #{cmd.join(" ")}"
  Command.new(cmd)
end

#find_image(name, tag = "", opts = { }) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/docker/cli/command_factory.rb', line 33

def find_image(name, tag = "", opts = { })
  name = "" if name.nil?
  cmd = []
  cmd << Cli.docker_exe
  cmd << "images"
  cmd << "-q"
  if not_empty?(tag)
    cmd << "#{name}:#{tag}"
  else
    cmd << name
  end

  logger.debug "Find image: #{cmd.join(" ")}"

  Command.new(cmd)
end

#find_running_container(name, opts = { }) ⇒ Object

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/docker/cli/command_factory.rb', line 73

def find_running_container(name, opts = { })

  raise Error, "Name is mandatory" if is_empty?(name)     

  cmd = []
  cmd << Cli.docker_exe
  cmd << "ps"
  cmd << "-q"
  cmd << "-f"
  cmd << "name=\"#{name}\""

  logger.debug "Find container: #{cmd.join(" ")}"

  Command.new(cmd)
 
end

#run_command_in_running_container(container, command, opts = { }) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/docker/cli/command_factory.rb', line 192

def run_command_in_running_container(container, command, opts = {  })
  cmd = []
  cmd << Cli.docker_exe
  cmd << "container"
  cmd << "exec"

  isTty = false
  isInteractive = false
  if not_empty?(opts[:tty]) and opts[:tty] == true
    cmd << "-t" 
    isTty = true
  end
  if not_empty?(opts[:interactive]) and opts[:interactive] == true
    cmd << "-i" 
    isInteractive = true
  end

  cmd << container

  if is_empty?(command)
    cmd << "/bin/bash --login"
  else
    cmd << command
  end

  logger.debug "Run command in running container : #{cmd.join(" ")}"
  Command.new(cmd, ((isTty or isInteractive) ? true : false))
end

#start_container(container, opts = { }) ⇒ Object

run_container_from_image



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/docker/cli/command_factory.rb', line 136

def start_container(container, opts = { })

  opts = {} if opts.nil?
  cmd = []
  cmd << Cli.docker_exe
  cmd << "container"
  cmd << "start"
  cmd << container

  logger.debug "Start Container : #{cmd.join(" ")}"
  Command.new(cmd)
end

#stop_container(container, opts = { }) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/docker/cli/command_factory.rb', line 166

def stop_container(container, opts = { })

  cmd = []
  cmd << Cli.docker_exe
  cmd << "container"
  cmd << "stop"
  cmd << container

  logger.debug "Stop Container : #{cmd.join(" ")}"
  Command.new(cmd)
end