|
| 1 | +import yaml |
| 2 | +import util |
| 3 | +from os import path |
| 4 | +from dataclasses import dataclass |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class Config: |
| 10 | + tags: list |
| 11 | + inclusions: list |
| 12 | + exclusions: list |
| 13 | + exitCodes: set |
| 14 | + restartPolicyEnabled: bool |
| 15 | + restartPolicyCountNotify: int |
| 16 | + telegramChatId: str |
| 17 | + telegramToken: str |
| 18 | + discordUrl: str |
| 19 | + slackUrl: str |
| 20 | + |
| 21 | + |
| 22 | +conf = Config(None, None, None, {1, 139}, False, None, None, None, None, None) |
| 23 | + |
| 24 | + |
| 25 | +def printSetConfig(finalConf): |
| 26 | + resultStr = "The following config params were set:\n" |
| 27 | + if finalConf.tags is not None: |
| 28 | + resultStr += f"- tags = {conf.tags}\n" |
| 29 | + if finalConf.inclusions is not None: |
| 30 | + resultStr += f"- inclusions = {conf.inclusions}\n" |
| 31 | + if finalConf.exclusions is not None: |
| 32 | + resultStr += f"- exclusions = {conf.exclusions}\n" |
| 33 | + |
| 34 | + resultStr += f"- container_exit_codes = {conf.exitCodes}\n" |
| 35 | + resultStr += f"- container_restart_policy_enabled = {conf.restartPolicyEnabled}\n" |
| 36 | + |
| 37 | + if finalConf.restartPolicyCountNotify is not None and finalConf.restartPolicyEnabled is True: |
| 38 | + resultStr += f"- container_restart_policy_max_count_notify = {conf.restartPolicyCountNotify}\n" |
| 39 | + if finalConf.telegramToken is not None: |
| 40 | + resultStr += f"- telegram.token = {conf.telegramToken}\n" |
| 41 | + if finalConf.telegramChatId is not None: |
| 42 | + resultStr += f"- telegram.chat_id = {conf.telegramChatId}\n" |
| 43 | + if finalConf.discordUrl is not None: |
| 44 | + resultStr += f"- discord.url = {conf.discordUrl}\n" |
| 45 | + if finalConf.slackUrl is not None: |
| 46 | + resultStr += f"- slack.url = {conf.slackUrl}\n" |
| 47 | + |
| 48 | + print(resultStr) |
| 49 | + |
| 50 | + |
| 51 | +def initConfig(): |
| 52 | + try: |
| 53 | + if path.exists('/yaml/config.yml'): |
| 54 | + with open('/yaml/config.yml') as f: |
| 55 | + docs = yaml.load_all(f, Loader=yaml.FullLoader) |
| 56 | + |
| 57 | + for doc in docs: |
| 58 | + for k, v in doc.items(): |
| 59 | + if k == "general_settings" and v is not None: |
| 60 | + for sk, sv in v.items(): |
| 61 | + if sk == "tags": |
| 62 | + conf.tags = sv |
| 63 | + if sk == "inclusions": |
| 64 | + tmpSet = set() |
| 65 | + for item in sv: |
| 66 | + tmpSet.add(util.safeCast(item, str)) |
| 67 | + conf.inclusions = set(filter(None, tmpSet)) |
| 68 | + if sk == "exclusions": |
| 69 | + tmpSet = set() |
| 70 | + for item in sv: |
| 71 | + tmpSet.add(util.safeCast(item, str)) |
| 72 | + conf.exclusions = set(filter(None, tmpSet)) |
| 73 | + |
| 74 | + # KONTEJNERI |
| 75 | + if k == "container_settings": |
| 76 | + for ck, cv in v.items(): |
| 77 | + if ck == "exit_codes" and cv is not None: |
| 78 | + tmpSet = set() |
| 79 | + for item in cv: |
| 80 | + tmpSet.add(util.safeCast(item, int, None)) |
| 81 | + conf.exitCodes = set(filter(None, tmpSet)) |
| 82 | + if ck == "restart_policy" and cv is not None: |
| 83 | + for resKey, resVal in cv.items(): |
| 84 | + if resKey == "enabled": |
| 85 | + conf.restartPolicyEnabled = util.safeCastBool(resVal, False) |
| 86 | + if resKey == "max_count_notify": |
| 87 | + conf.restartPolicyCountNotify = util.safeCast(resVal, int, None) |
| 88 | + |
| 89 | + # INNTEGRACIIII |
| 90 | + if k == "integrations": |
| 91 | + for intKey, intVal in v.items(): |
| 92 | + if intKey == "telegram" and intVal is not None: |
| 93 | + for telKey, telVal in intVal.items(): |
| 94 | + if telKey == "token": |
| 95 | + conf.telegramToken = telVal |
| 96 | + if telKey == "chat_id": |
| 97 | + conf.telegramChatId = util.safeCast(telVal, int, None) |
| 98 | + |
| 99 | + if intKey == "discord" and intVal is not None: |
| 100 | + for disKey, disVal in intVal.items(): |
| 101 | + if disKey == "url": |
| 102 | + conf.discordUrl = disVal |
| 103 | + |
| 104 | + if intKey == "slack" and intVal is not None: |
| 105 | + for disKey, disVal in intVal.items(): |
| 106 | + if disKey == "url": |
| 107 | + conf.slackUrl = disVal |
| 108 | + |
| 109 | + # default to 5 count notify if the restart policy has been enabled and notify hasn't been set |
| 110 | + if conf.restartPolicyEnabled is True and conf.restartPolicyCountNotify is None: |
| 111 | + conf.restartPolicyCountNotify = 5 |
| 112 | + |
| 113 | + # if the integrations are left on their default value - set them to None |
| 114 | + if conf.slackUrl == "<PASTE YOUR SLACK WEBHOOK HERE>": |
| 115 | + conf.slackUrl = None |
| 116 | + if conf.discordUrl == "<PASTE YOUR DISCORD WEBHOOK HERE>": |
| 117 | + conf.discordUrl = None |
| 118 | + if conf.telegramToken == "<PASTE YOUR TELEGRAM BOT TOKEN HERE>": |
| 119 | + conf.telegramToken = None |
| 120 | + conf.telegramChatId = None |
| 121 | + if conf.telegramChatId == "<PASTE YOUR TELEGRAM BOT CHAT_ID HERE>": |
| 122 | + conf.telegramChatId = None |
| 123 | + conf.telegramToken = None |
| 124 | + |
| 125 | + if conf.telegramToken is None and conf.telegramChatId is None and conf.discordUrl is None and conf.slackUrl is None: |
| 126 | + print("ERROR: At least one integration has to be set - now exiting!") |
| 127 | + sys.exit(0) |
| 128 | + elif (conf.telegramToken is not None and conf.telegramChatId is None) or ( |
| 129 | + conf.telegramToken is None and conf.telegramChatId is not None): |
| 130 | + print("ERROR: When using Telegram, both token and chat id must be set - now exiting!") |
| 131 | + sys.exit(0) |
| 132 | + |
| 133 | + printSetConfig(conf) |
| 134 | + return conf |
| 135 | + |
| 136 | + else: |
| 137 | + print( |
| 138 | + "ERROR: config.yml file not found (please bind the volume that contains the config.yml file) - now exiting!") |
| 139 | + sys.exit(0) |
| 140 | + |
| 141 | + except Exception as e: |
| 142 | + print("ERROR: config.yml file is not a valid yml file - now exiting!", e) |
| 143 | + sys.exit(0) |
0 commit comments