Class: Dockerun::Cli::CommandFactory

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

Instance Method Summary collapse

Instance Method Details

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/dockerun/cli/command_factory.rb', line 187

def attach_container(container, opts = { })

  opts = {} if opts.nil?
  cmd = []
  cmd << DockerCli.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 = { }, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dockerun/cli/command_factory.rb', line 12

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

  opts = {  } if opts.nil?
  cmd = []
  cmd << "cd #{opts[:working_dir]} && " if not_empty?(opts[:working_dir])
  cmd << DockerCli.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

Create container from image directly e.g. > docker run -it <image> “/bin/bash”



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dockerun/cli/command_factory.rb', line 137

def create_container_from_image(image, opts = { })
  opts = {} if opts.nil?
  cmd = []
  cmd << DockerCli.docker_exe
  cmd << "run"
  cmd << "-i" if opts[:interactive] == true
  cmd << "-t" if opts[:tty] == true
  cmd << "-d" if opts[:detached] == true
  cmd << "--rm" if opts[:del] == 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)

  if opts[:match_user] == true
    ui = UserInfo.
    gi = UserInfo.group_info
    cmd << "-u #{ui[:uid]}:#{gi[:gid]}"
  end

  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



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/dockerun/cli/command_factory.rb', line 217

def delete_container(container, opts = { })

  cmd = []
  cmd << DockerCli.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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dockerun/cli/command_factory.rb', line 54

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

  if not_empty?(name)

    cmd = []
    cmd << DockerCli.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_container_names_by_image_name(image_name, opts = { }) ⇒ Object

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dockerun/cli/command_factory.rb', line 114

def find_container_names_by_image_name(image_name, opts = { })

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

  cmd = []
  cmd << DockerCli.docker_exe
  cmd << "ps"
  cmd << "-a" if opts[:all_containers] == true
  cmd << "-f"
  cmd << "ancestor=\"#{image_name}\""
  cmd << "--format \"{{.Names}}\""

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

  Command.new(cmd)
 
end

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

Find from container even if it is already stopped

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dockerun/cli/command_factory.rb', line 95

def find_from_all_container(name, opts = { })
  raise Error, "Name is required" if is_empty?(name)     
  cmd = []
  cmd << DockerCli.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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dockerun/cli/command_factory.rb', line 37

def find_image(name, tag = "", opts = { })
  name = "" if name.nil?
  cmd = []
  cmd << DockerCli.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:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dockerun/cli/command_factory.rb', line 77

def find_running_container(name, opts = { })

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

  cmd = []
  cmd << DockerCli.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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/dockerun/cli/command_factory.rb', line 230

def run_command_in_running_container(container, command, opts = {  })
  cmd = []
  cmd << DockerCli.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



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dockerun/cli/command_factory.rb', line 173

def start_container(container, opts = { })

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

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

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



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/dockerun/cli/command_factory.rb', line 204

def stop_container(container, opts = { })

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

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