Scripts for keepalive to start and stop service

This commit is contained in:
shirenn 2021-06-04 16:59:17 +02:00 committed by root
commit 9c4ce6f114
2 changed files with 47 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
keepalived.json

46
keepalived.py Executable file
View file

@ -0,0 +1,46 @@
#!/bin/env python3
import argparse
import json
import os
import subprocess
import sys
from enum import Enum
path = os.path.dirname(os.path.abspath(__file__))
class Type(Enum):
GROUP = 'GROUP'
INSTANCE = 'INSTANCE'
def __str__(self):
return self.value
class State(Enum):
MASTER = 'MASTER'
BACKUP = 'BACKUP'
FAULT = 'FAULT'
STOP = 'STOP'
DELETED = 'DELETED'
def __str__(self):
return self.value
def handling(state):
if state == State.MASTER:
return 'start'
else:
return 'stop'
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Start and stop service on keepalived switch')
parser.add_argument('TYPE', type=Type, choices=list(Type))
parser.add_argument('NAME', type=str)
parser.add_argument('STATE', type=State, choices=list(State))
parser.add_argument('PRIORITY', type=int)
args = parser.parse_args()
with open(os.path.join(path, "keepalived.json")) as config_file:
config = json.load(config_file)
for service in config['services'][args.NAME]:
subprocess.run(['systemctl', handling(args.STATE), service])