|
23 | 23 | from hashlib import sha1
|
24 | 24 | from json import loads, dumps
|
25 | 25 | from subprocess import Popen, PIPE
|
26 |
| -from os import access, X_OK |
| 26 | +from tempfile import mkstemp |
| 27 | +from os import access, X_OK, remove |
27 | 28 | from os.path import isfile, abspath, normpath, dirname, join, basename
|
28 | 29 |
|
29 | 30 | import requests
|
@@ -99,31 +100,48 @@ def index():
|
99 | 100 | join(hooks, 'all')
|
100 | 101 | ]
|
101 | 102 |
|
| 103 | + # Check permissions |
| 104 | + scripts = [s for s in scripts if isfile(s) and access(s, X_OK)] |
| 105 | + if not scripts: |
| 106 | + return '' |
| 107 | + |
| 108 | + # Save payload to temporal file |
| 109 | + _, tmpfile = mkstemp() |
| 110 | + with open(tmpfile, 'w') as pf: |
| 111 | + pf.write(dumps(payload)) |
| 112 | + |
102 | 113 | # Run scripts
|
103 | 114 | ran = {}
|
104 | 115 | for s in scripts:
|
105 |
| - if isfile(s) and access(s, X_OK): |
106 | 116 |
|
107 |
| - proc = Popen( |
108 |
| - [s, dumps(payload)], |
109 |
| - shell=True, |
110 |
| - stdout=PIPE, stderr=PIPE |
111 |
| - ) |
112 |
| - stdout, stderr = proc.communicate() |
| 117 | + proc = Popen( |
| 118 | + [s, tmpfile, event], |
| 119 | + shell=True, |
| 120 | + stdout=PIPE, stderr=PIPE |
| 121 | + ) |
| 122 | + stdout, stderr = proc.communicate() |
| 123 | + |
| 124 | + ran[basename(s)] = { |
| 125 | + 'returncode': proc.returncode, |
| 126 | + 'stdout': stdout, |
| 127 | + 'stderr': stderr, |
| 128 | + } |
113 | 129 |
|
114 |
| - ran[basename(s)] = { |
115 |
| - 'returncode': proc.returncode, |
116 |
| - 'stdout': stdout, |
117 |
| - 'stderr': stderr, |
118 |
| - } |
| 130 | + # Log errors if a hook failed |
| 131 | + if proc.returncode != 0: |
| 132 | + logging.error('{} : {} \n{}'.format( |
| 133 | + s, proc.returncode, stderr |
| 134 | + )) |
119 | 135 |
|
120 |
| - output = '' |
| 136 | + # Remove temporal file |
| 137 | + remove(tmpfile) |
121 | 138 |
|
122 | 139 | info = config.get('return_scripts_info', False)
|
123 |
| - if info: |
124 |
| - output = dumps(ran, sort_keys=True, indent=4) |
125 |
| - logging.debug(output) |
| 140 | + if not info: |
| 141 | + return '' |
126 | 142 |
|
| 143 | + output = dumps(ran, sort_keys=True, indent=4) |
| 144 | + logging.info(output) |
127 | 145 | return output
|
128 | 146 |
|
129 | 147 |
|
|
0 commit comments