Module: Sys

Extended by:
Sys
Included in:
Sys
Defined in:
lib/nub/sys.rb

Instance Method Summary collapse

Instance Method Details

#any_key?Boolean

Wait for any key to be pressed

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/nub/sys.rb', line 31

def any_key?
  begin
    state = `stty -g`
    `stty raw -echo -icanon isig`
    STDIN.getc.chr
  ensure
    `stty #{state}`
  end
end

#caller_filenameObject

Get the caller’s filename for the caller of the function this call is nested in not the function this call is called in



44
45
46
47
# File 'lib/nub/sys.rb', line 44

def caller_filename
  path = caller_locations(2, 1).first.path
  return File.basename(path)
end

#captureObject

Capture STDOUT to a string



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nub/sys.rb', line 51

def capture
  stdout, stderr = StringIO.new, StringIO.new
  $stdout, $stderr = stdout, stderr

  result = Proc.new.call

  $stdout, $stderr = STDOUT, STDERR
  $stdout.flush
  $stderr.flush

  return OpenStruct.new(result: result, stdout: stdout.string, stderr: stderr.string)
end

#env(var, required: true) ⇒ Object

Get the given environment variable by nam

Parameters:

  • var (String)

    name of the environment var

  • required (Bool) (defaults to: true)

    require that the variable exists by default



67
68
69
70
71
# File 'lib/nub/sys.rb', line 67

def env(var, required:true)
  value = ENV[var]
  Log.die("#{var} env variable is required!") if required && !value
  return value
end

#exec(cmd, env: {}, die: true) ⇒ Object

Run the system command in an exception wrapper

Parameters:

  • cmd (String/Array)

    cmd to execute

  • env (Hash) (defaults to: {})

    optional environment variables to set

  • die (Bool) (defaults to: true)

    fail on errors if true



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nub/sys.rb', line 78

def exec(cmd, env:{}, die:true)
  result = false
  puts("exec: #{cmd.is_a?(String) ? cmd : cmd * ' '}")

  begin
    if cmd.is_a?(String)
      result = system(env, cmd, out: $stdout, err: :out)
    else
      result = system(env, *cmd, out: $stdout, err: :out)
    end
  rescue Exception => e
    result = false
    puts(e.message.colorize(:red))
    puts(e.backtrace.inspect.colorize(:red))
    exit if die
  end

  !puts("Error: failed to execute command properly".colorize(:red)) and
    exit unless !die or result

  return result
end

#exec_status(cmd, die: true, check: nil) ⇒ Object

Execute the shell command and print status

Parameters:

  • cmd (String)

    command to execute

  • die (bool) (defaults to: true)

    exit on true



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/nub/sys.rb', line 105

def exec_status(cmd, die:true, check:nil)
  out = `#{cmd}`
  status = true
  status = check == out if !check.nil?
  status = $?.exitstatus == 0 if check.nil?

  #if status
  if status
    Log.puts("...success!".colorize(:green), stamp:false)
  else
    Log.puts("...failed!".colorize(:red), stamp:false)
    Log.puts(out.colorize(:red)) and exit if die
  end

  return status
end

#getpassObject

Read a password from stdin without echoing



124
125
126
127
128
129
# File 'lib/nub/sys.rb', line 124

def getpass
  print("Enter Password: ")
  pass = STDIN.noecho(&:gets).strip
  puts
  return pass
end

#rm_rf(path) ⇒ Object

Remove given dir or file Params:

path

path to delete

returns

path that was deleted



135
136
137
138
# File 'lib/nub/sys.rb', line 135

def rm_rf(path)
  Sys.exec("rm -rf #{path}")
  return path
end

#umount(mount, retries: 1) ⇒ Object

Unmount the given mount point Params:

mount

mount point to unmount

retries

number of times to retry



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/nub/sys.rb', line 144

def umount(mount, retries:1)
  check = ->(mnt){
    match = `mount`.split("\n").find{|x| x.include?(mnt)}
    match = match.split('overlay').first if match
    return (match and match.include?(mnt))
  }

  success = false
  while not success and retries > 0
    if check[mount]
      success = system('umount', '-fv', mount)
    else
      success = true
    end

    # Sleep for a second if failed
    sleep(1) and retries -= 1 unless success
  end

  # Die if still mounted
  !puts("Error: Failed to umount #{mount}".colorize(:red)) and
    exit if check[mount]
end