Created
October 1, 2022 19:34
-
-
Save malikperang/e70443dde155f9ff447310e350f3f7c8 to your computer and use it in GitHub Desktop.
Stop and Disable OpenStack Service Using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import os | |
import json | |
tmp_file = "tmp_list.json" | |
if os.path.exists(tmp_file): | |
os.remove(tmp_file) | |
else: | |
print("Not exists") | |
cmd="systemctl list-units \ | |
--type service \ | |
'openstack-*'\ | |
--all \ | |
--plain \ | |
--no-legend \ | |
--no-pager \ | |
| sed 's/ \{1,\}/,/g' \ | |
| jq \ | |
--raw-input \ | |
--slurp \ | |
'split(\"\n\") | map(split(\",\")) | .[0:-1] | map( { \"unit\": .[0], \"load\": .[1], \"active\": .[2], \"sub\": .[3] } )'" | |
cmd=os.popen(cmd).read() | |
f = open("tmp_list.json","a") | |
f.write(cmd) | |
f.close() | |
f = open("tmp_list.json") | |
data=json.load(f) | |
for i in data: | |
print(i['unit']) | |
cmd="systemctl stop " + i['unit'] | |
cmd_disable="systemctl disable " + i['unit'] | |
os.popen(cmd).read(); | |
os.popen(cmd_disable).read(); | |
if os.path.exists(tmp_file): | |
os.remove(tmp_file) | |
else: | |
print("Not exists") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment