class DockletCLI

Public Instance Methods

build() click to toggle source
# 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
clean() click to toggle source
# 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_volumes() click to toggle source
# 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) click to toggle source
# 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
console() click to toggle source
# File lib/dklet/cli.rb, line 34
def console
  pp registry
  require 'byebug'
  byebug 
  puts "=ok"
end
container_run(cmds, opts = {}) click to toggle source
# File lib/dklet/cli.rb, line 340
    def container_run(cmds, opts = {})
      opts = options.merge(opts)
      # get target container
      cid = opts[:cid] || ops_container
      tmprun = opts[:tmp]
      if tmprun
        dkcmd = "docker run -t -d" 
        dkcmd += " --network #{netname}" if netname
        dkcmd += " #{opts[:opts]}" if opts[:opts]
        img = opts[:image] || docker_image
        cid = `#{dkcmd} #{img} sleep 3d`.chomp
      end
      abort "No container found!" unless cid

      # how to run
      new_cmds = if Dklet::Util.single_line?(cmds)
          <<~Desc
            docker exec -it #{opts[:opts]} #{cid} #{cmds}
          Desc
        else
          cmds = cmds.join("\n") if cmds.is_a?(Array)
          tfile = tmpfile_for(cmds)
          dst_file = "/tmp/dklet-#{File.basename(tfile)}-#{rand(10000)}"
          <<~Desc
            docker cp --archive #{tfile} #{cid}:#{dst_file}
            docker exec -it #{opts[:opts]} #{cid} sh #{'-x' if opts[:debug]} #{dst_file}
            docker exec #{cid} rm -f #{dst_file}
          Desc
        end
      unless opts[:quiet]
        puts "==commands to run on #{'tmp' if tmprun} container #{cid.size == 64 ? cid[0..11] : cid}"
        puts cmds
        if opts[:debug]
          puts "====by run on host"
          puts new_cmds 
        end
        puts "==end of print commands" 
      end
      system new_cmds unless opts[:dry]

      if tmprun
        system <<~Desc
          docker rm -f #{cid} >/dev/null
        Desc
      end
    end
daemon() click to toggle source
# File lib/dklet/cli.rb, line 70
def daemon
  invoke :build, [], {}
  system "docker run -d #{options[:opts]} #{docker_image}" unless options[:dry]
end
image() click to toggle source
# File lib/dklet/cli.rb, line 152
def image
  system "docker images #{docker_image}"
end
image_name() click to toggle source
# File lib/dklet/cli.rb, line 147
def image_name
  puts docker_image
end
inspect_info() click to toggle source
# 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
invoke2(task, args, options) click to toggle source

github.com/erikhuda/thor/issues/73

# File lib/dklet/cli.rb, line 325
def invoke2(task, args, options)
  (klass, task) = Thor::Util.find_class_and_command_by_namespace(task)
  klass.new.invoke(task, args, options)
end
invoke_clean() click to toggle source
# File lib/dklet/cli.rb, line 309
def invoke_clean
  invoke :clean, [], {}
end
invoke_hooks_for(name = :main, type: :after) click to toggle source
# File lib/dklet/cli.rb, line 313
def invoke_hooks_for(name = :main, type: :after)
  hooks_name = "#{name}_#{type}_hooks".to_sym
  hooks = fetch(hooks_name)
  if hooks && !hooks.empty?
    hooks.each do |hook|
      # eval by receiver dierectly
      instance_eval &hook if hook.respond_to?(:call)
    end
  end
end
log(cid = ops_container) click to toggle source
# 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
main() click to toggle source
# 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) click to toggle source
# File lib/dklet/cli.rb, line 289
def mock1(time)
  puts "invoked at #{time}"
end
mock2() click to toggle source
# 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) click to toggle source
# 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
netls() click to toggle source
# File lib/dklet/cli.rb, line 206
  def netls()
    system <<~Desc
      docker network ls
    Desc
  end
netps(net = netname) click to toggle source
# 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) click to toggle source
# File lib/dklet/cli.rb, line 169
def netup(net = netname)
  return unless net
  ensure_docker_net(net)
  puts "network #{net} working"
end
note() click to toggle source
# File lib/dklet/cli.rb, line 101
def note
  puts user_notes.join("\n")
end
ps() click to toggle source
# 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
reset() click to toggle source
# 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) click to toggle source
# 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
spec() click to toggle source
# 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
system_run(cmds, opts={}) click to toggle source

encapsulate run commands behaviors in system

# File lib/dklet/cli.rb, line 331
def system_run(cmds, opts={})
  unless options[:quiet]
    puts cmds
  end
  unless options[:dry]
    system cmds
  end
end
version() click to toggle source
# File lib/dklet/cli.rb, line 17
def version
  puts Dklet.version
end
vols() click to toggle source
# File lib/dklet/cli.rb, line 222
  def vols
    system <<~Desc
      ls -l #{volumes_root}/
    Desc
  end