Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/_quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

See ``terrarium --help`` for a complete list of options and commands.

.. note::

The following documentation assumes you are familiar with `virtualenv
<http://www.virtualenv.org/en/latest/>`_. If not, run through `this
tutorial <http://docs.python-guide.org/en/latest/dev/virtualenvs/>`_. We'll
wait.

Creating a new environment
##########################

Expand All @@ -12,7 +19,7 @@ includes the packages defined in ``requirements.txt``

.. code-block:: shell-session

$ terrarium --target env install requirements.txt
$ terrarium --target path/to/env install requirements.txt

Replacing an existing environment
#################################
Expand All @@ -22,8 +29,8 @@ existing activated virtual environment with a different set of packages.

.. code-block:: shell-session

$ terrarium --target env install requirements.txt
$ source env/bin/activate
$ terrarium --target path/to/env install requirements.txt
$ source path/to/env/bin/activate
$ terrarium install other_requirements.txt

.. note::
Expand Down
1 change: 1 addition & 0 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nose
requests==1.0.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is already mentioned in setup.py, you don't need to also add it here.

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def main():
python_version = sys.version_info[:2]
install_requires = [
'virtualenv>=1.7.2,<=1.9.1',
'requests>=1.0.0',
]
if python_version < (2, 7) or (3, 0) <= python_version <= (3, 1):
install_requires += ['argparse']
Expand Down
82 changes: 57 additions & 25 deletions terrarium/terrarium.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile
import shutil
import logging
import requests

from logging import getLogger, StreamHandler

Expand Down Expand Up @@ -393,6 +394,50 @@ def _get_s3_bucket(self):
pass
return boto.s3.bucket.Bucket(conn, name=self.args.s3_bucket)

def _attempt_s3_download(self, target):
# attempt to download from public S3 url
base_s3_url = "https://%(bucket_name)s.s3.amazonaws.com/%(key)s"
remote_key = self.make_remote_key()
bucket_name = self.args.s3_bucket
s3_url = base_s3_url % {"bucket_name": bucket_name, "key": remote_key}
r = requests.get(s3_url, stream=True)
if r.ok:
fd, archive = tempfile.mkstemp()
with open(archive, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)

self.extract(archive, target)
os.close(fd)
os.unlink(archive)
return True
# Check if permissions denied
elif not self.args.s3_access_key and 'AccessDenied' in r.content:
print (
'Access was denied to s3 bucket hash %s '
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we're trying the anonymous download even if a key is provided, right? I don't think this message is accurate.

'and no key was provided. '
'Make sure the s3 bucket hash is public '
'readable.' % (remote_key)
)

if not boto:
return False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should give a different message if an access key was provided, but boto isn't installed.

bucket = self._get_s3_bucket()
if bucket:
key = bucket.get_key(remote_key)
if key:
logger.info(
'Downloading %s/%s from S3 '
'(this may take time) ...'
% (self.args.s3_bucket, remote_key)
)
fd, archive = tempfile.mkstemp()
key.get_contents_to_filename(archive)
self.extract(archive, target)
os.close(fd)
os.unlink(archive)
return True

def download(self, target):
if self.args.storage_dir:
remote_archive = os.path.join(
Expand All @@ -413,23 +458,10 @@ def download(self, target):
os.unlink(local_archive)
return True
logger.error('Download archive failed')
if boto and self.args.s3_bucket:
bucket = self._get_s3_bucket()
if bucket:
remote_key = self.make_remote_key()
key = bucket.get_key(remote_key)
if key:
logger.info(
'Downloading %s/%s from S3 '
'(this may take time) ...'
% (self.args.s3_bucket, remote_key)
)
fd, archive = tempfile.mkstemp()
key.get_contents_to_filename(archive)
self.extract(archive, target)
os.close(fd)
os.unlink(archive)
return True

if self.args.s3_bucket:
download_successful = self._attempt_s3_download(target)
return download_successful

def make_remote_key(self):
import platform
Expand Down Expand Up @@ -493,8 +525,14 @@ def upload(self, target):
target,
self.args.storage_dir,
)
if boto and self.args.s3_bucket:
self.upload_to_s3(target)
if self.args.s3_bucket:
if boto:
self.upload_to_s3(target)
else:
logger.critical(
"--upload combined with --s3-bucket requires that you "
"have boto installed, which does not appear to be the case"
)

def create_bootstrap(self, dest):
extra_text = (
Expand Down Expand Up @@ -784,12 +822,6 @@ def parse_args():
args = ap.parse_args()
args.add_sensitive_arguments(*sensitive_arguments)

if not boto and args.s3_bucket is not None:
ap.error(
'--s3-bucket requires that you have boto installed, '
'which does not appear to be the case'
)

return args


Expand Down
11 changes: 3 additions & 8 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,18 +500,13 @@ def test_logging_output(self):
'Terrarium is finished\n'
))

def test_boto_required_to_use_s3_bucket(self):
def test_boto_not_required_to_use_s3_bucket(self):
self._add_test_requirement()

output = self.assertInstall(
return_code=2,
self.assertInstall(
return_code=0,
s3_bucket='bucket',
)
self.assertTrue(
'error: --s3-bucket requires that you have boto installed, '
'which does not appear to be the case'
in output[1]
)

def test_sensitive_arguments_are_sensitive(self):
command = 'hash %s' % (
Expand Down