├── .gitignore ├── Dockerfile ├── LICENSE.md ├── README.md └── ebs-attach.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | test.sh 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:14.04 2 | MAINTAINER Louis Garman "louisgarman@gmail.com" 3 | # 4 | # install deps/tools 5 | # 6 | RUN apt-get -q update 7 | RUN apt-get install -y python-requests python-boto 8 | 9 | ADD ebs-attach.py ebs-attach.py 10 | 11 | ENTRYPOINT ["/usr/bin/python", "ebs-attach.py"] 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 leg100 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | docker-ebs-attach 2 | ============== 3 | 4 | Docker container that attaches an EBS volume to the local ec2 instance. Detaches volume on exit. 5 | 6 | Usage: 7 | 8 | ``` 9 | docker run -it --rm leg100/ebs-attach \ 10 | --volumeid vol-123123 \ 11 | --device /dev/xvdf \ 12 | --region eu-west-1 13 | ``` 14 | 15 | Note: it relies on the ec2 instance possessing an IAM profile with the following privileges: 16 | 17 | - ec2:AttachVolume 18 | - ec2:DetachVolume 19 | - ec2:DescribeVolumes 20 | -------------------------------------------------------------------------------- /ebs-attach.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import argparse 4 | import os 5 | import signal 6 | import sys 7 | import time 8 | 9 | import boto.ec2 10 | import boto.exception 11 | import requests 12 | 13 | # on close, detach ebs 14 | parser = argparse.ArgumentParser(description='Attach and mount EBS volume to local instance') 15 | parser.add_argument('--volumeid', metavar='', default=os.environ.get('VOLUME_ID'), 16 | help='Volume ID of EBS volume to attach') 17 | parser.add_argument('--device', metavar='', default=os.environ.get('DEVICE'), 18 | help='Device to expose volume on') 19 | parser.add_argument('--region', metavar='', default=os.environ.get('REGION'), 20 | help='AWS region') 21 | args = parser.parse_args() 22 | 23 | # AWS metadata service that provides credentials is unreliable 24 | # so retry several times 25 | attempts = 0 26 | while True: 27 | try: 28 | conn = boto.ec2.connect_to_region(args.region) 29 | except boto.exception.NoAuthHandlerFound: 30 | print "Couldn't find auth credentials handler, trying again" 31 | sys.stdout.flush() 32 | 33 | attempts += 1 34 | if attempts > 5: 35 | print "Tried 5 times, giving up" 36 | sys.exit(3) 37 | else: 38 | time.sleep(1) 39 | else: 40 | break 41 | 42 | instance = requests.get("http://169.254.169.254/latest/meta-data/instance-id").content 43 | 44 | existing_vols = conn.get_all_volumes([args.volumeid]) 45 | if len(existing_vols) > 0: 46 | vol = existing_vols[0] 47 | 48 | if vol.attach_data.instance_id == instance: 49 | print "Volume {} already attached to {}".format(args.volumeid, instance) 50 | sys.stdout.flush() 51 | else: 52 | conn.attach_volume(args.volumeid, instance, args.device) or sys.exit(1) 53 | print "Attached volume {} to device {} on instance {}".format(args.volumeid, args.device, instance) 54 | sys.stdout.flush() 55 | else: 56 | print "Cannot find volume {}".format(args.volumeid) 57 | sys.exit(2) 58 | 59 | def detach_func(volume, instance, device): 60 | def handler(*args, **kwargs): 61 | attempts = 0 62 | 63 | while True: 64 | try: 65 | print "Detaching volume {} from device {} on instance {}".format(volume, device, instance) 66 | conn.detach_volume(volume, instance, device) 67 | except boto.exception.NoAuthHandlerFound: 68 | print "Couldn't find auth credentials handler, trying again" 69 | sys.stdout.flush() 70 | 71 | attempts += 1 72 | if attempts > 5: 73 | print "Tried 5 times, giving up" 74 | sys.exit(3) 75 | else: 76 | time.sleep(1) 77 | else: 78 | sys.exit(0) 79 | return handler 80 | 81 | detach = detach_func(args.volumeid, instance, args.device) 82 | signal.signal(signal.SIGTERM, detach) 83 | signal.signal(signal.SIGINT, detach) 84 | 85 | while True: 86 | time.sleep(5) 87 | --------------------------------------------------------------------------------