rewritten mirror script(and added god monitoring scripts)

This commit is contained in:
dev
2010-06-14 11:24:33 +00:00
parent 57b3fd10b1
commit bd1df73f6b
6 changed files with 139 additions and 20 deletions

View File

@ -1,33 +1,77 @@
from werkzeug import BaseRequest, BaseResponse, run_simple
from werkzeug.exceptions import HTTPException
from werkzeug.routing import RequestRedirect
from flask import Flask, redirect, request, render_template, abort
from random import randint
from sys import argv
import json
class Request(BaseRequest):
"""Useful subclass of the default request that knows how to build urls."""
# try to create an memcache client
if len(argv[3:]) > 0:
try:
try:
from cmemcache import Client
except ImportError:
from memcache import Client
client=Client(argv[3:])
except ImportError:
client=None
def __init__(self, environ):
BaseRequest.__init__(self, environ)
# create application
app=Flask(__name__)
# extract domain
domain=argv[1]
class Response(BaseResponse):
"""Subclass of base response that has a default mimetype of text/html."""
default_mimetype = 'text/html'
# extract port
port=int(argv[2])
def read_mirrors():
file = open('mirrors', 'r')
dat = file.read()
file.close()
return dat.split('\n')
lines=dat.split('\n')
ret={}
for line in lines:
try:
obj=json.loads(line)
except ValueError:
pass
if 'protocol' not in obj:
continue
protocol=obj['protocol']
if protocol not in ret:
ret[protocol]=[]
ret[protocol].append(obj)
return ret
def app(environ, start_response):
"""The WSGI application that connects all together."""
req = Request(environ)
mirrors = read_mirrors()
mirror = mirrors[randint(0, len(mirrors) - 1)]
resp = RequestRedirect(mirror % req.path)
return resp(environ, start_response)
@app.route('/')
def index():
return redirect('http://www.%s/download' % domain)
@app.route('/select/<path:f>')
def select(f):
mirrors=read_mirrors()
obj=[]
for protocol in mirrors.keys():
a={}
a['name']=protocol.upper()
a['mirrors']=mirrors[protocol]
for mirror in a['mirrors']:
mirror['url']=mirror['url'] % f
obj.append(a)
return render_template('select.html', mirrors=obj, file=f, domain=domain)
@app.route('/<protocol>/<path:f>')
def get(protocol, f):
mirrors=read_mirrors()
if not protocol in mirrors:
abort(404)
mirrors=mirrors[protocol]
return redirect(mirrors.keys()[randint(0, len(mirrors.keys()) - 1)] % f)
@app.route('/<f>')
def old_get(f):
return redirect('http://i2p.googlecode.com/files/%s' % f)
if __name__ == '__main__':
run_simple('localhost', 5008, app)
app.debug=True
app.run(port=port)

40
mirror.i2p2/mirror.god Normal file
View File

@ -0,0 +1,40 @@
DOMAINS.keys.each do |domain|
God.watch do |w|
w.name="i2p-mirror-#{domain}"
w.interval=30.seconds
w.start="/usr/bin/env python app.py #{domain} #{DOMAINS[domain][:port]}"
w.start_grace=10.seconds
w.restart_grace=10.seconds
w.dir=WDIR
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval=5.seconds
c.running=false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above=210.megabytes
c.times=[3,5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above=90.percent
c.times=7
end
end
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times=5
c.within=5.minute
c.transition=:unmonitored
c.retry_in=10.minutes
c.retry_times=5
c.retry_within=2.hours
end
end
end
end

View File

@ -1 +1,2 @@
http://i2p.googlecode.com/files%s
{"url": "http://golden.mtveurope.org/~yang/i2p_mirror/%s", "org": "VServer.si", "org_url": "http://www.vserver.si", "protocol": "http", "country": "lu"}
{"url": "http://a.mirror.geti2p.net/releases/current/%s", "org": "welterde", "protocol": "http", "country": "de"}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>I2P - Mirror selection - /{{ file }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
</head>
<body>
<div class="logo">
<a href="{{ domain }}" class="fade"><img src="{{ url_for('static', filename='images/i2plogo.png') }}" alt="I2P Logo" title="Invisible Internet Project (I2P)" /></a>
</div>
<div class="header">
<h1>Mirror selection</h1>
<h2>File: /{{ file }}</h2>
</div>
{% for protocol in mirrors -%}
<div class="protocol">
<h3>{{ protocol.name }}</h3>
<ul>
{% for mirror in protocol.mirrors -%}
<li><a href="{{ mirror.url }}">{{ mirror.url }}</a> {% if mirror.org_url %}<a href="{{ mirror.org_url }}">{% endif %}{{ mirror.org }}{% if mirror.org_url %}</a>{% endif %}</li>
{% endfor %}
</ul>
</div>
{%- endfor %}
</body>
</html>

5
run.god.example Normal file
View File

@ -0,0 +1,5 @@
God.pid_file_directory='/home/i2p/pids'
DOMAINS={
'i2p2.de' => {:port => 8090},
'geti2p.net' => {:port => 8091},
'i2project.