Module: Bixby::Provision::Util::File
- Included in:
- Base
- Defined in:
- lib/bixby/provision/dsl/util/file.rb
Instance Method Summary collapse
-
#chmod(path, mode, opts = {}) ⇒ Object
Change mode of the given path.
- #chown(path, chown, opts = {}) ⇒ Object
-
#sha256sum(filename) ⇒ String
Get the SHA-256 hex digest of the given file.
-
#which(cmd) ⇒ String
(also: #command_exists?)
Locate the given command, if it exists.
Instance Method Details
#chmod(path, mode, opts = {}) ⇒ Object
Change mode of the given path
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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 = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 |
#sha256sum(filename) ⇒ String
Get the SHA-256 hex digest of the given file
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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) ⇒ String Also known as: command_exists?
Locate the given command, if it exists
87 88 89 90 91 92 93 94 |
# 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 |