module Bixby::Provision::Util::File

Public Instance Methods

chmod(path, mode, opts={}) click to toggle source

Change mode of the given path

@param [String] path @param [String] mode File mode given as a string, same as the input to the ‘chmod` command

# File lib/bixby/provision/dsl/util/file.rb, line 34
def chmod(path, mode, opts={})
  return if mode.nil?

  if mode.kind_of? String then
    mode.strip!
    return if mode.empty?

  elsif mode.kind_of? Fixnum then
    # mode should always be a string
    # convert fixnum to octal
    mode = sprintf("%o", mode)
    if mode.length > 4 then
      # only want the right-most 4 chars
      # ex: File.stat = mode=0100440 (file r--r-----) => 33056 => "100440" => "0440"
      mode = mode[mode.length-4, mode.length]
    end
  end

  logger.info "[chmod] #{path} -> '#{mode}'"

  # always as root
  if opts[:recurse] or opts[:recursively] then
    logged_sudo("chmod -R #{mode} #{path}")
  else
    logged_sudo("chmod #{mode} #{path}")
  end
end
chown(path, chown, opts={}) click to toggle source
# File lib/bixby/provision/dsl/util/file.rb, line 7
def chown(path, chown, opts={})
  if chown.nil? then
    return
  end
  chown.strip!
  return if chown.empty?

  user, group = chown.split(/:/)
  user = Process.uid if user == "$USER"
  group = Process.gid if group == "$GROUP"
  uid = get_uid(user)
  gid = get_gid(group)

  logger.info "[chown] #{path} -> '#{get_user(uid)}" + (gid ? ":#{get_group(gid)}'" : "'")

  # always as root
  if opts[:recurse] or opts[:recursively] then
    logged_sudo("chown -R #{uid}:#{gid} #{path}")
  else
    logged_sudo("chown #{uid}:#{gid} #{path}")
  end
end
command_exists?(cmd)
Alias for: which
sha256sum(filename) click to toggle source

Get the SHA-256 hex digest of the given file

@param [String] filename

@return [String] sha256 hash in hexadecimal form

# File lib/bixby/provision/dsl/util/file.rb, line 67
def sha256sum(filename)
  if ::File.readable? filename then
    return Digest::SHA2.new(256).file(filename).hexdigest()
  end

  # read as root
  if cmd = which("sha256sum") then
    return logged_sudo("#{cmd} #{filename}").stdout.split(/\s+/).first
  end

  # use cat - may not work for binaries
  str = logged_sudo("cat #{filename}").stdout
  return Digest::SHA2.new(256).update(str).hexdigest()
end
which(cmd) click to toggle source

Locate the given command, if it exists

@param [String] cmd to locate

@return [String] path to command if it exists, or nil

# File lib/bixby/provision/dsl/util/file.rb, line 87
def which(cmd)
  ret = systemu("which #{cmd}")
  if ret.success? then
    return ret.stdout.strip
  else
    return nil
  end
end
Also aliased as: command_exists?