]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/root.py
WUI: add time page was rendered to client and introducer welcome pages. closes #1972
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / root.py
1 import time, os
2
3 from twisted.internet import address
4 from twisted.web import http
5 from nevow import rend, url, tags as T
6 from nevow.inevow import IRequest
7 from nevow.static import File as nevow_File # TODO: merge with static.File?
8 from nevow.util import resource_filename
9
10 import allmydata # to display import path
11 from allmydata import get_package_versions_string
12 from allmydata.util import log
13 from allmydata.interfaces import IFileNode
14 from allmydata.web import filenode, directory, unlinked, status, operations
15 from allmydata.web import storage
16 from allmydata.web.common import abbreviate_size, getxmlfile, WebError, \
17      get_arg, RenderMixin, get_format, get_mutable_type, TIME_FORMAT
18
19
20 class URIHandler(RenderMixin, rend.Page):
21     # I live at /uri . There are several operations defined on /uri itself,
22     # mostly involved with creation of unlinked files and directories.
23
24     def __init__(self, client):
25         rend.Page.__init__(self, client)
26         self.client = client
27
28     def render_GET(self, ctx):
29         req = IRequest(ctx)
30         uri = get_arg(req, "uri", None)
31         if uri is None:
32             raise WebError("GET /uri requires uri=")
33         there = url.URL.fromContext(ctx)
34         there = there.clear("uri")
35         # I thought about escaping the childcap that we attach to the URL
36         # here, but it seems that nevow does that for us.
37         there = there.child(uri)
38         return there
39
40     def render_PUT(self, ctx):
41         req = IRequest(ctx)
42         # either "PUT /uri" to create an unlinked file, or
43         # "PUT /uri?t=mkdir" to create an unlinked directory
44         t = get_arg(req, "t", "").strip()
45         if t == "":
46             file_format = get_format(req, "CHK")
47             mutable_type = get_mutable_type(file_format)
48             if mutable_type is not None:
49                 return unlinked.PUTUnlinkedSSK(req, self.client, mutable_type)
50             else:
51                 return unlinked.PUTUnlinkedCHK(req, self.client)
52         if t == "mkdir":
53             return unlinked.PUTUnlinkedCreateDirectory(req, self.client)
54         errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
55                   "and POST?t=mkdir")
56         raise WebError(errmsg, http.BAD_REQUEST)
57
58     def render_POST(self, ctx):
59         # "POST /uri?t=upload&file=newfile" to upload an
60         # unlinked file or "POST /uri?t=mkdir" to create a
61         # new directory
62         req = IRequest(ctx)
63         t = get_arg(req, "t", "").strip()
64         if t in ("", "upload"):
65             file_format = get_format(req)
66             mutable_type = get_mutable_type(file_format)
67             if mutable_type is not None:
68                 return unlinked.POSTUnlinkedSSK(req, self.client, mutable_type)
69             else:
70                 return unlinked.POSTUnlinkedCHK(req, self.client)
71         if t == "mkdir":
72             return unlinked.POSTUnlinkedCreateDirectory(req, self.client)
73         elif t == "mkdir-with-children":
74             return unlinked.POSTUnlinkedCreateDirectoryWithChildren(req,
75                                                                     self.client)
76         elif t == "mkdir-immutable":
77             return unlinked.POSTUnlinkedCreateImmutableDirectory(req,
78                                                                  self.client)
79         errmsg = ("/uri accepts only PUT, PUT?t=mkdir, POST?t=upload, "
80                   "and POST?t=mkdir")
81         raise WebError(errmsg, http.BAD_REQUEST)
82
83     def childFactory(self, ctx, name):
84         # 'name' is expected to be a URI
85         try:
86             node = self.client.create_node_from_uri(name)
87             return directory.make_handler_for(node, self.client)
88         except (TypeError, AssertionError):
89             raise WebError("'%s' is not a valid file- or directory- cap"
90                            % name)
91
92 class FileHandler(rend.Page):
93     # I handle /file/$FILECAP[/IGNORED] , which provides a URL from which a
94     # file can be downloaded correctly by tools like "wget".
95
96     def __init__(self, client):
97         rend.Page.__init__(self, client)
98         self.client = client
99
100     def childFactory(self, ctx, name):
101         req = IRequest(ctx)
102         if req.method not in ("GET", "HEAD"):
103             raise WebError("/file can only be used with GET or HEAD")
104         # 'name' must be a file URI
105         try:
106             node = self.client.create_node_from_uri(name)
107         except (TypeError, AssertionError):
108             # I think this can no longer be reached
109             raise WebError("'%s' is not a valid file- or directory- cap"
110                            % name)
111         if not IFileNode.providedBy(node):
112             raise WebError("'%s' is not a file-cap" % name)
113         return filenode.FileNodeDownloadHandler(self.client, node)
114
115     def renderHTTP(self, ctx):
116         raise WebError("/file must be followed by a file-cap and a name",
117                        http.NOT_FOUND)
118
119 class IncidentReporter(RenderMixin, rend.Page):
120     def render_POST(self, ctx):
121         req = IRequest(ctx)
122         log.msg(format="User reports incident through web page: %(details)s",
123                 details=get_arg(req, "details", ""),
124                 level=log.WEIRD, umid="LkD9Pw")
125         req.setHeader("content-type", "text/plain")
126         return "An incident report has been saved to logs/incidents/ in the node directory."
127
128 SPACE = u"\u00A0"*2
129
130 class Root(rend.Page):
131
132     addSlash = True
133     docFactory = getxmlfile("welcome.xhtml")
134
135     def __init__(self, client, clock=None):
136         rend.Page.__init__(self, client)
137         self.client = client
138         # If set, clock is a twisted.internet.task.Clock that the tests
139         # use to test ophandle expiration.
140         self.child_operations = operations.OphandleTable(clock)
141         try:
142             s = client.getServiceNamed("storage")
143         except KeyError:
144             s = None
145         self.child_storage = storage.StorageStatus(s, self.client.nickname)
146
147         self.child_uri = URIHandler(client)
148         self.child_cap = URIHandler(client)
149
150         self.child_file = FileHandler(client)
151         self.child_named = FileHandler(client)
152         self.child_status = status.Status(client.get_history())
153         self.child_statistics = status.Statistics(client.stats_provider)
154         static_dir = resource_filename("allmydata.web", "static")
155         for filen in os.listdir(static_dir):
156             self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
157
158     def child_helper_status(self, ctx):
159         # the Helper isn't attached until after the Tub starts, so this child
160         # needs to created on each request
161         return status.HelperStatus(self.client.helper)
162
163     child_report_incident = IncidentReporter()
164     #child_server # let's reserve this for storage-server-over-HTTP
165
166     # FIXME: This code is duplicated in root.py and introweb.py.
167     def data_rendered_at(self, ctx, data):
168         return time.strftime(TIME_FORMAT, time.localtime())
169     def data_version(self, ctx, data):
170         return get_package_versions_string()
171     def data_import_path(self, ctx, data):
172         return str(allmydata)
173     def render_my_nodeid(self, ctx, data):
174         tubid_s = "TubID: "+self.client.get_long_tubid()
175         return T.td(title=tubid_s)[self.client.get_long_nodeid()]
176     def data_my_nickname(self, ctx, data):
177         return self.client.nickname
178
179     def render_services(self, ctx, data):
180         ul = T.ul()
181         try:
182             ss = self.client.getServiceNamed("storage")
183             stats = ss.get_stats()
184             if stats["storage_server.accepting_immutable_shares"]:
185                 msg = "accepting new shares"
186             else:
187                 msg = "not accepting new shares (read-only)"
188             available = stats.get("storage_server.disk_avail")
189             if available is not None:
190                 msg += ", %s available" % abbreviate_size(available)
191             ul[T.li[T.a(href="storage")["Storage Server"], ": ", msg]]
192         except KeyError:
193             ul[T.li["Not running storage server"]]
194
195         if self.client.helper:
196             stats = self.client.helper.get_stats()
197             active_uploads = stats["chk_upload_helper.active_uploads"]
198             ul[T.li["Helper: %d active uploads" % (active_uploads,)]]
199         else:
200             ul[T.li["Not running helper"]]
201
202         return ctx.tag[ul]
203
204     def data_introducer_furl_prefix(self, ctx, data):
205         ifurl = self.client.introducer_furl
206         # trim off the secret swissnum
207         (prefix, _, swissnum) = ifurl.rpartition("/")
208         if not ifurl:
209             return None
210         if swissnum == "introducer":
211             return ifurl
212         else:
213             return "%s/[censored]" % (prefix,)
214
215     def data_introducer_description(self, ctx, data):
216         if self.data_connected_to_introducer(ctx, data) == "no":
217             return "Introducer not connected"
218         return "Introducer"
219
220     def data_connected_to_introducer(self, ctx, data):
221         if self.client.connected_to_introducer():
222             return "yes"
223         return "no"
224
225     def data_helper_furl_prefix(self, ctx, data):
226         try:
227             uploader = self.client.getServiceNamed("uploader")
228         except KeyError:
229             return None
230         furl, connected = uploader.get_helper_info()
231         if not furl:
232             return None
233         # trim off the secret swissnum
234         (prefix, _, swissnum) = furl.rpartition("/")
235         return "%s/[censored]" % (prefix,)
236
237     def data_helper_description(self, ctx, data):
238         if self.data_connected_to_helper(ctx, data) == "no":
239             return "Helper not connected"
240         return "Helper"
241
242     def data_connected_to_helper(self, ctx, data):
243         try:
244             uploader = self.client.getServiceNamed("uploader")
245         except KeyError:
246             return "no" # we don't even have an Uploader
247         furl, connected = uploader.get_helper_info()
248
249         if furl is None:
250             return "not-configured"
251         if connected:
252             return "yes"
253         return "no"
254
255     def data_known_storage_servers(self, ctx, data):
256         sb = self.client.get_storage_broker()
257         return len(sb.get_all_serverids())
258
259     def data_connected_storage_servers(self, ctx, data):
260         sb = self.client.get_storage_broker()
261         return len(sb.get_connected_servers())
262
263     def data_services(self, ctx, data):
264         sb = self.client.get_storage_broker()
265         return sorted(sb.get_known_servers(), key=lambda s: s.get_serverid())
266
267     def render_service_row(self, ctx, server):
268         nodeid = server.get_serverid()
269
270         ctx.fillSlots("peerid", server.get_longname())
271         ctx.fillSlots("nickname", server.get_nickname())
272         rhost = server.get_remote_host()
273         if rhost:
274             if nodeid == self.client.nodeid:
275                 rhost_s = "(loopback)"
276             elif isinstance(rhost, address.IPv4Address):
277                 rhost_s = "%s:%d" % (rhost.host, rhost.port)
278             else:
279                 rhost_s = str(rhost)
280             addr = rhost_s
281             connected = "yes"
282             since = server.get_last_connect_time()
283         else:
284             addr = "N/A"
285             connected = "no"
286             since = server.get_last_loss_time()
287         announced = server.get_announcement_time()
288         announcement = server.get_announcement()
289         version = announcement["my-version"]
290         service_name = announcement["service-name"]
291
292         ctx.fillSlots("address", addr)
293         ctx.fillSlots("connected", connected)
294         ctx.fillSlots("connected-bool", bool(rhost))
295         ctx.fillSlots("since", time.strftime(TIME_FORMAT,
296                                              time.localtime(since)))
297         ctx.fillSlots("announced", time.strftime(TIME_FORMAT,
298                                                  time.localtime(announced)))
299         ctx.fillSlots("version", version)
300         ctx.fillSlots("service_name", service_name)
301
302         return ctx.tag
303
304     def render_download_form(self, ctx, data):
305         # this is a form where users can download files by URI
306         form = T.form(action="uri", method="get",
307                       enctype="multipart/form-data")[
308             T.fieldset[
309             T.legend(class_="freeform-form-label")["Download a file"],
310             T.div["Tahoe-URI to download:"+SPACE,
311                   T.input(type="text", name="uri")],
312             T.div["Filename to download as:"+SPACE,
313                   T.input(type="text", name="filename")],
314             T.input(type="submit", value="Download!"),
315             ]]
316         return T.div[form]
317
318     def render_view_form(self, ctx, data):
319         # this is a form where users can download files by URI, or jump to a
320         # named directory
321         form = T.form(action="uri", method="get",
322                       enctype="multipart/form-data")[
323             T.fieldset[
324             T.legend(class_="freeform-form-label")["View a file or directory"],
325             "Tahoe-URI to view:"+SPACE,
326             T.input(type="text", name="uri"), SPACE*2,
327             T.input(type="submit", value="View!"),
328             ]]
329         return T.div[form]
330
331     def render_upload_form(self, ctx, data):
332         # This is a form where users can upload unlinked files.
333         # Users can choose immutable, SDMF, or MDMF from a radio button.
334
335         upload_chk  = T.input(type='radio', name='format',
336                               value='chk', id='upload-chk',
337                               checked='checked')
338         upload_sdmf = T.input(type='radio', name='format',
339                               value='sdmf', id='upload-sdmf')
340         upload_mdmf = T.input(type='radio', name='format',
341                               value='mdmf', id='upload-mdmf')
342
343         form = T.form(action="uri", method="post",
344                       enctype="multipart/form-data")[
345             T.fieldset[
346             T.legend(class_="freeform-form-label")["Upload a file"],
347             T.div["Choose a file:"+SPACE,
348                   T.input(type="file", name="file", class_="freeform-input-file")],
349             T.input(type="hidden", name="t", value="upload"),
350             T.div[upload_chk,  T.label(for_="upload-chk") [" Immutable"],           SPACE,
351                   upload_sdmf, T.label(for_="upload-sdmf")[" SDMF"],                SPACE,
352                   upload_mdmf, T.label(for_="upload-mdmf")[" MDMF (experimental)"], SPACE*2,
353                   T.input(type="submit", value="Upload!")],
354             ]]
355         return T.div[form]
356
357     def render_mkdir_form(self, ctx, data):
358         # This is a form where users can create new directories.
359         # Users can choose SDMF or MDMF from a radio button.
360
361         mkdir_sdmf = T.input(type='radio', name='format',
362                              value='sdmf', id='mkdir-sdmf',
363                              checked='checked')
364         mkdir_mdmf = T.input(type='radio', name='format',
365                              value='mdmf', id='mkdir-mdmf')
366
367         form = T.form(action="uri", method="post",
368                       enctype="multipart/form-data")[
369             T.fieldset[
370             T.legend(class_="freeform-form-label")["Create a directory"],
371             mkdir_sdmf, T.label(for_='mkdir-sdmf')[" SDMF"],                SPACE,
372             mkdir_mdmf, T.label(for_='mkdir-mdmf')[" MDMF (experimental)"], SPACE*2,
373             T.input(type="hidden", name="t", value="mkdir"),
374             T.input(type="hidden", name="redirect_to_result", value="true"),
375             T.input(type="submit", value="Create a directory"),
376             ]]
377         return T.div[form]
378
379     def render_incident_button(self, ctx, data):
380         # this button triggers a foolscap-logging "incident"
381         form = T.form(action="report_incident", method="post",
382                       enctype="multipart/form-data")[
383             T.fieldset[
384             T.input(type="hidden", name="t", value="report-incident"),
385             "What went wrong?"+SPACE,
386             T.input(type="text", name="details"), SPACE,
387             T.input(type="submit", value=u"Save \u00BB"),
388             ]]
389         return T.div[form]