in source/code/actions/ec2_resize_instance_action.py [0:0]
def is_completed(self, start_data):
def task_is_triggered_by_tag_event():
task_change_events = self._events_.get(handlers.ec2_tag_event_handler.EC2_TAG_EVENT_SOURCE, {}).get(
handlers.TAG_CHANGE_EVENT, [])
return handlers.ec2_tag_event_handler.EC2_CHANGED_INSTANCE_TAGS_EVENT in task_change_events
def tags_to_delete():
tags = {}
tags_on_instance = self.instance.get("Tags", {})
for t in list(tags_on_instance.keys()):
if (self.scale_up_tagfilter and t in self.scale_up_tagfilter.get_filter_keys()) or \
(self.scale_down_tagfilter and t in self.scale_down_tagfilter.get_filter_keys()):
self._logger_.info(INF_REMOVE_TAG.format({t: tags_on_instance[t]}, self.instance_id))
tags[t] = tagging.TAG_DELETE
return tags
def delete_up_down_filter_tags():
tags = tags_to_delete()
if len(tags) > 0:
tagging.set_ec2_tags(ec2_client=self.ec2_client,
tags=tags,
can_delete=True,
logger=self._logger_,
resource_ids=[self.instance_id])
def set_tags_on_resized_instance(new_instance_type, original_type):
# tags set by action
tags = self.build_tags_from_template(parameter_name=PARAM_RESIZED_INSTANCE_TAGS,
tag_variables={
TAG_PLACEHOLDER_NEW_INSTANCE_TYPE: new_instance_type,
TAG_PLACEHOLDER_ORG_INSTANCE_TYPE: original_type
})
try:
# if task is triggered by tagging event
if task_is_triggered_by_tag_event():
# up or down tags filters should not match new tags as it would re-trigger execution of the task
if self.resize_mode == RESIZE_BY_STEP:
for t in list(tags.keys()):
# remove tags that match up or down tag filters
if (self.scale_up_tagfilter and t in self.scale_up_tagfilter.get_filter_keys()) or \
(self.scale_down_tagfilter and t in self.scale_down_tagfilter.get_filter_keys()):
self._logger_.info(INF_TAGS_NOT_SET_STEP.format({t: tags[t]}, self.instance_id))
del tags[t]
tags.update(tags_to_delete())
self.set_ec2_instance_tags_with_event_loop_check(client=self.ec2_client,
instance_ids=[self.instance_id],
tags_to_set=tags)
except Exception as tag_ex:
raise_exception(ERR_SET_TAGS, self.instance_id, tag_ex)
resized = not start_data.get("not-resized", False)
need_start = start_data.get("instance-running", True)
if not resized and not need_start:
delete_up_down_filter_tags()
self._logger_.info(INF_STOPPED_INSTANCE, self.instance_id)
return self.result
if not need_start and resized:
set_tags_on_resized_instance(start_data["new-instance-type"], start_data.get("org-instance-type", ""))
return self.result
# get current state of instance
instance = self._get_instance()
self._logger_.debug("Instance data is {}", safe_json(instance, indent=3))
state_code = instance["StateCode"] & 0xFF
# resized instance is running, done...
if state_code == EC2_STATE_RUNNING:
# instance is running
self._logger_.info(INF_INSTANCE_RUNNING, self.instance_id)
if resized:
set_tags_on_resized_instance(instance["InstanceType"], start_data.get("org-instance-type", ""))
else:
delete_up_down_filter_tags()
return self.result
# in pending state, wait for next completion check
if state_code == EC2_STATE_PENDING:
return None
raise_exception(ERR_INSTANCE_NOT_IN_STARTING_STATE, self.instance_id, instance)