Skip to content

Commit

Permalink
[SQUASHME] More filters
Browse files Browse the repository at this point in the history
  • Loading branch information
aske committed Oct 30, 2017
1 parent dad0ccf commit 086eb2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
31 changes: 19 additions & 12 deletions nixops/backends/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,16 @@ def connect_route53(self):
def _get_spot_instance_request_by_id(self, request_id, allow_missing=False):
"""Get spot instance request object by id."""
self.connect()
result = self._conn.get_all_spot_instance_requests([request_id])

request_id_filter = {'Name': 'spot-instance-request-id', 'Values': [request_id]}
# TODO: error handling
result = self._conn.meta.client.describe_spot_instance_requests(Filters=[request_id_filter])['SpotInstanceRequests']
if len(result) == 0:
if allow_missing:
return None
raise EC2InstanceDisappeared("Spot instance request ‘{0}’ disappeared!".format(request_id))
return result[0]


def _get_instance(self, instance_id=None, allow_missing=False, update=False):
"""Get instance object for this machine, with caching"""
if not instance_id: instance_id = self.vm_id
Expand Down Expand Up @@ -305,13 +307,12 @@ def _get_instance(self, instance_id=None, allow_missing=False, update=False):
def _get_snapshot_by_id(self, snapshot_id):
"""Get snapshot object by instance id."""
self.connect()
snapshots = self._conn.get_all_snapshots([snapshot_id])
snapshots = [snapshot for snapshot in self._conn.snapshots.filter(Filters=[{'Name': 'snapshot-id',
'Values': [snapshot_id]}])]
if len(snapshots) != 1:
raise Exception("unable to find snapshot ‘{0}’".format(snapshot_id))
return snapshots[0]



def _wait_for_ip(self):
self.log_start("waiting for IP address... ".format(self.name))

Expand Down Expand Up @@ -556,7 +557,9 @@ def _assign_elastic_ip(self, elastic_ipv4, check):
instance = self._get_instance(update=True)
self.log_end("")

addresses = self._conn.get_all_addresses(addresses=[elastic_ipv4])
elastic_ip_filter = {'Name': 'public-ip', 'Values': [elastic_ipv4]}
# TODO: errors
addresses = self._conn.meta.client.describe_addresses(Filters=[elastic_ip_filter])['Addresses']
if addresses[0].instance_id != "" \
and addresses[0].instance_id is not None \
and addresses[0].instance_id != self.vm_id \
Expand All @@ -566,7 +569,8 @@ def _assign_elastic_ip(self, elastic_ipv4, check):
raise Exception("elastic IP ‘{0}’ already in use...".format(elastic_ipv4))
else:
self.log("associating IP address ‘{0}’...".format(elastic_ipv4))
addresses[0].associate(self.vm_id)
self._conn.meta.client.associate_address(AllocationId=addresses[0].allocation_id,
InstanceId=self.vm_id)
self.log_start("waiting for address to be associated with this machine... ")
instance = self._get_instance(update=True)
while True:
Expand All @@ -585,9 +589,12 @@ def _assign_elastic_ip(self, elastic_ipv4, check):
self.ssh_pinged = False

elif self.elastic_ipv4 != None:
addresses = self._conn.get_all_addresses(addresses=[self.elastic_ipv4])
elastic_ip_filter = {'Name': 'public-ip', 'Values': [elastic_ipv4]}
# TODO: errors
addresses = self._conn.meta.client.describe_addresses(Filters=[elastic_ip_filter])['Addresses']
if len(addresses) == 1 and addresses[0].instance_id == self.vm_id:
self.log("disassociating IP address ‘{0}’...".format(self.elastic_ipv4))
# TODO
self._conn.disassociate_address(public_ip=self.elastic_ipv4)
else:
self.log("address ‘{0}’ was not associated with instance ‘{1}’".format(self.elastic_ipv4, self.vm_id))
Expand Down Expand Up @@ -663,9 +670,9 @@ def create_instance(self, defn, zone, devmap, user_data, ebs_optimized):
self.log_start("waiting for spot instance request ‘{0}’ to be fulfilled... ".format(self.spot_instance_request_id))
while True:
request = self._get_spot_instance_request_by_id(self.spot_instance_request_id)
self.log_continue("[{0}] ".format(request.status.code))
if request.status.code == "fulfilled": break
if request.status.code in {"schedule-expired", "canceled-before-fulfillment", "bad-parameters", "system-error"}:
self.log_continue("[{0}] ".format(request.status_code))
if request.status_code == "fulfilled": break
if request.status_code in {"schedule-expired", "canceled-before-fulfillment", "bad-parameters", "system-error"}:
self.spot_instance_request_id = None
self.log_end("")
raise Exception("spot instance request failed with result ‘{0}’".format(request.status.code))
Expand Down Expand Up @@ -707,7 +714,7 @@ def _cancel_spot_request(self):
while True:
request = self._get_spot_instance_request_by_id(self.spot_instance_request_id, allow_missing=True)
if request is None: break
self.log_continue("[{0}] ".format(request.status.code))
self.log_continue("[{0}] ".format(request.status_code))
if request.instance_id is not None and request.instance_id != self.vm_id:
if self.vm_id is not None:
raise Exception("spot instance request got fulfilled unexpectedly as instance ‘{0}’".format(request.instance_id))
Expand Down
3 changes: 2 additions & 1 deletion nixops/ec2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def handle_exception(e):
def get_volume_by_id(conn, volume_id, allow_missing=False):
"""Get volume object by volume id."""
try:
volumes = conn.get_all_volumes([volume_id])
volumes = [vol for vol in conn.volumes.filter(Filters=[{'Name': 'volume-id',
'Values': [volume_id]}])]
if len(volumes) != 1:
raise Exception("unable to find volume ‘{0}’".format(volume_id))
return volumes[0]
Expand Down

0 comments on commit 086eb2a

Please sign in to comment.