-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck_dns.py
executable file
·109 lines (91 loc) · 4.08 KB
/
check_dns.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
#!/usr/bin/env python
import os.path
import posixpath
import urllib2
import transaction
from scriptspony.model import queue
from scriptspony import rt, vhosts
from scripts import hosts, keytab, log, auth
@log.exceptions
def check_dns():
if keytab.exists():
keytab.auth()
# Use a list so all the ids are resolved early and transactions aren't
# a problem
for tid in [
t.id
for t in queue.Ticket.query.filter(
queue.Ticket.state.in_([u"moira", u"dns"])
).all()
]:
t = queue.Ticket.get(tid)
if hosts.points_at_scripts(t.hostname):
path = posixpath.normpath(
posixpath.join(
"/mit",
t.locker,
"web_scripts",
vhosts.get_path(t.locker, t.hostname),
)
)
wordpress = (
"This site looks like a WordPress blog; for the new URL to work properly, you'll need to access the WordPress admin interface via your old URL, go to General Settings, and change the WordPress address and Blog address to 'http://%s'."
% t.hostname
)
# Try to figure out what's up with the hostname currently
try:
page = urllib2.urlopen("http://%s/" % t.hostname)
content = page.read()
if (
'<meta name="generator" content="WordPress' in content
or "wp-login" in page.geturl()
):
sitestatus = wordpress
else:
sitestatus = "Your site appears to be working properly. Have fun!"
except urllib2.HTTPError as e:
if "wp-login" in e.geturl():
sitestatus = wordpress
elif e.code == 404:
sitestatus = (
"There doesn't seem to be any content currently at %s; make sure that directory exists and has an index.html, index.cgi, or similar, or change this hostname to point somewhere else at https://pony.scripts.mit.edu."
% path
)
elif e.code == 403:
sitestatus = (
"Visiting that page yields a Forbidden error; this is often caused by a lack of valid content at %s. Putting an index.html, index.cgi, or similar there may solve this. Alternately, you may just have your site password-protected or cert-protected."
% path
)
elif e.code == 401:
sitestatus = "Visiting that page yields an Unauthorized error. This generally means that you have your site password-protected or cert-protected, so we can't confirm whether it's working."
else:
sitestatus = (
"Visiting that page yields a %s error, suggesting a problem with the content at %s. Email us at [email protected] if you need help resolving this."
% (e.code, path)
)
subject = u"Re: Request for hostname %s" % t.hostname
body = u"""Hello,
Just wanted to let you know that the hostname %(hostname)s is now configured and working. It currently points to %(path)s. Visit http://%(hostname)s/ to check it out.
%(sitestatus)s
Let us know if you run into any issues.
~The SIPB Scripts Team
https://scripts.mit.edu/
/set status=resolved
""" % dict(
hostname=t.hostname, locker=t.locker, path=path, sitestatus=sitestatus
)
rt.call("ticket/%d/comment" % (t.rtid,), Action="correspond", Text=body)
t.addEvent(
type=u"mail",
state=u"resolved",
by=u"dns",
target=u"user",
subject=subject,
body=body,
)
transaction.commit()
if __name__ == "__main__":
auth.set_user_from_parent_process()
from paste.deploy import loadapp
loadapp("config:development.ini", relative_to=os.path.dirname(__file__))
check_dns()