class Default

Constants

DOCKIT_FILE
GIT_CMD

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/dockit/cli.rb, line 64
def initialize(*args)
  super
  ENV['DOCKER_HOST'] = options.host

  # passed to Excon, default is 60sec,
  Docker.options[:read_timeout] = options.timeout

  unless @@root_echoed
    say "Project root: #{project_root}", :red
    @@root_echoed=true
  end
end

Public Instance Methods

_list(what) click to toggle source
# File lib/dockit/cli.rb, line 78
def _list(what)
  puts what.to_s.capitalize, dockit.send(what).keys.collect {|s| "  #{s}"}
end
build(*services) click to toggle source
# File lib/dockit/cli.rb, line 201
def build(*services)
  exec(services) do |s|
    s.build()
  end
end
cleanup() click to toggle source
# File lib/dockit/cli.rb, line 159
def cleanup
  Dockit::Container.clean(
    force: options['force-containers'], except: options['except-containers']
  ) if options[:containers]

  Dockit::Image.clean(
    force: options['force-images'], except: options['except-images']
  ) if options[:images]

  if options[:volumes] && Docker.version['ApiVersion'].to_f >= 1.21
    Dockit::Volume.clean
  else
    say "Volumes not supported on API versions < 1.21", :red
  end
end
config(service=nil) click to toggle source
# File lib/dockit/cli.rb, line 176
def config(service=nil)
  exec(service) do |s|
    say s.config.instance_variable_get('@config').to_yaml
  end
end
git_build(*services) click to toggle source
# File lib/dockit/cli.rb, line 229
def git_build(*services)
  exec(services) do |s|
    unless repos = s.config.get(:repos)
      say "'repos' not defined in config file. Exiting…", :red
      exit 1
    end
    path    = s.config.get(:repos_path)
    treeish = unless path.nil? || path.empty?
                "#{options.branch}:#{path}"
              else
                options.branch
              end
    say "Exporting in #{Dir.pwd}", :green
    say "<- #{repos} #{treeish}", :green

    if options.package
      unless packages = s.config.get(:package)
        say "'packages' not defined in config file. Exiting…", :red
        exit 1
      end

      say '-> package.tar.gz', :green
      export(repos, treeish, 'package.tar.gz', packages)
    end

    say '-> repos.tar.gz', :green
    export(repos, treeish, 'repos.tar.gz')

    hash = `git rev-parse #{options.branch}`.chomp
    say "Creating '.branch' tag file (#{options.branch}:#{hash})", :blue
    File.write('.branch', "#{options.branch}:#{hash}\n")

    s.build
  end
end
help(*args) click to toggle source
Calls superclass method
# File lib/dockit/cli.rb, line 91
def help(*args)
  super
  if args.count < 1
    say "Run 'dockit list' to see the complete list of SERVICEs."
    say "Run 'dockit help COMMAND' to see command specific options."
  end
end
list() click to toggle source
# File lib/dockit/cli.rb, line 106
def list
  _list :modules
  _list :services
end
module_directory(klass) click to toggle source
# File lib/dockit/cli.rb, line 82
def module_directory(klass)
  File.dirname(dockit.modules[klass.to_s.downcase])
end
project_root() click to toggle source
# File lib/dockit/cli.rb, line 86
def project_root
  dockit.root
end
pull(registry, *services) click to toggle source
# File lib/dockit/cli.rb, line 194
def pull(registry, *services)
  exec(services) do |s|
    s.pull(registry, options[:tag], options[:force])
  end
end
push(registry, *services) click to toggle source
# File lib/dockit/cli.rb, line 185
def push(registry, *services)
  exec(services) do |s|
    s.push(registry, options[:tag], options[:force])
  end
end
sh(service=nil, *cmd) click to toggle source
# File lib/dockit/cli.rb, line 123
def sh(service=nil, *cmd)
  exec(service) do |s|
    cmd  = %w[sh -l] if cmd.empty?
    name = ['exec', cmd.first, s.name].join('-')

    # in case image has an entrypoint, use the cmd as the entrypoint
    (entrypoint, *cmd) = cmd
    say "Starting #{name} with #{entrypoint} #{cmd}", :green
    s.start(
      transient: true,
      verbose: options.verbose,
      create: {
        Entrypoint: [entrypoint],
        Cmd: cmd,
        name: name,
        Tty: true,
        AttachStdin: true,
        AttachStdout: true,
        AttachStderr: true,
        OpenStdin: true,
        StdinOnce: true,
      })
  end
end
start(service=nil, *cmd) click to toggle source
# File lib/dockit/cli.rb, line 114
def start(service=nil, *cmd)
  opts = options.merge(create: { tty: options[:transient] })
  opts[:create][:Cmd] = cmd unless cmd.empty?
  exec(service) do |s|
    s.start(opts)
  end
end
version() click to toggle source
# File lib/dockit/cli.rb, line 100
def version
  say "Dockit version #{Dockit::VERSION}"
end

Private Instance Methods

_file(service) click to toggle source
# File lib/dockit/cli.rb, line 303
def _file(service)
  return dockit.services[service] if service && dockit.services[service]
  return DOCKIT_FILE if File.exist?(DOCKIT_FILE)
  say "No config file in current directory", :red
  help
  exit
end
dockit() click to toggle source
# File lib/dockit/cli.rb, line 281
def dockit
  @@dockit ||= Dockit::Env.new(debug: options[:debug])
end
exec(services) { |service(locals: locals)| ... } click to toggle source
# File lib/dockit/cli.rb, line 285
def exec(services)
  services = [services] unless services.is_a?(Array)
  services.push(nil) unless services.count.positive?
  services.each do |service|
    file = _file(service)
    if file != DOCKIT_FILE
      say "Processing #{service}", :blue
      # problem w/ path length for docker build, so change to local directory
      Dir.chdir(File.dirname(file))
    end

    locals = options[:locals]||{}
    locals[:env] = options[:env] || ''
    yield Dockit::Service.new(locals: locals)

  end
end
export(repos, branch, archive, args='') click to toggle source
# File lib/dockit/cli.rb, line 267
def export(repos, branch, archive, args='')
  if repos =~ /\A\./
    repos = File.absolute_path(repos) # git 2.11.0 unhappy w/ '../..'
  end

  cmd = GIT_CMD % [archive, repos, branch, args]
  say "#{cmd}\n", :blue if options.debug

  unless system(cmd)
    say "Export error", :red
    exit 1
  end
end