commit 9c4ce6f1143a003abb893ad2bd1b894334c23118 Author: shirenn Date: Fri Jun 4 16:59:17 2021 +0200 Scripts for keepalive to start and stop service diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7d55d55 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +keepalived.json diff --git a/keepalived.py b/keepalived.py new file mode 100755 index 0000000..0d81b03 --- /dev/null +++ b/keepalived.py @@ -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])