mirror of
https://github.com/Security-Onion-Solutions/securityonion.git
synced 2025-12-06 17:22:49 +01:00
Implement locking to so-firewall script
This commit is contained in:
@@ -15,10 +15,13 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
lockFile = "/tmp/so-firewall.lock"
|
||||||
hostgroupsFilename = "/opt/so/saltstack/local/salt/firewall/hostgroups.local.yaml"
|
hostgroupsFilename = "/opt/so/saltstack/local/salt/firewall/hostgroups.local.yaml"
|
||||||
portgroupsFilename = "/opt/so/saltstack/local/salt/firewall/portgroups.local.yaml"
|
portgroupsFilename = "/opt/so/saltstack/local/salt/firewall/portgroups.local.yaml"
|
||||||
defaultPortgroupsFilename = "/opt/so/saltstack/default/salt/firewall/portgroups.yaml"
|
defaultPortgroupsFilename = "/opt/so/saltstack/default/salt/firewall/portgroups.yaml"
|
||||||
@@ -329,7 +332,7 @@ def apply():
|
|||||||
proc = subprocess.run(['salt-call', 'state.apply', 'firewall', 'queue=True'])
|
proc = subprocess.run(['salt-call', 'state.apply', 'firewall', 'queue=True'])
|
||||||
return proc.returncode
|
return proc.returncode
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
options = []
|
options = []
|
||||||
args = sys.argv[1:]
|
args = sys.argv[1:]
|
||||||
for option in args:
|
for option in args:
|
||||||
@@ -356,8 +359,30 @@ def main():
|
|||||||
"addportgroup": addportgroup
|
"addportgroup": addportgroup
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = commands.get(args[0], showUsage)
|
code=1
|
||||||
code = cmd(options, args[1:])
|
|
||||||
|
try:
|
||||||
|
lockAttempts = 0
|
||||||
|
maxAttempts = 30
|
||||||
|
while lockAttempts < maxAttempts:
|
||||||
|
lockAttempts = lockAttempts + 1
|
||||||
|
try:
|
||||||
|
f = open(lockFile, "x")
|
||||||
|
f.close()
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
if lockAttempts == maxAttempts:
|
||||||
|
print("Lock file (" + lockFile + ") could not be created; proceeding without lock.")
|
||||||
|
|
||||||
|
cmd = commands.get(args[0], showUsage)
|
||||||
|
code = cmd(options, args[1:])
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
os.remove(lockFile)
|
||||||
|
except:
|
||||||
|
print("Lock file (" + lockFile + ") already removed")
|
||||||
|
|
||||||
sys.exit(code)
|
sys.exit(code)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user