Module: Diskman::System
- Defined in:
- lib/diskman/system.rb
Class Method Summary collapse
-
.bytes2human(b) ⇒ Object
Convert bytes into a human-friendly representation.
-
.exec!(cmd, safe: true, sudo: true) ⇒ Object
Execute a command.
-
.prepare_sudo_session! ⇒ Object
If sudo prompts for the password when a pipeline with pv has already started then we’re unable to enter the password.
Class Method Details
.bytes2human(b) ⇒ Object
Convert bytes into a human-friendly representation.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/diskman/system.rb', line 22 def self.bytes2human(b) return '0B' if b <= 0 # Use 1000 to match the misleading way disk capacities are # advertised. k = 1000 suffixes = ['T', 'G', 'M', 'K', 'B'] suffixes.each_with_index do |suffix, i| threshold = k ** (suffixes.length - i - 1) if b >= threshold return (b / threshold).to_s + suffix end end end |
.exec!(cmd, safe: true, sudo: true) ⇒ Object
Execute a command. If sudo is true, ensures sudo is ready before running the command. If safe is true, ensures the user definitely wants to run the command before running it.
14 15 16 17 18 19 |
# File 'lib/diskman/system.rb', line 14 def self.exec!(cmd, safe: true, sudo: true) Confirmer.check! if safe prepare_sudo_session! if sudo puts exec cmd end |
.prepare_sudo_session! ⇒ Object
If sudo prompts for the password when a pipeline with pv has already started then we’re unable to enter the password. Run sudo –validate first to ensure that we are preauthenticated.
6 7 8 |
# File 'lib/diskman/system.rb', line 6 def self.prepare_sudo_session! system('sudo --validate') end |