-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalwaysdata.py
281 lines (259 loc) · 11 KB
/
alwaysdata.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from alwaysdata_api import Auth, Domain, Site, Subdomain, SSLCertificate
from tools import isSubdomain
import checker
class DomainCheckerAlwaysdata(checker.DomainChecker):
ALWAYSDATA_API_KEY = ""
ALWAYSDATA_ACCOUNT_LIST = []
ALWAYSDATA_ACCOUNT_DOMAIN = ""
account_data = {}
def __init__(self, domains, to_do, ALWAYSDATA_API_KEY,
ALWAYSDATA_ACCOUNT_LIST, ALWAYSDATA_ACCOUNT_DOMAIN):
super().__init__(domains, to_do)
self.ALWAYSDATA_API_KEY = ALWAYSDATA_API_KEY
self.ALWAYSDATA_ACCOUNT_LIST = ALWAYSDATA_ACCOUNT_LIST
self.ALWAYSDATA_ACCOUNT_DOMAIN = ALWAYSDATA_ACCOUNT_DOMAIN
self.account_data = {}
if self.ALWAYSDATA_ACCOUNT_DOMAIN not in self.ALWAYSDATA_ACCOUNT_LIST:
raise Exception("ALWAYSDATA_ACCOUNT_DOMAIN must \
be in ALWAYSDATA_ACCOUNT_LIST")
for account in self.ALWAYSDATA_ACCOUNT_LIST:
data = {"auth": Auth(account, self.ALWAYSDATA_API_KEY)}
data["aldomains"] = Domain.list(auth=data["auth"])
data["alsubdomains"] = Subdomain.list(auth=data["auth"])
data["alsite"] = Site.list(auth=data["auth"])
data["alsslcert"] = SSLCertificate.list(auth=data["auth"])
self.account_data[account] = data
def check(self):
account_domain = self.account_data[self.ALWAYSDATA_ACCOUNT_DOMAIN]
domains_add = []
subdomains_add = []
site_add_dom = []
sslcert_add = []
sslcert_assign = []
for d in self.domains:
if d.sitehost == 'extern':
continue
print("Check %s domain ..." % (d.name))
if d.account is None:
print(" ERROR account not specified")
continue
domain_account = self.account_data[d.account]
aldomains = account_domain["aldomains"]
alsubdomains = domain_account["alsubdomains"]
alsite = domain_account["alsite"]
alsslcert = domain_account["alsslcert"]
found = False
if not isSubdomain(d.name):
for ad in aldomains:
if ad.name == d.name:
found = True
break
if not found:
domains_add.append(d)
else:
for asd in alsubdomains:
if asd.hostname == d.name:
found = True
break
if not found:
subdomains_add.append(d)
found_site = False
for asite in alsite:
if asite.name == d.sitehost:
if d.name + '/' in asite.addresses:
found_site = True
break
if not found_site:
site_add_dom.append(d)
ssl = None
for assl in alsslcert:
if d.name == assl.name:
ssl = assl
break
if ssl is None:
sslcert_add.append(d)
alsubdom = None
for e in alsubdomains:
if e.hostname == d.name:
alsubdom = e
break
if alsubdom is not None and \
(alsubdom.ssl_certificate is None or
alsubdom.ssl_certificate['href'] != ssl.href):
sslcert_assign.append(d)
try:
self.to_do["domains_add"] = self.to_do["domains_add"] + domains_add
except Exception:
self.to_do["domains_add"] = domains_add
try:
self.to_do["subdomains_add"] = self.to_do["subdomains_add"] \
+ subdomains_add
except Exception:
self.to_do["subdomains_add"] = subdomains_add
try:
self.to_do["site_add_dom"] = self.to_do["site_add_dom"] \
+ site_add_dom
except Exception:
self.to_do["site_add_dom"] = site_add_dom
try:
self.to_do["sslcert_add"] = self.to_do["sslcert_add"] + sslcert_add
except Exception:
self.to_do["sslcert_add"] = sslcert_add
try:
self.to_do["sslcert_assign"] = self.to_do["sslcert_assign"] \
+ sslcert_assign
except Exception:
self.to_do["sslcert_assign"] = sslcert_assign
def update(self):
if self.to_do == []:
self.check()
account_domain = self.account_data[self.ALWAYSDATA_ACCOUNT_DOMAIN]
for domain in self.to_do["domains_add"]:
for ac, ac_data in self.account_data.items():
for d in ac_data["aldomains"]:
if d.name == domain.name:
self.__remove_domain(d, ac)
d = Domain(name=domain.name)
try:
d.post(auth=account_domain["auth"])
print("Domain %s added to alwaysdata" % (domain.name))
except Exception:
print("Failed to add %s domain "
"to alwaysdata" % (domain.name))
for domain in self.to_do["subdomains_add"]:
if domain.sitehost == 'extern':
continue
domain_account = self.account_data[domain.account]
site = None
for ac, ac_data in self.account_data.items():
alsite = ac_data["alsite"]
for e in alsite:
if domain.name + '/' in e.addresses:
self.__remove_domain_site(domain, e, ac)
alsite = domain_account["alsite"]
for e in alsite:
if e.name == domain.sitehost:
site = e
break
if domain.name + '/' in site.addresses:
continue
if site is None:
print("Cannot find site "
"%s to add domain %s" % (domain.sitehost, domain.name))
continue
site.addresses.append(domain.name + '/')
try:
site.patch(auth=domain_account["auth"])
print("SubDomain %s added "
"to %s site" % (domain.name, domain.sitehost))
except Exception:
print("Failed to add %s SubDomain "
"to %s site" % (domain.name, domain.sitehost))
for domain in self.to_do["site_add_dom"]:
if domain.sitehost == 'extern':
continue
domain_account = self.account_data[domain.account]
site = None
for ac, ac_data in self.account_data.items():
alsite = ac_data["alsite"]
for e in alsite:
if domain.name + '/' in e.addresses:
self.__remove_domain_site(domain, e, ac)
alsite = domain_account["alsite"]
for e in alsite:
if e.name == domain.sitehost:
site = e
break
if site is None:
print("Cannot find site %s to add "
"domain %s" % (domain.sitehost, domain.name))
continue
if domain.name + '/' in site.addresses:
continue
site.addresses.append(domain.name + '/')
try:
site.patch(auth=domain_account["auth"])
print("Domain %s added to "
"%s site" % (domain.name, domain.sitehost))
except Exception:
print("Failed to add %s Domain to "
"%s site" % (domain.name, domain.sitehost))
for domain in self.to_do["sslcert_add"]:
skip = False
for ac, ac_data in self.account_data.items():
for ssl in ac_data["alsslcert"]:
if ssl.name == domain.name:
if domain.account == ac:
skip = True
break
self.__remove_sslcert(ssl, ac)
if skip:
break
if skip:
continue
domain_account = self.account_data[domain.account]
ssl = SSLCertificate(name=domain.name)
try:
ssl.post(auth=ac_data["auth"])
self.to_do["sslcert_assign"].append(domain)
print("SslCertificate created for %s" % (domain.name))
except Exception:
print("Failed to create SslCertificate for %s."
" Try later. Maybe your certificate is "
"being created." % (domain.name))
for domain in self.to_do["sslcert_assign"]:
domain_account = self.account_data[domain.account]
alsslcert = domain_account["alsslcert"]
alsubdomains = domain_account["alsubdomains"]
ssl = None
for assl in alsslcert:
if domain.name == assl.name:
ssl = assl
break
alsubdom = None
for e in alsubdomains:
if e.hostname == domain.name:
alsubdom = e
break
if ssl is None or alsubdom is None:
print("Cannot found sslcertificate "
"or domain: %s" % (domain.name))
continue
alsubdom.ssl_certificate = ssl.id
try:
alsubdom.patch(auth=domain_account["auth"])
print("Assign certificate to "
"%s domain" % (domain.name))
except Exception:
print("Failed to assign certificate to "
"%s domain" % (domain.name))
for account_name, account_data in self.account_data.items():
alsite = account_data["alsite"]
for e in alsite:
try:
e.restart(auth=account_data['auth'])
print("Restart " + account_name + ":" + e.name)
except Exception:
print("Failed to restart " + e.name)
def __remove_domain_site(self, domain, site, account):
site.addresses.remove(domain.name + '/')
try:
site.patch(auth=self.account_data[account]['auth'])
print("Domain %s removed from %s site" % (domain.name, site.name))
except Exception:
print("Failed to remove %s "
"Domain from %s site" % (domain.name, domain.sitehost))
def __remove_domain(self, domain, account):
try:
domain.delete(auth=self.account_data[account]['auth'])
print("Domain %s removed" % (domain.name))
except Exception:
print("Failed to remove %s "
"Domain" % (domain.name))
def __remove_sslcert(self, sslcert, account):
try:
sslcert.delete(auth=self.account_data[account]['auth'])
print("SSL Certificate %s removed" % (sslcert.name))
except Exception:
print("Failed to remove %s "
"SSL Certificate" % (sslcert.name))