forked from rfancn/aliyun-ddns-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.py
169 lines (137 loc) · 5.99 KB
/
record.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# coding=utf-8
"""
Copyright (C) 2010-2013, Ryan Fan
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import sys
if sys.version_info < (3,):
import string
def lower_func(s):
return string.lower(s)
else:
def lower_func(s):
return str.lower(s)
from utils import DDNSUtils
from yunresolver import YunResolver
class LocalDomainRecord(object):# pylint: disable=too-few-public-methods
"""
Local domain record created from config file
"""
VALID_TYPES = ('A', 'AAAA')
def __init__(self, config, section):
# set the local record alias to be section
self.alias = section
self.domainname = config.get_option_value(section, "domain")
self.rr = self.subdomain = config.get_option_value(section, "sub_domain")
self.type = config.get_option_value(section, "type", default="A")
if not self.domainname:
raise ValueError("Failed initializing LocalDomainRecord: " \
"Invalid domain in config file.")
if not self.rr:
raise ValueError("Failed initializing LocalDomainRecord: " \
"Invalid sub_domain in config file.")
if self.type.upper() not in self.VALID_TYPES:
raise ValueError("Failed initializing LocalDomainRecord: " \
"Invalid type in config file.")
class RemoteDomainRecord(object):# pylint: disable=too-many-instance-attributes, too-few-public-methods
"""
Remote domain record created from Aliyun server
"""
def __init__(self, domain_record_info):
self.domainname = None
self.recordid = None
self.rr = None
self.type = None
self.value = None
self.ttl = None
self.priority = None
self.line = None
self.status = None
self.locked = False
# convert json record key name to lowercased one
converted_domain_record_info = dict(zip(map(lower_func, domain_record_info.keys()),
domain_record_info.values()))
for k in converted_domain_record_info.keys():
self.__dict__[k] = converted_domain_record_info[k]
class DDNSDomainRecordManager(object):
"""
Manager class used to manage local domain record and remote domain records
"""
def __init__(self, config):
self.config = config
self.resolver = YunResolver(self.config.access_id, self.config.access_key, self.config.debug)
self.local_record_list = self.get_local_record_list()
def get_local_record_list(self):
"""
Create local domain record objectes based on local config info
:return: list of LocalDomainRecord objects
"""
local_record_list = []
for section in self.config.get_domain_record_sections():
try:
local_record = LocalDomainRecord(self.config, section)
except ValueError:
continue
local_record_list.append(local_record)
return local_record_list
def find_local_record(self, remote_record):
"""
Find LocalDomainRecord based on RemoteDomainRecord
:param RemoteDomainRecord
:return: LocalDomainRecord or None
"""
for local_record in self.local_record_list:
if all(getattr(local_record, attr) == getattr(remote_record, attr)
for attr in ('domainname', 'rr', 'type')):
return local_record
return None
def fetch_remote_record(self, local_record):
"""
Fetch RemoteDomainReord from Aliyun server by using LocalDomainRecord info
:param LocalDomainRecord
:return: RemoteDomainRecord or None
"""
# Aliyun use fuzzy matching pattern for RR and type keyword
fuzzy_matched_list = self.resolver.describe_domain_records(local_record.domainname,
rr_keyword=local_record.rr,
type_keyword=local_record.type)
if not fuzzy_matched_list:
DDNSUtils.err("Failed to fetch remote DomainRecords.")
return None
exact_matched_list = []
check_keys = ('DomainName', 'RR', 'Type')
for rec in fuzzy_matched_list:
if all(rec.get(key, None) == getattr(local_record, key.lower()) for key in check_keys):
exact_matched_list.append(rec)
if not exact_matched_list:
return None
if len(exact_matched_list) > 1:
DDNSUtils.err("Duplicate DomainRecord in Aliyun: {rec.subdomain}.{rec.domainname}"
.format(rec=local_record))
return None
try:
remote_record = RemoteDomainRecord(exact_matched_list[0])
except Exception as ex:
raise ex
return remote_record
def update(self, remote_record, current_public_ip):
"""
Update RemoteDomainRecord 's value to current public IP on Aliyun server
:param RemoteDomainRecord:
:param current public IP
:return: True or False
"""
return self.resolver.update_domain_record(remote_record.recordid,
rr=remote_record.rr,
record_value=current_public_ip)