module DockerCore::Shell

Public Class Methods

architecture() click to toggle source
# File lib/docker_core.rb, line 415
def self.architecture
  hash = { x86: '386', x86_64: 'amd64', armhf: 'armv6', armv7l: 'armv7', aarch64: 'arm64' }
  machine = "#{Etc.uname[:machine]}"
  return hash.fetch(machine.to_sym, machine)
end
change_mode(mode, *arguments) click to toggle source

@param [Array<String>] arguments @param [String, Integer] mode

# File lib/docker_core.rb, line 447
def self.change_mode(mode, *arguments)
  self.find_paths(*arguments).each do |path|
    FileUtils.chmod_R(mode, path, force: true)
  end
end
change_owner(*arguments, user: USER, group: GROUP) click to toggle source

@param [Array<String>] arguments @param [String] user @param [String] group

# File lib/docker_core.rb, line 439
def self.change_owner(*arguments, user: USER, group: GROUP)
  self.find_paths(*arguments).each do |path|
    FileUtils.chown_R(user, group, path, force: true)
  end
end
clear_folders(*arguments, mode: nil) click to toggle source

@param [Array<String>] arguments @param [Integer ,nil] mode

# File lib/docker_core.rb, line 639
def self.clear_folders(*arguments, mode: nil)
  self.remove_paths(*arguments)
  self.make_folders(*arguments, mode: mode)
end
deflate_file(file, path, echo: false) click to toggle source

@param [String] file @param [String] path @param [Boolean] echo

# File lib/docker_core.rb, line 602
def self.deflate_file(file, path, echo: false)
  Zlib::GzipWriter.open(file, Zlib::BEST_COMPRESSION) do

    # @type [Zlib::GzipWriter] writer
  |writer|
    writer.write(self.tape_archive(path, echo: echo))
  end
end
download_file(uri, file) click to toggle source

@param [String] uri @param [String] file

# File lib/docker_core.rb, line 511
def self.download_file(uri, file)
  uri = self.resolve_link(uri)
  content = Net::HTTP.get(URI(uri))
  return File.binwrite(file, content)
end
find_paths(*arguments) click to toggle source

@param [Array<String>] arguments @return [Array<String>]

# File lib/docker_core.rb, line 428
def self.find_paths(*arguments)
  return Dir.glob(arguments.flatten, File::FNM_DOTMATCH).map do |item|
    next File.path(item)
  end.filter do |item|
    next false == %w[. ..].include?(File.basename(item))
  end.uniq
end
github_latest_version(repository) click to toggle source

@param [String] repository

# File lib/docker_core.rb, line 645
def self.github_latest_version(repository)
  uri = URI("https://api.github.com/repos/#{repository}/releases/latest")
  data = JSON.parse(Net::HTTP.get(uri))['tag_name']
  return "#{data}"

end
inflate_file(file, path, echo: false) click to toggle source

@param [String] file @param [String] path @param [Boolean] echo

# File lib/docker_core.rb, line 554
def self.inflate_file(file, path, echo: false)
  Zlib::GzipReader.open(file) do

    # @type [Zlib::GzipReader] reader
  |reader|
    self.unpack_archive(reader, path, echo: echo)
  end
end
is_active_unit(unit) click to toggle source

@param [String] unit

# File lib/docker_core.rb, line 422
def self.is_active_unit(unit)
  return 'active' == Process.capture("systemctl is-active #{unit}").downcase
end
make_folders(*arguments, mode: nil) click to toggle source

@param [Array<String>] arguments @param [Integer, nil] mode

# File lib/docker_core.rb, line 455
def self.make_folders(*arguments, mode: nil)
  stack = []
  paths = arguments.map do |path|
    next File.path(path)
  end.uniq

  paths.each do |path|
    until path == stack.last
      stack << path
      path = File.dirname(path)
    end
  end

  stack.uniq.each do |path|
    if File.exist?(path) && false == File.directory?(path)
      File.delete(path)
    end
  end

  FileUtils.mkdir_p(paths, mode: mode)
end
move_paths(source, target) click to toggle source

@param [String, Array<String>] source @param [String] target

# File lib/docker_core.rb, line 486
def self.move_paths(source, target)
  paths = [source].flatten
  FileUtils.mv(self.find_paths(*paths), target)
end
remove_paths(*arguments) click to toggle source

@param [Array<String>] arguments

# File lib/docker_core.rb, line 478
def self.remove_paths(*arguments)
  self.find_paths(*arguments).each do |path|
    FileUtils.rm_rf(path)
  end
end
tape_archive(path, echo: false) click to toggle source

@param [String] path @param [Boolean] echo

# File lib/docker_core.rb, line 565
def self.tape_archive(path, echo: false)
  io = StringIO.new('')
  offset = path.size + 1

  Gem::Package::TarWriter.new(io) do

    # @type [Gem::Package::TarWriter] writer
  |writer|
    self.find_paths(File.join(path, '**/*')).each do |name|
      mode = File.stat(name).mode
      file = name.slice(offset ..)

      if echo
        Color.echo(": #{file}", Color::GREEN)
      end

      if File.directory?(name)
        writer.mkdir(file, mode)
        next
      end

      if File.file?(name)
        writer.add_file(file, mode) do |stream|
          stream.write(File.binread(name))
        end
        next
      end
    end
  end

  io.rewind
  return io.string
end
unpack_archive(io, path, echo: false) click to toggle source

@param [Object] io @param [String] path @param [Boolean] echo

# File lib/docker_core.rb, line 520
def self.unpack_archive(io, path, echo: false)
  Gem::Package::TarReader.new(io) do

    # @type [Gem::Package::TarReader] reader
  |reader|
    reader.rewind
    reader.each do |entry|
      # @type [Gem::Package::TarHeader] header
      header = entry.header
      mode = header.mode
      file = File.join(path, entry.full_name)

      if echo
        Color.echo(": #{file}", Color::GREEN)
      end

      if entry.directory?
        self.make_folders(file, mode: mode)
        next
      end

      if entry.file?
        self.make_folders(File.dirname(file))
        File.binwrite(file, entry.read)
        self.change_mode(mode, file)
        next
      end
    end
  end
end
update_user(uid: 500, gid: 500) click to toggle source

@param [Integer] uid @param [Integer] gid

# File lib/docker_core.rb, line 613
def self.update_user(uid: 500, gid: 500)
  uid = ENV.fetch('DOCKER_CORE_UID', uid)
  gid = ENV.fetch('DOCKER_CORE_GID', gid)

  Process.run('deluser', USER, throw: false)
  Process.run('delgroup', GROUP, throw: false)
  Process.run('addgroup', { system: true, gid: gid }, GROUP)
  Process.run('adduser', { system: true, 'disabled-password': true, 'no-create-home': true, ingroup: GROUP, gecos: USER, shell: '/bin/bash', uid: uid }, USER)
end