Skip to content
Draft
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ options:
```console
% vol attach -h
usage: vol attach [-h] [-f FSTYPE] [-i INSTANCE_ID] [-k KMS_KEY_ID] [-m MOUNTPOINT] [-n NO_SNAPSHOTS]
[-r VOLUME_INITIALIZATION_RATE] -s SIZE -t TAGS [-T SNAPSHOT_TAGS] [-w WIPE]
[-z AVAILABILITY_ZONE]
[-o IOPS] [-p THROUGHPUT] [-r VOLUME_INITIALIZATION_RATE] -s SIZE -t TAGS
[-T SNAPSHOT_TAGS] [-v VOLUME_TYPE] [-w WIPE] [-z AVAILABILITY_ZONE]

* if wipe is specified, delete the volume with the specified tags in the specified availability zone
* find any volume with the specified tags and availability zone and any snapshots with the specified tags
Expand Down Expand Up @@ -171,12 +171,17 @@ options:
(default: None)
-n, --no-snapshots NO_SNAPSHOTS
do not use snapshots to create volumes (default: False)
-o, --iops IOPS provisioned iops (default: None)
-p, --throughput THROUGHPUT
provisioned throughput (default: None)
-r, --volume-initialization-rate VOLUME_INITIALIZATION_RATE
volume initialization rate in MiB/s (default: None)
-s, --size SIZE volume size in GB, if it needs to be created
-t, --tags TAGS match volume tags: t1=v1,...,tN=vN
-T, --snapshot-tags SNAPSHOT_TAGS
match snapshot tags: t1=v1,...,tN=vN (default: None)
-v, --volume-type VOLUME_TYPE
volume type (default: gp3)
-w, --wipe WIPE delete the matching volume first (default: False)
-z, --availability-zone AVAILABILITY_ZONE
availability zone, derived from instance-id if not set (default: None)
Expand Down
24 changes: 16 additions & 8 deletions vol
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ def get_args():
(['-k', '--kms-key-id'], {'help': 'KMS key to use for volume encryption, otherwise default'}),
(['-m', '--mountpoint'], {'help': 'mountpoint, only on <instance-id>, requires util-linux, create if doesn\'t exist'}),
(['-n', '--no-snapshots'], {'type': bool, 'default': False, 'help': 'do not use snapshots to create volumes'}),
(['-o', '--iops'], {'type': int, 'default': None, 'help': 'provisioned iops'}),
(['-p', '--throughput'], {'type': int, 'default': None, 'help': 'provisioned throughput'}),
(['-r', '--volume-initialization-rate'], {'type': int, 'default': None, 'help': 'volume initialization rate in MiB/s'}),
(['-s', '--size'], {'type': int, 'required': True, 'help': 'volume size in GB, if it needs to be created'}),
(['-t', '--tags'], {'type': tag_type, 'required': True, 'help': 'match volume tags: t1=v1,...,tN=vN'}),
(['-T', '--snapshot-tags'], {'type': tag_type, 'help': 'match snapshot tags: t1=v1,...,tN=vN'}),
(['-v', '--volume-type'], {'default': 'gp3', 'help': 'volume type'}),
(['-w', '--wipe'], {'type': bool, 'default': False, 'help': 'delete the matching volume first'}),
(['-z', '--availability-zone'], {'help': 'availability zone, derived from instance-id if not set'}),
),
Expand Down Expand Up @@ -451,7 +454,7 @@ def print_result(result):

############################# HIGH-LEVEL HELPERS ##############################

def create_volume(tags, snapshot, availability_zone, size, kms_key_id, volume_initialization_rate):
def create_volume(tags, snapshot, availability_zone, size, kms_key_id, volume_initialization_rate, volume_type, iops, throughput):
if get_volumes(tags, availability_zone) != []:
err('TOO_MANY_VOLUMES')

Expand All @@ -463,9 +466,11 @@ def create_volume(tags, snapshot, availability_zone, size, kms_key_id, volume_in
'Encrypted': True,
'Size': size,
'TagSpecifications': [{'ResourceType': 'volume', 'Tags': get_tag_spec(tags)}],
'VolumeType': 'gp3',
} | ({'KmsKeyId': kms_key_id} if kms_key_id else {}) \
'VolumeType': volume_type,
} | ({'Iops': iops} if iops else {}) \
| ({'KmsKeyId': kms_key_id} if kms_key_id else {}) \
| ({'SnapshotId': snapshot['SnapshotId']} if snapshot else {}) \
| ({'Throughput': throughput} if throughput else {}) \
Comment on lines +470 to +473

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Also modify existing volumes if iops/throughput is different than their current values

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

And what if the volume type changes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Maybe allow doing this during detach as well? E.g. to lower iops/throughput overnight, etc.

| ({'VolumeInitializationRate': volume_initialization_rate} if volume_initialization_rate and snapshot else {})

volume = ec2.create_volume(**options)
Expand Down Expand Up @@ -635,20 +640,23 @@ def list(tags, snapshot_tags, availability_zone):

return volumes_result | snapshots_result

def attach(tags, snapshot_tags, instance_id, availability_zone, wipe, size, kms_key_id,
mountpoint, fstype, no_snapshots, volume_initialization_rate):
def attach(tags, snapshot_tags, instance_id, availability_zone, wipe, size, kms_key_id, mountpoint,
fstype, no_snapshots, volume_initialization_rate, volume_type, iops, throughput):
if wipe:
match get_volumes(tags, availability_zone):
case [vol]:
delete_volume(vol)

options = {
'availability_zone': availability_zone,
'iops': iops,
'kms_key_id': kms_key_id,
'size': size,
'tags' : tags,
'snapshot': None,
'tags' : tags,
'throughput': throughput,
'volume_initialization_rate': volume_initialization_rate,
'volume_type': volume_type,
}

match get_volumes(tags, availability_zone), get_snapshots(tags), (get_snapshots(snapshot_tags) if snapshot_tags else []):
Expand Down Expand Up @@ -746,8 +754,8 @@ def main():
instance_and_az_match(instance_id, availability_zone)

extra_args = {'instance_id': instance_id, 'availability_zone': availability_zone}
arg_names = ['tags', 'snapshot_tags', 'wipe', 'size', 'kms_key_id', 'mountpoint',
'fstype', 'no_snapshots', 'volume_initialization_rate']
arg_names = ['tags', 'snapshot_tags', 'wipe', 'size', 'kms_key_id', 'mountpoint', 'fstype',
'no_snapshots', 'volume_initialization_rate', 'volume_type', 'iops', 'throughput']

print_result(attach(**{k: getattr(args, k) for k in arg_names} | extra_args))
case 'detach':
Expand Down
Loading