|
1 | | -from s3contents.ipycompat import ContentsManager |
2 | | -from s3contents.ipycompat import HasTraits, Unicode |
| 1 | +from notebook.services.contents.filemanager import FileContentsManager |
| 2 | + |
| 3 | +import s3fs |
3 | 4 |
|
4 | 5 | from traitlets import ( |
| 6 | + HasTraits, |
5 | 7 | Any, |
6 | 8 | Bool, |
7 | 9 | Dict, |
|
12 | 14 | Unicode, |
13 | 15 | validate, |
14 | 16 | default, |
| 17 | + Instance |
15 | 18 | ) |
16 | 19 |
|
17 | | -class BookstoreContentsArchiver(ContentsManager, HasTraits): |
| 20 | +import json |
| 21 | + |
| 22 | +from .bookstore_config import BookstoreSettings |
| 23 | + |
| 24 | +class BookstoreContentsArchiver(FileContentsManager): |
| 25 | + """ |
| 26 | + Archives notebooks to S3 on save |
18 | 27 | """ |
19 | | - Archives contents via one ContentsManager and passes through to |
20 | | - another ContentsManager. |
21 | 28 |
|
22 | | - Likely route: |
| 29 | + def __init__(self, *args, **kwargs): |
| 30 | + super(FileContentsManager, self).__init__(*args, **kwargs) |
| 31 | + # opt ourselves into being part of the Jupyter App that should have Bookstore Settings applied |
| 32 | + self.settings = BookstoreSettings(parent=self) |
23 | 33 |
|
24 | | - * Write directly to S3 on post_save_hook |
| 34 | + self.fs = s3fs.S3FileSystem(key=self.settings.s3_access_key_id, |
| 35 | + secret=self.settings.s3_secret_access_key, |
| 36 | + client_kwargs={ |
| 37 | + "endpoint_url": self.settings.s3_endpoint_url, |
| 38 | + "region_name": self.settings.s3_region_name |
| 39 | + }, |
| 40 | + config_kwargs={}, |
| 41 | + s3_additional_kwargs={}) |
25 | 42 |
|
26 | | - """ |
27 | | - pass |
| 43 | + @property |
| 44 | + def delimiter(self): |
| 45 | + """It's a slash! Normally this could be configurable. This leaves room for that later, |
| 46 | + keeping it centralized for now""" |
| 47 | + return "/" |
| 48 | + |
| 49 | + @property |
| 50 | + def full_prefix(self): |
| 51 | + """Full prefix: bucket + workspace prefix""" |
| 52 | + return self.delimiter.join([self.settings.s3_bucket, self.settings.workspace_prefix]) |
| 53 | + |
| 54 | + def s3_path(self, path): |
| 55 | + """compute the s3 path based on the bucket, prefix, and the path to the notebook""" |
| 56 | + return self.delimiter.join([self.full_prefix, path]) |
| 57 | + |
| 58 | + def run_pre_save_hook(self, model, path, **kwargs): |
| 59 | + """Store notebook to S3 when saves happen |
| 60 | + """ |
| 61 | + if model['type'] != 'notebook': |
| 62 | + return |
| 63 | + |
| 64 | + # TODO: store the hash of the notebook to not write on every save |
| 65 | + notebook_contents = json.dumps(model['content']) |
| 66 | + |
| 67 | + full_path = self.s3_path(path) |
| 68 | + |
| 69 | + # write to S3 |
| 70 | + # TODO: Write to S3 asynchronously to not block other server operations |
| 71 | + with self.fs.open(full_path, mode='wb') as f: |
| 72 | + f.write(notebook_contents.encode('utf-8')) |
0 commit comments