2008-05-19 19:57:04 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from twisted.internet import address
|
|
|
|
from twisted.web import http
|
2009-02-14 00:42:34 +00:00
|
|
|
from nevow import rend, url, loaders, tags as T
|
2008-05-19 19:57:04 +00:00
|
|
|
from nevow.inevow import IRequest
|
|
|
|
from nevow.static import File as nevow_File # TODO: merge with static.File?
|
|
|
|
from nevow.util import resource_filename
|
|
|
|
from formless import webform
|
|
|
|
|
|
|
|
import allmydata # to display import path
|
|
|
|
from allmydata import get_package_versions_string
|
|
|
|
from allmydata import provisioning
|
2008-08-05 19:09:21 +00:00
|
|
|
from allmydata.util import idlib, log
|
2009-07-15 06:45:10 +00:00
|
|
|
from allmydata.interfaces import IFileNode, UnhandledCapTypeError
|
2008-10-22 00:03:07 +00:00
|
|
|
from allmydata.web import filenode, directory, unlinked, status, operations
|
2009-02-20 19:15:54 +00:00
|
|
|
from allmydata.web import reliability, storage
|
|
|
|
from allmydata.web.common import abbreviate_size, getxmlfile, WebError, \
|
2009-04-08 02:13:40 +00:00
|
|
|
get_arg, RenderMixin, boolean_of_arg
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
|
2008-05-19 22:19:25 +00:00
|
|
|
class URIHandler(RenderMixin, rend.Page):
|
2008-05-19 19:57:04 +00:00
|
|
|
# I live at /uri . There are several operations defined on /uri itself,
|
2008-07-07 07:18:16 +00:00
|
|
|
# mostly involved with creation of unlinked files and directories.
|
2009-01-18 17:56:08 +00:00
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def __init__(self, client):
|
|
|
|
rend.Page.__init__(self, client)
|
|
|
|
self.client = client
|
|
|
|
|
2008-05-19 19:57:04 +00:00
|
|
|
def render_GET(self, ctx):
|
|
|
|
req = IRequest(ctx)
|
|
|
|
uri = get_arg(req, "uri", None)
|
|
|
|
if uri is None:
|
|
|
|
raise WebError("GET /uri requires uri=")
|
|
|
|
there = url.URL.fromContext(ctx)
|
|
|
|
there = there.clear("uri")
|
|
|
|
# I thought about escaping the childcap that we attach to the URL
|
|
|
|
# here, but it seems that nevow does that for us.
|
|
|
|
there = there.child(uri)
|
|
|
|
return there
|
|
|
|
|
|
|
|
def render_PUT(self, ctx):
|
|
|
|
req = IRequest(ctx)
|
|
|
|
# either "PUT /uri" to create an unlinked file, or
|
|
|
|
# "PUT /uri?t=mkdir" to create an unlinked directory
|
|
|
|
t = get_arg(req, "t", "").strip()
|
|
|
|
if t == "":
|
2009-04-08 02:13:40 +00:00
|
|
|
mutable = boolean_of_arg(get_arg(req, "mutable", "false").strip())
|
2008-05-19 19:57:04 +00:00
|
|
|
if mutable:
|
2009-02-20 19:15:54 +00:00
|
|
|
return unlinked.PUTUnlinkedSSK(req, self.client)
|
2008-05-19 19:57:04 +00:00
|
|
|
else:
|
2009-02-20 19:15:54 +00:00
|
|
|
return unlinked.PUTUnlinkedCHK(req, self.client)
|
2008-05-19 19:57:04 +00:00
|
|
|
if t == "mkdir":
|
2009-02-20 19:15:54 +00:00
|
|
|
return unlinked.PUTUnlinkedCreateDirectory(req, self.client)
|
2008-05-19 19:57:04 +00:00
|
|
|
errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
|
|
|
|
"and POST?t=mkdir")
|
|
|
|
raise WebError(errmsg, http.BAD_REQUEST)
|
|
|
|
|
|
|
|
def render_POST(self, ctx):
|
|
|
|
# "POST /uri?t=upload&file=newfile" to upload an
|
|
|
|
# unlinked file or "POST /uri?t=mkdir" to create a
|
|
|
|
# new directory
|
|
|
|
req = IRequest(ctx)
|
|
|
|
t = get_arg(req, "t", "").strip()
|
|
|
|
if t in ("", "upload"):
|
|
|
|
mutable = bool(get_arg(req, "mutable", "").strip())
|
|
|
|
if mutable:
|
2009-02-20 19:15:54 +00:00
|
|
|
return unlinked.POSTUnlinkedSSK(req, self.client)
|
2008-05-19 19:57:04 +00:00
|
|
|
else:
|
2009-02-20 19:15:54 +00:00
|
|
|
return unlinked.POSTUnlinkedCHK(req, self.client)
|
2008-05-19 19:57:04 +00:00
|
|
|
if t == "mkdir":
|
2009-02-20 19:15:54 +00:00
|
|
|
return unlinked.POSTUnlinkedCreateDirectory(req, self.client)
|
2008-05-19 19:57:04 +00:00
|
|
|
errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
|
|
|
|
"and POST?t=mkdir")
|
|
|
|
raise WebError(errmsg, http.BAD_REQUEST)
|
|
|
|
|
|
|
|
def childFactory(self, ctx, name):
|
|
|
|
# 'name' is expected to be a URI
|
|
|
|
try:
|
2009-02-20 19:15:54 +00:00
|
|
|
node = self.client.create_node_from_uri(name)
|
|
|
|
return directory.make_handler_for(node, self.client)
|
2009-07-15 06:45:10 +00:00
|
|
|
except (TypeError, UnhandledCapTypeError, AssertionError):
|
2008-05-19 19:57:04 +00:00
|
|
|
raise WebError("'%s' is not a valid file- or directory- cap"
|
|
|
|
% name)
|
|
|
|
|
|
|
|
class FileHandler(rend.Page):
|
|
|
|
# I handle /file/$FILECAP[/IGNORED] , which provides a URL from which a
|
|
|
|
# file can be downloaded correctly by tools like "wget".
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def __init__(self, client):
|
|
|
|
rend.Page.__init__(self, client)
|
|
|
|
self.client = client
|
|
|
|
|
2008-05-19 19:57:04 +00:00
|
|
|
def childFactory(self, ctx, name):
|
|
|
|
req = IRequest(ctx)
|
|
|
|
if req.method not in ("GET", "HEAD"):
|
|
|
|
raise WebError("/file can only be used with GET or HEAD")
|
|
|
|
# 'name' must be a file URI
|
|
|
|
try:
|
2009-02-20 19:15:54 +00:00
|
|
|
node = self.client.create_node_from_uri(name)
|
2009-07-15 06:45:10 +00:00
|
|
|
except (TypeError, UnhandledCapTypeError, AssertionError):
|
2008-05-19 19:57:04 +00:00
|
|
|
raise WebError("'%s' is not a valid file- or directory- cap"
|
|
|
|
% name)
|
|
|
|
if not IFileNode.providedBy(node):
|
|
|
|
raise WebError("'%s' is not a file-cap" % name)
|
2009-02-20 19:15:54 +00:00
|
|
|
return filenode.FileNodeDownloadHandler(self.client, node)
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
def renderHTTP(self, ctx):
|
|
|
|
raise WebError("/file must be followed by a file-cap and a name",
|
|
|
|
http.NOT_FOUND)
|
|
|
|
|
2008-08-05 19:09:21 +00:00
|
|
|
class IncidentReporter(RenderMixin, rend.Page):
|
|
|
|
def render_POST(self, ctx):
|
|
|
|
req = IRequest(ctx)
|
|
|
|
log.msg(format="User reports incident through web page: %(details)s",
|
|
|
|
details=get_arg(req, "details", ""),
|
2008-08-26 01:57:59 +00:00
|
|
|
level=log.WEIRD, umid="LkD9Pw")
|
2008-08-05 19:09:21 +00:00
|
|
|
req.setHeader("content-type", "text/plain")
|
|
|
|
return "Thank you for your report!"
|
|
|
|
|
2009-02-14 00:42:34 +00:00
|
|
|
class NoReliability(rend.Page):
|
|
|
|
docFactory = loaders.xmlstr('''\
|
|
|
|
<html xmlns:n="http://nevow.com/ns/nevow/0.1">
|
|
|
|
<head>
|
|
|
|
<title>AllMyData - Tahoe</title>
|
|
|
|
<link href="/webform_css" rel="stylesheet" type="text/css"/>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h2>"Reliability" page not available</h2>
|
2009-02-19 08:44:35 +00:00
|
|
|
<p>Please install the python "NumPy" module to enable this page.</p>
|
2009-02-14 00:42:34 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
''')
|
2008-10-22 00:03:07 +00:00
|
|
|
|
2008-05-19 19:57:04 +00:00
|
|
|
class Root(rend.Page):
|
|
|
|
|
|
|
|
addSlash = True
|
|
|
|
docFactory = getxmlfile("welcome.xhtml")
|
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
def __init__(self, client):
|
|
|
|
rend.Page.__init__(self, client)
|
|
|
|
self.client = client
|
2008-10-22 05:13:54 +00:00
|
|
|
self.child_operations = operations.OphandleTable()
|
2009-02-20 21:29:26 +00:00
|
|
|
try:
|
|
|
|
s = client.getServiceNamed("storage")
|
|
|
|
except KeyError:
|
|
|
|
s = None
|
|
|
|
self.child_storage = storage.StorageStatus(s)
|
2008-10-22 05:13:54 +00:00
|
|
|
|
2009-02-20 19:15:54 +00:00
|
|
|
self.child_uri = URIHandler(client)
|
|
|
|
self.child_cap = URIHandler(client)
|
|
|
|
|
|
|
|
self.child_file = FileHandler(client)
|
|
|
|
self.child_named = FileHandler(client)
|
|
|
|
self.child_status = status.Status(client) # TODO: use client.history
|
|
|
|
self.child_statistics = status.Statistics(client.stats_provider)
|
|
|
|
|
|
|
|
def child_helper_status(self, ctx):
|
|
|
|
# the Helper isn't attached until after the Tub starts, so this child
|
|
|
|
# needs to created on each request
|
|
|
|
try:
|
|
|
|
helper = self.client.getServiceNamed("helper")
|
|
|
|
except KeyError:
|
|
|
|
helper = None
|
|
|
|
return status.HelperStatus(helper)
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
child_webform_css = webform.defaultCSS
|
|
|
|
child_tahoe_css = nevow_File(resource_filename('allmydata.web', 'tahoe.css'))
|
|
|
|
|
|
|
|
child_provisioning = provisioning.ProvisioningTool()
|
2009-02-17 02:56:58 +00:00
|
|
|
if reliability.is_available():
|
2009-02-14 00:42:34 +00:00
|
|
|
child_reliability = reliability.ReliabilityTool()
|
|
|
|
else:
|
|
|
|
child_reliability = NoReliability()
|
2008-05-19 19:57:04 +00:00
|
|
|
|
2008-08-05 19:09:21 +00:00
|
|
|
child_report_incident = IncidentReporter()
|
2009-02-20 21:29:26 +00:00
|
|
|
#child_server # let's reserve this for storage-server-over-HTTP
|
2008-08-05 19:09:21 +00:00
|
|
|
|
2009-05-03 20:34:42 +00:00
|
|
|
# FIXME: This code is duplicated in root.py and introweb.py.
|
2008-05-19 19:57:04 +00:00
|
|
|
def data_version(self, ctx, data):
|
|
|
|
return get_package_versions_string()
|
|
|
|
def data_import_path(self, ctx, data):
|
|
|
|
return str(allmydata)
|
|
|
|
def data_my_nodeid(self, ctx, data):
|
2009-02-20 19:15:54 +00:00
|
|
|
return idlib.nodeid_b2a(self.client.nodeid)
|
2008-06-03 22:02:10 +00:00
|
|
|
def data_my_nickname(self, ctx, data):
|
2009-02-20 19:15:54 +00:00
|
|
|
return self.client.nickname
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
def render_services(self, ctx, data):
|
|
|
|
ul = T.ul()
|
|
|
|
try:
|
2009-02-20 19:15:54 +00:00
|
|
|
ss = self.client.getServiceNamed("storage")
|
2009-02-20 21:29:26 +00:00
|
|
|
stats = ss.get_stats()
|
|
|
|
if stats["storage_server.accepting_immutable_shares"]:
|
|
|
|
msg = "accepting new shares"
|
|
|
|
else:
|
|
|
|
msg = "not accepting new shares (read-only)"
|
|
|
|
available = stats.get("storage_server.disk_avail")
|
|
|
|
if available is not None:
|
|
|
|
msg += ", %s available" % abbreviate_size(available)
|
|
|
|
ul[T.li[T.a(href="storage")["Storage Server"], ": ", msg]]
|
2008-05-19 19:57:04 +00:00
|
|
|
except KeyError:
|
|
|
|
ul[T.li["Not running storage server"]]
|
|
|
|
|
|
|
|
try:
|
2009-02-20 19:15:54 +00:00
|
|
|
h = self.client.getServiceNamed("helper")
|
2008-05-19 19:57:04 +00:00
|
|
|
stats = h.get_stats()
|
|
|
|
active_uploads = stats["chk_upload_helper.active_uploads"]
|
|
|
|
ul[T.li["Helper: %d active uploads" % (active_uploads,)]]
|
|
|
|
except KeyError:
|
|
|
|
ul[T.li["Not running helper"]]
|
|
|
|
|
|
|
|
return ctx.tag[ul]
|
|
|
|
|
|
|
|
def data_introducer_furl(self, ctx, data):
|
2009-02-20 19:15:54 +00:00
|
|
|
return self.client.introducer_furl
|
2008-05-19 19:57:04 +00:00
|
|
|
def data_connected_to_introducer(self, ctx, data):
|
2009-02-20 19:15:54 +00:00
|
|
|
if self.client.connected_to_introducer():
|
2008-05-19 19:57:04 +00:00
|
|
|
return "yes"
|
|
|
|
return "no"
|
|
|
|
|
|
|
|
def data_helper_furl(self, ctx, data):
|
|
|
|
try:
|
2009-02-20 19:15:54 +00:00
|
|
|
uploader = self.client.getServiceNamed("uploader")
|
2008-05-19 19:57:04 +00:00
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
furl, connected = uploader.get_helper_info()
|
|
|
|
return furl
|
|
|
|
def data_connected_to_helper(self, ctx, data):
|
|
|
|
try:
|
2009-02-20 19:15:54 +00:00
|
|
|
uploader = self.client.getServiceNamed("uploader")
|
2008-05-19 19:57:04 +00:00
|
|
|
except KeyError:
|
|
|
|
return "no" # we don't even have an Uploader
|
|
|
|
furl, connected = uploader.get_helper_info()
|
|
|
|
if connected:
|
|
|
|
return "yes"
|
|
|
|
return "no"
|
|
|
|
|
|
|
|
def data_known_storage_servers(self, ctx, data):
|
2009-06-23 02:10:47 +00:00
|
|
|
sb = self.client.get_storage_broker()
|
|
|
|
return len(sb.get_all_serverids())
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
def data_connected_storage_servers(self, ctx, data):
|
2009-06-23 02:10:47 +00:00
|
|
|
sb = self.client.get_storage_broker()
|
|
|
|
return len(sb.get_all_servers())
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
def data_services(self, ctx, data):
|
2009-06-23 02:10:47 +00:00
|
|
|
sb = self.client.get_storage_broker()
|
|
|
|
return sb.get_all_descriptors()
|
|
|
|
|
|
|
|
def render_service_row(self, ctx, descriptor):
|
|
|
|
nodeid = descriptor.get_serverid()
|
|
|
|
|
2008-06-03 22:02:10 +00:00
|
|
|
ctx.fillSlots("peerid", idlib.nodeid_b2a(nodeid))
|
2009-06-23 02:10:47 +00:00
|
|
|
ctx.fillSlots("nickname", descriptor.get_nickname())
|
|
|
|
rhost = descriptor.get_remote_host()
|
|
|
|
if rhost:
|
2009-02-20 19:15:54 +00:00
|
|
|
if nodeid == self.client.nodeid:
|
2008-05-19 19:57:04 +00:00
|
|
|
rhost_s = "(loopback)"
|
|
|
|
elif isinstance(rhost, address.IPv4Address):
|
|
|
|
rhost_s = "%s:%d" % (rhost.host, rhost.port)
|
|
|
|
else:
|
|
|
|
rhost_s = str(rhost)
|
|
|
|
connected = "Yes: to " + rhost_s
|
2009-06-23 02:10:47 +00:00
|
|
|
since = descriptor.get_last_connect_time()
|
2008-05-19 19:57:04 +00:00
|
|
|
else:
|
|
|
|
connected = "No"
|
2009-06-23 02:10:47 +00:00
|
|
|
since = descriptor.get_last_loss_time()
|
|
|
|
announced = descriptor.get_announcement_time()
|
|
|
|
announcement = descriptor.get_announcement()
|
|
|
|
version = announcement["version"]
|
|
|
|
service_name = announcement["service-name"]
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
|
|
|
|
ctx.fillSlots("connected", connected)
|
2009-06-23 02:10:47 +00:00
|
|
|
ctx.fillSlots("connected-bool", bool(rhost))
|
|
|
|
ctx.fillSlots("since", time.strftime(TIME_FORMAT,
|
|
|
|
time.localtime(since)))
|
2008-05-19 19:57:04 +00:00
|
|
|
ctx.fillSlots("announced", time.strftime(TIME_FORMAT,
|
2009-06-23 02:10:47 +00:00
|
|
|
time.localtime(announced)))
|
|
|
|
ctx.fillSlots("version", version)
|
|
|
|
ctx.fillSlots("service_name", service_name)
|
2008-05-19 19:57:04 +00:00
|
|
|
|
|
|
|
return ctx.tag
|
|
|
|
|
|
|
|
def render_download_form(self, ctx, data):
|
|
|
|
# this is a form where users can download files by URI
|
|
|
|
form = T.form(action="uri", method="get",
|
|
|
|
enctype="multipart/form-data")[
|
|
|
|
T.fieldset[
|
|
|
|
T.legend(class_="freeform-form-label")["Download a file"],
|
2009-05-26 23:25:45 +00:00
|
|
|
T.div["Tahoe-URI to download: ",
|
|
|
|
T.input(type="text", name="uri")],
|
|
|
|
T.div["Filename to download as: ",
|
|
|
|
T.input(type="text", name="filename")],
|
2008-05-19 19:57:04 +00:00
|
|
|
T.input(type="submit", value="Download!"),
|
|
|
|
]]
|
|
|
|
return T.div[form]
|
|
|
|
|
|
|
|
def render_view_form(self, ctx, data):
|
|
|
|
# this is a form where users can download files by URI, or jump to a
|
|
|
|
# named directory
|
|
|
|
form = T.form(action="uri", method="get",
|
|
|
|
enctype="multipart/form-data")[
|
|
|
|
T.fieldset[
|
|
|
|
T.legend(class_="freeform-form-label")["View a file or directory"],
|
2009-05-26 23:25:45 +00:00
|
|
|
"Tahoe-URI to view: ",
|
2008-05-19 19:57:04 +00:00
|
|
|
T.input(type="text", name="uri"), " ",
|
|
|
|
T.input(type="submit", value="View!"),
|
|
|
|
]]
|
|
|
|
return T.div[form]
|
|
|
|
|
|
|
|
def render_upload_form(self, ctx, data):
|
|
|
|
# this is a form where users can upload unlinked files
|
|
|
|
form = T.form(action="uri", method="post",
|
|
|
|
enctype="multipart/form-data")[
|
|
|
|
T.fieldset[
|
|
|
|
T.legend(class_="freeform-form-label")["Upload a file"],
|
2009-05-26 23:25:45 +00:00
|
|
|
T.div["Choose a file: ",
|
|
|
|
T.input(type="file", name="file", class_="freeform-input-file")],
|
2008-05-19 19:57:04 +00:00
|
|
|
T.input(type="hidden", name="t", value="upload"),
|
2009-05-26 23:25:45 +00:00
|
|
|
T.div[T.input(type="checkbox", name="mutable"), T.label(for_="mutable")["Create mutable file"],
|
|
|
|
" ", T.input(type="submit", value="Upload!")],
|
2008-05-19 19:57:04 +00:00
|
|
|
]]
|
|
|
|
return T.div[form]
|
|
|
|
|
|
|
|
def render_mkdir_form(self, ctx, data):
|
|
|
|
# this is a form where users can create new directories
|
|
|
|
form = T.form(action="uri", method="post",
|
|
|
|
enctype="multipart/form-data")[
|
|
|
|
T.fieldset[
|
2009-05-26 23:25:45 +00:00
|
|
|
T.legend(class_="freeform-form-label")["Create a directory"],
|
2008-05-19 19:57:04 +00:00
|
|
|
T.input(type="hidden", name="t", value="mkdir"),
|
|
|
|
T.input(type="hidden", name="redirect_to_result", value="true"),
|
2009-05-26 23:25:45 +00:00
|
|
|
T.input(type="submit", value="Create a directory"),
|
2008-05-19 19:57:04 +00:00
|
|
|
]]
|
|
|
|
return T.div[form]
|
|
|
|
|
2008-08-05 19:09:21 +00:00
|
|
|
def render_incident_button(self, ctx, data):
|
|
|
|
# this button triggers a foolscap-logging "incident"
|
|
|
|
form = T.form(action="report_incident", method="post",
|
|
|
|
enctype="multipart/form-data")[
|
|
|
|
T.fieldset[
|
|
|
|
T.legend(class_="freeform-form-label")["Report an Incident"],
|
|
|
|
T.input(type="hidden", name="t", value="report-incident"),
|
|
|
|
"What went wrong?: ",
|
|
|
|
T.input(type="text", name="details"), " ",
|
|
|
|
T.input(type="submit", value="Report!"),
|
|
|
|
]]
|
|
|
|
return T.div[form]
|