Skip to content

Commit 62bf871

Browse files
committed
load balancer in fix mode
1 parent 8b3bd33 commit 62bf871

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ A very important part, probably needs the most work in the long term future.
101101

102102
#### load balancer script - scripts/loadbalancer
103103

104-
Round Robin DNS based load balancer, script for health checking and updating records. It pushes status messages to a Telegram bot.
105-
106-
Currently it's running in read-only mode, DNS updates need manual confirmation.
104+
A Round Robin DNS based load balancer script for health checking and updating records. It pushes status messages to a Telegram bot.
107105

108106
## Self hosting
109107

@@ -167,6 +165,10 @@ See [dev setup docs](docs/dev_setup.md).
167165

168166
## Changelog
169167

168+
##### v0.2
169+
170+
Load-balancing script is running in write mode, updating records when needed.
171+
170172
##### v0.1
171173

172174
Everything works. 1 server for tile gen, 2 servers for HTTP host. Load-balancing script is running in a read-only mode.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# every minute
2-
* * * * * ofm sudo /data/ofm/venv/bin/python -u /data/ofm/loadbalancer/loadbalancer.py check >> /data/ofm/loadbalancer/logs/check.log 2>&1
2+
* * * * * ofm sudo /data/ofm/venv/bin/python -u /data/ofm/loadbalancer/loadbalancer.py fix >> /data/ofm/loadbalancer/logs/check.log 2>&1
33

44

scripts/loadbalancer/loadbalancer.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ def check_or_fix(fix=False):
7676
if not working_hosts:
7777
working_hosts = set(c['http_host_list'])
7878

79-
update_records(c, working_hosts)
79+
message = 'OFM loadbalancer FIX found no working hosts, reverting to full list!'
80+
print(message)
81+
telegram_send_message(message, c['telegram_token'], c['telegram_chat_id'])
82+
83+
updated = update_records(c, working_hosts)
84+
if updated:
85+
message = f'OFM loadbalancer FIX modified records, new records: {working_hosts}'
86+
print(message)
87+
telegram_send_message(message, c['telegram_token'], c['telegram_chat_id'])
8088

8189

8290
def run_area(c, area):
@@ -117,14 +125,16 @@ def get_target_version(area):
117125
return response.text.strip()
118126

119127

120-
def update_records(c, working_hosts):
128+
def update_records(c, working_hosts) -> bool:
121129
config = dotenv_values('/data/ofm/config/cloudflare.ini')
122130
cloudflare_api_token = config['dns_cloudflare_api_token']
123131

124132
domain = '.'.join(c['domain_ledns'].split('.')[-2:])
125133
zone_id = get_zone_id(domain, cloudflare_api_token=cloudflare_api_token)
126134

127-
set_records_round_robin(
135+
updated = False
136+
137+
updated |= set_records_round_robin(
128138
zone_id=zone_id,
129139
name=c['domain_ledns'],
130140
host_ip_set=working_hosts,
@@ -134,7 +144,7 @@ def update_records(c, working_hosts):
134144
cloudflare_api_token=cloudflare_api_token,
135145
)
136146

137-
set_records_round_robin(
147+
updated |= set_records_round_robin(
138148
zone_id=zone_id,
139149
name=c['domain_cf'],
140150
host_ip_set=working_hosts,
@@ -143,6 +153,8 @@ def update_records(c, working_hosts):
143153
cloudflare_api_token=cloudflare_api_token,
144154
)
145155

156+
return updated
157+
146158

147159
if __name__ == '__main__':
148160
cli()

scripts/loadbalancer/loadbalancer_lib/cloudflare.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def set_records_round_robin(
5555
proxied: bool,
5656
comment: str = None,
5757
cloudflare_api_token: str,
58-
):
58+
) -> bool:
5959
headers = {'Authorization': f'Bearer {cloudflare_api_token}'}
6060

6161
dns_records = get_dns_records_round_robin(zone_id, cloudflare_api_token=cloudflare_api_token)
@@ -64,7 +64,7 @@ def set_records_round_robin(
6464
current_ips = {r['content'] for r in current_records}
6565
if current_ips == host_ip_set:
6666
print(f'No need to update records: {name} currently set: {sorted(current_ips)}')
67-
return
67+
return False
6868

6969
# changing records
7070

@@ -92,6 +92,8 @@ def set_records_round_robin(
9292
data = res.json()
9393
assert data['success'] is True
9494

95+
return True
96+
9597

9698
def delete_record(zone_id, *, id_: str, cloudflare_api_token: str):
9799
headers = {'Authorization': f'Bearer {cloudflare_api_token}'}

scripts/loadbalancer/loadbalancer_lib/curl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def pycurl_status(url, domain, host_ip):
1414
c.setopt(c.CAINFO, '/etc/ssl/certs/ca-certificates.crt')
1515
c.setopt(c.RESOLVE, [f'{domain}:443:{host_ip}'])
1616
c.setopt(c.NOBODY, True)
17+
c.setopt(c.TIMEOUT, 5)
1718
c.perform()
1819
status_code = c.getinfo(c.RESPONSE_CODE)
1920
c.close()
@@ -33,6 +34,7 @@ def pycurl_get(url, domain, host_ip):
3334
c.setopt(c.CAINFO, '/etc/ssl/certs/ca-certificates.crt')
3435
c.setopt(c.RESOLVE, [f'{domain}:443:{host_ip}'])
3536
c.setopt(c.WRITEDATA, buffer)
37+
c.setopt(c.TIMEOUT, 5)
3638
c.perform()
3739
status_code = c.getinfo(c.RESPONSE_CODE)
3840
c.close()

website/src/pages/index.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { Content as RestText } from '../content/index/rest.md'
2323
Have a look at the default styles and read more about how to integrate it to your website or
2424
app here:
2525
</p>
26-
<p style="margin: 20px auto;"><a href="/quick_start">Quick Start Guide</a></p>
26+
27+
<a class="quick-start-button" href="/quick_start">Quick Start Guide</a>
2728

2829
<Donate />
2930
<RestText />

0 commit comments

Comments
 (0)