Class: DockletCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/dklet/cli.rb

Instance Method Summary collapse

Instance Method Details

#buildObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dklet/cli.rb', line 77

def build
  return unless dockerfile

  unless options[:dry]
    invoke_hooks_for(:build, type: :before)
  end

  cmd = "docker build --tag #{docker_image}"
  net = build_net
  cmd += " --network #{net}" if net
  cmd += " #{options[:opts]}" if options[:opts]

  bpath = smart_build_context_path
  cmd = if bpath
    "#{cmd} --file #{dockerfile} #{bpath}"
  else # nil stand for do not need build context
    "cat #{dockerfile} | #{cmd} -"
  end
  puts "build command:\n  #{cmd}" if options[:debug]

  system cmd unless options[:dry]
end

#cleanObject



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

def clean
  invoke_hooks_for(:clean, type: :before)

  unless specfile # do not clean container if compose-file exists
    cids = containers_for_release
    unless cids.empty?
      str_ids = cids.join(' ')
      system <<~Desc
        echo ==clean containers: #{str_ids}
        docker rm --force #{str_ids}
      Desc
    end
  end

  invoke_hooks_for(:clean)

  if options[:image] && dockerfile
    system <<~Desc
      echo ==clean image: #{docker_image}
      docker rmi --force #{docker_image} 2>/dev/null
    Desc
  end
end

#clear_app_volumesObject



229
230
231
232
233
234
235
# File 'lib/dklet/cli.rb', line 229

def clear_app_volumes
  if app_volumes.directory?
    if options[:force] || yes?("Remove app volumes dir: #{app_volumes} (y/n)?")
      app_volumes.rmtree
    end
  end
end

#comprun(*args) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/dklet/cli.rb', line 213

def comprun(*args)
  cmd = <<~Desc
    #{compose_cmd} #{args.join(' ')}
  Desc
  puts cmd if options[:debug]
  system cmd unless options[:dry]
end

#consoleObject



34
35
36
37
38
39
# File 'lib/dklet/cli.rb', line 34

def console
  pp registry
  require 'byebug'
  byebug 
  puts "=ok"
end

#daemonObject



70
71
72
73
# File 'lib/dklet/cli.rb', line 70

def daemon
  invoke :build, [], {}
  system "docker run -d #{options[:opts]} #{docker_image}" unless options[:dry]
end

#imageObject



152
153
154
# File 'lib/dklet/cli.rb', line 152

def image
  system "docker images #{docker_image}"
end

#image_nameObject



147
148
149
# File 'lib/dklet/cli.rb', line 147

def image_name
  puts docker_image
end

#inspect_infoObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/dklet/cli.rb', line 252

def inspect_info
  cmd = nil
  if options[:image]
    cmd = "docker inspect #{docker_image}"
  elsif options[:container]
    cid = containers_for_release.first || container_name || ops_container
    cmd = "docker inspect #{cid}"
  else
    h = {
      script: dklet_script,
      script_path: script_path,
      script_name: script_name,
      appname: appname,
      env: env,
      release: app_release,
      full_release_name: full_release_name,
      container_name: container_name,
      image: docker_image,
      approot: approot,
      build_root: build_root,
      build_net: build_net,
      release_labels: release_label_hash,
      network: netname,
      voluemes_root: volumes_root,
      app_volumes: app_volumes,
      domains: proxy_domains,
      dsl_methods: dsl_methods,
      registry: registry
    }
    pp h
  end
  system cmd if cmd
end

#log(cid = ops_container) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/dklet/cli.rb', line 42

def log(cid = ops_container)
  unless cid
    container_missing
    return
  end
  system <<~Desc
    docker logs -t -f --details #{cid}
  Desc
end

#mainObject



25
26
27
28
29
30
31
# File 'lib/dklet/cli.rb', line 25

def main
  invoke_clean if options[:preclean] && task_opts(:main)[:preclean] != false

  invoke_hooks_for(:main, type: :before)
  invoke :build, [], {} if options[:build] && task_opts(:main)[:build] != false
  invoke_hooks_for(:main)
end

#mock1(time) ⇒ Object



289
290
291
# File 'lib/dklet/cli.rb', line 289

def mock1(time)
  puts "invoked at #{time}"
end

#mock2Object



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/dklet/cli.rb', line 294

def mock2
  invoke :mock1, [Time.now]
  puts 'first invoked'
  invoke :mock1, [Time.now]
  puts 'sencond invoked'

  invoke2 :mock1, [Time.now], {}
  puts 'third invoked'
  invoke2 :mock1, [Time.now], {}
  puts '4th invoked'
end

#netdown(net = netname) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dklet/cli.rb', line 176

def netdown(net = netname)
  puts "cleaning net: #{net}" if options[:debug]
  return unless net
  return unless find_net(net)

  cids = containers_in_net(net)
  binded = !cids.empty?
  if binded 
    if options[:force] || yes?("#{cids.size} containers linked, FORCELY remove(y|n)?")
      system "docker rm -f #{cids.join(' ')}"
      binded = false
    end
  end
  if binded
    puts "#{net} has binded resources, skipped"
  else
    system "docker network rm #{net}"
    puts "network #{net} cleaned"
  end
end

#netlsObject



206
207
208
209
210
# File 'lib/dklet/cli.rb', line 206

def netls()
  system <<~Desc
    docker network ls
  Desc
end

#netps(net = netname) ⇒ Object



198
199
200
201
202
203
# File 'lib/dklet/cli.rb', line 198

def netps(net = netname)
  return unless net
  system <<~Desc
    docker ps -f network=#{net} -a
  Desc
end

#netup(net = netname) ⇒ Object



169
170
171
172
173
# File 'lib/dklet/cli.rb', line 169

def netup(net = netname)
  return unless net
  ensure_docker_net(net)
  puts "network #{net} working"
end

#noteObject



101
102
103
# File 'lib/dklet/cli.rb', line 101

def note
  puts user_notes.join("\n")
end

#psObject



158
159
160
161
162
163
164
165
166
# File 'lib/dklet/cli.rb', line 158

def ps
  cmd = if options[:imaged]
      "docker ps -f ancestor=#{docker_image} -a"
    else
      "docker ps #{container_filters_for_release} -a"
    end
  puts cmd if options[:debug]
  system cmd unless options[:dry]
end

#resetObject



238
239
240
241
242
243
244
245
246
247
# File 'lib/dklet/cli.rb', line 238

def reset
  if env =~ /^prod/
    return unless (options[:force] || yes?("RESET #{full_release_name}?"))
  end
  system <<~Desc
    #{dklet_script} clean
    #{dklet_script} clear_app_volumes
    #{dklet_script}
  Desc
end

#runsh(*cmds) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/dklet/cli.rb', line 57

def runsh(*cmds)
  if cmds.empty?
    cmds = 'sh' 
  else
    cmds = cmds.join(' ') if options[:oneline]
  end
  container_run(cmds)
end

#specObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/dklet/cli.rb', line 135

def spec
  if options[:spec] && specfile
    puts File.read(specfile)
    puts "# rendered at #{specfile}" if options[:debug]
  end
  if options[:dockerfile] && dockerfile
    puts File.read(dockerfile)
    puts "# Dockerfile at #{dockerfile} " if options[:debug]
  end
end

#versionObject



17
18
19
# File 'lib/dklet/cli.rb', line 17

def version
  puts Dklet.version
end

#volsObject



222
223
224
225
226
# File 'lib/dklet/cli.rb', line 222

def vols
  system <<~Desc
    ls -l #{volumes_root}/
  Desc
end