#!/usr/bin/python
import asyncio
+import botocore.exceptions
import datetime
import hashlib
import logging
def instance_id(self):
return self.data.instance_id
+ @property
+ def instance_type(self):
+ return self.data.instance_type
+
@lazy_property
def instance(self):
if self.instance_id:
def _start(self):
log.info("Starting %s" % self)
+ # Set correct instance type
+ self._set_instance_type()
+
# Send the start signal
self.instance.start()
log.debug("%s has been started" % self)
+ def _set_instance_type(self):
+ """
+ Changes the type of this instance
+ """
+ # Don't try setting instance type if nothing is configured
+ if not self.instance_type:
+ return
+
+ # Check if this needs changing at all
+ if self.instance.instance_type == self.instance_type:
+ return
+
+ log.debug("Changing instance type of %s to %s" % (self, self.instance_type))
+
+ # Send the change
+ try:
+ self.instance.modify_attribute(
+ InstanceType={
+ "Value" : self.instance_type,
+ }
+ )
+
+ # Log an error if this request wasn't successful
+ except botocore.exceptions.ClientError as e:
+ log.warning("Could not change instance type of %s: %s" % (self, e))
+
async def stop(self):
"""
Stops this instance on AWS