nub 0.0.70 → 0.0.72
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nub/sys.rb +122 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c381c27337a97c6edadf9e4a928a4dd7adee88e6af49d0437089362fb89862da
|
4
|
+
data.tar.gz: fcb68bf919c81141910718a1af78b84cff7b7ff936697d0610e69b8ca54da2a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9750357d487da6f02a19a0b1664ae4c774cfea1207a7fad6218f60f69908c4aa7161a690f8d1a10d2fc835df9066171829842873d0a4341d02c8c491db0ddd02
|
7
|
+
data.tar.gz: 793329dd53c30bc8c90050f5f63bf1a18d254caeb058b8a447501572e14a2c32ae31bfbc4bceacb2cd0088234fcb7b205e426b11b601831203c0f57d5a3b615e
|
data/lib/nub/sys.rb
CHANGED
@@ -27,15 +27,6 @@ require_relative 'log'
|
|
27
27
|
module Sys
|
28
28
|
extend self
|
29
29
|
|
30
|
-
# Get the given environment variable by nam
|
31
|
-
# @param var [String] name of the environment var
|
32
|
-
# @param required [Bool] require that the variable exists by default
|
33
|
-
def env(var, required:true)
|
34
|
-
value = ENV[var]
|
35
|
-
Log.die("#{var} env variable is required!") if required && !value
|
36
|
-
return value
|
37
|
-
end
|
38
|
-
|
39
30
|
# Wait for any key to be pressed
|
40
31
|
def any_key?
|
41
32
|
begin
|
@@ -70,6 +61,60 @@ module Sys
|
|
70
61
|
return OpenStruct.new(result: result, stdout: stdout.string, stderr: stderr.string)
|
71
62
|
end
|
72
63
|
|
64
|
+
# Drop root privileges to original user
|
65
|
+
# Only affects ruby commands not system commands
|
66
|
+
# Params:
|
67
|
+
# +returns+:: uid, gid of prior user
|
68
|
+
def drop_privileges()
|
69
|
+
uid = gid = nil
|
70
|
+
|
71
|
+
if Process.uid.zero?
|
72
|
+
uid, gid = Process.uid, Process.gid
|
73
|
+
sudo_uid, sudo_gid = ENV['SUDO_UID'].to_i, ENV['SUDO_GID'].to_i
|
74
|
+
Process::Sys.setegid(sudo_uid)
|
75
|
+
Process::Sys.seteuid(sudo_gid)
|
76
|
+
end
|
77
|
+
|
78
|
+
return uid, gid
|
79
|
+
end
|
80
|
+
|
81
|
+
# Get the given environment variable by nam
|
82
|
+
# @param var [String] name of the environment var
|
83
|
+
# @param required [Bool] require that the variable exists by default
|
84
|
+
def env(var, required:true)
|
85
|
+
value = ENV[var]
|
86
|
+
Log.die("#{var} env variable is required!") if required && !value
|
87
|
+
return value
|
88
|
+
end
|
89
|
+
|
90
|
+
# Run the system command in an exception wrapper
|
91
|
+
# @param cmd [String/Array] cmd to execute
|
92
|
+
# @param env [Hash] optional environment variables to set
|
93
|
+
# @param die [Bool] fail on errors if true
|
94
|
+
# @returns boolean status, string message
|
95
|
+
def exec(cmd, env:{}, die:true)
|
96
|
+
result = false
|
97
|
+
puts("exec: #{cmd.is_a?(String) ? cmd : cmd * ' '}")
|
98
|
+
|
99
|
+
begin
|
100
|
+
if cmd.is_a?(String)
|
101
|
+
result = system(env, cmd, out: $stdout, err: :out)
|
102
|
+
else
|
103
|
+
result = system(env, *cmd, out: $stdout, err: :out)
|
104
|
+
end
|
105
|
+
rescue Exception => e
|
106
|
+
result = false
|
107
|
+
puts(e.message.colorize(:red))
|
108
|
+
puts(e.backtrace.inspect.colorize(:red))
|
109
|
+
exit if die
|
110
|
+
end
|
111
|
+
|
112
|
+
!puts("Error: failed to execute command properly".colorize(:red)) and
|
113
|
+
exit unless !die or result
|
114
|
+
|
115
|
+
return result
|
116
|
+
end
|
117
|
+
|
73
118
|
# Read a password from stdin without echoing
|
74
119
|
# @returns pass [String] the password read in
|
75
120
|
def getpass
|
@@ -78,6 +123,74 @@ module Sys
|
|
78
123
|
puts
|
79
124
|
return pass
|
80
125
|
end
|
126
|
+
|
127
|
+
# Update pacman database
|
128
|
+
def pacman_update
|
129
|
+
success = false
|
130
|
+
while not success
|
131
|
+
begin
|
132
|
+
Sys.exec("pacman -Sy")
|
133
|
+
success = true
|
134
|
+
rescue Exception => e
|
135
|
+
puts(e.message)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Raise privileges if dropped earlier
|
141
|
+
# Only affects ruby commands not system commands
|
142
|
+
# Params:
|
143
|
+
# +uid+:: uid of user to assume
|
144
|
+
# +gid+:: gid of user to assume
|
145
|
+
def raise_privileges(uid, gid)
|
146
|
+
if uid and gid
|
147
|
+
Process::Sys.seteuid(uid)
|
148
|
+
Process::Sys.setegid(gid)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Remove given dir or file
|
153
|
+
# Params:
|
154
|
+
# +path+:: path to delete
|
155
|
+
# +returns+:: path that was deleted
|
156
|
+
def rm_rf(path)
|
157
|
+
Sys.exec("rm -rf #{path}")
|
158
|
+
return path
|
159
|
+
end
|
160
|
+
|
161
|
+
# Unmount the given mount point
|
162
|
+
# Params:
|
163
|
+
# +mount+:: mount point to unmount
|
164
|
+
# +retries+:: number of times to retry
|
165
|
+
def umount(mount, retries:1)
|
166
|
+
check = ->(mnt){
|
167
|
+
match = `mount`.split("\n").find{|x| x.include?(mnt)}
|
168
|
+
match = match.split('overlay').first if match
|
169
|
+
return (match and match.include?(mnt))
|
170
|
+
}
|
171
|
+
|
172
|
+
success = false
|
173
|
+
while not success and retries > 0
|
174
|
+
if check[mount]
|
175
|
+
success = system('umount', '-fv', mount)
|
176
|
+
else
|
177
|
+
success = true
|
178
|
+
end
|
179
|
+
|
180
|
+
# Sleep for a second if failed
|
181
|
+
sleep(1) and retries -= 1 unless success
|
182
|
+
end
|
183
|
+
|
184
|
+
# Die if still mounted
|
185
|
+
!puts("Error: Failed to umount #{mount}".colorize(:red)) and
|
186
|
+
exit if check[mount]
|
187
|
+
end
|
188
|
+
|
189
|
+
# Get the current user taking into account sudo
|
190
|
+
# @returns the current user
|
191
|
+
def user
|
192
|
+
return Process.uid.zero? ? Etc.getpwuid(ENV['SUDO_UID'].to_i).name : ENV['USER']
|
193
|
+
end
|
81
194
|
end
|
82
195
|
|
83
196
|
# vim: ft=ruby:ts=2:sw=2:sts=2
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.72
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Crummett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|