This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
102 lines (89 loc) · 2.67 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import json
from logging import getLogger
from logging.config import dictConfig
from os import environ
from pathlib import Path
from typing import Optional
from cloudflare_ddns import CloudflareDDNS, Configuration, SiteConfiguration
log_level = environ.get("LOG_LEVEL", "INFO")
dictConfig({
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": "%(asctime)s [%(levelname)-8s] %(name)-12s: %(message)s"
},
},
"handlers": {
"default": {
"level": "NOTSET",
"formatter": "standard",
"class": "logging.StreamHandler",
}
},
"loggers": {
"": {
"handlers": ["default"],
"level": "WARNING",
"propagate": True
},
"cloudflare_ddns": {
"handlers": ["default"],
"level": log_level,
"propagate": False
},
"__main__": {
"handlers": ["default"],
"level": log_level,
"propagate": False
}
}
})
logger = getLogger(__name__)
def main():
config: Optional[Configuration] = load_from_file()
if config is None:
config = load_from_env()
if config is None:
logger.error("Fail to any configs")
return
CloudflareDDNS(config).update()
def load_from_file() -> Optional[Configuration]:
config_path = environ.get("CONFIG_PATH")
if config_path is None:
logger.debug("CONFIG_PATH is not set")
return None
path = Path(config_path)
if path.is_file():
with open(path, "r", encoding="utf-8") as file:
try:
data = json.load(file)
return Configuration.from_dict(data)
except (TypeError, ValueError, IOError):
logger.exception("Fail to load from file")
else:
logger.warning(f"{path.absolute()} is not a file")
return None
def load_from_env() -> Optional[Configuration]:
try:
ttl = environ.get("TTL")
ttl = 1 if ttl is None else int(ttl)
proxied = environ.get("PROXIED", "")
if proxied is None:
proxied = True
else:
proxied = proxied.lower() in ("yes", "true", "1")
site = SiteConfiguration(
proxied=proxied,
ttl=ttl,
email=environ.get("EMAIL"),
api_key=environ.get("API_KEY"),
zone=environ.get("ZONE"),
domain=environ.get("DOMAIN"),
)
return Configuration(config=[site])
except (TypeError, ValueError):
logger.exception("Fail to load from environment")
return None
if __name__ == "__main__":
main()