diff options
Diffstat (limited to '_.lua')
| -rwxr-xr-x | _.lua | 170 |
1 files changed, 170 insertions, 0 deletions
| @@ -0,0 +1,170 @@ | |||
| 1 | #!/usr/bin/env luajit | ||
| 2 | |||
| 3 | |||
| 4 | -- Most of this _ stuff was copied from apt-panopticon. | ||
| 5 | local _ = {} | ||
| 6 | |||
| 7 | _.version = '0.0 crap' | ||
| 8 | |||
| 9 | _.D = function(s) print('DEBUG ' .. s) end | ||
| 10 | _.I = function(s) print('INFO ' .. s) end | ||
| 11 | _.T = function(s) print('TIMEOUT ' .. s) end | ||
| 12 | _.W = function(s) print('WARNING ' .. s) end | ||
| 13 | _.E = function(s) print('ERROR ' .. s) end | ||
| 14 | _.C = function(s) print('CRITICAL ' .. s) end | ||
| 15 | local D = _.D | ||
| 16 | local I = _.I | ||
| 17 | local T = _.T | ||
| 18 | local W = _.W | ||
| 19 | local E = _.E | ||
| 20 | local C = _.C | ||
| 21 | |||
| 22 | |||
| 23 | _.readCmd = function(cmd) | ||
| 24 | local result = {} | ||
| 25 | local output = io.popen(cmd) | ||
| 26 | if nil ~= output then | ||
| 27 | for l in output:lines() do | ||
| 28 | table.insert(result, l) | ||
| 29 | end | ||
| 30 | end | ||
| 31 | -- While this does return the same things as os.execute(), it's just as useless. | ||
| 32 | output:close() | ||
| 33 | return result | ||
| 34 | end | ||
| 35 | |||
| 36 | |||
| 37 | _._ = function(c) | ||
| 38 | local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c} | ||
| 39 | local n = 0 | ||
| 40 | |||
| 41 | exe.cmd = '{ ' | ||
| 42 | if 'table' == type(c) then | ||
| 43 | for i, l in ipairs(c) do | ||
| 44 | n = n + 1 | ||
| 45 | exe.cmd = exe.cmd .. l .. ' ; ' | ||
| 46 | end | ||
| 47 | elseif 'string' == type(c) then | ||
| 48 | for l in string.gmatch(c, "\n*([^\n]+)\n*") do | ||
| 49 | if '' ~= l then | ||
| 50 | n = n + 1 | ||
| 51 | exe.cmd = exe.cmd .. l .. ' ; ' | ||
| 52 | end | ||
| 53 | end | ||
| 54 | end | ||
| 55 | exe.cmd = exe.cmd .. ' } ' | ||
| 56 | if 1 == n then exe.cmd = c end | ||
| 57 | |||
| 58 | function exe:log() | ||
| 59 | self.logging = true | ||
| 60 | return self | ||
| 61 | end | ||
| 62 | function exe:show() | ||
| 63 | self.showing = true | ||
| 64 | return self | ||
| 65 | end | ||
| 66 | function exe:Nice(c) | ||
| 67 | if nil == c then | ||
| 68 | self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd | ||
| 69 | else | ||
| 70 | self.cmd = self.cmd .. ' ionice -c3 nice -n 19 ' .. c .. ' ' | ||
| 71 | end | ||
| 72 | return self | ||
| 73 | end | ||
| 74 | function exe:timeout(c) | ||
| 75 | -- timeout returns a status of - command status if --preserve-status; "128+9" (actually 137) if --kill-after ends up being done; 124 if it had to TERM; command status if all went well. | ||
| 76 | -- --kill-after means "send KILL after TERM fails. | ||
| 77 | if nil == c then | ||
| 78 | self.cmd = 'timeout --kill-after=10.0 --foreground 42.0s ' .. self.cmd | ||
| 79 | else | ||
| 80 | self.cmd = 'timeout --kill-after=10.0 --foreground ' .. c .. ' ' .. self.cmd | ||
| 81 | end | ||
| 82 | return self | ||
| 83 | end | ||
| 84 | function exe:also(c) | ||
| 85 | if nil == c then c = '' else c = ' ' .. c end | ||
| 86 | self.cmd = self.cmd .. '; ' .. c .. ' ' | ||
| 87 | return self | ||
| 88 | end | ||
| 89 | function exe:And(c) | ||
| 90 | if nil == c then c = '' else c = ' ' .. c end | ||
| 91 | self.cmd = self.cmd .. ' && ' .. c .. ' ' | ||
| 92 | return self | ||
| 93 | end | ||
| 94 | function exe:Or(c) | ||
| 95 | if nil == c then c = '' end | ||
| 96 | self.cmd = self.cmd .. ' || ' .. c .. ' ' | ||
| 97 | return self | ||
| 98 | end | ||
| 99 | function exe:noErr() | ||
| 100 | self.cmd = self.cmd .. ' 2>/dev/null ' | ||
| 101 | return self | ||
| 102 | end | ||
| 103 | function exe:noOut() | ||
| 104 | self.cmd = self.cmd .. ' 1>/dev/null ' | ||
| 105 | return self | ||
| 106 | end | ||
| 107 | function exe:wait(w) | ||
| 108 | self.cmd = self.cmd .. ' && touch ' .. w .. ' ' | ||
| 109 | return self | ||
| 110 | end | ||
| 111 | function exe:Do() | ||
| 112 | --[[ "The condition expression of a control structure can return any | ||
| 113 | value. Both false and nil are considered false. All values different | ||
| 114 | from nil and false are considered true (in particular, the number 0 | ||
| 115 | and the empty string are also true)." | ||
| 116 | says the docs, I beg to differ.]] | ||
| 117 | if true == self.logging then D(" executing - " .. self.cmd) end | ||
| 118 | --[[ Damn os.execute() | ||
| 119 | Lua 5.1 says it returns "a status code, which is system-dependent" | ||
| 120 | Lua 5.2 says it returns true/nil, "exit"/"signal", the status code. | ||
| 121 | I'm getting 7168 or 0. No idea what the fuck that is. | ||
| 122 | local ok, rslt, status = os.execute(s) | ||
| 123 | ]] | ||
| 124 | self.lines = _.readCmd(self.cmd .. '; echo "$?"', 'r') | ||
| 125 | -- The last line will be the command's returned status, collect everything else in lines. | ||
| 126 | self.status = tonumber(self.lines[#self.lines]) | ||
| 127 | self.lines[#self.lines] = nil | ||
| 128 | if true == self.showing then for i, l in ipairs(self.lines) do D(l) end end | ||
| 129 | if (137 == self.status) or (124 == self.status) then | ||
| 130 | T("timeout killed " .. self.status .. ' ' .. self.command) | ||
| 131 | elseif (nil == self.status) then | ||
| 132 | I("STATUS |" .. "NIL" .. '| ' .. self.command) | ||
| 133 | elseif (0 ~= self.status) then | ||
| 134 | I("STATUS |" .. self.status .. '| ' .. self.command) | ||
| 135 | end | ||
| 136 | return self | ||
| 137 | end | ||
| 138 | function exe:fork(host) | ||
| 139 | if nil ~= host then self.cmd = self.cmd .. '; r=$?; if [ $r -ge 124 ]; then echo "$r ' .. host .. ' failed forked command ' .. string.gsub(self.cmd, '"', "'") .. '"; fi' end | ||
| 140 | self.cmd = '{ ' .. self.cmd .. ' ; } & ' | ||
| 141 | if true == self.logging then D(" forking - " .. self.cmd) end | ||
| 142 | os.execute(self.cmd) | ||
| 143 | return self | ||
| 144 | end | ||
| 145 | return exe | ||
| 146 | end | ||
| 147 | local __ = _._ | ||
| 148 | |||
| 149 | |||
| 150 | _.exists = function(c) | ||
| 151 | if 0 == __('which ' .. c):Do().status then return true end | ||
| 152 | return false | ||
| 153 | end | ||
| 154 | |||
| 155 | |||
| 156 | _.killEmAll = function(all) | ||
| 157 | for i,l in ipairs(all) do | ||
| 158 | local c = 0 | ||
| 159 | while 0 ~= tonumber(__("pgrep -u $USER -xc " .. l):Do().lines[1]) do | ||
| 160 | local s = 'TERM' | ||
| 161 | if c > 1 then s = 'KILL'; __("sleep " .. c):Do() end | ||
| 162 | __("pkill -" .. s .. " -u $USER -x " .. l):log():Do() | ||
| 163 | c = c + 1 | ||
| 164 | end | ||
| 165 | end | ||
| 166 | end | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | return _ | ||
