Skip to content
This repository was archived by the owner on Jun 3, 2023. It is now read-only.

Commit 0a09e12

Browse files
committed
Improve naming and fix static type checking
* Improve documentation * Move dns -> server_admin * Fix mypy config * Add a representation to Cluster * Remove type: ignore directives * Rename dns -> assign_dns * Use a clusters list instead of relying on Cluster.instances * Update mypy to 0.700 * "Friendly Name" -> "Server Friendly Name"
1 parent 1c243f0 commit 0a09e12

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The application expects AWS API secrets as the environment variables `AWS_ACCESS
1212

1313
Run with `./run.py` or `flask run` to serve the web interface in development mode on `localhost:5000` with Flask. To serve with `gunicorn` on `localhost:8000`, run the command in `Procfile`. Simulate a deployment with `heroku local`.
1414

15-
Some toy data is created in `dns.create_instances`.
15+
Some toy data is created by `dns.create_instances`.
1616

1717
### Deploy
1818

File renamed without changes.

‎app/dns/dns.py renamed to ‎app/server_admin/dns.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import List
66

77
import boto
8-
import route53 # type: ignore
8+
import route53
99

1010
AWS_ACCESS_ID = os.environ["AWS_ACCESS_ID"]
1111
AWS_ACCESS_SECRET = os.environ["AWS_ACCESS_SECRET"]
@@ -24,6 +24,9 @@ def __init__(self, cluster_id: int) -> None:
2424
self.cluster_name = self._name()
2525
self.subdomain = self._subdomain()
2626

27+
def __repr__(self) -> str:
28+
return f"<{type(self).__name__}(cluster_id={self.cluster_id})>"
29+
2730
def _name(self) -> str:
2831
name_dict = {1: "Los Angeles",
2932
2: "New York",
@@ -72,8 +75,7 @@ def add_to_rotation(self) -> None:
7275
conn = boto.connect_route53(aws_access_key_id=AWS_ACCESS_ID,
7376
aws_secret_access_key=AWS_ACCESS_SECRET)
7477

75-
changes = boto.route53.record.ResourceRecordSets(conn, # type: ignore
76-
self.zone.id)
78+
changes = boto.route53.record.ResourceRecordSets(conn, self.zone.id)
7779
change = changes.add_change("UPSERT", fqdn, "A")
7880

7981
for ip_address in set(ips):
@@ -99,8 +101,7 @@ def remove_from_rotation(self) -> None:
99101
conn = boto.connect_route53(aws_access_key_id=AWS_ACCESS_ID,
100102
aws_secret_access_key=AWS_ACCESS_SECRET)
101103

102-
changes = boto.route53.record.ResourceRecordSets(conn, # type: ignore
103-
self.zone.id)
104+
changes = boto.route53.record.ResourceRecordSets(conn, self.zone.id)
104105

105106
# This should be simple, but seemingly the API complains unless
106107
# it's updated in this arduous fashion.
@@ -115,7 +116,7 @@ def remove_from_rotation(self) -> None:
115116

116117
changes.commit()
117118

118-
def dns() -> list:
119+
def assign_dns() -> list:
119120
"""Grabs all A records for the hosted zone
120121
and assigns them to class variables"""
121122
print("Fetching DNS A records... ", end="", flush=True)
@@ -138,10 +139,9 @@ def dns() -> list:
138139

139140
def create_instances() -> None:
140141
"""Instantiates clusters and their servers"""
142+
clusters = []
141143
for i in range(1, Cluster.n + 1):
142-
Cluster(i)
143-
144-
clusters = Cluster.instances
144+
clusters.append(Cluster(i))
145145

146146
la1 = clusters[0].create_server("la1")
147147
ny1 = clusters[1].create_server("ny1")
@@ -166,14 +166,14 @@ def update_server_dns(dns_records: list) -> None:
166166
server.dns = record.name
167167

168168
def print_servers() -> None:
169-
"""ASCII analogy of the server UI."""
169+
"""ASCII analogy of the server UI"""
170170
print("\n\033[1mID\t Name\t Cluster\t DNS\t\t\t IP\033[0m")
171171
for server in Server.instances:
172172
print(server.server_id, "\t", server.friendly_name, "\t",
173173
server.cluster_name, "\t", server.dns, "\t", server.ip_string)
174174

175175
def print_dns(dns_records: list) -> None:
176-
"""ASCII analogy of the DNS UI."""
176+
"""ASCII analogy of the DNS UI"""
177177
print("\n\033[1mDomain\t\t\t\t\t IP(s)\t\t Server(s)\t Cluster\033[0m")
178178
for record in dns_records:
179179
matching_servers = []
@@ -187,7 +187,7 @@ def print_dns(dns_records: list) -> None:
187187
def main() -> None:
188188
"""Entry point"""
189189
create_instances()
190-
records = dns()
190+
records = assign_dns()
191191
update_server_dns(records)
192192

193193
print_servers()

‎app/templates/servers.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ <h1>Servers</h1>
3232
<thead>
3333
<tr>
3434
<th>ID</th>
35-
<th>Friendly Name&nbsp;&#x25BC;</th>
35+
<th>Server Friendly Name&nbsp;&#x25BC;</th>
3636
<th>Cluster</th>
3737
<th>DNS</th>
3838
<th>Rotation</th>

‎app/views.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import flask
44
import werkzeug
5-
from .dns.dns import Cluster, Server, dns, update_server_dns, create_instances
5+
6+
from .server_admin.dns import Cluster, Server, assign_dns, update_server_dns, \
7+
create_instances
68

79
BP = flask.Blueprint("views", __name__, url_prefix="/")
810

@@ -26,20 +28,21 @@ def index() -> flask.signals.template_rendered:
2628

2729
@APP.route("/servers")
2830
def servers_ui() -> flask.signals.template_rendered:
29-
"""renders the server UI"""
30-
update_server_dns(dns())
31+
"""Renders the server UI"""
32+
update_server_dns(assign_dns())
3133
servers = Server.instances
3234
return flask.render_template("servers.html", title="Servers", servers=servers)
3335

3436
@APP.route("/dns")
3537
def dns_ui() -> flask.signals.template_rendered:
36-
"""renders the DNS UI"""
38+
"""Renders the DNS UI"""
3739
return flask.render_template("dns.html", title="DNS", zone=Cluster.zone,
38-
servers=Server.instances, dns_records=dns())
40+
servers=Server.instances,
41+
dns_records=assign_dns())
3942

4043
@APP.route("/rotate", methods=["GET", "POST"])
4144
def rotate() -> werkzeug.wrappers.Response:
42-
""" Moves the server(s) into / out of DNS rotation """
45+
"""Moves the server(s) into / out of DNS rotation"""
4346
if flask.request.method == "POST":
4447
action = flask.request.form.get("action")
4548
server_id = int(flask.request.form.get("server")) # type: ignore
@@ -63,9 +66,9 @@ def rotate() -> werkzeug.wrappers.Response:
6366
return flask.redirect("/servers")
6467

6568
def main() -> None:
66-
"""main view function"""
69+
"""Main view function"""
6770
create_instances()
68-
records = dns()
71+
records = assign_dns()
6972
update_server_dns(records)
7073
#print_servers()
7174
#print_dns(records)

‎.mypy.ini renamed to ‎mypy.ini

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ check_untyped_defs = True
33
warn_redundant_casts = True
44
disallow_untyped_defs = True
55
warn_unused_ignores = True
6+
strict_equality = True
67

78
[mypy-route53]
89
ignore_missing_imports = True
10+
follow_imports=skip
11+
follow_imports_for_stubs=True
912

10-
[mypy-boto]
11-
ignore_missing_imports = True
13+
[mypy-boto.*]
14+
follow_imports=skip
15+
follow_imports_for_stubs=True

‎requirements_dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
mypy >= 0.670
1+
mypy >= 0.700
22
pylint >= 2.3.1
33
python-dotenv >= 0.10.1

0 commit comments

Comments
 (0)