def attach_volume(ec2, volume_id, attach_as): """Attaches a volume to the current instance""" try: ec2.attach_volume(volume_id, instance_id(), attach_as) except Exception as e: logging.exception(e) sys.exit(3)
def attach(self, server=None): if self.attachment_state == 'attached': print 'already attached' return None if server: self.server = server self.put() ec2 = self.get_ec2_connection() ec2.attach_volume(self.volume_id, self.server.instance_id, self.device)
def attach_ebs_volume(instance_id, volume_id): print('Trying to attach volume') try: ec2.attach_volume(volume_id, instance_id, '/dev/sdh') print('Successfully attached volume {} to instance {}'.format(volume_id, instance_id)) # Give it some time to become availabe sleep(20) except: print('Could not attach volume.')
def attach_ebs_volume(instance_id, volume_id): print('Trying to attach volume') try: ec2.attach_volume(volume_id, instance_id, '/dev/sdh') print('Successfully attached volume {} to instance {}'.format( volume_id, instance_id)) # Give it some time to become availabe sleep(20) except: print('Could not attach volume.')
def create_volume(snapshot_id, mount_point): for snapshot in ec2.get_all_snapshots(snapshot_ids=[snapshot_id]): volume = snapshot.create_volume(availability_zone, volume_type='gp2') while volume.volume_state() != 'available': time.sleep(1) volume.update() device_name = _get_available_device_name() ec2.attach_volume(volume.id, instance_id, device_name) while volume.attachment_state() != 'attached': time.sleep(1) volume.update() try: os.makedirs(mount_point) except: pass status = subprocess.call(['mount', device_name, mount_point]) return volume
def attach_to_instance(ec2, volume_id, instance_id, device, max_timeout): interval = 5 print 'Attaching volume {0} to instance {1}'.format(volume_id, instance_id) response = ec2.attach_volume(volume_id, instance_id, device) if response: start_time = time.time() while ( max_timeout >= (time.time() - start_time) ): status = get_ebs_status(ec2, volume_id) print ' * Status of {0} is {1}'.format(volume_id, status) if status == 'in-use': print '==================' return True else: time.sleep(interval) raise Exception('Timed out. Volume {0} is not in-use after {1} seconds.'.format(response.id, max_timeout)) return
def attach_volume(ec2, volume_id, attach_as): ec2.attach_volume(volume_id, instance_id(), attach_as)
DISK_SZ = 10 instance_id = boto.utils.get_instance_metadata()['instance-id'] print("running on instance " + instance_id) ec2 = boto.ec2.connect_to_region(REGION) print("creating volume...") vol = ec2.create_volume(DISK_SZ, ZONE, volume_type='gp2') _status_err_exit(vol, 'available', 'creating volume') print("created volume " + vol.id) print("adding tags...") ec2.create_tags([vol.id], {"Name": 'jbox_user_disk_template'}) print("attaching at " + DEVICE + " ...") ec2.attach_volume(vol.id, instance_id, DEVICE) if (not _wait_for_status(vol, 'in-use')) or (not _wait_for_device(DEVICE)): _err_exit("attaching at " + DEVICE) _sh_err_exit(lambda: sh.sudo.mkfs(DEVICE, t="ext4"), 'making ext4 file system') if not os.path.exists(MOUNT): os.makedirs(MOUNT) _sh_err_exit(lambda: sh.sudo.mount(DEVICE, MOUNT), 'mounting device at ' + MOUNT) _sh_err_exit( lambda: sh.sudo.chown('-R', str(os.getuid()) + ':' + str(os.getgid()), MOUNT ), 'changing file owmership') _sh_err_exit(lambda: sh.sudo.umount(MOUNT),
DISK_SZ = 10 instance_id = boto.utils.get_instance_metadata()['instance-id'] print("running on instance " + instance_id) ec2 = boto.ec2.connect_to_region(REGION) print("creating volume...") vol = ec2.create_volume(DISK_SZ, ZONE, volume_type='gp2') _status_err_exit(vol, 'available', 'creating volume') print("created volume " + vol.id) print("adding tags...") ec2.create_tags([vol.id], {"Name": 'jbox_user_disk_template'}) print("attaching at " + DEVICE + " ...") ec2.attach_volume(vol.id, instance_id, DEVICE) if (not _wait_for_status(vol, 'in-use')) or (not _wait_for_device(DEVICE)): _err_exit("attaching at " + DEVICE) _sh_err_exit(lambda: sh.sudo.mkfs(DEVICE, t="ext4"), 'making ext4 file system') if not os.path.exists(MOUNT): os.makedirs(MOUNT) _sh_err_exit(lambda: sh.sudo.mount(DEVICE, MOUNT), 'mounting device at ' + MOUNT) _sh_err_exit(lambda: sh.sudo.chown('-R', str(os.getuid())+':'+str(os.getgid()), MOUNT), 'changing file owmership') _sh_err_exit(lambda: sh.sudo.umount(MOUNT), 'ummounting device from ' + MOUNT) os.rmdir(MOUNT) print('creating snapshot...') snap = vol.create_snapshot('JuliaBox User Disk Snapshot') _status_err_exit(snap, 'completed', 'creating snapshot')